diff -r 6e47101f975da906be39a5f07d6ad8622682fcaa -r a5343ffaee9348b73724f22d91a71ff346ac250a include/ptypes.h --- a/include/ptypes.h Fri May 10 22:36:44 2013 -0500 +++ b/include/ptypes.h Fri May 10 23:40:01 2013 -0500 @@ -461,7 +461,7 @@ // tpodlist is a fully-inlined template based on _podlist -template class tpodlist: public _podlist +template class ptpublic tpodlist: public _podlist { protected: X& dozero(X& t) { if (initzero) memset(&t, 0, sizeof(X)); return t; } @@ -984,7 +984,6 @@ ptpublic friend void ptdecl apack(variant&); ptpublic friend bool ptdecl anext(const variant& a, int&, variant& item); ptpublic friend bool ptdecl anext(const variant& a, int&, variant& item, string& key); - ptpublic friend bool ptdecl anext(const variant& a, int&, variant& item, variant& key); ptpublic friend int ptdecl aadd(variant&, const variant& item); ptpublic friend void ptdecl aput(variant&, int index, const variant& item); ptpublic friend void ptdecl ains(variant&, int index, const variant& item); @@ -1053,7 +1052,74 @@ virtual ~evariant(); }; +enum { + ARR_ASSOCIATIVE, + ARR_LIST, + ARR_NULL +}; +/* + The reason for a tparray is that while a variant can be an + associative array - it stores numeric keys as hex strings + this can be very problematic if you want to loop through + all the elements and know if a given key was a number. + (For example for JSON encoding) + + A tparray uses a variant for both the keys/values so + this really opens it up to a wide range of uses. Also, + it performs no conversions on the data so when you loop + through all the keys/vals they are what you stored them as. + + This object's complexity is O(n) and it could in theory be better + by sectioning off the arrays such that + ------------------------------------------------------- + | string | string | string | number | number | number | + ------------------------------------------------------- + This would increase the complexity of adding items, and + if you don't have a very diverse number of items them you + may not benefit much from the algorithm. + +*/ +class ptpublic tparray : public component +{ +protected: + int tag; + tpodlist _keys; + tpodlist _vals; +public: + tparray() { tag = ARR_NULL; } + //anext function acts similar for variants and loops through elements + + // This is allowed for both types + ptpublic friend bool ptdecl anext(const tparray& a, int&, variant& val); + + // This is only allowed for Associative arrays as a list doesn't have keys + ptpublic friend bool ptdecl anext(const tparray& a, int&, variant& val, variant & key); + + /* + Initialy the object will be NULL - but calling one of these functions + will change the type (permanently) + */ + ptpublic friend void ptdecl add(tparray & t, const variant & val); + ptpublic friend void ptdecl add(tparray & t, const variant & key, const variant & val); + + // get will perform a O(n) search for the matching variant in the keys array + ptpublic friend variant ptdecl get(tparray & t, const variant & val); + + // This will return the value at the given index + ptpublic friend variant ptdecl at(tparray & t, int index); + + // This will return the key at the given index + ptpublic friend variant ptdecl keyat(tparray & t, int index); + + friend bool isassoc(const tparray& t); + friend bool islist(const tparray& t); + friend bool isnull(const tparray& t); +}; + +inline bool isassoc(const tparray& t) { return t.tag == ARR_ASSOCIATIVE; } +inline bool islist(const tparray& t) { return t.tag == ARR_LIST; } +inline bool isnull(const tparray& t) { return t.tag == ARR_NULL; } // -------------------------------------------------------------------- // // --- pre-2.0 compatibility declarations ----------------------------- // @@ -1142,6 +1208,7 @@ inline void del(textmap& m, const string& k) { m.del(k); } + #endif // PTYPES19_COMPAT diff -r 6e47101f975da906be39a5f07d6ad8622682fcaa -r a5343ffaee9348b73724f22d91a71ff346ac250a src/ptparray.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/ptparray.cpp Fri May 10 23:40:01 2013 -0500 @@ -0,0 +1,75 @@ +#include +#include +#include + +#include "ptypes.h" + + +PTYPES_BEGIN + +bool ptdecl anext(const tparray& a, int& i, variant& val) +{ + variant t; + return anext(a, i, val, t); +} + +bool ptdecl anext(const tparray& a, int& i, variant& val, variant & key) +{ + if (a._keys.get_count() == i) + { + return false; + } + else + { + val = a._vals[i]; + key = a._keys[i]; + i++; + return true; + } +} + +void ptdecl add(tparray & t, const variant & val) +{ + if (t.tag == ARR_NULL || t.tag == ARR_LIST) + { + t.tag = ARR_LIST; + t._vals.add(val); + } + + //else drop it +} + +void ptdecl add(tparray & t, const variant & key, const variant & val) +{ + if (t.tag == ARR_NULL || t.tag == ARR_ASSOCIATIVE) + { + t.tag = ARR_ASSOCIATIVE; + t._vals.add(val); + t._keys.add(key); + + } +} + +variant ptdecl get(tparray & t, const variant & val) +{ + for(int i = 0; i < t._keys.get_count(); i++) + { + if (t._keys[i] == val) + { + return t._vals[i]; + } + } + return NULL; +} + +variant ptdecl at(tparray & t, int index) +{ + return t._vals[index]; +} + +variant ptdecl keyat(tparray & t, int index) +{ + return t._keys[index]; +} + +PTYPES_END \ No newline at end of file diff -r 6e47101f975da906be39a5f07d6ad8622682fcaa -r a5343ffaee9348b73724f22d91a71ff346ac250a src/pvariant.cxx --- a/src/pvariant.cxx Fri May 10 22:36:44 2013 -0500 +++ b/src/pvariant.cxx Fri May 10 23:40:01 2013 -0500 @@ -588,24 +588,6 @@ return true; } -bool ptdecl anext(const variant& array, int& index, variant& item, variant & key) -{ - if (array.tag != VAR_ARRAY) - { - clear(item); - return false; - } - if (index < 0 || index >= array.value.a->get_count()) - { - clear(item); - return false; - } - item = array.value.a->doget(index)->var; - key = array.value.a->doget(index)->key; - index++; - return true; -} - void ptdecl add(variant & v, const variant & var) { put(v, alength(v), var); diff -r 6e47101f975da906be39a5f07d6ad8622682fcaa -r a5343ffaee9348b73724f22d91a71ff346ac250a win32/PTypes_Lib.vcxproj --- a/win32/PTypes_Lib.vcxproj Fri May 10 22:36:44 2013 -0500 +++ b/win32/PTypes_Lib.vcxproj Fri May 10 23:40:01 2013 -0500 @@ -216,6 +216,7 @@ %(AdditionalIncludeDirectories) %(PreprocessorDefinitions) + %(AdditionalIncludeDirectories) %(PreprocessorDefinitions) diff -r 6e47101f975da906be39a5f07d6ad8622682fcaa -r a5343ffaee9348b73724f22d91a71ff346ac250a win32/PTypes_Lib.vcxproj.filters --- a/win32/PTypes_Lib.vcxproj.filters Fri May 10 22:36:44 2013 -0500 +++ b/win32/PTypes_Lib.vcxproj.filters Fri May 10 23:40:01 2013 -0500 @@ -182,6 +182,9 @@ Inet + + Types +