AmArg.h

00001 /*
00002  * $Id: AmArg.h 711 2008-02-10 18:52:44Z sayer $
00003  *
00004  * Copyright (C) 2002-2003 Fhg Fokus
00005  *
00006  * This file is part of sems, a free SIP media server.
00007  *
00008  * sems is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version
00012  *
00013  * For a license to use the ser software under conditions
00014  * other than those described here, or to purchase support for this
00015  * software, please contact iptel.org by e-mail at the following addresses:
00016  *    info@iptel.org
00017  *
00018  * sems is distributed in the hope that it will be useful,
00019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00021  * GNU General Public License for more details.
00022  *
00023  * You should have received a copy of the GNU General Public License 
00024  * along with this program; if not, write to the Free Software 
00025  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00026  */
00027 
00028 #ifndef _AmArg_h_
00029 #define _AmArg_h_
00030 
00031 #include <assert.h>
00032 #include <string.h>
00033 
00034 #include <vector>
00035 using std::vector;
00036 
00037 #include <string>
00038 using std::string;
00039 
00040 #include <map>
00041 
00042 #include "log.h"
00043 
00045 class ArgObject {
00046  public:
00047   ArgObject() { }
00048   virtual ~ArgObject() { }
00049 };
00050 
00051 struct ArgBlob {
00052   char* data;
00053   int   len;
00054   
00055 ArgBlob() 
00056 : data(NULL),len(0)
00057   {  
00058   }
00059 
00060   ArgBlob(const ArgBlob& a) {
00061     len = a.len;
00062     data = (char*)malloc(len);
00063     if (data)
00064       memcpy(data, a.data, len);
00065   }
00066   
00067   ArgBlob(const char* _data, int _len) {
00068     len = _len;
00069     data = (char*)malloc(len);
00070     if (data)
00071       memcpy(data, _data, len);
00072   }
00073   
00074   ~ArgBlob() { if (data) free(data); }
00075 };
00076 
00078 class AmArg
00079 {
00080  public:
00081   // type enum
00082   enum {
00083     Undef=0,
00084 
00085     Int,
00086     Double,
00087     CStr,
00088     AObject,            // for passing pointers to objects not owned by AmArg
00089     Blob,
00090     
00091     Array,
00092     Struct
00093   };
00094 
00095   struct OutOfBoundsException {
00096     OutOfBoundsException() { }
00097   };
00098 
00099   struct TypeMismatchException {
00100     TypeMismatchException() { }
00101   };
00102   
00103   typedef std::vector<AmArg> ValueArray;
00104   typedef std::map<std::string, AmArg> ValueStruct; 
00105 
00106  private:
00107   // type
00108   short type;
00109     
00110   // value
00111   union {
00112     int            v_int;
00113     double         v_double;
00114     const char*    v_cstr;
00115     ArgObject*     v_obj;
00116     ArgBlob*       v_blob;
00117     ValueArray*    v_array;
00118     ValueStruct*   v_struct;
00119   };
00120 
00121 
00122   void assertArray();
00123   void assertArray() const;
00124 
00125   void assertStruct();
00126   void assertStruct() const;
00127 
00128   void invalidate();
00129 
00130  public:
00131  AmArg() 
00132    : type(Undef) 
00133   { }
00134   
00135   AmArg(const AmArg& v);
00136   
00137  AmArg(const int& v)
00138    : type(Int),
00139     v_int(v)
00140     { }
00141   
00142  AmArg(const double& v)
00143    : type(Double),
00144     v_double(v)
00145     { }
00146   
00147  AmArg(const char* v)
00148    : type(CStr)
00149   {
00150     v_cstr = strdup(v);
00151   }
00152   
00153  AmArg(const ArgBlob v)
00154    : type(Blob)
00155   {
00156     v_blob = new ArgBlob(v);
00157   }
00158 
00159   // convenience constructors
00160   AmArg(vector<std::string>& v);
00161   AmArg(const vector<int>& v );
00162   AmArg(const vector<double>& v);
00163   AmArg(std::map<std::string, std::string>& v);
00164   AmArg(std::map<std::string, AmArg>& v);
00165   
00166   ~AmArg() { invalidate(); }
00167 
00168   short getType() const { return type; }
00169 
00170   AmArg& operator=(const AmArg& rhs);
00171 
00172 #define isArgArray(a) (AmArg::Array == a.getType())
00173 #define isArgStruct(a)(AmArg::Struct == a.getType())
00174 #define isArgDouble(a) (AmArg::Array == a.getType())
00175 #define isArgInt(a) (AmArg::Int == a.getType())
00176 #define isArgCStr(a) (AmArg::CStr == a.getType())
00177 #define isArgAObject(a) (AmArg::AObject == a.getType())
00178 #define isArgBlob(a) (AmArg::Blob == a.getType())
00179 
00180 #define assertArgArray(a)                       \
00181   if (!isArgArray(a))                           \
00182     throw AmArg::TypeMismatchException();
00183 #define assertArgDouble(a)                      \
00184   if (!isArgDouble(a))                          \
00185     throw AmArg::TypeMismatchException();
00186 #define assertArgInt(a)                         \
00187   if (!isArgInt(a))                             \
00188     throw AmArg::TypeMismatchException();
00189 #define assertArgCStr(a)                        \
00190   if (!isArgCStr(a))                            \
00191     throw AmArg::TypeMismatchException();
00192 #define assertArgAObject(a)                     \
00193   if (!isArgAObject(a))                         \
00194     throw AmArg::TypeMismatchException();   
00195 #define assertArgBlob(a)                        \
00196   if (!isArgBlob(a))                            \
00197     throw AmArg::TypeMismatchException();
00198 #define assertArgStruct(a)                      \
00199   if (!isArgStruct(a))                          \
00200     throw AmArg::TypeMismatchException();
00201 
00202   void setBorrowedPointer(ArgObject* v) {
00203     type = AObject;
00204     v_obj = v;
00205   }
00206 
00207   int         asInt()    const { return v_int; }
00208   double      asDouble() const { return v_double; }
00209   const char* asCStr()   const { return v_cstr; }
00210   ArgObject*  asObject() const { return v_obj; }
00211   ArgBlob*    asBlob()   const { return v_blob; }
00212 
00213   vector<string>     asStringVector()    const; 
00214   vector<int>        asIntVector()       const; 
00215   vector<double>     asDoubleVector()    const; 
00216   vector<ArgObject*> asArgObjectVector() const; 
00217   vector<ArgBlob>    asArgBlobVector()   const; 
00218 
00219   // operations on arrays
00220   void assertArray(size_t s);
00221 
00222   void push(const AmArg& a);
00223 
00224   void concat(const AmArg& a);
00225   
00226   const size_t size() const;
00227 
00229   AmArg& get(size_t idx);
00230 
00232   AmArg& get(size_t idx) const;
00233 
00235   AmArg& operator[](size_t idx);
00237   AmArg& operator[](size_t idx) const;
00238 
00240   AmArg& operator[](int idx);
00242   AmArg& operator[](int idx) const;
00243 
00244   AmArg& operator[](std::string key);
00245   AmArg& operator[](std::string key) const;
00246   AmArg& operator[](const char* key);
00247   AmArg& operator[](const char* key) const;
00248 
00250   bool hasMember(const std::string& name) const;
00251   bool hasMember(const char* name) const;
00252 
00253   std::vector<std::string> enumerateKeys() const;
00254   ValueStruct::const_iterator begin() const;
00255   ValueStruct::const_iterator end() const;
00256 
00269   void assertArrayFmt(const char* format) const;
00270 };
00271 
00272 #endif
00273 

Generated on Fri May 16 12:02:05 2008 for SEMS by  doxygen 1.5.1
Home |  Recent changes |  Search |  Glossary |  Sitemap |  Login