Utils/Daemon: Add warning when the scheduler has registered events at a fork()
[senf.git] / Packets / PacketImpl.hh
1 // $Id$
2 //
3 // Copyright (C) 2007
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 public header */
25
26 #ifndef HH_PacketImpl_
27 #define HH_PacketImpl_ 1
28
29 // Custom includes
30 #include <memory>
31 #include <vector>
32 #include <boost/utility.hpp>
33 #include "../Utils/pool_alloc_mixin.hh"
34 #include "PacketTypes.hh"
35 #include "../Utils/singleton.hh"
36
37 //#include "PacketImpl.mpp"
38 ///////////////////////////////hh.p////////////////////////////////////////
39
40 namespace senf {
41 namespace detail {
42
43     struct AnnotationIndexerBase
44     {
45         static unsigned maxAnnotations;
46         static std::vector<bool> & small();
47     };
48
49     template <class Annotation>
50     struct AnnotationIndexer 
51         : public senf::singleton< AnnotationIndexer<Annotation> >, 
52           public AnnotationIndexerBase
53     {
54         AnnotationIndexer();
55         unsigned index_;
56         static unsigned index();
57         static bool const Small = (sizeof(Annotation) <= sizeof(void*));
58     };
59
60     struct AnnotationP
61     {
62         virtual ~AnnotationP();
63     };
64
65     template <class Annotation>
66     struct TAnnotationP
67         : public AnnotationP
68     {
69         Annotation annotation;
70     };
71
72     template <class Annotation, bool Small = AnnotationIndexer<Annotation>::Small>
73     struct GetAnnotation
74     {
75         static Annotation & get(AnnotationP * & p);
76     };
77
78 /*
79     template <class Annotation>
80     struct GetAnnotation<Annotation, true>
81     {
82         static Annotation & get(AnnotationP * & p);
83     };
84 */
85
86     /** \brief Internal: Packet data storage
87
88         \internal
89
90         This is the class holding the packet data and the interpreter chain. All manipulations of
91         the packet data are performed via the interface exported here. This is very important, since
92         PacketImpl will update the interpreters (that is the vector indices stored therein) whenever
93         the data is changed.
94      */
95     class PacketImpl
96         : boost::noncopyable,
97           public pool_alloc_mixin<PacketImpl>
98     {
99     public:
100         typedef senf::detail::packet::byte byte;
101         typedef senf::detail::packet::raw_container raw_container;
102         typedef senf::detail::packet::size_type size_type;
103         typedef senf::detail::packet::difference_type difference_type;
104         typedef senf::detail::packet::interpreter_list interpreter_list;
105         typedef senf::detail::packet::iterator iterator;
106         typedef senf::detail::packet::const_iterator const_iterator;
107         typedef senf::detail::packet::refcount_t refcount_t;
108
109         // structors
110
111         PacketImpl();
112         PacketImpl(size_type size, byte initValue);
113         template <class InputIterator>
114         PacketImpl(InputIterator b, InputIterator e);
115         ~PacketImpl();
116
117         // rerference/memory management
118
119         void add_ref(refcount_t n=1);
120         void release(refcount_t n=1);
121         refcount_t refcount() const;
122
123         // Interpreter chain
124
125         PacketInterpreterBase * first();
126         PacketInterpreterBase * last();
127
128         PacketInterpreterBase * next(PacketInterpreterBase * p);
129         PacketInterpreterBase * prev(PacketInterpreterBase * p);
130
131         void appendInterpreter    (PacketInterpreterBase * p);
132         void prependInterpreter   (PacketInterpreterBase * p);
133         void truncateInterpreters (PacketInterpreterBase * p);
134         void truncateInterpretersBackwards (PacketInterpreterBase * p);
135
136         // Data container
137
138         iterator begin();
139         iterator end();
140         size_type size();
141
142         void insert(PacketData * self, iterator pos, byte v);
143         void insert(PacketData * self, iterator pos, size_type n, byte v);
144         template <class ForwardIterator>
145         void insert(PacketData * self, iterator pos, ForwardIterator f, ForwardIterator l);
146
147         void erase(PacketData * self, iterator pos);
148         void erase(PacketData * self, iterator first, iterator last);
149         void clear(PacketData * self);
150
151         // Annotations
152         template <class Annotation>
153         Annotation & annotation();
154
155         /** \brief Internal: Keep PacketImpl instance alive
156
157             \internal
158
159             The Guard will keep the PacketImpl instance alive during a members execution time
160             It the refcount should drop to 0, PacketImpl will be deleted after the member
161             has completed executing.
162          */
163         struct Guard {
164             Guard(PacketImpl * impl);
165             ~Guard();
166             PacketImpl * p;
167         };
168
169     private:
170         refcount_t refcount_;
171         raw_container data_;
172         interpreter_list interpreters_;
173         
174         typedef std::vector<AnnotationP*> Annotations;
175         Annotations annotations_;
176
177         void eraseInterpreters(interpreter_list::iterator b, interpreter_list::iterator e);
178         void updateIterators(PacketData * self, difference_type pos, difference_type n);
179     };
180
181 }}
182
183 ///////////////////////////////hh.e////////////////////////////////////////
184 #endif
185 #if !defined(HH_Packets__decls_) && !defined(HH_PacketImpl_i_)
186 #define HH_PacketImpl_i_
187 #include "PacketImpl.cci"
188 //#include "PacketImpl.ct"
189 #include "PacketImpl.cti"
190 #endif
191
192 \f
193 // Local Variables:
194 // mode: c++
195 // fill-column: 100
196 // c-file-style: "senf"
197 // indent-tabs-mode: nil
198 // ispell-local-dictionary: "american"
199 // compile-command: "scons -u test"
200 // comment-column: 40
201 // End:
202