jsobject.h
Last updated: 12-Sep-2025
Go to the documentation of this file.
1Â 2Â #ifndef IPC_JSOBJECT_H_150611 3Â #define IPC_JSOBJECT_H_150611 4Â 9Â #include <string> 10Â #include <map> 11Â #include <vector> 12Â #include <limits.h> 13Â 14Â #if defined _WIN32 && defined VFI_IPC_SHARED_EXPORT 15Â # define DllSpec __declspec(dllexport) 16Â #elif defined __GNUC__ && defined VFI_IPC_SHARED_EXPORT 17Â # define DllSpec __attribute__((visibility ("default"))) 18Â #else 19Â # define DllSpec 20Â #endif 21Â 22Â namespace vfiipc { 23Â #if 0 24Â } // for automatic indentation 25Â #endif 26Â 28Â class DllSpec JSObject { 29Â 30Â public: 32Â enum JSType { 33Â JSTNull, 34Â JSTBool, 35Â JSTString, 36Â JSTInt, 37Â JSTFloat, 38Â JSTObject, 39Â JSTArray 40Â }; 41Â 42Â protected: 43Â JSType v_type; 44Â bool v_bool; 45Â std::string v_string; 46Â double v_floatnum; 47Â long v_intnum; 48Â std::map<std::string,JSObject> v_object; 49Â std::vector<JSObject> v_array; 50Â 51Â static JSObject NullObject; 52Â 53Â int parse(const char *s); 54Â int parse(const char *s, unsigned nestinglevel); 55Â 56Â void dump(std::string &s,bool (*filter_cb)(const std::string &key)) const; 57Â void prettyDump(std::string &out, int indent) const; 58Â 59Â public: 60Â 62Â JSObject() { v_type=JSTNull; } 63Â 65Â JSType type() const { return v_type; } 66Â 68Â bool isNull() const { return v_type==JSTNull; } 69Â 71Â bool isBool() const { return v_type==JSTBool; } 72Â 74Â bool isString() const { return v_type==JSTString; } 75Â 77Â bool isNumber() const { return v_type==JSTFloat || v_type==JSTInt; } 78Â 80Â bool isObject() const { return v_type==JSTObject; } 81Â 83Â bool isArray() const { return v_type==JSTArray; } 84Â 86Â std::string getString() const; 87Â 89Â std::string getString(const char *defaultval) const { return isNull() ? defaultval : getString(); } 90Â 95Â const std::string *getStringP() const; 96Â 101Â std::string *getStringP(); 102Â 104Â double getNumber() const; 105Â 107Â long getInt() const; 108Â 110Â long long getInt64() const; 111Â 113Â bool getBool() const; 114Â 116Â operator bool() const { return getBool(); } 117Â 119Â operator std::string() const { return getString(); } 120Â 122Â operator char() const { return (char)getInt(); } 123Â 125Â operator unsigned char() const { return (unsigned char)getInt(); } 126Â 128Â operator short() const { return (short)getInt(); } 129Â 131Â operator unsigned short() const { return (unsigned short)getInt();} 132Â 134Â operator int() const { return (int)getInt(); } 135Â 137Â operator unsigned() const { return (unsigned)getInt(); } 138Â 140Â operator long() const { return (long)getInt(); } 141Â 143Â operator unsigned long() const { return (unsigned long)getInt(); } 144Â 146Â operator long long() const { return (long long)getInt64(); } 147Â 149Â operator unsigned long long() const { return (unsigned long long)getInt64(); } 150Â 152Â operator float() const { return (float)getNumber(); } 153Â 155Â operator double() const { return (double)getNumber(); } 156Â 158Â void clear() { v_type=JSTNull; v_string.clear(); v_object.clear(); v_array.clear(); } 159Â 161Â JSObject &operator=(bool val) { v_type=JSTBool; v_bool=val; return *this; } 162Â 164Â JSObject &operator=(char *val) { v_type=JSTString; v_string=val?val:""; return *this; } 165Â 167Â JSObject &operator=(const char *val) { v_type=JSTString; v_string=val?val:""; return *this; } 168Â 170Â JSObject &operator=(const std::string &val) { v_type=JSTString; v_string=val; return *this; } 171Â 173Â JSObject &operator=(double val) { v_type=JSTFloat; v_floatnum=val; return *this; } 174Â 176Â JSObject &operator=(float val) { v_type=JSTFloat; v_floatnum=val; return *this; } 177Â 179Â JSObject &operator=(long long val) { 180Â if(val>LONG_MAX || val<LONG_MIN) {v_type=JSTFloat; v_floatnum=val;} 181Â else {v_type=JSTInt; v_intnum=val;} 182Â return *this; 183Â } 184Â 186Â JSObject &operator=(unsigned long long val) { 187Â if(val>LONG_MAX) {v_type=JSTFloat; v_floatnum=val;} 188Â else {v_type=JSTInt; v_intnum=val;} 189Â return *this; 190Â } 191Â 193Â template<typename T> JSObject &operator=(T val) { v_type=JSTInt; v_intnum=val; return *this; } 194Â 198Â bool exists(const char *elem) const; 199Â 203Â bool exists(const std::string &elem) const; 204Â 206Â void erase(const char *elem); 207Â 209Â void erase(const std::string &elem); 210Â 215Â JSObject &operator()(const char *elem); 216Â 221Â const JSObject &operator()(const char *elem) const; 222Â 227Â JSObject &operator()(const std::string &elem); 228Â 233Â const JSObject &operator()(const std::string &elem) const; 234Â 239Â JSObject &operator[](int idx); 240Â 245Â const JSObject &operator[](int idx) const; 246Â 249Â unsigned size() const { return v_type==JSTArray?v_array.size():0; } 250Â 255Â void resize(unsigned new_size); 256Â 262Â typedef std::map<std::string,JSObject>::iterator iterator; 263Â typedef std::map<std::string,JSObject>::const_iterator const_iterator; 264Â 266Â iterator begin() { return v_object.begin(); } 267Â 269Â iterator end() { return v_object.end(); } 270Â 272Â const_iterator begin() const { return v_object.begin(); } 273Â 275Â const_iterator end() const { return v_object.end(); } 276Â 280Â bool load(const std::string &s) { return parse(s.c_str())==(int)s.length(); } 281Â 285Â std::string dump() const; 286Â 290Â void dump(std::string &s) const; 291Â 295Â void swap(JSObject &o); 296Â 297Â 302Â std::string logdump(bool (*filter_cb)(const std::string &key)) const; 303Â 307Â void prettyDump(std::string &s) const; 308Â 311Â static const char *getVersion(); 312Â }; 313Â 314Â } // namespace vfiipc 315Â 316Â #undef DllSpec 317Â 319Â #endif
Rate this article: