File: ParserCategory.hxx

package info (click to toggle)
resiprocate 1%3A1.11.0~beta1-3%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 37,944 kB
  • sloc: cpp: 206,325; xml: 12,515; sh: 12,418; ansic: 6,973; makefile: 2,315; php: 1,150; python: 355; sql: 142; objc: 91; perl: 21; csh: 5
file content (361 lines) | stat: -rw-r--r-- 12,465 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#if !defined(RESIP_PARSERCATEGORY_HXX)
#define RESIP_PARSERCATEGORY_HXX 

#include <iosfwd>
#include <vector>
#include <set>
#include "resip/stack/HeaderTypes.hxx"
#include "resip/stack/LazyParser.hxx"
#include "resip/stack/ParameterTypes.hxx"
#include "rutil/Data.hxx"
#include "rutil/BaseException.hxx"
#include "rutil/StlPoolAllocator.hxx"
#include "rutil/PoolBase.hxx"

#include "rutil/resipfaststreams.hxx"

#define defineParam(_enum, _name, _type, _RFC_ref_ignored)                      \
      const _enum##_Param::DType& param(const _enum##_Param& paramType) const;  \
      _enum##_Param::DType& param(const _enum##_Param& paramType)

namespace resip
{
class UnknownParameter;
class ExtensionParameter;
class Parameter;
class ParseBuffer;

/**
   @brief Base class for all SIP grammar elements that can have parameters.

   The pattern for accessing the parameters in a ParserCategory is very similar
   to accessing headers in a SipMessage. Each parameter type has an access token
   class (a subclass of ParamBase), and a corresponding ParserCategory::param() 
   function that takes an instance of that subclass as an argument, and returns
   the correct type for that parameter. Common examples of access-token include 
   p_tag, p_q, p_lr, p_expires, p_branch, etc.

   @code
      NameAddr& contact = sip.header(h_Contacts).front();
      if(contact.exists(p_q))
      {
         QValueParameter& q = contact.param(p_q);
         // do stuff with q
      }

      NameAddr& to = sip.header(h_To);
      if(to.exists(p_tag))
      {
         DataParameter& toTag = to.param(p_tag);
         // do stuff with toTag
      }

      Via& topVia = sip.header(h_Vias).front();
      if(topVia.exists(p_branch))
      {
         BranchParameter& branch = topVia.param(p_branch);
         // do stuff with branch
      }
   @endcode

   Note the calls to ParserCategory::exists() in the code above; calling 
   ParserCategory::param() when the relevant parameter doesn't exist will either
   cause the parameter to be created, or an exception to be thrown (if you're
   working with a const reference).

   In some cases, you will need to access parameter-types that are not natively 
   supported by the stack (ie, don't have an access-token). ExtensionParameter 
   will allow you to construct an access-token at runtime that will retrieve the
   parameter as a raw Data. Here's an example:

   @code
      // We need to access the foo parameter on the Request-Uri
      RequestLine& rLine = sip.header(h_RequestLine);
      static ExtensionParameter p_foo("foo");
      if(rLine.uri().exists(p_foo))
      {
         Data& foo = rLine.uri().param(p_foo);
      }
   @endcode

   @todo Maybe a better name? IHaveParams? ElemWithParams?
   @ingroup resip_crit
*/
class ParserCategory : public LazyParser
{
    public:
      enum {UnknownParserCategory = -1};

      // NoCommaTokenizing:        commas do not indicate a new header value
      // CommasAllowedOutputMulti: multi headers can be received with commas but
      //                           output them on separate lines.
      // CommasAllowedOutputCommas: multi headers can be received with commas
      //                            and will always output with commas when
      //                            parsed.  
      enum {NoCommaTokenizing = 0, CommasAllowedOutputMulti = 1, CommasAllowedOutputCommas = 3};

      /**
         @internal
         @brief Constructor used by SipMessage. Unless you _really_ know what
            you're doing, don't touch this.
      */
      ParserCategory(const HeaderFieldValue& headerFieldValue, 
                     Headers::Type type,
                     PoolBase* pool=0);

      /**
         @internal
         @brief Constructor used by SipMessage. Unless you _really_ know what
            you're doing, don't touch this.
      */
      ParserCategory(const char* buf, 
                     int length, 
                     Headers::Type type,
                     PoolBase* pool=0);

      /**
         @internal
         @brief Copy c'tor.
      */
      ParserCategory(const ParserCategory& rhs,
                     PoolBase* pool=0);

      /**
         @internal
         @brief Assignment operator.
      */
      ParserCategory& operator=(const ParserCategory& rhs);

      virtual ~ParserCategory();

      virtual ParserCategory* clone() const = 0;
      virtual Parameter* createParam(ParameterTypes::Type type, ParseBuffer& pb, const std::bitset<256>& terminators, PoolBase* pool);

      // Do a placement new
      virtual ParserCategory* clone(void* location) const = 0;

      // Do a pool allocated new
      virtual ParserCategory* clone(PoolBase* pool) const = 0;

      /**
         @brief Checks for the existence of a natively supported parameter.
         @param paramType The accessor token for the parameter.
         @return true iff the parameter is present.
      */
      inline bool exists(const ParamBase& paramType) const
      {
          checkParsed();
          return (getParameterByEnum(paramType.getTypeNum()) != NULL);
      }

      /**
         @brief Removes a natively supported parameter, if the parameter is 
            present.
         @param paramType The accessor token for the parameter.
      */
      void remove(const ParamBase& paramType);

      // !dlb! causes compiler error in windows -- change template to const T*
      /**
         @brief Const accessor for non-natively-supported parameter types.
         @throw ParserCategory::Exception if this parameter doesn't exist.
         @param param The runtime constructed parameter accessor.
         @return The parameter, as a raw Data.
      */
      const Data& param(const ExtensionParameter& param) const;
      
      /**
         @brief Accessor for non-natively-supported parameter types.
            Will create the parameter if it does not exist.
         @param param The runtime constructed parameter accessor.
         @return The parameter, as a raw Data.
      */
      Data& param(const ExtensionParameter& param);

      /**
         @brief Removes a non-natively-supported parameter, if the parameter is 
            present.
         @param param The accessor token for the parameter.
      */
      void remove(const ExtensionParameter& param); 

      /**
         @brief Checks for the existence of a non-natively-supported parameter.
         @param param The runtime constructed accessor token.
         @return true iff the parameter is present.
      */
      bool exists(const ExtensionParameter& param) const;

      typedef std::set<ParameterTypes::Type> ParameterTypeSet;      
      
      static const ParameterTypeSet EmptyParameterTypeSet;      

      /**
         @brief Removes all known parameters except those that are specified in 
            set.
         @param set The set of parameters to retain, as an enum.
         @note This does not remove unknown parameters.
      */
      void removeParametersExcept(const ParameterTypeSet& set = EmptyParameterTypeSet);
      void clearUnknownParameters();

      /**
         @brief Exception class used by ParserCategory.
      */
      class Exception : public BaseException
      {
         public:
            Exception(const Data& msg, const Data& file, const int line)
               : BaseException(msg, file, line) {}

            const char* name() const { return "ParserCategory::Exception"; }
      };

      /**
         @internal
         @brief Causes this ParserCategory to parse parameters out of pb.
         @param pb The ParseBuffer to parse params from.
      */
      void parseParameters(ParseBuffer& pb);

      /**
         @brief Encodes parameters as they should appear on the wire.
         @param str The ostream to encode to.
         @return str
      */
      EncodeStream& encodeParameters(EncodeStream& str) const;
      
      // used to compare 2 parameter lists for equality in an order independent way
      /**
         @internal
         @brief An order-sensitive hash over the set of parameters in this 
            ParserCategory.
         @return The hash value as a Data.
      */
      Data commutativeParameterHash() const;
      
      /**
         @internal
         @brief Typeless parameter get interface.
      */
      Parameter* getParameterByEnum(ParameterTypes::Type type) const;

      /**
         @internal
         @brief Removes a parameter.
         @param type The parameter type.
      */
      void removeParameterByEnum(ParameterTypes::Type type);

      /**
         @internal
         @brief Typeless parameter put interface.
      */
      void setParameter(const Parameter* parameter);

      /**
         @brief Returns the number of known (natively supported) parameters.
      */
      int numKnownParams() const {return (int)mParameters.size();};

      /**
         @brief Returns the number of unknown parameters.
      */
      int numUnknownParams() const {return (int)mUnknownParameters.size();};

   protected:
      ParserCategory(PoolBase* pool=0);

      Parameter* getParameterByData(const Data& data) const;
      void removeParameterByData(const Data& data);
      inline PoolBase* getPool()
      {
         return mPool;
      }

      inline void freeParameter(Parameter* p)
      {
         if(p)
         {
            p->~Parameter();
            if(mPool)
            {
               mPool->deallocate(p);
               return;
            }
            ::operator delete(p);
         }
      }

      virtual const Data& errorContext() const;

      typedef std::vector<Parameter*, StlPoolAllocator<Parameter*, PoolBase> > ParameterList; 
      ParameterList mParameters;
      ParameterList mUnknownParameters;
      PoolBase* mPool;
      Headers::Type mHeaderType;
   private:
      void clear();
      void copyParametersFrom(const ParserCategory& other);
      friend EncodeStream& operator<<(EncodeStream&, const ParserCategory&);
      friend class NameAddr;
};

EncodeStream&
operator<<(EncodeStream&, const ParserCategory& category);

}

#undef defineParam

#endif

/* ====================================================================
 * The Vovida Software License, Version 1.0 
 * 
 * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 
 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
 *    and "Vovida Open Communication Application Library (VOCAL)" must
 *    not be used to endorse or promote products derived from this
 *    software without prior written permission. For written
 *    permission, please contact vocal@vovida.org.
 *
 * 4. Products derived from this software may not be called "VOCAL", nor
 *    may "VOCAL" appear in their name, without prior written
 *    permission of Vovida Networks, Inc.
 * 
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
 * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 * 
 * ====================================================================
 * 
 * This software consists of voluntary contributions made by Vovida
 * Networks, Inc. and many individuals on behalf of Vovida Networks,
 * Inc.  For more information on Vovida Networks, Inc., please see
 * <http://www.vovida.org/>.
 *
 */