⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ptypes.h

📁 使用stl技术,(还没看,是听说的)
💻 H
📖 第 1 页 / 共 3 页
字号:
// 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) == CLASS_INFILE. this scheme is for
// internal use by PTypes and Objection; partly replaces the costly C++ RTTI
// system.
//

// first level of inheritance
const int CLASS_UNDEFINED = 0x00000000;
const int CLASS_INSTM     = 0x00000001;
const int CLASS_OUTSTM    = 0x00000002;
const int CLASS_UNIT      = 0x00000003;

// second level of inheritance
const int CLASS_INFILE    = 0x00000100 | CLASS_INSTM;
const int CLASS_OUTFILE   = 0x00000100 | CLASS_OUTSTM;
const int CLASS_INMEMORY  = 0x00000200 | CLASS_INSTM;
const int CLASS_OUTMEMORY = 0x00000200 | CLASS_OUTSTM;

// third level of inheritance
const int CLASS_LOGFILE   = 0x00010000 | CLASS_OUTFILE;


class objlist;

class ptpublic component: public unknown 
{
protected:
    int       refcount;     // reference counting, used by addref() and release()
    objlist*  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* addref(component*);
    ptpublic friend bool release(component*);
    ptpublic 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)   // MSVC can't handle this template correctly if we name
    { return (T*)addref((component*)c); }    // it as addref(). (gcc is fine though)


template <class T> class compref
{
protected:
    T* ref;
public:
    compref()                                   { ref = 0; }
    compref(const compref& r)                   { ref = taddref<T>(r.ref); }
    compref(T* u)                               { ref = taddref<T>(u); }
    ~compref()                                  { release(ref); }
    compref& operator= (const compref& r)       { release(ref); ref = taddref<T>(r.ref); return *this; }
    compref& operator= (T* u)                   { release(ref); ref = taddref<T>(u); return *this; }
    T&   operator* ()                           { return *ref; }
    T*   operator-> ()                          { return ref; }
    bool operator== (const compref& r) const    { return ref == r.ref; }
    bool operator== (const T* u) const          { return ref == u; }
    bool operator!= (const compref& r) const    { return ref != r.ref; }
    bool operator!= (const T* u) const          { return ref != u; }
         operator T* ()                         { return ref; }
};


// -------------------------------------------------------------------- //
// ---  exception class ----------------------------------------------- //
// -------------------------------------------------------------------- //


class ptpublic exceptobj: public unknown 
{
protected:
    string message;
public:
    exceptobj(const char* imsg);
    exceptobj(const string& imsg);
    virtual ~exceptobj();
    virtual string get_message() { return message; }
};


//
// conversion exception class
// (see stringtoie() and stringtoue()
//


class ptpublic econv: public exceptobj
{
protected:
public:
    econv(const char* msg): exceptobj(msg)  {}
    econv(const string& msg): exceptobj(msg)  {}
    virtual ~econv();
};


// -------------------------------------------------------------------- //
// ---  object list --------------------------------------------------- //
// -------------------------------------------------------------------- //


class ptpublic objlist: public unknown 
{
protected:
    unknown**   list;
    int         count;
    int         capacity;
    bool        ownobjects;

    void idxerror() const;

    void setcapacity(int newcap);
    int  getcount() const { return count; }
    void setcount(int newcount);
    void grow();
    void clear();
    void insitem(int i, unknown* iobj);
    void putitem(int i, unknown* iobj);
    void delitem(int i);
    unknown* getitem(int i) const  { return list[i]; }

public:
    objlist();
    objlist(bool iownobjects);
    virtual ~objlist();

    void checkidx(int i) const                      { if (i < 0 || i >= count) idxerror(); }

    ptpublic friend int      length(const objlist& s);
    ptpublic friend void     setlength(objlist& s, int newcount);
    ptpublic friend void     pack(objlist& s);
    ptpublic friend void     clear(objlist& s);
    ptpublic friend void     ins(objlist& s, int i, unknown* iobj);
    ptpublic friend int      add(objlist& s, unknown* iobj);
    ptpublic friend void     put(objlist& s, int i, unknown* iobj);
    ptpublic friend unknown* get(const objlist& s, int i);
    ptpublic friend int      push(objlist& s, unknown* obj);
    ptpublic friend unknown* top(const objlist& s);
    ptpublic friend unknown* pop(objlist& s);
    ptpublic friend void     del(objlist& s, int i);
    ptpublic friend int      indexof(const objlist& s, unknown* iobj);

    unknown* operator[] (int i) const;
};


inline int length(const objlist& s)                { return s.getcount(); }
inline void setlength(objlist& s, int newcount)    { s.setcount(newcount); }
inline void pack(objlist& s)                       { s.setcapacity(s.count); }
inline void clear(objlist& s)                      { s.clear(); }
inline unknown* top(const objlist& s)              { return get(s, length(s) - 1); }
inline int push(objlist& s, unknown* obj)          { return add(s, obj); }

#ifndef CHECK_BOUNDS
inline unknown* get(const objlist& s, int i)       { return s.getitem(i); }
#endif

inline unknown* objlist::operator[] (int i) const  { return PTYPES_NAMESPACE::get(*this, i); }


// a fully inlined objlist template for use with any class
// derived from unknown. provided for extra typechecking and
// to get rid of typecasts. 'unknown* t = obj' assignments in
// these functions are to check whether T is derived from
// unknown. similarly, template lists are provided for strlist
// and strmap.

template <class T> class tobjlist: public objlist
{
public:
    tobjlist(): objlist()  {}
    tobjlist(bool iownobjects): objlist(iownobjects)  {}

    friend inline void ins(tobjlist& s, int i, T* obj)
        { unknown* t = obj; PTYPES_NAMESPACE::ins(s, i, t); }

    friend inline int add(tobjlist& s, T* obj)
        { unknown* t = obj; return PTYPES_NAMESPACE::add(s, t); }

    friend inline void put(tobjlist& s, int i, T* obj)
        { unknown* t = obj; PTYPES_NAMESPACE::put(s, i, t); }

    friend inline int indexof(const tobjlist& s, T* obj)
        { unknown* t = obj; return PTYPES_NAMESPACE::indexof(s, t); }

    friend inline int push(tobjlist& s, T* obj)
        { unknown* t = obj; return PTYPES_NAMESPACE::push(s, t); }

    friend inline T* pop(tobjlist& s)
        { return (T*)PTYPES_NAMESPACE::pop((objlist&)s); }

    friend inline T* top(const tobjlist& s)
        { return (T*)PTYPES_NAMESPACE::get(s, length(s) - 1); }

    friend inline T* get(const tobjlist& s, int i)
        { return (T*)PTYPES_NAMESPACE::get((const objlist&)s, i); }

    T* operator[] (int i) const
        { return (T*)PTYPES_NAMESPACE::get(*this, i); }
};


// -------------------------------------------------------------------- //
// ---  string list --------------------------------------------------- //
// -------------------------------------------------------------------- //


struct _stritem 
{
    string str;
    unknown* obj;
};

typedef _stritem* pstritem;
const int _stritemsize = sizeof(_stritem);

enum slflags 
{
    SL_SORTED = 0x0001,
    SL_DUPLICATES = 0x0002,
    SL_CASESENS = 0x0004,
    SL_OWNOBJECTS = 0x0008
};


class ptpublic strlist: public unknown 
{
protected:
    _stritem*   list;
    int         count;
    int         capacity;
    slflags     flags;

    void idxerror() const;
    void sortederror() const;
    void notsortederror() const;
    void duperror() const;

    void setcapacity(int newcap);
    int  getcount() const { return count; }
    void grow();
    void clear();
    virtual int compare(const string& s1, const string& s2) const;
    bool search(const string& s, int& index) const;
    void insitem(int i, const string& istr, unknown* iobj);
    void putstr(int i, const string& istr);
    void putobj(int i, unknown* iobj);
    void delitem(int i);
    const string& getstr(int i) const  { return list[i].str; }
    unknown* getobj(int i) const  { return list[i].obj; }

public:
    strlist();
    strlist(slflags iflags);
    virtual ~strlist();

    void checkidx(int i) const                  { if (i < 0 || i >= count) idxerror(); }
    bool get_sorted() const                     { return (SL_SORTED & flags) != 0; }
    bool get_duplicates() const                 { return (SL_DUPLICATES & flags) != 0; }
    bool get_casesens() const                   { return (SL_CASESENS & flags) != 0; }
    bool get_ownobjects() const                 { return (SL_OWNOBJECTS & flags) != 0; }
    int  get_capacity() const                   { return capacity; }

    ptpublic friend int      length(const strlist& s);
    ptpublic friend void     clear(strlist& s);
    ptpublic friend void     pack(strlist& s);
    ptpublic friend bool     search(const strlist& s, const string& key, int& index);
    ptpublic friend void     ins(strlist& s, int i, const string& key, unknown* obj);
    ptpublic friend int      add(strlist& s, const string& key, unknown* obj);
    ptpublic friend void     put(strlist& s, int i, const string& key, unknown* obj);
    ptpublic friend void     put(strlist& s, int i, unknown* obj);
    ptpublic friend unknown* get(const strlist& s, int i);
    ptpublic friend const string& getstr(const strlist& s, int i);
    ptpublic friend void     del(strlist& s, int i);
    ptpublic friend int      find(const strlist& s, const string& key);
    ptpublic friend int      indexof(const strlist& s, unknown* obj);
    ptpublic friend string   valueof(const strlist& s, const char* key);

    unknown* operator[] (int i) const;
};


inline int length(const strlist& s)                   { return s.getcount(); }
inline void clear(strlist& s)                         { s.clear(); }
inline void pack(strlist& s)                          { s.setcapacity(s.count); }

#ifndef CHECK_BOUNDS
inline const string& getstr(const strlist& s, int i)  { return s.getstr(i); }
inline unknown* get(const strlist& s, int i)          { return s.getobj(i); }
#endif

inline unknown* strlist::operator[] (int i) const     { return PTYPES_NAMESPACE::get(*this, i); }


template <class T> class tstrlist: public strlist
{
public:
    tstrlist(): strlist() {}
    tstrlist(slflags iflags): strlist(iflags)  {}

    friend inline void ins(tstrlist& s, int i, const string& str, T* obj)
        { unknown* t = obj; PTYPES_NAMESPACE::ins(s, i, str, t); }

    friend inline int  add(tstrlist& s, const string& str, T* obj)
        { unknown* t = obj; return PTYPES_NAMESPACE::add(s, str, t); }

    friend inline void put(tstrlist& s, int i, const string& str, T* obj)
        { unknown* t = obj; PTYPES_NAMESPACE::put(s, i, str, t); }

    friend inline void put(tstrlist& s, int i, T* obj)
        { unknown* t = obj; PTYPES_NAMESPACE::put(s, i, t); }

    friend inline int indexof(const tstrlist& s, T* obj)
        { unknown* t = obj; return PTYPES_NAMESPACE::indexof(s, t); }

    friend inline T* get(const tstrlist& s, int i)
        { return (T*)PTYPES_NAMESPACE::get((const strlist&)s, i); }

    T* operator[] (int i) const
        { return (T*)PTYPES_NAMESPACE::get(*this, i); }
};



// -------------------------------------------------------------------- //
// ---  string map ---------------------------------------------------- //
// -------------------------------------------------------------------- //

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -