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 PacketImpl internal header */
26 #ifndef IH_SENF_senf_Packets_PacketImpl_
27 #define IH_SENF_senf_Packets_PacketImpl_ 1
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>
43 ///////////////////////////////ih.p////////////////////////////////////////
47 struct ComplexAnnotation;
48 void dumpPacketAnnotationRegistry(std::ostream & os);
52 template <class Annotation>
53 struct IsComplexAnnotation
54 : public boost::mpl::or_< boost::is_convertible<Annotation*, ComplexAnnotation*>,
56 boost::mpl::sizeof_<Annotation>,
57 boost::mpl::int_<SENF_PACKET_ANNOTATION_SLOTSIZE> > >
60 class AnnotationRegistry
61 : public senf::singleton<AnnotationRegistry>
66 using senf::singleton<AnnotationRegistry>::instance;
68 template <class Annotation> class RegistrationProxy;
71 template <class Annotation> class Entry;
73 template <class Annotation>
74 key_t registerAnnotation();
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;
81 template <class Annotation>
82 static key_t lookup();
84 key_t keyBegin() const;
87 void dumpRegistrations(std::ostream & os);
92 struct RegistrationBase
94 virtual ~RegistrationBase () {};
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;
102 template <class Annotation>
104 : public RegistrationBase
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); }
116 key_t simpleAnnotationCount_;
117 key_t complexAnnotationCount_;
119 typedef boost::ptr_map<key_t, RegistrationBase> Registry;
122 friend class senf::singleton<AnnotationRegistry>;
125 template <class Annotation>
126 class AnnotationRegistry::RegistrationProxy
131 AnnotationRegistry::Entry<Annotation>::key_ =
132 AnnotationRegistry::instance().registerAnnotation<Annotation>();
136 class AnnotationRegistry::EntryBase
139 virtual ~EntryBase() {}
141 virtual void * get() = 0;
144 template <class Annotation>
145 class AnnotationRegistry::Entry
146 : public AnnotationRegistry::EntryBase
148 static RegistrationProxy<Annotation> proxy_;
149 static AnnotationRegistry::key_t key_;
151 // We use this member to force instantiation of proxy_ ...
152 static AnnotationRegistry::key_t key()
153 { senf::IGNORE(&proxy_); return key_; }
155 virtual void * get() { return & annotation_; }
158 Annotation annotation_;
160 friend class AnnotationRegistry::RegistrationProxy<Annotation>;
165 ///////////////////////////////ih.e////////////////////////////////////////
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"