4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 \brief Route internal header */
30 #include <boost/type_traits/is_convertible.hpp>
31 #include <boost/type_traits/is_base_of.hpp>
32 #include <boost/mpl/if.hpp>
33 #include <boost/mpl/bool.hpp>
34 #include <boost/static_assert.hpp>
36 ///////////////////////////////ih.p////////////////////////////////////////
44 // This is the RoutingTraits implementation for Connectors. Events are handled in the
45 // specialization below
46 template <class Connector, bool isEvent>
47 struct RoutingTraitsImplementation
49 BOOST_STATIC_ASSERT((boost::is_base_of<connector::Connector, Connector>::value));
51 static bool const notifySource = boost::is_base_of<
52 connector::ActiveConnector, Connector>::value;
53 static bool const notifyTarget = boost::is_base_of<
54 connector::PassiveConnector, Connector>::value;
56 static bool const dataSource = boost::is_base_of<
57 connector::InputConnector, Connector>::value;
58 static bool const dataTarget = boost::is_base_of<
59 connector::OutputConnector, Connector>::value;
61 typedef Connector type;
64 // RoutingTraits specialization for Event types. Events may be both dataSource or dataTarget but
65 // cannot be notifySource.
66 template <class Event>
67 struct RoutingTraitsImplementation<Event,true>
69 static bool const notifySource = false;
70 static bool const notifyTarget = true;
72 static bool const dataSource = true;
73 static bool const dataTarget = true;
75 typedef EventDescriptor type;
78 // The RoutingTraits give routing related information about the argument type:
79 // - Wether the type is a notifySource or notifyTarget
80 // - Wether the type is dataSource or dataTarget
81 // - Provide the generalized target type
83 // The real implementation is in RoutingTraitsImplementation which is appropriately specialized
85 template <class Object>
87 : public RoutingTraitsImplementation<Object,
88 boost::is_convertible<Object*,
89 EventDescriptor*>::value>
92 // This is the generic route implementation for all routes. It just provides access to the
94 template <class Source, class Target, class Base>
95 class BaseRouteImplementation
99 typedef Source source_type;
100 typedef Target target_type;
102 Source & source() const;
103 Target & target() const;
106 BaseRouteImplementation(module::Module & module, Source & source, Target & target);
113 // The ForwardingRouteImplementation is based on the same BaseRouteImplementation
114 // as non-forwarding routes are but injects a different base-class (the third template
115 // argument to BaseRouteImplementation). ForwardingRouteImplementation has two additional
117 // 1) Register the ForwardingRoute with the notifySource
118 // 2) Implement the abstract ForwardingRoute interface
120 // Since we don't know explicitly, which of Source or Target is the notifySource or
121 // notifyTarget, the implementation calls registerRoute and notifyThrottle/notifyUnthrottle on
122 // *both*, the source and target, however qualified with an additional argument of type
123 // boost::mpl::bool_ which is used to select the correct overloads, of which the 'false'
124 // overload always is a no-op. This way, only the correct call will generate any code, the
125 // disabled call will be optimized away.
126 template <class Source, class Target>
127 class ForwardingRouteImplementation
128 : public BaseRouteImplementation<Source, Target, ForwardingRoute>
130 typedef BaseRouteImplementation<Source, Target, ForwardingRoute> Base;
133 ForwardingRouteImplementation(module::Module & module, Source & source, Target & target);
136 // send a throttle/unthrottle notification only if the second argument is a 'true' type
137 template <class T> void notifyThrottle(T & ob, boost::mpl::bool_<true> const &);
138 template <class T> void notifyThrottle(T & ob, boost::mpl::bool_<false> const &);
139 template <class T> void notifyUnthrottle(T & ob, boost::mpl::bool_<true> const &);
140 template <class T> void notifyUnthrottle(T & ob, boost::mpl::bool_<false> const &);
142 template <class T> bool throttled(T & ob, boost::mpl::bool_<true> const &) const;
143 template <class T> bool throttled(T & ob, boost::mpl::bool_<false> const &) const;
145 virtual void v_notifyThrottle();
146 virtual void v_notifyUnthrottle();
147 virtual bool v_throttled() const;
150 // This helper class finds the base-class suitable for a specific route. Routes are classified
152 // 1) A forwarding routes is a routed which forwards notifications from a notifySource to a
153 // notifyTarget. Forwarding routes are implemneted using ForwardingRouteImplementation
154 // 2) Non-forwarding routes don't forward notifications. They are implemented directly
155 // using BaseRouteImplementation
156 template <class Source, class Target>
157 struct RouteImplementationBase
159 typedef RoutingTraits<Source> srcTrait;
160 typedef RoutingTraits<Target> trgTrait;
162 static bool const isForwarding = (srcTrait::notifySource && trgTrait::notifyTarget)
163 || (srcTrait::notifyTarget && trgTrait::notifySource);
165 typedef typename boost::mpl::if_c<
167 ForwardingRouteImplementation<Source,Target>,
168 BaseRouteImplementation<Source,Target,RouteBase> >::type base;
171 // RouteImplementation2 has two purposes:
172 // 1) Ensure, that routing is always from a data source to a data target
173 // 2) To find the correct base-class. This is delegated to RouteImplementationBase
174 template <class Source, class Target>
175 class RouteImplementation2
176 : public RouteImplementationBase<Source,Target>::base
178 typedef typename RouteImplementationBase<Source,Target>::base Base;
180 BOOST_STATIC_ASSERT( RoutingTraits<Source>::dataSource &&
181 RoutingTraits<Target>::dataTarget );
184 RouteImplementation2(module::Module & module, Source & source, Target & target);
187 // RouteImplementation just forwards to RouteImplementation2 replacing the template arguments
188 // with the appropriately generalized type: If either Source or Target is an Event type, it is
189 // replaced with the general Event base-class EventDescriptor. Connector types are left as is.
190 template <class Source, class Target>
191 class RouteImplementation
192 : public RouteImplementation2<typename RoutingTraits<Source>::type,
193 typename RoutingTraits<Target>::type>
195 typedef RouteImplementation2<typename RoutingTraits<Source>::type,
196 typename RoutingTraits<Target>::type> Base;
199 RouteImplementation(module::Module & module, Source & source, Target & target);
206 ///////////////////////////////ih.e////////////////////////////////////////
213 // comment-column: 40
214 // c-file-style: "senf"
215 // indent-tabs-mode: nil
216 // ispell-local-dictionary: "american"
217 // compile-command: "scons -u test"