convert line ends
[canaan.git] / prj / cam / h / persist.h
blob20f381ff8c00c74afdf70e47767c283494144919
1 /*
2 @Copyright Looking Glass Studios, Inc.
3 1996,1997,1998,1999,2000 Unpublished Work.
4 */
6 ///////////////////////////////////////////////////////////////////////////////
7 // $Header: r:/t2repos/thief2/libsrc/script/persist.h,v 1.2 1999/06/29 19:10:57 mahk Exp $
8 //
9 // sPersistent is a weird little utility struct for loading and
10 // saving the basic types used by scripts, including vectors and
11 // cMultiParms.
13 // A struct or class which wants to be persistent should descend
14 // from sPersistent. For the sake of one of our macros, it will
15 // also want a default constructor.
17 #ifndef __PERSIST_H
18 #define __PERSIST_H
20 #pragma once
23 #define kPersistNameMax 31
26 typedef int (*tPersistIOFunc)(void * pContext, void * p, size_t size);
29 struct sPersistent
31 virtual ~sPersistent() { };
33 BOOL ReadWrite(tPersistIOFunc pfnIO, void * pContextIO, BOOL fReading);
34 BOOL Reading() { return gm_fReading; };
36 BOOL Persistent(string &);
37 BOOL Persistent(const char *&);
38 BOOL Persistent(void *, size_t);
39 BOOL Persistent(const char *, size_t);
40 BOOL Persistent(real &);
41 BOOL Persistent(integer &);
42 BOOL Persistent(vector &);
43 BOOL Persistent(boolean &);
44 BOOL Persistent(ulong &);
45 BOOL Persistent(cMultiParm &);
47 virtual BOOL Persistence() { return FALSE; }
48 virtual const char *GetName() { return "no factory"; }
50 protected:
51 static tPersistIOFunc gm_pfnIO;
52 static void * gm_pContextIO;
53 static BOOL gm_fReading;
56 ///////////////////////////////////////
58 inline BOOL sPersistent::ReadWrite(tPersistIOFunc pfnIO,
59 void * pContextIO, BOOL fReading)
61 gm_pContextIO = pContextIO;
62 gm_pfnIO = pfnIO;
63 gm_fReading = fReading;
65 return Persistence();
68 ///////////////////////////////////////
70 inline BOOL sPersistent::Persistent(void * p, size_t size)
72 return (*gm_pfnIO)(gm_pContextIO, p, size) == size;
75 ///////////////////////////////////////
77 inline BOOL sPersistent::Persistent(const char * p, size_t size)
79 return Persistent((void *)p, size);
82 ///////////////////////////////////////
84 inline BOOL sPersistent::Persistent(real & r)
86 return (*gm_pfnIO)(gm_pContextIO, &r, sizeof(real)) == sizeof(real);
89 ///////////////////////////////////////
91 inline BOOL sPersistent::Persistent(integer & i)
93 return (*gm_pfnIO)(gm_pContextIO, &i, sizeof(integer)) == sizeof(integer);
96 ///////////////////////////////////////
98 inline BOOL sPersistent::Persistent(vector & v)
100 return (*gm_pfnIO)(gm_pContextIO, &v, sizeof(vector)) == sizeof(vector);
103 ///////////////////////////////////////
105 inline BOOL sPersistent::Persistent(boolean & b)
107 return (*gm_pfnIO)(gm_pContextIO, &b, sizeof(boolean)) == sizeof(boolean);
110 ///////////////////////////////////////
112 inline BOOL sPersistent::Persistent(ulong & i)
114 return (*gm_pfnIO)(gm_pContextIO, &i, sizeof(ulong)) == sizeof(ulong);
118 ///////////////////////////////////////////////////////////////////////////////
121 typedef sPersistent * (*tPersistFactory)();
123 struct sPersistReg;
124 extern sPersistReg *g_pPersistFactoryList;
126 struct sPersistReg
128 sPersistReg(tPersistFactory pfnFactory, const char *pszName)
129 : m_pfnFactory(pfnFactory),
130 m_pszName(pszName)
132 m_pNext = g_pPersistFactoryList;
133 g_pPersistFactoryList = this;
136 const tPersistFactory m_pfnFactory;
137 const char *m_pszName;
139 sPersistReg *m_pNext;
142 extern sPersistent * PersistReadWrite(sPersistent *p, tPersistIOFunc pfnIO,
143 void * pContextIO, BOOL fReading);
145 extern sPersistReg* PersistLookupReg(const char* pszName);
147 ///////////////////////////////////////////////////////////////////////////////
150 #define PERSIST_DECLARE() \
151 virtual const char * GetName()
153 #define PERSIST_DECLARE_PERSISTENT() \
154 PERSIST_DECLARE(); \
155 BOOL Persistence()
157 ///////////////////////////////////////
159 #define PERSIST_IMPLEMENT(STR) \
161 sPersistent * g_PersistFactory##STR () \
163 sPersistent * p = new STR; \
164 return p; \
167 sPersistReg g_PersistReg##STR(g_PersistFactory##STR, #STR); \
169 const char * STR::GetName() \
171 return #STR; \
174 #define PERSIST_IMPLEMENT_PERSISTENT(STR) \
175 PERSIST_IMPLEMENT(STR); \
176 BOOL STR::Persistence()
178 #define PersistentEnum(e) \
179 Assert_(sizeof(e) == sizeof(int)); \
180 Persistent(*((int *)(&(e))))
182 ///////////////////////////////////////////////////////////////////////////////
184 #endif // ~__PERSIST_H