Packets: New Annotation implementation
[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 <string>
32 #include <boost/type_traits/is_convertible.hpp>
33 #include <boost/mpl/sizeof.hpp>
34 #include <boost/mpl/int.hpp>
35 #include <boost/mpl/or.hpp>
36 #include <boost/mpl/greater.hpp>
37 #include <boost/ptr_container/ptr_map.hpp>
38 #include <senf/Utils/TypeInfo.hh>
39 #include <senf/Utils/singleton.hh>
40 #include <senf/config.hh>
41 #include <senf/Utils/IgnoreValue.hh>
42
43 ///////////////////////////////ih.p////////////////////////////////////////
44
45 namespace senf {
46
47     struct ComplexAnnotation;
48     void dumpPacketAnnotationRegistry(std::ostream & os);
49
50 namespace detail {
51
52     template <class Annotation>
53     struct IsComplexAnnotation
54         : public boost::mpl::or_< boost::is_convertible<Annotation*, ComplexAnnotation*>,
55                                   boost::mpl::greater<
56                                       boost::mpl::sizeof_<Annotation>,
57                                       boost::mpl::int_<SENF_PACKET_ANNOTATION_SLOTSIZE> > >
58     {};
59
60     class AnnotationRegistry
61         : public senf::singleton<AnnotationRegistry>
62     {
63     public:
64         typedef int key_t;
65
66         using senf::singleton<AnnotationRegistry>::instance;
67
68         template <class Annotation> class RegistrationProxy;
69
70         class EntryBase;
71         template <class Annotation> class Entry;
72
73         template <class Annotation>
74         key_t registerAnnotation();
75
76         void dump(key_t key, std::ostream & os, void * annotation) const;
77         std::string name(key_t key) const;
78         bool isComplex(key_t key) const;
79         unsigned size(key_t key) const;
80
81         template <class Annotation>
82         static key_t lookup();
83
84         key_t keyBegin() const;
85         key_t keyEnd() const;
86
87         void dumpRegistrations(std::ostream & os);
88
89     private:
90         AnnotationRegistry();
91
92         struct RegistrationBase
93         {
94             virtual ~RegistrationBase () {};
95             key_t key;
96             virtual void v_dump(std::ostream & os, void * annotation) const = 0;
97             virtual std::string v_name() const = 0;
98             virtual bool v_isComplex() const = 0;
99             virtual unsigned v_size() const = 0;
100         };
101
102         template <class Annotation>
103         struct Registration
104             : public RegistrationBase
105         {
106             void v_dump(std::ostream & os, void * annotation) const
107                 { os << * static_cast<Annotation*>(annotation); }
108             std::string v_name() const
109                 { return prettyName(typeid(Annotation)); }
110             bool v_isComplex() const
111                 { return boost::is_convertible<Annotation*, ComplexAnnotation*>::value; }
112             unsigned v_size() const
113                 { return sizeof(Annotation); }
114         };
115
116         key_t simpleAnnotationCount_;
117         key_t complexAnnotationCount_;
118
119         typedef boost::ptr_map<key_t, RegistrationBase> Registry;
120         Registry registry_;
121
122         friend class senf::singleton<AnnotationRegistry>;
123     };
124
125     template <class Annotation>
126     class AnnotationRegistry::RegistrationProxy
127     {
128     public:
129         RegistrationProxy()
130             {
131                 AnnotationRegistry::Entry<Annotation>::key_ =
132                     AnnotationRegistry::instance().registerAnnotation<Annotation>();
133             }
134     };
135
136     class AnnotationRegistry::EntryBase
137     {
138     public:
139         virtual ~EntryBase() {}
140
141         virtual void * get() = 0;
142     };
143
144     template <class Annotation>
145     class AnnotationRegistry::Entry
146         : public AnnotationRegistry::EntryBase
147     {
148         static RegistrationProxy<Annotation> proxy_;
149         static AnnotationRegistry::key_t key_;
150     public:
151         // We use this member to force instantiation of proxy_ ...
152         static AnnotationRegistry::key_t key()
153             { senf::IGNORE(&proxy_); return key_; }
154
155         virtual void * get() { return & annotation_; }
156
157     private:
158         Annotation annotation_;
159
160         friend class AnnotationRegistry::RegistrationProxy<Annotation>;
161     };
162
163 }}
164
165 ///////////////////////////////ih.e////////////////////////////////////////
166 #endif
167
168 \f
169 // Local Variables:
170 // mode: c++
171 // fill-column: 100
172 // comment-column: 40
173 // c-file-style: "senf"
174 // indent-tabs-mode: nil
175 // ispell-local-dictionary: "american"
176 // compile-command: "scons -u test"
177 // End: