619859fecc17c278621e272da42eefdbc484a06c
[senf.git] / senf / Packets / PacketImpl.ih
1 // $Id$
2 //
3 // Copyright (C) 2010
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 PacketImpl internal header */
25
26 #ifndef IH_SENF_senf_Packets_PacketImpl_
27 #define IH_SENF_senf_Packets_PacketImpl_ 1
28
29 // Custom includes
30 #include <iostream>
31 #include <map>
32 #include <string>
33 #include <ext/functional>
34 #include <boost/iterator/transform_iterator.hpp>
35 #include <boost/type_traits/is_convertible.hpp>
36 #include <boost/mpl/sizeof.hpp>
37 #include <boost/mpl/int.hpp>
38 #include <boost/mpl/or.hpp>
39 #include <boost/mpl/greater.hpp>
40 #include <boost/ptr_container/ptr_map.hpp>
41 #include <senf/Utils/TypeInfo.hh>
42 #include <senf/Utils/singleton.hh>
43 #include <senf/config.hh>
44 #include <senf/Utils/IgnoreValue.hh>
45
46 //-/////////////////////////////////////////////////////////////////////////////////////////////////
47
48 namespace senf {
49
50     struct ComplexAnnotation;
51
52 namespace detail {
53
54     template <class Annotation>
55     struct IsComplexAnnotation
56         : public boost::mpl::or_< boost::is_convertible<Annotation*, ComplexAnnotation*>,
57                                   boost::mpl::greater<
58                                       boost::mpl::sizeof_<Annotation>,
59                                       boost::mpl::int_<SENF_PACKET_ANNOTATION_SLOTSIZE> > >
60     {};
61
62     class AnnotationRegistry
63         : public senf::singleton<AnnotationRegistry>
64     {
65     public:
66         typedef int key_type;
67
68     private:
69         struct RegistrationBase
70         {
71             virtual ~RegistrationBase () {};
72             key_type key;
73             virtual void v_dump(std::ostream & os, void * annotation) const = 0;
74             virtual std::string v_name() const = 0;
75             virtual bool v_isComplex() const = 0;
76             virtual unsigned v_size() const = 0;
77         };
78
79         template <class Annotation>
80         struct Registration
81             : public RegistrationBase
82         {
83             void v_dump(std::ostream & os, void * annotation) const
84                 { os << * static_cast<Annotation*>(annotation); }
85             std::string v_name() const
86                 { return prettyName(typeid(Annotation)); }
87             bool v_isComplex() const
88                 { return boost::is_convertible<Annotation*, ComplexAnnotation*>::value; }
89             unsigned v_size() const
90                 { return sizeof(Annotation); }
91         };
92
93         typedef boost::ptr_map<key_type, RegistrationBase> Registry;
94         // Index must be a multi-map since two identically named classes
95         // both in the anonymous namespace both have the same demangled name.
96         // we could sort on the mangled name instead ...
97         typedef std::multimap<std::string, key_type> Index;
98
99     public:
100         typedef boost::transform_iterator< ::__gnu_cxx::select2nd<Index::value_type>,
101                                            Index::const_iterator > iterator;
102
103         using senf::singleton<AnnotationRegistry>::instance;
104
105         template <class Annotation> class RegistrationProxy;
106
107         class EntryBase;
108         template <class Annotation> class Entry;
109
110         template <class Annotation>
111         key_type registerAnnotation();
112
113         void dump(key_type key, std::ostream & os, void * annotation) const;
114         std::string name(key_type key) const;
115         bool isComplex(key_type key) const;
116         unsigned size(key_type key) const;
117
118         template <class Annotation>
119         static key_type lookup();
120
121         iterator begin() const;
122         iterator end() const;
123
124         void dumpRegistrations(std::ostream & os);
125
126     private:
127         AnnotationRegistry();
128
129         key_type simpleAnnotationCount_;
130         key_type complexAnnotationCount_;
131
132         Registry registry_;
133         // The index is needed to ensure a persistent and reproducible
134         // ordering of the annotations when dumping
135         Index index_;
136
137         friend class senf::singleton<AnnotationRegistry>;
138     };
139
140     template <class Annotation>
141     class AnnotationRegistry::RegistrationProxy
142     {
143     public:
144         RegistrationProxy()
145             {
146                 AnnotationRegistry::Entry<Annotation>::key_ =
147                     AnnotationRegistry::instance().registerAnnotation<Annotation>();
148             }
149     };
150
151     class AnnotationRegistry::EntryBase
152     {
153     public:
154         virtual ~EntryBase() {}
155
156         virtual void * get() = 0;
157
158         typedef EntryBase * ptr;
159         virtual ptr clone() const = 0;
160     };
161
162     inline AnnotationRegistry::EntryBase::ptr new_clone( AnnotationRegistry::EntryBase const & entry)
163     {
164         return entry.clone();
165     }
166
167     template <class Annotation>
168     class AnnotationRegistry::Entry
169         : public AnnotationRegistry::EntryBase
170     {
171         static RegistrationProxy<Annotation> proxy_;
172         static AnnotationRegistry::key_type key_;
173     public:
174         // We use this member to force instantiation of proxy_ ...
175         static AnnotationRegistry::key_type key()
176             { senf::IGNORE(&proxy_); return key_; }
177
178         virtual void * get() { return & annotation_; }
179         virtual EntryBase::ptr clone() const { return new Entry<Annotation>( *this); }
180
181     private:
182         Annotation annotation_;
183
184         friend class AnnotationRegistry::RegistrationProxy<Annotation>;
185     };
186
187 }}
188
189 //-/////////////////////////////////////////////////////////////////////////////////////////////////
190 #endif
191
192 \f
193 // Local Variables:
194 // mode: c++
195 // fill-column: 100
196 // comment-column: 40
197 // c-file-style: "senf"
198 // indent-tabs-mode: nil
199 // ispell-local-dictionary: "american"
200 // compile-command: "scons -u test"
201 // End: