PData.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007 #ifndef PDATA_H
00008 #define PDATA_H
00009
00010 #include <pthread.h>
00011 #include <unistd.h>
00012 #include <sstream>
00013
00014 enum {PTYPE_NULL = 1, PTYPE_INT = 2, PTYPE_FLOAT = 4, PTYPE_STRING = 8, PTYPE_LIST = 16, PTYPE_MAP = 32};
00015
00016
00017 #include "PDataNull.h"
00018 #include "PDataInt.h"
00019 #include "PDataFloat.h"
00020 #include "PDataString.h"
00021 #include "PDataList.h"
00022 #include "PDataMap.h"
00023
00024 using namespace std;
00025
00026 extern void PDataGrammarCallParser(char* evalstring);
00027 extern int yyparse();
00028
00031 class PData {
00032
00033 public:
00034
00037
00038
00039
00040 PData(PData* owner = NULL);
00042 ~PData();
00043
00044
00049
00050
00051
00052
00053 PData(int v, PData* owner = NULL);
00057 PData(long v, PData* owner = NULL);
00061 PData(float v, PData* owner = NULL);
00065 PData(double v, PData* owner = NULL);
00069 PData(string v, PData* owner = NULL);
00071
00073
00074
00075
00076
00077 PData(const PData& v, PData* owner = NULL);
00079
00084
00085
00099 PData(const char* command, const char* evalstring, int len=-1);
00102 PData(const char* command, string evalstring);
00105 PData(string command, string evalstring);
00107
00111
00112
00113 int operator=(int v);
00115 long operator=(long v);
00117 float operator=(float v);
00119 double operator=(double v);
00121 string operator=(string v);
00123 PData& operator=(PData& v) { v._checkParsed(); delete _datum; _datum = _makeNewDatum(v._datum); _setDirty(); return v; }
00125 int set(int v) {return operator=(v);}
00127 long set(long v) {return operator=(v);}
00129 float set(float v) {return operator=(v);}
00131 double set(double v) {return operator=(v);}
00133 string set(string v) {return operator=(v);}
00135 PData& set(PData& v) {return operator=(v);}
00137
00140
00141
00142
00143
00144 PData& eval(string evalstring);
00145
00151 PData& eval(const char* evalstring, int len=-1);
00152
00155 PData& init(string typestring);
00160 PData& init(const char* typestring, int len=-1);
00161
00164 PData& raw(string rawbytes);
00169 PData& raw(const char* rawbytes, int len=-1);
00171
00174
00175
00176 operator int() {_checkParsed(); return (int)(*_datum);}
00178 operator long() {_checkParsed(); return (long)(*_datum);}
00180 operator float() {_checkParsed(); return (float)(*_datum);}
00182 operator double() {_checkParsed(); return (double)(*_datum);}
00184 operator string() {_checkParsed(); return (string)(*_datum);}
00186 int getInt() {_checkParsed(); return (int)(*_datum);}
00188 long getLong() {_checkParsed(); return (long)(*_datum);}
00190 float getFloat() {_checkParsed(); return (float)(*_datum);}
00192 double getDouble() {_checkParsed(); return (double)(*_datum);}
00194 string getString() {_checkParsed(); return (string)(*_datum);}
00196
00198
00199
00200 PData& operator[](string index) {_checkParsed(); return *((*_datum)[index]);}
00202 PData& operator[](const char* index) {_checkParsed(); return *((*_datum)[index]);}
00204 PData& operator[](int index) {_checkParsed(); return *((*_datum)[index]);}
00206 PData& get(string index) {return operator[](index);}
00208 PData& get(const char* index) {return operator[](index);}
00210 PData& get(int index) {return operator[](index);}
00212
00214
00215
00216 void clear() {_checkParsed(); _datum->clear(); _setDirty();}
00218 void setNull() {delete _datum; _datum = new PDataNull(this); _setDirty();}
00220 void append(const PData& v) {const_cast<PData&>(v)._checkParsed(); _checkParsed(); _datum->append(v); _setDirty();}
00222 void erase(int index) {_checkParsed(); _datum->erase(index); _setDirty();}
00224 void erase(string index) {_checkParsed(); _datum->erase(index); _setDirty();}
00226 void erase(const char* index) {_checkParsed(); _datum->erase(index); _setDirty();}
00228 void resize(int size) {_checkParsed(); _datum->resize(size); _setDirty();}
00230
00232
00233
00234 string getType() {_checkParsed(); return _datum->getType();}
00236 int getTypeEnum() {_checkParsed(); return _datum->getTypeEnum();}
00238 int size() {_checkParsed(); return _datum->size();}
00240 int isRaw() {return _is_raw;}
00241 long getIndex() { return _index; }
00243 int has(const PData& v) {const_cast<PData&>(v)._checkParsed(); _checkParsed(); return _datum->has(v);}
00245 int hasKey(const char* v) {_checkParsed(); return _datum->hasKey(v);}
00247 vector<string> keys() {_checkParsed(); return _datum->keys();}
00250 void iterateKeys() {_checkParsed(); _datum->iterateKeys();}
00252 int hasNextKey() {_checkParsed(); return _datum->hasNextKey();}
00254 string getNextKey() {_checkParsed(); return _datum->getNextKey();}
00255
00257 int operator==(const PData& v) {return equals(v);}
00259 int equals(const PData& v) {return (const_cast<PData&>(v).toString() == toString());}
00262 int find(const PData& v) {const_cast<PData&>(v)._checkParsed(); _checkParsed(); return _datum->find(v);}
00264
00266
00267
00268 friend ostream& operator<<(ostream& o, PData& p) { o << p.toString(); return o; }
00270 string toString() { _updateStringCache(); return string(_string_cache); }
00271
00276 void exportBuffer(void (*receiveCallback)(const char* buffer, int size));
00277
00283 char* insertTransmit(char* buf_start, char* buf_end);
00284
00287 int getTransmitSize();
00289
00294 static string& quoteString(string& s);
00295
00300 static string& unquoteString(string& s);
00301
00302 private:
00303
00304
00305 static void ParserStartMap();
00306 static void ParserMapKey(char* label);
00307 static void ParserMapInsert();
00308 static void ParserEndMap();
00309 static void ParserStartList();
00310 static void ParserListInsert();
00311 static void ParserEndList();
00312 friend void PDataGrammarCallParser(char* evalstring);
00313 friend int yyparse();
00314 friend int yylex();
00315
00316 private:
00317 static pthread_mutex_t _ParserLock;
00318 pthread_mutex_t _buffer_lock;
00319 long _index;
00320 PData* _owner;
00321 PDataNull* _datum;
00322 int _is_dirty;
00323 int _is_raw;
00324 char* _string_cache;
00325 int _string_cache_bufsize;
00326 int _string_cache_strsize;
00327 int _is_cached_parse;
00328 enum {INIT_STRING_CACHE_SIZE = 50};
00329
00330 void _init(PData* owner);
00331 void paramConstructor(const char* command, const char* evalstring, int len=-1);
00332 void _parserWrapper();
00333 void _checkParsed();
00334 PDataNull* _makeNewDatum(PDataNull* pdn);
00335 void _updateStringCache();
00336 void _forceStringCache();
00337 void _resizeStringCache(int size);
00338 void _setStringCache(const char* evalstring, int len);
00339 void _setDirty();
00340 void _deleteDatum() {delete _datum;}
00341 void _replaceDatum(PDataNull* pdn) {_deleteDatum(); _datum = pdn;}
00342
00343
00344 static PData* _parser_target;
00345
00346 };
00347
00348 #endif