123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- #ifndef _mdl_h_
- #define _mdl_h_
- //////////////////////////////////////////////////////////////////////////////
- //
- // Stack
- //
- //////////////////////////////////////////////////////////////////////////////
- typedef TStack<TRef<IObject> > ObjectStack;
- //////////////////////////////////////////////////////////////////////////////
- //
- // Functions
- //
- //////////////////////////////////////////////////////////////////////////////
- class IBinaryReaderSite : public IObject {
- public:
- virtual DWORD GetDWORD() = 0;
- virtual ZString GetString() = 0;
- virtual BYTE* GetPointer() = 0;
- virtual void MovePointer(int count) = 0;
- virtual IObject* GetMemoryObject() = 0;
- virtual IObject* GetObject() = 0;
- template<class Type>
- void GetStructure(Type*& ptype)
- {
- ptype = (Type*)GetPointer();
- MovePointer(sizeof(Type));
- }
- template<class Type>
- void CopyStructure(Type* ptype)
- {
- ZAssert(ptype);
- memcpy(ptype, (Type*)GetPointer(), sizeof(Type));
- MovePointer(sizeof(Type));
- }
- };
- class IFunction : public IObject {
- public:
- virtual TRef<IObject> Apply(ObjectStack& stack) = 0;
- virtual TRef<IObject> Read(IBinaryReaderSite* psite, ObjectStack& stack);
- };
- //////////////////////////////////////////////////////////////////////////////
- //
- // MDL types
- //
- //////////////////////////////////////////////////////////////////////////////
- class MDLType : public IObject {
- public:
- virtual int GetSize() = 0;
- virtual void Destruct(BYTE*& pbyte) = 0;
- virtual void StackRead(BYTE*& pbyte, ObjectStack& stack) = 0;
- virtual void Read(BYTE*& pbyte, IBinaryReaderSite* psite, ObjectStack& stack) = 0;
- virtual ZString GetCPPType() = 0;
- virtual ZString GetHungarianPrefix() = 0;
- virtual bool IsStruct()
- {
- return false;
- }
- /*
- virtual void Write(void*& pv, IMDLBinaryFile* pmdlFile)
- {
- ZUnimplemented();
- return NULL;
- }
- */
- bool IsTypeOf(MDLType* ptype)
- {
- return ptype == this;
- }
- };
- typedef TList<TRef<MDLType> > MDLTypeList;
- TRef<MDLType> CreateStringMDLType();
- TRef<MDLType> CreateIObjectMDLType(const ZString& strType, const ZString& strPrefix);
- ZString GetStructHeader(MDLType* ptype, const ZString& strIdentifier);
- //////////////////////////////////////////////////////////////////////////////
- //
- // BaseMDLType
- //
- //////////////////////////////////////////////////////////////////////////////
- template<class Type>
- class TBaseMDLType : public MDLType {
- private:
- ZString m_str;
- ZString m_strPrefix;
- public:
- TBaseMDLType(const ZString& str, const ZString& strPrefix) :
- m_str(str),
- m_strPrefix(strPrefix)
- {
- }
- int GetSize()
- {
- return sizeof(Type);
- }
- virtual bool IsStruct()
- {
- return false;
- }
- void Destruct(BYTE*& pbyte)
- {
- pbyte += sizeof(Type);
- }
- void StackRead(BYTE*& pv, ObjectStack& stack)
- {
- TRef<TStaticValue<Type> > pvalue; CastTo(pvalue, (IObject*)stack.Pop());
- *(Type*)pv = pvalue->GetValue();
- pv += sizeof(Type);
- }
- void Read(BYTE*& pv, IBinaryReaderSite* psite, ObjectStack& stack)
- {
- psite->CopyStructure((Type*)pv);
- pv += sizeof(Type);
- }
- ZString GetCPPType()
- {
- return m_str;
- }
- ZString GetHungarianPrefix()
- {
- return m_strPrefix;
- }
- };
- //////////////////////////////////////////////////////////////////////////////
- //
- // MDL Objects
- //
- //////////////////////////////////////////////////////////////////////////////
- class IMDLObject : public IObject {
- private:
- TRef<MDLType> m_ptype;
- int m_index;
- public:
- IMDLObject(MDLType* ptype) :
- m_ptype(ptype),
- m_index(-1)
- {
- }
- ~IMDLObject()
- {
- BYTE* pbyte = GetDataPointer();
- m_ptype->Destruct(pbyte);
- }
- #ifdef _DebugNewDefined_
- #undef new
- void *operator new(size_t alloc)
- {
- return ::new(__FILE__, __LINE__) BYTE[alloc];
- }
- void* operator new(size_t alloc, int size)
- {
- BYTE* pbyte = (BYTE*)::new(__FILE__, __LINE__) BYTE[alloc + size];
- memset(pbyte + alloc, 0, size);
- return pbyte;
- }
- static IMDLObject* Allocate(int size, MDLType* ptype)
- {
- return new(size) IMDLObject(ptype);
- }
- #define new new(__FILE__, __LINE__)
- #else
- void *operator new(size_t alloc)
- {
- return ::new BYTE[alloc];
- }
- void* operator new(size_t alloc, int size)
- {
- BYTE* pbyte = (BYTE*)::new BYTE[alloc + size];
- memset(pbyte + alloc, 0, size);
- return pbyte;
- }
- static IMDLObject* Allocate(int size, MDLType* ptype)
- {
- return new(size) IMDLObject(ptype);
- }
- #endif
- BYTE* GetDataPointer()
- {
- return (BYTE*)(this + 1);
- }
- void SetIndex(int index)
- {
- m_index = index;
- }
- int GetIndex()
- {
- return m_index;
- }
- MDLType* GetType()
- {
- return m_ptype;
- }
- /* !!! write is a Value method not a IObject method
- void Write(IMDLBinaryFile* pmdlFile)
- {
- GetType()->Write(GetDataPointer(), pmdlFile);
- }
- */
- };
- //////////////////////////////////////////////////////////////////////////////
- //
- // Object Pair
- //
- //////////////////////////////////////////////////////////////////////////////
- class IObjectPair : public IObject {
- TRef<IObject> m_pfirst;
- TRef<IObject> m_psecond;
- public:
- IObjectPair(IObject* pfirst, IObject* psecond) :
- m_pfirst(pfirst),
- m_psecond(psecond)
- {
- }
- IObject* GetFirst() { return m_pfirst; }
- IObject* GetSecond() { return m_psecond; }
- IObject* GetNth(int index);
- IObject* GetLastNth(int index);
- };
- #endif
|