/src/resiprocate/resip/stack/ParserContainerBase.hxx
Line | Count | Source |
1 | | #ifndef RESIP_ParserContainerBase_hxx |
2 | | #define RESIP_ParserContainerBase_hxx |
3 | | |
4 | | #include "resip/stack/ParserCategory.hxx" |
5 | | #include <iosfwd> |
6 | | #include "resip/stack/HeaderTypes.hxx" |
7 | | #include <vector> |
8 | | |
9 | | #include "rutil/StlPoolAllocator.hxx" |
10 | | #include "rutil/PoolBase.hxx" |
11 | | |
12 | | namespace resip |
13 | | { |
14 | | |
15 | | class HeaderFieldValueList; |
16 | | class PoolBase; |
17 | | |
18 | | /** |
19 | | @class ParserContainerBase |
20 | | @brief Abstract Base class implemented in derived class ParserContainer |
21 | | */ |
22 | | class ParserContainerBase |
23 | | { |
24 | | public: |
25 | | typedef size_t size_type; |
26 | | |
27 | | /** |
28 | | @brief constructor; sets the type only |
29 | | */ |
30 | | ParserContainerBase(Headers::Type type = Headers::UNKNOWN); |
31 | | |
32 | | /** |
33 | | @brief constructor; sets the type only |
34 | | */ |
35 | | ParserContainerBase(Headers::Type type, |
36 | | PoolBase& pool); |
37 | | |
38 | | /** |
39 | | @brief copy constructor copies the mType and the mParsers from the rhs |
40 | | @note this is a shallow copy |
41 | | */ |
42 | | ParserContainerBase(const ParserContainerBase& rhs); |
43 | | |
44 | | ParserContainerBase(const ParserContainerBase& rhs, |
45 | | PoolBase& pool); |
46 | | |
47 | | /** |
48 | | @brief assignment operator copies the mParsers from the rhs |
49 | | @note this is a shallow copy |
50 | | */ |
51 | | ParserContainerBase& operator=(const ParserContainerBase& rhs); |
52 | | |
53 | | /** |
54 | | @brief virtual destructor - empty in this class |
55 | | */ |
56 | | virtual ~ParserContainerBase(); |
57 | | |
58 | | /** |
59 | | @brief clear the mParsers vector |
60 | | */ |
61 | 0 | inline void clear() {freeParsers(); mParsers.clear();} |
62 | | |
63 | | /** |
64 | | @brief pure virtual function to be implemented in derived classes |
65 | | with the intention of cloning this object |
66 | | */ |
67 | | virtual ParserContainerBase* clone() const = 0; |
68 | | |
69 | | /** |
70 | | @brief return the size of the mParsers vector |
71 | | */ |
72 | 0 | inline size_t size() const {return mParsers.size();} |
73 | | |
74 | | /** |
75 | | @brief return true or false indicating whether the mParsers vector is |
76 | | empty or not |
77 | | */ |
78 | 0 | inline bool empty() const {return mParsers.empty();} |
79 | | |
80 | | /** |
81 | | @internal |
82 | | @brief the actual mechanics of parsing |
83 | | @todo add support for headers that are allowed to be empty like |
84 | | Supported, Accept-Encoding, Allow-Events, Allow, Accept, |
85 | | Accept-Language |
86 | | */ |
87 | | EncodeStream& encode(const Data& headerName, EncodeStream& str) const; |
88 | | |
89 | | /** |
90 | | @internal |
91 | | @brief the actual mechanics of parsing |
92 | | */ |
93 | | std::ostream& encode(Headers::Type type,std::ostream& str) const; |
94 | | |
95 | | /** |
96 | | @internal |
97 | | @brief the actual mechanics of parsing |
98 | | */ |
99 | | EncodeStream& encodeEmbedded(const Data& headerName, EncodeStream& str) const; |
100 | | |
101 | | /** |
102 | | @brief if mParsers vector is not empty, erase the first element |
103 | | */ |
104 | | void pop_front(); |
105 | | |
106 | | /** |
107 | | @brief if mParsers vector is not empty, erase the first element |
108 | | */ |
109 | | void pop_back(); |
110 | | |
111 | | /** |
112 | | @brief append the vector to the mParsers vector held locally |
113 | | @param rhs is the vector whose elements will be added to the |
114 | | local mParsers vector |
115 | | */ |
116 | | void append(const ParserContainerBase& rhs); |
117 | | |
118 | | /** |
119 | | @brief pure virtual function to be implemented in derived classes |
120 | | The intention is to provide an ability to parse all elements |
121 | | in the mParsers vector. |
122 | | */ |
123 | | virtual void parseAll()=0; |
124 | | |
125 | | bool getHeaderValueByIndex(size_t index, Data& headerValue) const; |
126 | | |
127 | | protected: |
128 | | const Headers::Type mType; |
129 | | |
130 | | /** |
131 | | @internal |
132 | | */ |
133 | | class HeaderKit |
134 | | { |
135 | | public: |
136 | | static const HeaderKit Empty; |
137 | | |
138 | 2 | HeaderKit(): pc(0){} |
139 | | |
140 | | // Poor man's move c'tor, watch out! |
141 | | HeaderKit(const HeaderKit& orig) |
142 | 252 | : pc(orig.pc), |
143 | 252 | hfv(orig.hfv) |
144 | 252 | { |
145 | 252 | HeaderKit& nc_orig = const_cast<HeaderKit&>(orig); |
146 | 252 | std::swap(nc_orig.pc, pc); |
147 | 252 | hfv.swap(nc_orig.hfv); |
148 | 252 | } |
149 | | |
150 | | // Poor man's move semantics, watch out! |
151 | | HeaderKit& operator=(const HeaderKit& rhs) |
152 | 0 | { |
153 | 0 | if(this!=&rhs) |
154 | 0 | { |
155 | 0 | HeaderKit& nc_orig = const_cast<HeaderKit&>(rhs); |
156 | 0 | std::swap(nc_orig.pc, pc); |
157 | 0 | hfv.swap(nc_orig.hfv); |
158 | 0 | } |
159 | 0 | return *this; |
160 | 0 | } |
161 | | |
162 | | ~HeaderKit() |
163 | 252 | {} |
164 | | |
165 | | EncodeStream& encode(EncodeStream& str) const |
166 | 0 | { |
167 | 0 | if(pc) |
168 | 0 | { |
169 | 0 | pc->encode(str); |
170 | 0 | } |
171 | 0 | else |
172 | 0 | { |
173 | 0 | hfv.encode(str); |
174 | 0 | } |
175 | 0 | return str; |
176 | 0 | } |
177 | | |
178 | | ParserCategory* pc; |
179 | | HeaderFieldValue hfv; |
180 | | }; |
181 | | |
182 | | typedef std::vector<HeaderKit, StlPoolAllocator<HeaderKit, PoolBase> > Parsers; |
183 | | /** |
184 | | @brief the actual list (vector) of parsers on which encoding is done |
185 | | */ |
186 | | Parsers mParsers; |
187 | | PoolBase* mPool; |
188 | | |
189 | | /** |
190 | | @brief copy header kits |
191 | | */ |
192 | | void copyParsers(const Parsers& parsers); |
193 | | |
194 | | /** |
195 | | @brief free parser containers |
196 | | */ |
197 | | void freeParsers(); |
198 | | |
199 | | inline void freeParser(HeaderKit& kit) |
200 | 252 | { |
201 | 252 | if(kit.pc) |
202 | 252 | { |
203 | 252 | kit.pc->~ParserCategory(); |
204 | 252 | if(mPool) |
205 | 252 | { |
206 | 252 | mPool->deallocate(kit.pc); |
207 | 252 | } |
208 | 0 | else |
209 | 0 | { |
210 | 0 | ::operator delete(kit.pc); |
211 | 0 | } |
212 | 252 | kit.pc=0; |
213 | 252 | } |
214 | 252 | } |
215 | | |
216 | | inline ParserCategory* makeParser(const ParserCategory& orig) |
217 | 0 | { |
218 | 0 | return orig.clone(mPool); |
219 | 0 | } |
220 | | }; |
221 | | |
222 | | } |
223 | | |
224 | | #endif |
225 | | |
226 | | /* ==================================================================== |
227 | | * The Vovida Software License, Version 1.0 |
228 | | * |
229 | | * Copyright (c) 2023 SIP Spectrum, Inc. www.sipspectrum.com |
230 | | * Copyright (c) 2000-2005 |
231 | | * |
232 | | * Redistribution and use in source and binary forms, with or without |
233 | | * modification, are permitted provided that the following conditions |
234 | | * are met: |
235 | | * |
236 | | * 1. Redistributions of source code must retain the above copyright |
237 | | * notice, this list of conditions and the following disclaimer. |
238 | | * |
239 | | * 2. Redistributions in binary form must reproduce the above copyright |
240 | | * notice, this list of conditions and the following disclaimer in |
241 | | * the documentation and/or other materials provided with the |
242 | | * distribution. |
243 | | * |
244 | | * 3. The names "VOCAL", "Vovida Open Communication Application Library", |
245 | | * and "Vovida Open Communication Application Library (VOCAL)" must |
246 | | * not be used to endorse or promote products derived from this |
247 | | * software without prior written permission. For written |
248 | | * permission, please contact vocal@vovida.org. |
249 | | * |
250 | | * 4. Products derived from this software may not be called "VOCAL", nor |
251 | | * may "VOCAL" appear in their name, without prior written |
252 | | * permission of Vovida Networks, Inc. |
253 | | * |
254 | | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED |
255 | | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
256 | | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND |
257 | | * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA |
258 | | * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES |
259 | | * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL, |
260 | | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
261 | | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
262 | | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
263 | | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
264 | | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE |
265 | | * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
266 | | * DAMAGE. |
267 | | * |
268 | | * ==================================================================== |
269 | | * |
270 | | * This software consists of voluntary contributions made by Vovida |
271 | | * Networks, Inc. and many individuals on behalf of Vovida Networks, |
272 | | * Inc. For more information on Vovida Networks, Inc., please see |
273 | | * <http://www.vovida.org/>. |
274 | | * |
275 | | */ |