g++/final: don't add extra code to check for buffer overflows (-fno-stack-protector)
[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     //  - Whether the type is a notifySource or notifyTarget
89     //  - Whether 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     protected:
108         typedef Source source_type;
109         typedef Target target_type;
110
111         Source & source() const;
112         Target & target() const;
113
114         BaseRouteImplementation(module::Module & module, Source & source, Target & target);
115
116     private:
117         bool v_hasConnector(connector::Connector const & conn) const;
118         bool v_hasEvent(EventDescriptor const & event) const;
119
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;
124
125         Source * source_;
126         Target * target_;
127     };
128
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
132     // functions:
133     //  1) Register the ForwardingRoute with the notifySource
134     //  2) Implement the abstract ForwardingRoute interface
135     //
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>
145     {
146         typedef BaseRouteImplementation<Source, Target, ForwardingRoute> Base;
147
148     protected:
149         ForwardingRouteImplementation(module::Module & module, Source & source, Target & target);
150         ~ForwardingRouteImplementation();
151
152     private:
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 &);
158
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;
161
162         virtual void v_notifyThrottle();
163         virtual void v_notifyUnthrottle();
164         virtual bool v_throttled() const;
165     };
166
167     // This helper class finds the base-class suitable for a specific route. Routes are classified
168     // into two groups:
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
175     {
176         typedef RoutingTraits<Source> srcTrait;
177         typedef RoutingTraits<Target> trgTrait;
178
179         static bool const isForwarding = (srcTrait::notifySource && trgTrait::notifyTarget)
180             || (srcTrait::notifyTarget && trgTrait::notifySource);
181
182         typedef typename boost::mpl::if_c<
183             isForwarding,
184             ForwardingRouteImplementation<Source,Target>,
185             BaseRouteImplementation<Source,Target,RouteBase> >::type base;
186     };
187
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
194     {
195         typedef typename RouteImplementationBase<Source,Target>::base Base;
196
197         BOOST_STATIC_ASSERT( RoutingTraits<Source>::dataSource &&
198                              RoutingTraits<Target>::dataTarget );
199
200     protected:
201         RouteImplementation2(module::Module & module, Source & source, Target & target);
202     };
203
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>
211     {
212         typedef RouteImplementation2<typename RoutingTraits<Source>::type,
213                                      typename RoutingTraits<Target>::type> Base;
214
215     protected:
216         RouteImplementation(module::Module & module, Source & source, Target & target);
217     };
218
219 }}}
220
221 #endif
222
223 //-/////////////////////////////////////////////////////////////////////////////////////////////////
224 #endif
225
226 \f
227 // Local Variables:
228 // mode: c++
229 // fill-column: 100
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"
235 // End: