switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / PPI / Route.ih
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief Route internal header */
30
31 #ifndef IH_SENF_PPI_Route_
32 #define IH_SENF_PPI_Route_ 1
33
34 // Custom includes
35 #include <boost/type_traits/is_convertible.hpp>
36 #include <boost/type_traits/is_base_of.hpp>
37 #include <boost/mpl/if.hpp>
38 #include <boost/mpl/bool.hpp>
39 #include <boost/static_assert.hpp>
40
41 //-/////////////////////////////////////////////////////////////////////////////////////////////////
42
43 #ifndef DOXYGEN
44
45 namespace senf {
46 namespace ppi {
47 namespace detail {
48
49     // This is the RoutingTraits implementation for Connectors. Events are handled in the
50     // specialization below
51     template <class Connector, bool isEvent>
52     struct RoutingTraitsImplementation
53     {
54         BOOST_STATIC_ASSERT((boost::is_base_of<connector::Connector, Connector>::value));
55
56         static bool const event = false;
57
58         static bool const notifySource = boost::is_base_of<
59             connector::ActiveConnector, Connector>::value;
60         static bool const notifyTarget = boost::is_base_of<
61             connector::PassiveConnector, Connector>::value;
62
63         static bool const dataSource = boost::is_base_of<
64             connector::InputConnector, Connector>::value;
65         static bool const dataTarget = boost::is_base_of<
66             connector::OutputConnector, Connector>::value;
67
68         typedef Connector type;
69     };
70
71     // RoutingTraits specialization for Event types. Events may be both dataSource or dataTarget but
72     // cannot be notifySource.
73     template <class Event>
74     struct RoutingTraitsImplementation<Event,true>
75     {
76         static bool const event = true;
77
78         static bool const notifySource = false;
79         static bool const notifyTarget = true;
80
81         static bool const dataSource = true;
82         static bool const dataTarget = true;
83
84         typedef EventDescriptor type;
85     };
86
87     // The RoutingTraits give routing related information about the argument type:
88     //  - Wether the type is a notifySource or notifyTarget
89     //  - Wether the type is dataSource or dataTarget
90     //  - Provide the generalized target type
91     //
92     // The real implementation is in RoutingTraitsImplementation which is appropriately specialized
93     // for Events
94     template <class Object>
95     struct RoutingTraits
96         : public RoutingTraitsImplementation<Object,
97                                              boost::is_convertible<Object*,
98                                                                    EventDescriptor*>::value>
99     {};
100
101     // This is the generic route implementation for all routes. It just provides access to the
102     // source and target.
103     template <class Source, class Target, class Base>
104     class BaseRouteImplementation
105         : public Base
106     {
107     public:
108         typedef Source source_type;
109         typedef Target target_type;
110
111         Source & source() const;
112         Target & target() const;
113
114     protected:
115         BaseRouteImplementation(module::Module & module, Source & source, Target & target);
116
117     private:
118         bool v_hasConnector(connector::Connector const & conn) const;
119         bool v_hasEvent(EventDescriptor const & event) const;
120
121         bool isSame(connector::Connector const & conn, connector::Connector const & other) const;
122         bool isSame(connector::Connector const & conn, EventDescriptor const & other) const;
123         bool isSame(EventDescriptor const & event, connector::Connector const & other) const;
124         bool isSame(EventDescriptor const & event, EventDescriptor const & other) const;
125
126         Source * source_;
127         Target * target_;
128     };
129
130     // The ForwardingRouteImplementation is based on the same BaseRouteImplementation
131     // as non-forwarding routes are but injects a different base-class (the third template
132     // argument to BaseRouteImplementation). ForwardingRouteImplementation has two additional
133     // functions:
134     //  1) Register the ForwardingRoute with the notifySource
135     //  2) Implement the abstract ForwardingRoute interface
136     //
137     // Since we don't know explicitly, which of Source or Target is the notifySource or
138     // notifyTarget, the implementation calls registerRoute and notifyThrottle/notifyUnthrottle on
139     // *both*, the source and target, however qualified with an additional argument of type
140     // boost::mpl::bool_ which is used to select the correct overloads, of which the 'false'
141     // overload always is a no-op. This way, only the correct call will generate any code, the
142     // disabled call will be optimized away.
143     template <class Source, class Target>
144     class ForwardingRouteImplementation
145         : public BaseRouteImplementation<Source, Target, ForwardingRoute>
146     {
147         typedef BaseRouteImplementation<Source, Target, ForwardingRoute> Base;
148
149     protected:
150         ForwardingRouteImplementation(module::Module & module, Source & source, Target & target);
151         ~ForwardingRouteImplementation();
152
153     private:
154         // send a throttle/unthrottle notification  only if the second argument is a 'true' type
155         template <class T> void notifyThrottle(T & ob, boost::mpl::bool_<true> const &);
156         template <class T> void notifyThrottle(T & ob, boost::mpl::bool_<false> const &);
157         template <class T> void notifyUnthrottle(T & ob, boost::mpl::bool_<true> const &);
158         template <class T> void notifyUnthrottle(T & ob, boost::mpl::bool_<false> const &);
159
160         template <class T> bool throttled(T & ob, boost::mpl::bool_<true> const &) const;
161         template <class T> bool throttled(T & ob, boost::mpl::bool_<false> const &) const;
162
163         virtual void v_notifyThrottle();
164         virtual void v_notifyUnthrottle();
165         virtual bool v_throttled() const;
166     };
167
168     // This helper class finds the base-class suitable for a specific route. Routes are classified
169     // into two groups:
170     //  1) A forwarding routes is a routed which forwards notifications from a notifySource to a
171     //     notifyTarget. Forwarding routes are implemneted using ForwardingRouteImplementation
172     //  2) Non-forwarding routes don't forward notifications. They are implemented directly
173     //     using BaseRouteImplementation
174     template <class Source, class Target>
175     struct RouteImplementationBase
176     {
177         typedef RoutingTraits<Source> srcTrait;
178         typedef RoutingTraits<Target> trgTrait;
179
180         static bool const isForwarding = (srcTrait::notifySource && trgTrait::notifyTarget)
181             || (srcTrait::notifyTarget && trgTrait::notifySource);
182
183         typedef typename boost::mpl::if_c<
184             isForwarding,
185             ForwardingRouteImplementation<Source,Target>,
186             BaseRouteImplementation<Source,Target,RouteBase> >::type base;
187     };
188
189     // RouteImplementation2 has two purposes:
190     //  1) Ensure, that routing is always from a data source to a data target
191     //  2) To find the correct base-class. This is delegated to RouteImplementationBase
192     template <class Source, class Target>
193     class RouteImplementation2
194         : public RouteImplementationBase<Source,Target>::base
195     {
196         typedef typename RouteImplementationBase<Source,Target>::base Base;
197
198         BOOST_STATIC_ASSERT( RoutingTraits<Source>::dataSource &&
199                              RoutingTraits<Target>::dataTarget );
200
201     protected:
202         RouteImplementation2(module::Module & module, Source & source, Target & target);
203     };
204
205     // RouteImplementation just forwards to RouteImplementation2 replacing the template arguments
206     // with the appropriately generalized type: If either Source or Target is an Event type, it is
207     // replaced with the general Event base-class EventDescriptor. Connector types are left as is.
208     template <class Source, class Target>
209     class RouteImplementation
210         : public RouteImplementation2<typename RoutingTraits<Source>::type,
211                                       typename RoutingTraits<Target>::type>
212     {
213         typedef RouteImplementation2<typename RoutingTraits<Source>::type,
214                                      typename RoutingTraits<Target>::type> Base;
215
216     protected:
217         RouteImplementation(module::Module & module, Source & source, Target & target);
218     };
219
220 }}}
221
222 #endif
223
224 //-/////////////////////////////////////////////////////////////////////////////////////////////////
225 #endif
226
227 \f
228 // Local Variables:
229 // mode: c++
230 // fill-column: 100
231 // comment-column: 40
232 // c-file-style: "senf"
233 // indent-tabs-mode: nil
234 // ispell-local-dictionary: "american"
235 // compile-command: "scons -u test"
236 // End: