📄 ptypes.h
字号:
template <class X> void tobjlist<X>::dofree(void* item){ delete (X*)item;}template <class X> tobjlist<X>::~tobjlist(){ set_count(0);}// -------------------------------------------------------------------- //// --- tstrlist ------------------------------------------------------- //// -------------------------------------------------------------------- //// _strlist is a base for the tstrlist templateenum slflags{ SL_SORTED = 0x0001, SL_DUPLICATES = 0x0002, SL_CASESENS = 0x0004, SL_OWNOBJECTS = 0x0008,};struct _stritem{ string key; void* obj; _stritem(const string& ikey, void* iobj) : key(ikey), obj(iobj) {}};class ptpublic _strlist: protected tobjlist<_stritem>{protected: static void sortederror(); static void notsortederror(); static void duperror(); virtual void dofree(void* item); virtual int compare(const void* key, const void* item) const; virtual void dofreeobj(void* obj); // pure; tstrlist overrides it const string& dogetkey(int index) const { return doget(index)->key; } void* dogetobj(int index) const { return doget(index)->obj; } void doins(int index, const string& key, void* obj); void doput(int index, const string& key, void* obj); void doput(int index, void* obj);public: _strlist(int flags = 0); virtual ~_strlist(); int get_count() const { return count; } void set_count(int newcount) { tobjlist<_stritem>::set_count(newcount); } int get_capacity() const { return capacity; } void set_capacity(int newcap) { tobjlist<_stritem>::set_capacity(newcap); } void clear() { tobjlist<_stritem>::clear(); } void pack() { tobjlist<_stritem>::pack(); } bool get_sorted() const { return config.sorted; } bool get_duplicates() const { return config.duplicates; } bool get_casesens() const { return config.casesens; } bool get_ownobjects() const { return config.ownslobjects; } void set_ownobjects(bool newval) { config.ownslobjects = newval; } void ins(int index, const string& key, void* obj) { idxa(index); doins(index, key, obj); } void put(int index, const string& key, void* obj) { idx(index); doput(index, key, obj); } void put(int index, void* obj) { idx(index); doput(index, obj); } int put(const string& key, void* obj); int add(const string& key, void* obj); void* operator [](int index) const { idx(index); return dogetobj(index); } void* operator [](const char* key) const; const string& getkey(int index) const { idx(index); return dogetkey(index); } bool search(const char* key, int& index) const { return _objlist::search(key, index); } void del(int index) { idx(index); dodel(index); } void del(int index, int delcount) { idx(index); dodel(index, delcount); } void del(const char* key) { put(key, nil); } int indexof(const char* key) const; int indexof(void* obj) const;};// the tstrlist template implements a list of string/object pairs,// optionally sorted for fast searching by string key.template <class X> class tstrlist: public _strlist{protected: virtual void dofreeobj(void* obj);public: tstrlist(int flags = 0): _strlist(flags) {} virtual ~tstrlist(); void ins(int index, const string& key, X* obj) { _strlist::ins(index, key, obj); } void put(int index, const string& key, X* obj) { _strlist::put(index, key, obj); } void put(int index, X* obj) { _strlist::put(index, obj); } int put(const string& key, X* obj) { return _strlist::put(key, obj); } int add(const string& key, X* obj) { return _strlist::add(key, obj); } X* operator [](int index) const { return (X*)_strlist::operator [](index); } X* operator [](const char* key) const { return (X*)_strlist::operator [](key); } int indexof(X* obj) const { return _strlist::indexof(obj); } int indexof(const char* key) const { return _strlist::indexof(key); }#ifdef PTYPES19_COMPAT // pre-2.0 interface for backwards compatibility friend inline void ins(tstrlist& s, int i, const string& str, X* obj) { s.ins(i, str, obj); } friend inline int add(tstrlist& s, const string& str, X* obj) { return s.add(str, obj); } friend inline void put(tstrlist& s, int i, const string& str, X* obj) { s.put(i, str, obj); } friend inline void put(tstrlist& s, int i, X* obj) { s.put(i, obj); } friend inline int indexof(const tstrlist& s, X* obj) { return s.indexof(obj); } friend inline X* get(const tstrlist& s, int i) { return (X*)s[i]; }#endif};template <class X> void tstrlist<X>::dofreeobj(void* obj){ delete (X*)obj;}template <class X> tstrlist<X>::~tstrlist(){ set_count(0);}// -------------------------------------------------------------------- //// --- textmap -------------------------------------------------------- //// -------------------------------------------------------------------- //// textmap is a list of string pairs (key/value)struct _textitem{ string key; string value; _textitem(const string& ikey, const string& ivalue) : key(ikey), value(ivalue) {}};class ptpublic textmap: protected tobjlist<_textitem>{protected: virtual int compare(const void* key, const void* item) const; const string& dogetvalue(int index) const { return doget(index)->value; } const string& dogetkey(int index) const { return doget(index)->key; }public: textmap(bool casesens = false); virtual ~textmap(); int get_count() const { return tobjlist<_textitem>::get_count(); } void pack() { tobjlist<_textitem>::pack(); } void clear() { tobjlist<_textitem>::clear(); } int put(const string& key, const string& value); void del(int index) { idx(index); dodel(index); } void del(const char* key) { put(key, nullstring); } const string& get(int index) const { idx(index); return dogetvalue(index); } const string& getkey(int index) const { idx(index); return dogetkey(index); } const string& get(const char* key) const; const string& operator [](int index) const { return get(index); } const string& operator [](const char* key) const { return get(key); } int indexof(const char* key) const;};// -------------------------------------------------------------------- //// --- component ------------------------------------------------------ //// -------------------------------------------------------------------- //// the component class is an abstract class that provides reference// counting and delete notification mechanisms. all stream classes// in ptypes are derived from component.// class ID's for all basic types: the first byte (least significant)// contains the base ID, the next is for the second level of inheritance,// etc. total of 4 levels allowed for basic types. call classid() for an// object, mask out first N bytes of interest and compare with a CLASS_XXX// value. f.ex. to determine whether an object is of type infile or any// derivative: (o->classid() & 0xffff) == CLASS2_INFILE. this scheme is for// internal use by PTypes and Objection; partly replaces the costly C++ RTTI// system.// first level of inheritanceconst int CLASS_UNDEFINED = 0x00000000;const int CLASS_INSTM = 0x00000001;const int CLASS_OUTSTM = 0x00000002;const int CLASS_UNIT = 0x00000003;// second level of inheritanceconst int CLASS2_INFILE = 0x00000100 | CLASS_INSTM;const int CLASS2_INMEMORY = 0x00000200 | CLASS_INSTM;const int CLASS2_FDX = 0x00000300 | CLASS_INSTM;const int CLASS2_OUTFILE = 0x00000100 | CLASS_OUTSTM;const int CLASS2_OUTMEMORY = 0x00000200 | CLASS_OUTSTM;// third level of inheritanceconst int CLASS3_LOGFILE = 0x00010000 | CLASS2_OUTFILE;const int CLASS3_IPSTM = 0x00020000 | CLASS2_FDX;const int CLASS3_NPIPE = 0x00030000 | CLASS2_FDX;class ptpublic component: public unknown {protected: int refcount; // reference counting, used by addref() and release() tobjlist<component>* freelist; // list of components to notify about destruction, safer alternative to ref-counting void* typeinfo; // reserved for future use virtual void freenotify(component* sender);public: component(); virtual ~component(); void addnotification(component* obj); void delnotification(component* obj); ptpublic friend component* ptdecl addref(component*); ptpublic friend bool ptdecl release(component*); friend int refcount(component* c); virtual int classid(); void set_typeinfo(void* t) { typeinfo = t; } void* get_typeinfo() { return typeinfo; }};typedef component* pcomponent;inline int refcount(component* c) { return c->refcount; }template <class T> inline T* taddref(T* c) { return (T*)addref((component*)c); }template <class T> class compref{protected: T* ref;public: compref() { ref = 0; } compref(const compref<T>& r) { ref = taddref<T>(r.ref); } compref(T* c) { ref = taddref<T>(c); } ~compref() { release(ref); } compref<T>& operator =(T* c); compref<T>& operator =(const compref<T>& r) { return operator =(r.ref); } T& operator *() const { return *ref; } T* operator ->() const { return ref; } bool operator ==(const compref<T>& r) const { return ref == r.ref; } bool operator ==(T* c) const { return ref == c; } bool operator !=(const compref<T>& r) const { return ref != r.ref; } bool operator !=(T* c) const { return ref != c; } operator T*() const { return ref; }};template <class T> compref<T>& compref<T>::operator =(T* c){ release(tpexchange<T>(&ref, taddref<T>(c))); return *this;}// -------------------------------------------------------------------- //// --- variant -------------------------------------------------------- //// -------------------------------------------------------------------- //enum { VAR_NULL, VAR_INT, VAR_BOOL, VAR_FLOAT, VAR_STRING, VAR_ARRAY, VAR_OBJECT, VAR_COMPOUND = VAR_STRING};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -