Packets: Fix VariantParser invalid parser access bug
[senf.git] / PPI / Route.ih
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
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.
12 //
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.
17 //
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.
22
23 /** \file
24     \brief Route internal header */
25
26 #ifndef IH_SENF_PPI_Route_
27 #define IH_SENF_PPI_Route_ 1
28
29 // Custom includes
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>
35
36 ///////////////////////////////ih.p////////////////////////////////////////
37
38 #ifndef DOXYGEN
39
40 namespace senf {
41 namespace ppi {
42 namespace detail {
43
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
48     {
49         BOOST_STATIC_ASSERT((boost::is_base_of<connector::Connector, Connector>::value));
50         
51         static bool const event = false;
52
53         static bool const notifySource = boost::is_base_of<
54             connector::ActiveConnector, Connector>::value;
55         static bool const notifyTarget = boost::is_base_of<
56             connector::PassiveConnector, Connector>::value;
57
58         static bool const dataSource = boost::is_base_of<
59             connector::InputConnector, Connector>::value;
60         static bool const dataTarget = boost::is_base_of<
61             connector::OutputConnector, Connector>::value;
62         
63         typedef Connector type;
64     };
65
66     // RoutingTraits specialization for Event types. Events may be both dataSource or dataTarget but
67     // cannot be notifySource.
68     template <class Event>
69     struct RoutingTraitsImplementation<Event,true>
70     {
71         static bool const event = true;
72
73         static bool const notifySource = false;
74         static bool const notifyTarget = true;
75
76         static bool const dataSource = true;
77         static bool const dataTarget = true;
78
79         typedef EventDescriptor type;
80     };
81
82     // The RoutingTraits give routing related information about the argument type:
83     //  - Wether the type is a notifySource or notifyTarget
84     //  - Wether the type is dataSource or dataTarget
85     //  - Provide the generalized target type
86     //
87     // The real implementation is in RoutingTraitsImplementation which is appropriately specialized
88     // for Events
89     template <class Object>
90     struct RoutingTraits
91         : public RoutingTraitsImplementation<Object, 
92                                              boost::is_convertible<Object*,
93                                                                    EventDescriptor*>::value>
94     {};
95
96     // This is the generic route implementation for all routes. It just provides access to the 
97     // source and target.
98     template <class Source, class Target, class Base>
99     class BaseRouteImplementation
100         : public Base
101     {
102     public:
103         typedef Source source_type;
104         typedef Target target_type;
105
106         Source & source() const;
107         Target & target() const;
108
109     protected:
110         BaseRouteImplementation(module::Module & module, Source & source, Target & target);
111
112     private:
113         Source * source_;
114         Target * target_;
115     };
116
117     // The ForwardingRouteImplementation is based on the same BaseRouteImplementation
118     // as non-forwarding routes are but injects a different base-class (the third template
119     // argument to BaseRouteImplementation). ForwardingRouteImplementation has two additional
120     // functions:
121     //  1) Register the ForwardingRoute with the notifySource
122     //  2) Implement the abstract ForwardingRoute interface
123     //
124     // Since we don't know explicitly, which of Source or Target is the notifySource or
125     // notifyTarget, the implementation calls registerRoute and notifyThrottle/notifyUnthrottle on
126     // *both*, the source and target, however qualified with an additional argument of type
127     // boost::mpl::bool_ which is used to select the correct overloads, of which the 'false'
128     // overload always is a no-op. This way, only the correct call will generate any code, the
129     // disabled call will be optimized away.
130     template <class Source, class Target>
131     class ForwardingRouteImplementation
132         : public BaseRouteImplementation<Source, Target, ForwardingRoute>
133     {
134         typedef BaseRouteImplementation<Source, Target, ForwardingRoute> Base;
135         
136     protected:
137         ForwardingRouteImplementation(module::Module & module, Source & source, Target & target);
138
139     private:
140         // send a throttle/unthrottle notification  only if the second argument is a 'true' type
141         template <class T> void notifyThrottle(T & ob, boost::mpl::bool_<true> const &);
142         template <class T> void notifyThrottle(T & ob, boost::mpl::bool_<false> const &);
143         template <class T> void notifyUnthrottle(T & ob, boost::mpl::bool_<true> const &);
144         template <class T> void notifyUnthrottle(T & ob, boost::mpl::bool_<false> const &);
145
146         template <class T> bool throttled(T & ob, boost::mpl::bool_<true> const &) const;
147         template <class T> bool throttled(T & ob, boost::mpl::bool_<false> const &) const;
148
149         virtual void v_notifyThrottle();
150         virtual void v_notifyUnthrottle();
151         virtual bool v_throttled() const;
152     };
153
154     // This helper class finds the base-class suitable for a specific route. Routes are classified
155     // into two groups: 
156     //  1) A forwarding routes is a routed which forwards notifications from a notifySource to a
157     //     notifyTarget. Forwarding routes are implemneted using ForwardingRouteImplementation
158     //  2) Non-forwarding routes don't forward notifications. They are implemented directly
159     //     using BaseRouteImplementation
160     template <class Source, class Target>
161     struct RouteImplementationBase
162     {
163         typedef RoutingTraits<Source> srcTrait;
164         typedef RoutingTraits<Target> trgTrait;
165
166         static bool const isForwarding = (srcTrait::notifySource && trgTrait::notifyTarget)
167             || (srcTrait::notifyTarget && trgTrait::notifySource);
168         
169         typedef typename boost::mpl::if_c<
170             isForwarding, 
171             ForwardingRouteImplementation<Source,Target>, 
172             BaseRouteImplementation<Source,Target,RouteBase> >::type base;
173     };
174
175     // RouteImplementation2 has two purposes: 
176     //  1) Ensure, that routing is always from a data source to a data target
177     //  2) To find the correct base-class. This is delegated to RouteImplementationBase
178     template <class Source, class Target>
179     class RouteImplementation2
180         : public RouteImplementationBase<Source,Target>::base
181     {
182         typedef typename RouteImplementationBase<Source,Target>::base Base;
183
184         BOOST_STATIC_ASSERT( RoutingTraits<Source>::dataSource && 
185                              RoutingTraits<Target>::dataTarget );
186
187     protected:
188         RouteImplementation2(module::Module & module, Source & source, Target & target);
189     };
190
191     // RouteImplementation just forwards to RouteImplementation2 replacing the template arguments
192     // with the appropriately generalized type: If either Source or Target is an Event type, it is
193     // replaced with the general Event base-class EventDescriptor. Connector types are left as is.
194     template <class Source, class Target>
195     class RouteImplementation
196         : public RouteImplementation2<typename RoutingTraits<Source>::type,
197                                       typename RoutingTraits<Target>::type>
198     {
199         typedef RouteImplementation2<typename RoutingTraits<Source>::type,
200                                      typename RoutingTraits<Target>::type> Base;
201
202     protected:
203         RouteImplementation(module::Module & module, Source & source, Target & target);
204     };
205
206 }}}
207
208 #endif
209
210 ///////////////////////////////ih.e////////////////////////////////////////
211 #endif
212
213 \f
214 // Local Variables:
215 // mode: c++
216 // fill-column: 100
217 // comment-column: 40
218 // c-file-style: "senf"
219 // indent-tabs-mode: nil
220 // ispell-local-dictionary: "american"
221 // compile-command: "scons -u test"
222 // End: