/usr/share/cruisecontrol-bin-2.6.1/projects/qpid-trunk/cpp/src/qpid/amqp_0_10/apply.h

00001 #ifndef QPID_AMQP_0_10_APPLY_H
00002 #define QPID_AMQP_0_10_APPLY_H
00003 
00004 /*
00005  *
00006  * Licensed to the Apache Software Foundation (ASF) under one
00007  * or more contributor license agreements.  See the NOTICE file
00008  * distributed with this work for additional information
00009  * regarding copyright ownership.  The ASF licenses this file
00010  * to you under the Apache License, Version 2.0 (the
00011  * "License"); you may not use this file except in compliance
00012  * with the License.  You may obtain a copy of the License at
00013  * 
00014  *   http://www.apache.org/licenses/LICENSE-2.0
00015  * 
00016  * Unless required by applicable law or agreed to in writing,
00017  * software distributed under the License is distributed on an
00018  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
00019  * KIND, either express or implied.  See the License for the
00020  * specific language governing permissions and limitations
00021  * under the License.
00022  *
00023  */
00024 #include <boost/optional.hpp>
00025 
00026 namespace qpid {
00027 namespace amqp_0_10 {
00028 
00029 template <class F, class R=typename F::result_type> struct FunctionAndResult {
00030     F* functor;
00031     boost::optional<R> result;
00032 
00033     FunctionAndResult() : functor(0) {}
00034     template <class T> void invoke(T& t) { result=(*functor)(t); }
00035     template <class T> void invoke(const T& t) { result=(*functor)(t); }
00036     R getResult() { return *result; }
00037 };
00038 
00039 // void result is special case.
00040 template <class F> struct FunctionAndResult<F, void> {
00041     F* functor;
00042     
00043     FunctionAndResult() : functor(0) {}
00044     template <class T> void invoke(T& t) { (*functor)(t); }
00045     void getResult() {}
00046 };
00047 
00048 // Metafunction returning correct abstract visitor for Visitable type. 
00049 template <class Visitable> struct VisitorType {
00050     typedef typename Visitable::Visitor type;
00051 };
00052 template <class Visitable> struct VisitorType<const Visitable> {
00053     typedef typename Visitable::ConstVisitor type;
00054 };
00055     
00056 template <class Visitor, class F>
00057 struct ApplyVisitorBase : public Visitor, public FunctionAndResult<F> {};
00058 
00059 // Specialize for each visitor type
00060 template <class Visitable, class F> struct ApplyVisitor;
00061 
00066 template <class F, class Visitable>
00067 typename F::result_type apply(F& functor, Visitable& visitable) {
00068     ApplyVisitor<typename VisitorType<Visitable>::type, F> visitor;
00069     visitor.functor=&functor;
00070     visitable.accept(visitor);
00071     return visitor.getResult();
00072 }
00073 
00074 template <class F, class Visitable>
00075 typename F::result_type apply(const F& functor, Visitable& visitable) {
00076     ApplyVisitor<typename VisitorType<Visitable>::type, const F> visitor;
00077     visitor.functor=&functor;
00078     visitable.accept(visitor);
00079     return visitor.getResult();
00080 }
00081 
00082 template <class R> struct ApplyFunctor { typedef R result_type; };
00083 
00084 }} // namespace qpid::amqp_0_10
00085 
00086 #endif  

Generated on Thu Apr 10 11:08:17 2008 for Qpid by  doxygen 1.4.7