ptypes.h
来自「PTypes是一个扩充了多线程和网络功能的STL库」· C头文件 代码 · 共 1,161 行 · 第 1/4 页
H
1,161 行
cset& operator-= (const cset& s) { subtract(s); return *this; } cset& operator-= (char b) { exclude(b); return *this; } cset operator- (const cset& s) const { cset t = *this; return t -= s; } cset operator- (char b) const { cset t = *this; return t -= b; } cset& operator*= (const cset& s) { intersect(s); return *this; } cset operator* (const cset& s) const { cset t = *this; return t *= s; } cset operator! () const { cset t = *this; t.invert(); return t; } bool operator== (const cset& s) const { return eq(s); } bool operator!= (const cset& s) const { return !eq(s); } bool operator<= (const cset& s) const { return le(s); } bool operator>= (const cset& s) const { return s.le(*this); } friend cset operator+ (char b, const cset& s); friend bool operator& (char b, const cset& s); friend void assign(cset& s, const char* setinit); friend void clear(cset& s); friend void fill(cset& s); friend void include(cset& s, char b); friend void include(cset& s, char min, char max); friend void exclude(cset& s, char b); ptpublic friend string ptdecl asstring(const cset& s);};inline cset operator+ (char b, const cset& s) { return s + b; }inline bool operator& (char b, const cset& s) { return s.contains(b); }inline void assign(cset& s, const char* setinit) { s.assign(setinit); }inline void clear(cset& s) { s.clear(); }inline void fill(cset& s) { s.fill(); }inline void include(cset& s, char b) { s.include(b); }inline void include(cset& s, char min, char max) { s.include(min, max); }inline void exclude(cset& s, char b) { s.exclude(b); }// -------------------------------------------------------------------- //// --- basic abstract classes ----------------------------------------- //// -------------------------------------------------------------------- //// basic class with virtual destructor; historically was used as a base// for all list items. also helps to count the number of created and// destroyed objects in a program (objalloc global) in DEBUG mode, to// detect memory leaks. most classes in ptypes are derived from unknown.ptpublic extern int objalloc;class ptpublic unknown {private: // make all classes non-copyable by default unknown(const unknown&); const unknown& operator= (const unknown&);public:#ifdef COUNT_OBJALLOC unknown() { pincrement(&objalloc); } virtual ~unknown() { pdecrement(&objalloc); }#else unknown() { } virtual ~unknown() { }#endif};typedef unknown* punknown;// provide non-copyable base for all classes that are// not derived from 'unknown'class ptpublic noncopyable {private: noncopyable(const noncopyable&); const noncopyable& operator= (const noncopyable&);public: noncopyable() {} ~noncopyable() {}};// -------------------------------------------------------------------- //// --- exception ------------------------------------------------------ //// -------------------------------------------------------------------- //// the basic exception class. NOTE: the library always throws dynamically// allocated exception objects.class ptpublic exception: public unknown {protected: string message;public: exception(const char* imsg); exception(const string& imsg); virtual ~exception(); virtual string get_message() { return message; }};// conversion exception class for stringtoie() and stringtoue()class ptpublic econv: public exception{public: econv(const char* msg): exception(msg) {} econv(const string& msg): exception(msg) {} virtual ~econv();};// -------------------------------------------------------------------- //// --- tpodlist ------------------------------------------------------- //// -------------------------------------------------------------------- //// _podlist implements dynamic array of small POD structures; it serves// as a basis for all list types in the library. this class is undocumented.// tpodlist template must be used instead.class ptpublic _podlist: public noncopyable{protected: void* list; // pointer to the array int count; // number of items in the list int capacity; // allocated for the list int itemsize; // list item size static void idxerror(); _podlist& operator =(const _podlist& t); void grow(); void* doins(int index); void doins(int index, const _podlist&); void* doget(int index) const { return (char*)list + index * itemsize; } void dodel(int index); void dodel(int index, int count); void dopop();#ifdef CHECK_BOUNDS void idx(int index) const { if (unsigned(index) >= unsigned(count)) idxerror(); } void idxa(int index) const { if (unsigned(index) > unsigned(count)) idxerror(); }#else void idx(int) const { } void idxa(int) const { }#endifpublic: _podlist(int itemsize); ~_podlist(); int get_count() const { return count; } void set_count(int newcount, bool zero = false); int get_capacity() const { return capacity; } void set_capacity(int newcap); void clear() { set_count(0); } void pack() { set_capacity(count); } void* ins(int index) { idxa(index); return doins(index); } void ins(int index, const _podlist& t) { idxa(index); doins(index, t); } void* add(); void add(const _podlist& t); void* operator [](int index) { idx(index); return doget(index); } void* top() { return operator [](count - 1); } void del(int index) { idx(index); dodel(index); } void del(int index, int count) { idx(index); dodel(index, count); } void pop() { idx(0); dopop(); }};// tpodlist is a fully-inlined template based on _podlisttemplate <class X, bool initzero = false> class tpodlist: public _podlist{protected: X& dozero(X& t) { if (initzero) memset(&t, 0, sizeof(X)); return t; } X& doget(int index) const { return ((X*)list)[index]; } X& doins(int index) { X& t = *(X*)_podlist::doins(index); return dozero(t); } void doins(int index, const X& item) { *(X*)_podlist::doins(index) = item; }public: tpodlist(): _podlist(sizeof(X)) {} tpodlist<X, initzero>& operator =(const tpodlist<X, initzero>& t) { _podlist::operator =(t); return *this; } void set_count(int newcount) { _podlist::set_count(newcount, initzero); } X& ins(int index) { idxa(index); return doins(index); } void ins(int index, const X& item) { idxa(index); doins(index, item); } void ins(int index, const tpodlist<X, initzero>& t) { _podlist::ins(index, t); } X& add() { grow(); return dozero(doget(count++)); } void add(const X& item) { grow(); doget(count++) = item; } void add(const tpodlist<X, initzero>& t) { _podlist::add(t); } X& operator [](int index) { idx(index); return doget(index); } const X& operator [](int index) const { idx(index); return doget(index); } X& top() { idx(0); return doget(count - 1); }};// -------------------------------------------------------------------- //// --- tobjlist ------------------------------------------------------- //// -------------------------------------------------------------------- //// _objlist is a base for the tobjlist template, don't use it directly.// also, _objlist is a base for _strlist and derivatives.class ptpublic _objlist: public unknown, protected tpodlist<void*, true>{protected: struct { unsigned ownobjects :1; // list is responsible for destroying the items; used in _objlist unsigned ownslobjects :1; // same but for _strlist items (in _stritem structure) unsigned sorted :1; // sorted list (_objlist+) unsigned duplicates :1; // sorted: allows duplicate keys (_objlist+) unsigned casesens :1; // sorted: string comparison is case sensitive (_strlist+) unsigned _reserved :27; } config; _objlist(bool ownobjects); // we hide this ctor, since _objlist actually can't free objects void* doget(int index) const { return ((void**)list)[index]; } void doput(int index, void* obj); void dodel(int index); void dodel(int index, int count); void* dopop(); void dofree(int index, int count); virtual void dofree(void* obj); // pure method; defined in tobjlist instances virtual int compare(const void* key, const void* obj) const; // pure method; defined in _strlistpublic: _objlist(); virtual ~_objlist(); int get_count() const { return count; } void set_count(int newcount); int get_capacity() const { return capacity; } void set_capacity(int newcap) { tpodlist<void*,true>::set_capacity(newcap); } void clear() { set_count(0); } void pack() { tpodlist<void*,true>::pack(); } void ins(int index, void* obj) { tpodlist<void*,true>::ins(index, obj); } void add(void* obj) { tpodlist<void*,true>::add(obj); } void put(int index, void* obj) { idx(index); doput(index, obj); } void* operator [](int index) const { idx(index); return doget(index); } void* top() const { idx(0); return doget(count - 1); } void* pop() { idx(0); return dopop(); } void del(int index) { idx(index); dodel(index); } void del(int index, int count) { idx(index); dodel(index, count); } int indexof(void* obj) const; bool search(const void* key, int& index) const;};// the tobjlist template implements a list of pointers to arbitrary// structures. optionally can automatically free objects (ownobjects)// when removed from a list. only 2 virtual functions are being// instantiated by this template, the rest is static code in _objlist.template <class X> class tobjlist: public _objlist{protected: X* doget(int index) const { return (X*)_objlist::doget(index); } virtual void dofree(void* obj);public: tobjlist(bool ownobjects = false): _objlist(ownobjects) {} virtual ~tobjlist(); bool get_ownobjects() const { return config.ownobjects; } void set_ownobjects(bool newval) { config.ownobjects = newval; } void ins(int index, X* obj) { _objlist::ins(index, obj); } void add(X* obj) { _objlist::add(obj); } void put(int index, X* obj) { _objlist::put(index, obj); } X* operator [](int index) const { idx(index); return (X*)doget(index); } X* top() const { return (X*)_objlist::top(); } X* pop() { return (X*)_objlist::pop(); } int indexof(X* obj) const { return _objlist::indexof(obj); }#ifdef PTYPES19_COMPAT friend inline void ins(tobjlist& s, int i, X* obj) { s.ins(i, obj); } friend inline int add(tobjlist& s, X* obj) { s.add(obj); return s.get_count() - 1; } friend inline void put(tobjlist& s, int i, X* obj) { s.put(i, obj); } friend inline int indexof(const tobjlist& s, X* obj) { return s.indexof(obj); } friend inline int push(tobjlist& s, X* obj) { s.add(obj); return s.get_count() - 1; } friend inline X* pop(tobjlist& s) { return (X*)s.pop(); } friend inline X* top(const tobjlist& s) { return (X*)s.top(); } friend inline X* get(const tobjlist& s, int i) { return (X*)s[i]; }#endif};
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?