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 //-/////////////////////////////////////////////////////////////////////////////////////////////////
50 struct ComplexAnnotation;
54 template <class Annotation>
55 struct IsComplexAnnotation
56 : public boost::mpl::or_< boost::is_convertible<Annotation*, ComplexAnnotation*>,
58 boost::mpl::sizeof_<Annotation>,
59 boost::mpl::int_<SENF_PACKET_ANNOTATION_SLOTSIZE> > >
62 class AnnotationRegistry
63 : public senf::singleton<AnnotationRegistry>
69 struct RegistrationBase
71 virtual ~RegistrationBase () {};
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;
79 template <class Annotation>
81 : public RegistrationBase
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); }
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;
100 typedef boost::transform_iterator< ::__gnu_cxx::select2nd<Index::value_type>,
101 Index::const_iterator > iterator;
103 using senf::singleton<AnnotationRegistry>::instance;
105 template <class Annotation> class RegistrationProxy;
108 template <class Annotation> class Entry;
110 template <class Annotation>
111 key_type registerAnnotation();
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;
118 template <class Annotation>
119 static key_type lookup();
121 iterator begin() const;
122 iterator end() const;
124 void dumpRegistrations(std::ostream & os);
127 AnnotationRegistry();
129 key_type simpleAnnotationCount_;
130 key_type complexAnnotationCount_;
133 // The index is needed to ensure a persistent and reproducible
134 // ordering of the annotations when dumping
137 friend class senf::singleton<AnnotationRegistry>;
140 template <class Annotation>
141 class AnnotationRegistry::RegistrationProxy
146 AnnotationRegistry::Entry<Annotation>::key_ =
147 AnnotationRegistry::instance().registerAnnotation<Annotation>();
151 class AnnotationRegistry::EntryBase
154 virtual ~EntryBase() {}
156 virtual void * get() = 0;
158 typedef EntryBase * ptr;
159 virtual ptr clone() const = 0;
162 inline AnnotationRegistry::EntryBase::ptr new_clone( AnnotationRegistry::EntryBase const & entry)
164 return entry.clone();
167 template <class Annotation>
168 class AnnotationRegistry::Entry
169 : public AnnotationRegistry::EntryBase
171 static RegistrationProxy<Annotation> proxy_;
172 static AnnotationRegistry::key_type key_;
174 // We use this member to force instantiation of proxy_ ...
175 static AnnotationRegistry::key_type key()
176 { senf::IGNORE(&proxy_); return key_; }
178 virtual void * get() { return & annotation_; }
179 virtual EntryBase::ptr clone() const { return new Entry<Annotation>( *this); }
182 Annotation annotation_;
184 friend class AnnotationRegistry::RegistrationProxy<Annotation>;
189 //-/////////////////////////////////////////////////////////////////////////////////////////////////
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"