reSIProcate/stack  9694
ParserCategory.hxx
Go to the documentation of this file.
00001 #if !defined(RESIP_PARSERCATEGORY_HXX)
00002 #define RESIP_PARSERCATEGORY_HXX 
00003 
00004 #include <iosfwd>
00005 #include <vector>
00006 #include <set>
00007 #include "resip/stack/HeaderTypes.hxx"
00008 #include "resip/stack/LazyParser.hxx"
00009 #include "resip/stack/ParameterTypes.hxx"
00010 #include "rutil/Data.hxx"
00011 #include "rutil/BaseException.hxx"
00012 #include "rutil/StlPoolAllocator.hxx"
00013 #include "rutil/PoolBase.hxx"
00014 
00015 #include "rutil/resipfaststreams.hxx"
00016 
00017 #define defineParam(_enum, _name, _type, _RFC_ref_ignored)                      \
00018       const _enum##_Param::DType& param(const _enum##_Param& paramType) const;  \
00019       _enum##_Param::DType& param(const _enum##_Param& paramType)
00020 
00021 namespace resip
00022 {
00023 class UnknownParameter;
00024 class ExtensionParameter;
00025 class Parameter;
00026 class ParseBuffer;
00027 
00084 class ParserCategory : public LazyParser
00085 {
00086     public:
00087       enum {UnknownParserCategory = -1};
00088 
00089       // NoCommaTokenizing:        commas do not indicate a new header value
00090       // CommasAllowedOutputMulti: multi headers can be received with commas but
00091       //                           output them on separate lines.
00092       // CommasAllowedOutputCommas: multi headers can be received with commas
00093       //                            and will always output with commas when
00094       //                            parsed.  
00095       enum {NoCommaTokenizing = 0, CommasAllowedOutputMulti = 1, CommasAllowedOutputCommas = 3};
00096 
00102       ParserCategory(const HeaderFieldValue& headerFieldValue, 
00103                      Headers::Type type,
00104                      PoolBase* pool=0);
00105 
00111       ParserCategory(const char* buf, 
00112                      int length, 
00113                      Headers::Type type,
00114                      PoolBase* pool=0);
00115 
00120       ParserCategory(const ParserCategory& rhs,
00121                      PoolBase* pool=0);
00122 
00127       ParserCategory& operator=(const ParserCategory& rhs);
00128 
00129       virtual ~ParserCategory();
00130 
00131       virtual ParserCategory* clone() const = 0;
00132       virtual Parameter* createParam(ParameterTypes::Type type, ParseBuffer& pb, const std::bitset<256>& terminators, PoolBase* pool);
00133 
00134       // Do a placement new
00135       virtual ParserCategory* clone(void* location) const = 0;
00136 
00137       // Do a pool allocated new
00138       virtual ParserCategory* clone(PoolBase* pool) const = 0;
00139 
00145       inline bool exists(const ParamBase& paramType) const
00146       {
00147           checkParsed();
00148           return (getParameterByEnum(paramType.getTypeNum()) != NULL);
00149       }
00150 
00156       void remove(const ParamBase& paramType);
00157 
00158       // !dlb! causes compiler error in windows -- change template to const T*
00165       const Data& param(const ExtensionParameter& param) const;
00166       
00173       Data& param(const ExtensionParameter& param);
00174 
00180       void remove(const ExtensionParameter& param); 
00181 
00187       bool exists(const ExtensionParameter& param) const;
00188 
00189       typedef std::set<ParameterTypes::Type> ParameterTypeSet;      
00190       
00191       static const ParameterTypeSet EmptyParameterTypeSet;      
00192 
00199       void removeParametersExcept(const ParameterTypeSet& set = EmptyParameterTypeSet);
00200       void clearUnknownParameters();
00201 
00205       class Exception : public BaseException
00206       {
00207          public:
00208             Exception(const Data& msg, const Data& file, const int line)
00209                : BaseException(msg, file, line) {}
00210 
00211             const char* name() const { return "ParserCategory::Exception"; }
00212       };
00213 
00219       void parseParameters(ParseBuffer& pb);
00220 
00226       EncodeStream& encodeParameters(EncodeStream& str) const;
00227       
00228       // used to compare 2 parameter lists for equality in an order independent way
00235       Data commutativeParameterHash() const;
00236       
00241       Parameter* getParameterByEnum(ParameterTypes::Type type) const;
00242 
00248       void removeParameterByEnum(ParameterTypes::Type type);
00249 
00254       void setParameter(const Parameter* parameter);
00255 
00259       int numKnownParams() const {return (int)mParameters.size();};
00260 
00264       int numUnknownParams() const {return (int)mUnknownParameters.size();};
00265 
00266    protected:
00267       ParserCategory(PoolBase* pool=0);
00268 
00269       Parameter* getParameterByData(const Data& data) const;
00270       void removeParameterByData(const Data& data);
00271       inline PoolBase* getPool()
00272       {
00273          return mPool;
00274       }
00275 
00276       inline void freeParameter(Parameter* p)
00277       {
00278          if(p)
00279          {
00280             p->~Parameter();
00281             if(mPool)
00282             {
00283                mPool->deallocate(p);
00284                return;
00285             }
00286             ::operator delete(p);
00287          }
00288       }
00289 
00290       virtual const Data& errorContext() const;
00291 
00292       typedef std::vector<Parameter*, StlPoolAllocator<Parameter*, PoolBase> > ParameterList; 
00293       ParameterList mParameters;
00294       ParameterList mUnknownParameters;
00295       PoolBase* mPool;
00296       Headers::Type mHeaderType;
00297    private:
00298       void clear();
00299       void copyParametersFrom(const ParserCategory& other);
00300       friend EncodeStream& operator<<(EncodeStream&, const ParserCategory&);
00301       friend class NameAddr;
00302 };
00303 
00304 EncodeStream&
00305 operator<<(EncodeStream&, const ParserCategory& category);
00306 
00307 }
00308 
00309 #undef defineParam
00310 
00311 #endif
00312 
00313 /* ====================================================================
00314  * The Vovida Software License, Version 1.0 
00315  * 
00316  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
00317  * 
00318  * Redistribution and use in source and binary forms, with or without
00319  * modification, are permitted provided that the following conditions
00320  * are met:
00321  * 
00322  * 1. Redistributions of source code must retain the above copyright
00323  *    notice, this list of conditions and the following disclaimer.
00324  * 
00325  * 2. Redistributions in binary form must reproduce the above copyright
00326  *    notice, this list of conditions and the following disclaimer in
00327  *    the documentation and/or other materials provided with the
00328  *    distribution.
00329  * 
00330  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
00331  *    and "Vovida Open Communication Application Library (VOCAL)" must
00332  *    not be used to endorse or promote products derived from this
00333  *    software without prior written permission. For written
00334  *    permission, please contact vocal@vovida.org.
00335  *
00336  * 4. Products derived from this software may not be called "VOCAL", nor
00337  *    may "VOCAL" appear in their name, without prior written
00338  *    permission of Vovida Networks, Inc.
00339  * 
00340  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
00341  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00342  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
00343  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
00344  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
00345  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
00346  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00347  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00348  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00349  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00350  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00351  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00352  * DAMAGE.
00353  * 
00354  * ====================================================================
00355  * 
00356  * This software consists of voluntary contributions made by Vovida
00357  * Networks, Inc. and many individuals on behalf of Vovida Networks,
00358  * Inc.  For more information on Vovida Networks, Inc., please see
00359  * <http://www.vovida.org/>.
00360  *
00361  */