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 // - Whether the type is a notifySource or notifyTarget
89 // - Whether 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;
114 BaseRouteImplementation(module::Module & module, Source & source, Target & target);
117 bool v_hasConnector(connector::Connector const & conn) const;
118 bool v_hasEvent(EventDescriptor const & event) const;
120 bool isSame(connector::Connector const & conn, connector::Connector const & other) const;
121 bool isSame(connector::Connector const & conn, EventDescriptor const & other) const;
122 bool isSame(EventDescriptor const & event, connector::Connector const & other) const;
123 bool isSame(EventDescriptor const & event, EventDescriptor const & other) const;
129 // The ForwardingRouteImplementation is based on the same BaseRouteImplementation
130 // as non-forwarding routes are but injects a different base-class (the third template
131 // argument to BaseRouteImplementation). ForwardingRouteImplementation has two additional
133 // 1) Register the ForwardingRoute with the notifySource
134 // 2) Implement the abstract ForwardingRoute interface
136 // Since we don't know explicitly, which of Source or Target is the notifySource or
137 // notifyTarget, the implementation calls registerRoute and notifyThrottle/notifyUnthrottle on
138 // *both*, the source and target, however qualified with an additional argument of type
139 // boost::mpl::bool_ which is used to select the correct overloads, of which the 'false'
140 // overload always is a no-op. This way, only the correct call will generate any code, the
141 // disabled call will be optimized away.
142 template <class Source, class Target>
143 class ForwardingRouteImplementation
144 : public BaseRouteImplementation<Source, Target, ForwardingRoute>
146 typedef BaseRouteImplementation<Source, Target, ForwardingRoute> Base;
149 ForwardingRouteImplementation(module::Module & module, Source & source, Target & target);
150 ~ForwardingRouteImplementation();
153 // send a throttle/unthrottle notification only if the second argument is a 'true' type
154 template <class T> void notifyThrottle(T & ob, boost::mpl::bool_<true> const &);
155 template <class T> void notifyThrottle(T & ob, boost::mpl::bool_<false> const &);
156 template <class T> void notifyUnthrottle(T & ob, boost::mpl::bool_<true> const &);
157 template <class T> void notifyUnthrottle(T & ob, boost::mpl::bool_<false> const &);
159 template <class T> bool throttled(T & ob, boost::mpl::bool_<true> const &) const;
160 template <class T> bool throttled(T & ob, boost::mpl::bool_<false> const &) const;
162 virtual void v_notifyThrottle();
163 virtual void v_notifyUnthrottle();
164 virtual bool v_throttled() const;
167 // This helper class finds the base-class suitable for a specific route. Routes are classified
169 // 1) A forwarding routes is a routed which forwards notifications from a notifySource to a
170 // notifyTarget. Forwarding routes are implemented using ForwardingRouteImplementation
171 // 2) Non-forwarding routes don't forward notifications. They are implemented directly
172 // using BaseRouteImplementation
173 template <class Source, class Target>
174 struct RouteImplementationBase
176 typedef RoutingTraits<Source> srcTrait;
177 typedef RoutingTraits<Target> trgTrait;
179 static bool const isForwarding = (srcTrait::notifySource && trgTrait::notifyTarget)
180 || (srcTrait::notifyTarget && trgTrait::notifySource);
182 typedef typename boost::mpl::if_c<
184 ForwardingRouteImplementation<Source,Target>,
185 BaseRouteImplementation<Source,Target,RouteBase> >::type base;
188 // RouteImplementation2 has two purposes:
189 // 1) Ensure, that routing is always from a data source to a data target
190 // 2) To find the correct base-class. This is delegated to RouteImplementationBase
191 template <class Source, class Target>
192 class RouteImplementation2
193 : public RouteImplementationBase<Source,Target>::base
195 typedef typename RouteImplementationBase<Source,Target>::base Base;
197 BOOST_STATIC_ASSERT( RoutingTraits<Source>::dataSource &&
198 RoutingTraits<Target>::dataTarget );
201 RouteImplementation2(module::Module & module, Source & source, Target & target);
204 // RouteImplementation just forwards to RouteImplementation2 replacing the template arguments
205 // with the appropriately generalized type: If either Source or Target is an Event type, it is
206 // replaced with the general Event base-class EventDescriptor. Connector types are left as is.
207 template <class Source, class Target>
208 class RouteImplementation
209 : public RouteImplementation2<typename RoutingTraits<Source>::type,
210 typename RoutingTraits<Target>::type>
212 typedef RouteImplementation2<typename RoutingTraits<Source>::type,
213 typename RoutingTraits<Target>::type> Base;
216 RouteImplementation(module::Module & module, Source & source, Target & target);
223 //-/////////////////////////////////////////////////////////////////////////////////////////////////
230 // comment-column: 40
231 // c-file-style: "senf"
232 // indent-tabs-mode: nil
233 // ispell-local-dictionary: "american"
234 // compile-command: "scons -u test"