4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
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
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.
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.
19 // The Original Code is Fraunhofer FOKUS code.
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.
26 // Stefan Bund <g0dil@berlios.de>
29 \brief Route internal header */
31 #ifndef IH_SENF_PPI_Route_
32 #define IH_SENF_PPI_Route_ 1
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>
41 //-/////////////////////////////////////////////////////////////////////////////////////////////////
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
54 BOOST_STATIC_ASSERT((boost::is_base_of<connector::Connector, Connector>::value));
56 static bool const event = false;
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;
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;
68 typedef Connector type;
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>
76 static bool const event = true;
78 static bool const notifySource = false;
79 static bool const notifyTarget = true;
81 static bool const dataSource = true;
82 static bool const dataTarget = true;
84 typedef EventDescriptor type;
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
92 // The real implementation is in RoutingTraitsImplementation which is appropriately specialized
94 template <class Object>
96 : public RoutingTraitsImplementation<Object,
97 boost::is_convertible<Object*,
98 EventDescriptor*>::value>
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
108 typedef Source source_type;
109 typedef Target target_type;
111 Source & source() const;
112 Target & target() const;
115 BaseRouteImplementation(module::Module & module, Source & source, Target & target);
118 bool v_hasConnector(connector::Connector const & conn) const;
119 bool v_hasEvent(EventDescriptor const & event) const;
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;
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
134 // 1) Register the ForwardingRoute with the notifySource
135 // 2) Implement the abstract ForwardingRoute interface
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>
147 typedef BaseRouteImplementation<Source, Target, ForwardingRoute> Base;
150 ForwardingRouteImplementation(module::Module & module, Source & source, Target & target);
151 ~ForwardingRouteImplementation();
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 &);
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;
163 virtual void v_notifyThrottle();
164 virtual void v_notifyUnthrottle();
165 virtual bool v_throttled() const;
168 // This helper class finds the base-class suitable for a specific route. Routes are classified
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
177 typedef RoutingTraits<Source> srcTrait;
178 typedef RoutingTraits<Target> trgTrait;
180 static bool const isForwarding = (srcTrait::notifySource && trgTrait::notifyTarget)
181 || (srcTrait::notifyTarget && trgTrait::notifySource);
183 typedef typename boost::mpl::if_c<
185 ForwardingRouteImplementation<Source,Target>,
186 BaseRouteImplementation<Source,Target,RouteBase> >::type base;
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
196 typedef typename RouteImplementationBase<Source,Target>::base Base;
198 BOOST_STATIC_ASSERT( RoutingTraits<Source>::dataSource &&
199 RoutingTraits<Target>::dataTarget );
202 RouteImplementation2(module::Module & module, Source & source, Target & target);
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>
213 typedef RouteImplementation2<typename RoutingTraits<Source>::type,
214 typename RoutingTraits<Target>::type> Base;
217 RouteImplementation(module::Module & module, Source & source, Target & target);
224 //-/////////////////////////////////////////////////////////////////////////////////////////////////
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"