00001 #ifndef FITYPEDESC_H
00002 #define FITYPEDESC_H
00003
00004 #include "FIIterator.h"
00005 #include <Base/BaseUtility.h>
00006 #include <WinBasis/WBUtility.h>
00007 #include <set>
00008 #include <map>
00009 #include <algorithm>
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 namespace Spr {;
00035
00036
00037
00038 class FIVVectorBase{
00039 public:
00040 virtual size_t VItemSize() = 0;
00041 virtual size_t VSize() = 0;
00042 virtual void VResize(size_t s) = 0;
00043 virtual char* VBegin() = 0;
00044 virtual char* VEnd() = 0;
00045 virtual void VInsert(int pos, void* v) = 0;
00046 virtual void VPushBack(void* v) = 0;
00047 virtual void VPushBack() = 0;
00048 virtual void VPopBack() = 0;
00049 };
00050
00051
00052
00053 template <class T>
00054 class FIVVector: public FIVVectorBase, public std::vector<T>{
00055 virtual size_t VItemSize(){ return sizeof(T); }
00056 virtual size_t VSize(){ return size(); }
00057 virtual void VResize(size_t s){ resize(s); }
00058 virtual char* VBegin(){ return (char*)&*begin(); }
00059 virtual char* VEnd(){ return (char*) &*end(); }
00060 virtual void VInsert(int pos, void* v){ insert(begin()+pos, *(T*)v); }
00061 virtual void VPushBack(void* v){ push_back(*(T*)v); }
00062 virtual void VPushBack(){ push_back(T()); }
00063 virtual void VPopBack(){ pop_back(); }
00064 public:
00065 FIVVector(){}
00066 FIVVector(const std::vector<T>& v):std::vector<T>(v){}
00067 FIVVector& operator=(const std::vector<T>& v){ *(std::vector<T>*)this = v; return *this; }
00068 };
00069
00070 class FIDocNodeBase;
00071 class FIDocNodePos;
00072
00073 class FITypeDescDb;
00074
00075 class SPR_DLL FITypeDesc:public UTRefCount{
00076 public:
00077
00078 class SPR_DLL Field{
00079 public:
00080 Field(): length(1), lengthFieldOffset(0), lengthFieldSize(0), bReference(false), bVector(false){}
00081 ~Field();
00082
00083 UTString name;
00084
00085 UTRef<FITypeDesc> type;
00086
00087 int length;
00088
00089 UTString lengthFieldName;
00090
00091 int lengthFieldOffset;
00092
00093 int lengthFieldSize;
00094
00095 int bVector;
00096
00097 bool bReference;
00098
00099 size_t Size();
00100
00101 void Print(std::ostream& os) const;
00102
00103 bool Read(char*& to, size_t align, FIDocNodeBase* node, FIIterator& ctx);
00104
00105 void Write(FIDocNodeBase* node, FIIterator& ctx, const char*& from);
00106 };
00107
00108 class SPR_DLL Composit: public std::vector<Field>{
00109 public:
00110
00111 int Size(){ return Size(""); }
00112
00113 int Size(FIString id);
00114
00115 void Print(std::ostream& os) const;
00116
00117 bool Read(char*& to, size_t align, FIDocNodeBase* node, FIIterator& ctx);
00118
00119 void Write(FIDocNodeBase* node, FIIterator& ctx, const char*& from);
00120 };
00121 protected:
00122
00123 UTString typeName;
00124
00125 size_t size;
00126
00127 Composit composit;
00128
00129 friend class FITypeDescDb;
00130 public:
00131
00132 WBGuid guid;
00133
00134 FITypeDesc():size(0){}
00135
00136 FITypeDesc(UTString tn, int sz=0): typeName(tn), size(sz){}
00137
00138 ~FITypeDesc(){}
00139
00140 bool Init(const char* clsDef, FITypeDescDb& db);
00141
00142 void Print(std::ostream& os) const;
00143
00144 int Read(void* to, FIDocNodeBase* node, FIString id);
00145 int Read(void* to, FIDocNodeBase* node){ return Read(to, node, ""); }
00146
00147 bool Read(char*& to, size_t align, FIDocNodeBase* node, FIIterator& ctx);
00148
00149 int Write(FIDocNodeBase* node, const void* from);
00150
00151 void Write(FIDocNodeBase* node, FIIterator& ctx, const char*& from, UTString id);
00152
00153 UTString GetTypeName() const { return typeName; }
00154
00155 const GUID& GetGuid() const { return guid; };
00156
00157 size_t Size() { return size; }
00158
00159 Composit& GetComposit(){ return composit; }
00160 };
00161 inline bool operator < (const FITypeDesc& d1, const FITypeDesc& d2){
00162 return d1.GetTypeName().compare(d2.GetTypeName()) < 0;
00163 }
00164
00165 class SPR_DLL FITypeDescDb: public UTRefCount{
00166 public:
00167
00168 typedef std::set< UTRef<FITypeDesc>, UTContentsLess< UTRef<FITypeDesc> > > Db;
00169 struct ProtoDesc{
00170 FIString fileType;
00171 UTRef<FITypeDesc> desc;
00172 };
00173 typedef std::vector<ProtoDesc> ProtoDescs;
00174 protected:
00175 FIString prefix;
00176 Db db;
00177 static ProtoDescs protoDescs;
00178 public:
00179
00180 ~FITypeDescDb();
00181 static const ProtoDescs& GetProtoDescs(){ return protoDescs; }
00182
00183 void RegisterDesc(FITypeDesc* n){
00184 if (prefix.length() && n->typeName.compare(0, prefix.length(), prefix) == 0){
00185 n->typeName = n->typeName.substr(prefix.length());
00186 }
00187 db.insert(n);
00188 }
00189
00190 void RegisterProto(FITypeDesc* n);
00191
00192
00193 void Register(const char* type, const char* def, bool bProto=false){
00194 UTRef<FITypeDesc> desc = new FITypeDesc(type);
00195 desc->Init(def, *this);
00196 RegisterDesc(desc);
00197 if(bProto) RegisterProto(desc);
00198 }
00199
00200
00201 void SetPrefix(FIString p);
00202
00203
00204 FITypeDesc* Find(UTString tn);
00205
00206 void Print(std::ostream& os) const ;
00207 };
00208
00209
00210 #define VSIZE(x)
00211
00212
00213 #define REG_FIELD(type) RegisterDesc( new FITypeDesc(#type, sizeof(type)) )
00214
00215
00216 #define DEF_RECORDBASE(Type, Base, Def) \
00217 struct Type: public Base Def; \
00218 static const char* classNameOf##Type = #Type; \
00219 static const char* classDefOf##Type = #Def; \
00220
00221
00222
00223 #define DEF_RECORD(Type, Def) \
00224 struct Type Def; \
00225 static const char* classNameOf##Type = #Type; \
00226 static const char* classDefOf##Type = #Def; \
00227
00228
00229 #define REG_RECORD(Type) \
00230 Register(classNameOf##Type, classDefOf##Type)
00231
00232
00233 #define REG_RECORD_PROTO(Type) \
00234 Register(classNameOf##Type, classDefOf##Type, true)
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250 }
00251 #endif