?? data.h
字號:
/* * HT Editor * data.h * * Copyright (C) 2002, 2003 Stefan Weyergraf (stefan@weyergraf.de) * Copyright (C) 2002, 2003 Sebastian Biallas (sb@biallas.net) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */#ifndef __DATA_H__#define __DATA_H__#ifdef HAVE_CONFIG_H#include "config.h"#endif#include "io/types.h"#include <cstdlib>typedef uint32 ObjectID;typedef uint32 ID;class ObjectStream;struct BuildCtorArg {};template <typename T1, typename T2>inline bool instanceOf(const T2 *o){ return (dynamic_cast<const T1*>(o) != NULL);} /* * C style malloc support */class HTMallocRes;HTMallocRes ht_malloc(size_t);class HTMallocRes{private: friend HTMallocRes ht_malloc(size_t); const size_t mSize; HTMallocRes(size_t size) : mSize(size) { } HTMallocRes operator=(const HTMallocRes &); // not implementedpublic: template <typename T> operator T* () const { return static_cast<T*>(::malloc(mSize)); }};inline HTMallocRes ht_malloc(size_t size){ return HTMallocRes(size);}/** * Macro for creating object build functions */#define BUILDER(reg, obj, parent) Object *build_##obj(){BuildCtorArg a;return new obj(a);}#define BUILDER2(reg, obj) Object *build_##obj(){BuildCtorArg a;return new obj(a);}/** * Registers builder function by object id. */#define REGISTER(reg, obj) registerAtom(reg, (void*)build_##obj);/** * Unregisters builder function by object id. */#define UNREGISTER(reg, obj) unregisterAtom(reg);/* actually a str => bigendian-int *//** used to define ObjectIDs */#define MAGIC32(magic) (unsigned long)(((unsigned char)magic[0]<<24) | ((unsigned char)magic[1]<<16) | ((unsigned char)magic[2]<<8) | (unsigned char)magic[3])/** No/invalid object */#define OBJID_INVALID ((ObjectID)0)/** A placeholder object id */#define OBJID_TEMP ((ObjectID)-1)#define OBJID_OBJECT MAGIC32("DAT\x00")#define OBJID_ARRAY MAGIC32("DAT\x10")#define OBJID_STACK MAGIC32("DAT\x11")#define OBJID_BINARY_TREE MAGIC32("DAT\x20")#define OBJID_AVL_TREE MAGIC32("DAT\x21")#define OBJID_SET MAGIC32("DAT\x22")#define OBJID_SLINKED_LIST MAGIC32("DAT\x30")#define OBJID_QUEUE MAGIC32("DAT\x31")#define OBJID_DLINKED_LIST MAGIC32("DAT\x32")#define OBJID_KEYVALUE MAGIC32("DAT\x40")#define OBJID_SINT MAGIC32("DAT\x41")#define OBJID_SINT64 MAGIC32("DAT\x42")#define OBJID_UINT MAGIC32("DAT\x43")#define OBJID_UINT64 MAGIC32("DAT\x44")#define OBJID_FLOAT MAGIC32("DAT\x45")#define OBJID_MEMAREA MAGIC32("DAT\x48")#define OBJID_STRING MAGIC32("DAT\x50")#define OBJID_ISTRING MAGIC32("DAT\x51")#define OBJID_AUTO_COMPARE MAGIC32("DAT\xc0")/** * This is THE base class. */class Object {public: Object(BuildCtorArg&) {}; Object() {}; virtual ~Object() {}; void init() {}; virtual void done() {};/* new *//** * Standard object duplicator. * @returns copy of object */ virtual Object * clone() const;/** * Standard Object comparator. * @param obj object to compare to * @returns 0 for equality, negative number if |this<obj| and positive number if |this>obj| */ virtual int compareTo(const Object *obj) const;/** * Stringify object. * Stringify object in string-buffer <i>s</i>. Never writes more than * <i>maxlen</i> characters to <i>s</i>. If <i>maxlen</i> is > 0, a * trailing zero-character is written. * * @param buf pointer to buffer that receives object stringification * @param buflen size of buffer that receives object stringification * @returns number of characters written to <i>s</i>, not including the trailing zero */ virtual int toString(char *buf, int buflen) const;/** * Standard Object idle function. * Overwrite and register with htidle.cc::register_idle() * (FIXME) * * @returns true if working, false if really idle */ virtual bool idle();/** * Load object from object stream. * * @param s object stream to load this object from */ virtual void load(ObjectStream &s);/** * @returns unique object id. */ virtual ObjectID getObjectID() const;/** * stores object. * * @param s object stream to store this object into */ virtual void store(ObjectStream &s) const;};typedef int (*Comparator)(const Object *a, const Object *b);int autoCompare(const Object *a, const Object *b);typedef void* ObjHandle;const ObjHandle invObjHandle = NULL;const uint invIdx = ((uint)-1);/** * An Enumerator. */class Enumerator: public Object {public: Enumerator(BuildCtorArg&a): Object(a) {}; Enumerator() {}; /* extends Object */ virtual Enumerator * clone() const = 0; virtual int toString(char *buf, int buflen) const; /* new *//** * Count elements. * * @returns number of objects contained in this structure * @throws NotImplementedException if counting of elements is not supported */ virtual uint count() const = 0;/** * Compare objects. * Compare objects <i>a</i> and <i>b</i> and determine their (logical) * linear order. * * @param a object a * @param b object b * @returns 0 if <i>a</i> equals <i>b</i>, * a value >0 if <i>a</i> is greater than <i>b</i> and * a value <0 if <i>a</i> is less than <i>b</i> */ virtual int compareObjects(const Object *a, const Object *b) const = 0;/** * Test if contained. * Test if an object like <i>obj</i> is contained in this structure * * @param obj signature of object to find * @returns true if an object like <i>obj</i> is contained, false otherwise */ inline bool contains(const Object *obj) const { return find(obj) != invObjHandle; }/** * Test if empty. * @returns true if empty */ inline bool isEmpty() const { return count() == 0; }/** * Find equal object. * Find first object equaling <i>obj</i> in this structure * and if found return it's object handle. * * @param obj signature of object to find * @returns object handle of found object or <i>invObjHandle</i> if not found */ virtual ObjHandle find(const Object *obj) const;/** * Find greater-or-equal object. * Find lowest object being greater or equal compared to <i>obj</i> in this structure * and if found return it's object handle. (lowest, greater and equal are * defined via the compareTo method) * * @param obj signature of object to find * @returns object handle of found object or <i>invObjHandle</i> if not found */ virtual ObjHandle findGE(const Object *obj) const;/** * Find greater object. * Find lowest object being greater compared to <i>obj</i> in this structure * and if found return it's object handle. (lowest and greater are * defined via the compareTo method) * * @param obj signature of object to find * @returns object handle of found object or <i>invObjHandle</i> if not found */ virtual ObjHandle findG(const Object *obj) const;/** * Find lower-or-equal object. * Find greatest object being lower or equal compared to <i>obj</i> in this structure * and if found return it's object handle. (greatest, lower and equal are * defined via the compareTo method) * * @param obj signature of object to find * @returns object handle of found object or <i>invObjHandle</i> if not found */ virtual ObjHandle findLE(const Object *obj) const;/** * Find lower object. * Find greatest object being lower compared to <i>obj</i> in this structure * and if found return it's object handle. (greatest and lower are * defined via the compareTo method) * * @param obj signature of object to find * @returns object handle of found object or <i>invObjHandle</i> if not found */ virtual ObjHandle findL(const Object *obj) const;/** * Find object's handle by index. * * @param i index of object to find * @returns object handle of found object or <i>invObjHandle</i> if not found */ virtual ObjHandle findByIdx(int i) const = 0;/** * Find (logically) last element's object handle. * * @returns object handle of the last element or <i>invObjHandle</i> * if the structure is empty */ virtual ObjHandle findLast() const = 0;/** * Find (logically) previous element's object handle. * Find logically previous element (predecessor) of the object identified * by <i>h</i>. Predecessor of "the invalid object" is the last element * in this structure by definition. (ie. <i>findPrev(invObjHandle) := * findLast()</i>). * * @param h object handle to find a predecessor to * @returns object handle of predecessor or <i>invObjHandle</i> if <i>h</i> * identifies the first element. */ virtual ObjHandle findPrev(ObjHandle h) const = 0;/** * Find (logically) first element's object handle. * * @returns object handle of the first element or <i>invObjHandle</i> * if this structure is empty */ virtual ObjHandle findFirst() const = 0;/** * Find (logically) next element's object handle. * Find logically next element (successor) of the object, identified * by <i>h</i>. Successor of "the invalid object" is the first element * in this structure by definition. (ie. <i>findNext(invObjHandle) := * findFirst()</i>). * * @param h object handle to find a successor to * @returns object handle of successor or <i>invObjHandle</i> if <i>h</i> * identifies the last element. */ virtual ObjHandle findNext(ObjHandle h) const = 0;/** * Get object pointer from object handle. * * @param h object handle * @returns object pointer if <i>h</i> is valid, <i>NULL</i> otherwise */ virtual Object * get(ObjHandle h) const = 0;/** * Get object's index from its handle. * * @param h object handle of object * @returns index of object if <i>h</i> is valid, <i>InvIdx</i> otherwise. */ virtual uint getObjIdx(ObjHandle h) const = 0;/** * Get element by index. * Get the element with index <i>idx</i> (if possible). * * @param idx index of element to get * @returns pointer to the requested element or <i>NULL</i> if <i>idx</i> * is invalid. */ Object * operator [] (int idx) const { return get(findByIdx(idx)); }};#define foreach(XTYPE, X, E, code...)\for (ObjHandle temp0815 = (E).findFirst(); temp0815 != invObjHandle;) {\ XTYPE *X = (XTYPE*)(E).get(temp0815); \ temp0815 = (E).findNext(temp0815); \ {code;} \}#define foreachbwd(XTYPE, X, E, code...)\for (ObjHandle temp0815 = (E).findLast(); temp0815 != invObjHandle;) {\ XTYPE *X = (XTYPE*)(E).get(temp0815); \ temp0815 = (E).findPrev(temp0815); \ {code;} \}#define firstThat(XTYPE, X, E, condition) \{ \ XTYPE *Y = NULL; \ foreach(XTYPE, X, E, \ if (condition) { \ Y = X; \ break; \ } \ ) \ X = Y; \}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -