mdl.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #ifndef _mdl_h_
  2. #define _mdl_h_
  3. //////////////////////////////////////////////////////////////////////////////
  4. //
  5. // Stack
  6. //
  7. //////////////////////////////////////////////////////////////////////////////
  8. typedef TStack<TRef<IObject> > ObjectStack;
  9. //////////////////////////////////////////////////////////////////////////////
  10. //
  11. // Functions
  12. //
  13. //////////////////////////////////////////////////////////////////////////////
  14. class IBinaryReaderSite : public IObject {
  15. public:
  16. virtual DWORD GetDWORD() = 0;
  17. virtual ZString GetString() = 0;
  18. virtual BYTE* GetPointer() = 0;
  19. virtual void MovePointer(int count) = 0;
  20. virtual IObject* GetMemoryObject() = 0;
  21. virtual IObject* GetObject() = 0;
  22. template<class Type>
  23. void GetStructure(Type*& ptype)
  24. {
  25. ptype = (Type*)GetPointer();
  26. MovePointer(sizeof(Type));
  27. }
  28. template<class Type>
  29. void CopyStructure(Type* ptype)
  30. {
  31. ZAssert(ptype);
  32. memcpy(ptype, (Type*)GetPointer(), sizeof(Type));
  33. MovePointer(sizeof(Type));
  34. }
  35. };
  36. class IFunction : public IObject {
  37. public:
  38. virtual TRef<IObject> Apply(ObjectStack& stack) = 0;
  39. virtual TRef<IObject> Read(IBinaryReaderSite* psite, ObjectStack& stack);
  40. };
  41. //////////////////////////////////////////////////////////////////////////////
  42. //
  43. // MDL types
  44. //
  45. //////////////////////////////////////////////////////////////////////////////
  46. class MDLType : public IObject {
  47. public:
  48. virtual int GetSize() = 0;
  49. virtual void Destruct(BYTE*& pbyte) = 0;
  50. virtual void StackRead(BYTE*& pbyte, ObjectStack& stack) = 0;
  51. virtual void Read(BYTE*& pbyte, IBinaryReaderSite* psite, ObjectStack& stack) = 0;
  52. virtual ZString GetCPPType() = 0;
  53. virtual ZString GetHungarianPrefix() = 0;
  54. virtual bool IsStruct()
  55. {
  56. return false;
  57. }
  58. /*
  59. virtual void Write(void*& pv, IMDLBinaryFile* pmdlFile)
  60. {
  61. ZUnimplemented();
  62. return NULL;
  63. }
  64. */
  65. bool IsTypeOf(MDLType* ptype)
  66. {
  67. return ptype == this;
  68. }
  69. };
  70. typedef TList<TRef<MDLType> > MDLTypeList;
  71. TRef<MDLType> CreateStringMDLType();
  72. TRef<MDLType> CreateIObjectMDLType(const ZString& strType, const ZString& strPrefix);
  73. ZString GetStructHeader(MDLType* ptype, const ZString& strIdentifier);
  74. //////////////////////////////////////////////////////////////////////////////
  75. //
  76. // BaseMDLType
  77. //
  78. //////////////////////////////////////////////////////////////////////////////
  79. template<class Type>
  80. class TBaseMDLType : public MDLType {
  81. private:
  82. ZString m_str;
  83. ZString m_strPrefix;
  84. public:
  85. TBaseMDLType(const ZString& str, const ZString& strPrefix) :
  86. m_str(str),
  87. m_strPrefix(strPrefix)
  88. {
  89. }
  90. int GetSize()
  91. {
  92. return sizeof(Type);
  93. }
  94. virtual bool IsStruct()
  95. {
  96. return false;
  97. }
  98. void Destruct(BYTE*& pbyte)
  99. {
  100. pbyte += sizeof(Type);
  101. }
  102. void StackRead(BYTE*& pv, ObjectStack& stack)
  103. {
  104. TRef<TStaticValue<Type> > pvalue; CastTo(pvalue, (IObject*)stack.Pop());
  105. *(Type*)pv = pvalue->GetValue();
  106. pv += sizeof(Type);
  107. }
  108. void Read(BYTE*& pv, IBinaryReaderSite* psite, ObjectStack& stack)
  109. {
  110. psite->CopyStructure((Type*)pv);
  111. pv += sizeof(Type);
  112. }
  113. ZString GetCPPType()
  114. {
  115. return m_str;
  116. }
  117. ZString GetHungarianPrefix()
  118. {
  119. return m_strPrefix;
  120. }
  121. };
  122. //////////////////////////////////////////////////////////////////////////////
  123. //
  124. // MDL Objects
  125. //
  126. //////////////////////////////////////////////////////////////////////////////
  127. class IMDLObject : public IObject {
  128. private:
  129. TRef<MDLType> m_ptype;
  130. int m_index;
  131. public:
  132. IMDLObject(MDLType* ptype) :
  133. m_ptype(ptype),
  134. m_index(-1)
  135. {
  136. }
  137. ~IMDLObject()
  138. {
  139. BYTE* pbyte = GetDataPointer();
  140. m_ptype->Destruct(pbyte);
  141. }
  142. #ifdef _DebugNewDefined_
  143. #undef new
  144. void *operator new(size_t alloc)
  145. {
  146. return ::new(__FILE__, __LINE__) BYTE[alloc];
  147. }
  148. void* operator new(size_t alloc, int size)
  149. {
  150. BYTE* pbyte = (BYTE*)::new(__FILE__, __LINE__) BYTE[alloc + size];
  151. memset(pbyte + alloc, 0, size);
  152. return pbyte;
  153. }
  154. static IMDLObject* Allocate(int size, MDLType* ptype)
  155. {
  156. return new(size) IMDLObject(ptype);
  157. }
  158. #define new new(__FILE__, __LINE__)
  159. #else
  160. void *operator new(size_t alloc)
  161. {
  162. return ::new BYTE[alloc];
  163. }
  164. void* operator new(size_t alloc, int size)
  165. {
  166. BYTE* pbyte = (BYTE*)::new BYTE[alloc + size];
  167. memset(pbyte + alloc, 0, size);
  168. return pbyte;
  169. }
  170. static IMDLObject* Allocate(int size, MDLType* ptype)
  171. {
  172. return new(size) IMDLObject(ptype);
  173. }
  174. #endif
  175. BYTE* GetDataPointer()
  176. {
  177. return (BYTE*)(this + 1);
  178. }
  179. void SetIndex(int index)
  180. {
  181. m_index = index;
  182. }
  183. int GetIndex()
  184. {
  185. return m_index;
  186. }
  187. MDLType* GetType()
  188. {
  189. return m_ptype;
  190. }
  191. /* !!! write is a Value method not a IObject method
  192. void Write(IMDLBinaryFile* pmdlFile)
  193. {
  194. GetType()->Write(GetDataPointer(), pmdlFile);
  195. }
  196. */
  197. };
  198. //////////////////////////////////////////////////////////////////////////////
  199. //
  200. // Object Pair
  201. //
  202. //////////////////////////////////////////////////////////////////////////////
  203. class IObjectPair : public IObject {
  204. TRef<IObject> m_pfirst;
  205. TRef<IObject> m_psecond;
  206. public:
  207. IObjectPair(IObject* pfirst, IObject* psecond) :
  208. m_pfirst(pfirst),
  209. m_psecond(psecond)
  210. {
  211. }
  212. IObject* GetFirst() { return m_pfirst; }
  213. IObject* GetSecond() { return m_psecond; }
  214. IObject* GetNth(int index);
  215. IObject* GetLastNth(int index);
  216. };
  217. #endif