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
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>
46 ///////////////////////////////ih.p////////////////////////////////////////
50 struct ComplexAnnotation;
51 void dumpPacketAnnotationRegistry(std::ostream & os);
55 template <class Annotation>
56 struct IsComplexAnnotation
57 : public boost::mpl::or_< boost::is_convertible<Annotation*, ComplexAnnotation*>,
59 boost::mpl::sizeof_<Annotation>,
60 boost::mpl::int_<SENF_PACKET_ANNOTATION_SLOTSIZE> > >
63 class AnnotationRegistry
64 : public senf::singleton<AnnotationRegistry>
70 struct RegistrationBase
72 virtual ~RegistrationBase () {};
74 virtual void v_dump(std::ostream & os, void * annotation) const = 0;
75 virtual std::string v_name() const = 0;
76 virtual bool v_isComplex() const = 0;
77 virtual unsigned v_size() const = 0;
80 template <class Annotation>
82 : public RegistrationBase
84 void v_dump(std::ostream & os, void * annotation) const
85 { os << * static_cast<Annotation*>(annotation); }
86 std::string v_name() const
87 { return prettyName(typeid(Annotation)); }
88 bool v_isComplex() const
89 { return boost::is_convertible<Annotation*, ComplexAnnotation*>::value; }
90 unsigned v_size() const
91 { return sizeof(Annotation); }
94 typedef boost::ptr_map<key_type, RegistrationBase> Registry;
95 // Index must be a multi-map since two identically named classes
96 // both in the anonymous namespace both have the same demangled name.
97 // we could sort on the mangled name instead ...
98 typedef std::multimap<std::string, key_type> Index;
101 typedef boost::transform_iterator< ::__gnu_cxx::select2nd<Index::value_type>,
102 Index::const_iterator > iterator;
104 using senf::singleton<AnnotationRegistry>::instance;
106 template <class Annotation> class RegistrationProxy;
109 template <class Annotation> class Entry;
111 template <class Annotation>
112 key_type registerAnnotation();
114 void dump(key_type key, std::ostream & os, void * annotation) const;
115 std::string name(key_type key) const;
116 bool isComplex(key_type key) const;
117 unsigned size(key_type key) const;
119 template <class Annotation>
120 static key_type lookup();
122 iterator begin() const;
123 iterator end() const;
125 void dumpRegistrations(std::ostream & os);
128 AnnotationRegistry();
130 key_type simpleAnnotationCount_;
131 key_type complexAnnotationCount_;
134 // The index is needed to ensure a persistent and reproducible
135 // ordering of the annotations when dumping
138 friend class senf::singleton<AnnotationRegistry>;
141 template <class Annotation>
142 class AnnotationRegistry::RegistrationProxy
147 AnnotationRegistry::Entry<Annotation>::key_ =
148 AnnotationRegistry::instance().registerAnnotation<Annotation>();
152 class AnnotationRegistry::EntryBase
155 virtual ~EntryBase() {}
157 virtual void * get() = 0;
160 template <class Annotation>
161 class AnnotationRegistry::Entry
162 : public AnnotationRegistry::EntryBase
164 static RegistrationProxy<Annotation> proxy_;
165 static AnnotationRegistry::key_type key_;
167 // We use this member to force instantiation of proxy_ ...
168 static AnnotationRegistry::key_type key()
169 { senf::IGNORE(&proxy_); return key_; }
171 virtual void * get() { return & annotation_; }
174 Annotation annotation_;
176 friend class AnnotationRegistry::RegistrationProxy<Annotation>;
181 ///////////////////////////////ih.e////////////////////////////////////////
188 // comment-column: 40
189 // c-file-style: "senf"
190 // indent-tabs-mode: nil
191 // ispell-local-dictionary: "american"
192 // compile-command: "scons -u test"