␊ |
// tpodlist is a fully-inlined template based on _podlist␊ |
␊ |
template <class X, bool initzero = false> class tpodlist: public _podlist␊ |
template <class X, bool initzero = false> class ptpublic tpodlist: public _podlist␊ |
{␊ |
protected:␊ |
X& dozero(X& t) { if (initzero) memset(&t, 0, sizeof(X)); return t; }␊ |
|
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);␊ |
|
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<variant> _keys;␊ |
␉tpodlist<variant> _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 ----------------------------- //␊ |
|
inline void del(textmap& m, const string& k) { m.del(k); }␊ |
␊ |
␊ |
␊ |
#endif // PTYPES19_COMPAT␊ |
␊ |
␊ |