Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

363 lines
15 KiB

  1. #ifndef __SAFEPNT_H__
  2. #define __SAFEPNT_H__
  3. #ifndef ASSERT
  4. #define ASSERT(X)
  5. #endif
  6. void CALLBACK FreeSRowSet(LPSRowSet prs);
  7. #if defined(_MAC)
  8. #include <macdefs.h>
  9. class CSpTdxMemory
  10. : virtual public CTdxMemory
  11. {
  12. };
  13. #define CLASS_NEW : virtual public CSpTdxMemory
  14. #else
  15. #define CLASS_NEW
  16. #endif
  17. // safe pointer header file. snarfed from Cairo. The macro
  18. // SAFE_INTERFACE_PTR takes a single parameter (vs. two of original) of
  19. // type Interface, i.e. IMessage and generates a class SpInterface for it
  20. // i.e. SpIMessage which can be used in place of IMessage *. If you use this
  21. // header, AND place your locals correctly you don't need to release anything.
  22. //
  23. // with the void operator =, you can assign a NULL (0) pointer. This prevents
  24. // the destructor releasing the object. sort of Transfer to void.
  25. //
  26. // owner: muzok
  27. #if defined(WIN32)
  28. template <class Interface> class TSafeIPtr
  29. {
  30. Interface * m_pi;
  31. public:
  32. TSafeIPtr(void) { m_pi = NULL; }
  33. TSafeIPtr(Interface * pi) { m_pi = pi; }
  34. ~TSafeIPtr(void) { if (m_pi != NULL) m_pi->Release(); }
  35. void Transfer(Interface ** ppi) { *ppi = m_pi; m_pi = NULL; }
  36. operator Interface *() { return m_pi; }
  37. Interface * operator ->() { return m_pi; }
  38. Interface& operator *() { return *m_pi; }
  39. Interface** operator &() { return &m_pi; }
  40. void operator =(Interface * pi) { m_pi = pi; }
  41. void operator =(TSafeIPtr& i) { m_pi = i.m_pi; m_pi->AddRef(); }
  42. BOOL FIsValid(void) const { return m_pi != NULL; }
  43. void Release(void) {
  44. if (m_pi != NULL) { m_pi->Release(); m_pi = NULL; }}
  45. };
  46. #define SAFE_INTERFACE_PTR(a) typedef TSafeIPtr<a> Sp##a;
  47. #else // !WIN32
  48. #define SAFE_INTERFACE_PTR(Interface) \
  49. class Sp##Interface CLASS_NEW \
  50. { \
  51. public: \
  52. inline Sp##Interface(Interface* p):m_p(p) { if (0 != m_p) m_p->AddRef(); }\
  53. inline Sp##Interface(void) { m_p = NULL; } \
  54. inline ~Sp##Interface() { if (0 != m_p) m_p->Release(); } \
  55. inline void Transfer(Interface** p) { *p = m_p; m_p = 0; } \
  56. inline operator Interface *() { return m_p; } \
  57. inline Interface* operator ->() { return m_p; } \
  58. inline Interface& operator *() { return *m_p; } \
  59. inline Interface** operator &() { return &m_p; } \
  60. inline void operator =(Interface* pv) { m_p = pv; } \
  61. inline void operator =(Sp##Interface & v) { m_p = v.m_p; m_p->AddRef();} \
  62. inline BOOL FIsValid(void) const { return m_p != NULL; } \
  63. inline void Release(void) { if (0 != m_p) {m_p->Release(); m_p = NULL; } \
  64. return; } \
  65. private: \
  66. inline Sp##Interface(const Sp##Interface &) {;} \
  67. Interface* m_p; \
  68. }
  69. #endif // WIN32
  70. #if defined(MAPIDEFS_H)
  71. SAFE_INTERFACE_PTR(IABContainer);
  72. SAFE_INTERFACE_PTR(IAddrBook);
  73. SAFE_INTERFACE_PTR(IAttach);
  74. SAFE_INTERFACE_PTR(IDistList);
  75. SAFE_INTERFACE_PTR(IMailUser);
  76. SAFE_INTERFACE_PTR(IMAPIAdviseSink);
  77. SAFE_INTERFACE_PTR(IMAPIContainer);
  78. SAFE_INTERFACE_PTR(IMAPIFolder);
  79. SAFE_INTERFACE_PTR(IMAPIProp);
  80. SAFE_INTERFACE_PTR(IMAPISession);
  81. SAFE_INTERFACE_PTR(IMAPIStatus);
  82. SAFE_INTERFACE_PTR(IMAPITable);
  83. SAFE_INTERFACE_PTR(IMessage);
  84. SAFE_INTERFACE_PTR(IMsgStore);
  85. SAFE_INTERFACE_PTR(IProfSect);
  86. SAFE_INTERFACE_PTR(IPropData);
  87. SAFE_INTERFACE_PTR(ITableData);
  88. #endif // defined(MAPIDEFS_H)
  89. #if defined(MAPIFORM_H)
  90. SAFE_INTERFACE_PTR(IMAPIForm);
  91. SAFE_INTERFACE_PTR(IMAPIFormContainer);
  92. SAFE_INTERFACE_PTR(IMAPIFormFactory);
  93. SAFE_INTERFACE_PTR(IMAPIFormInfo);
  94. SAFE_INTERFACE_PTR(IMAPIFormMgr);
  95. SAFE_INTERFACE_PTR(IMAPIMessageSite);
  96. SAFE_INTERFACE_PTR(IMAPIViewContext);
  97. SAFE_INTERFACE_PTR(IPersistMessage);
  98. #endif // defined(MAPIFORM_H)
  99. #if defined (VLB_MIN)
  100. SAFE_INTERFACE_PTR(IVlbEnum);
  101. #endif // defined(VLB_MIN)
  102. #if defined(__objidl_h__) || defined(_STORAGE_H_)
  103. SAFE_INTERFACE_PTR(IStorage);
  104. SAFE_INTERFACE_PTR(IStream);
  105. SAFE_INTERFACE_PTR(IMalloc);
  106. SAFE_INTERFACE_PTR(ILockBytes);
  107. SAFE_INTERFACE_PTR(IEnumSTATSTG);
  108. SAFE_INTERFACE_PTR(IClassFactory);
  109. SAFE_INTERFACE_PTR(IUnknown);
  110. #endif // defined(__objidl_h__)
  111. #if defined(MAPIDEFS_H)
  112. #define SAFE_MAPI_ARRAY(MAPIBuffer) \
  113. class Sp##MAPIBuffer \
  114. CLASS_NEW \
  115. { \
  116. public: \
  117. inline Sp##MAPIBuffer(MAPIBuffer* p):m_p(p) { ; } \
  118. inline Sp##MAPIBuffer(void) { m_p = 0; } \
  119. inline ~Sp##MAPIBuffer() { MAPIFreeBuffer((LPVOID)m_p); } \
  120. inline void Transfer(MAPIBuffer** p) { *p = m_p; m_p = 0; } \
  121. inline operator MAPIBuffer *() { return m_p; } \
  122. inline MAPIBuffer* operator ->() { return m_p; } \
  123. inline MAPIBuffer& operator *() { return *m_p; } \
  124. inline MAPIBuffer** operator &() { return &m_p; } \
  125. inline void operator =(MAPIBuffer* pv) { ASSERT(0 == pv); m_p = pv; } \
  126. inline void Release(void) { MAPIFreeBuffer((LPVOID)m_p); m_p=NULL; } \
  127. private: \
  128. inline void operator =(Sp##MAPIBuffer &) {;} \
  129. inline Sp##MAPIBuffer(const Sp##MAPIBuffer &) {;} \
  130. MAPIBuffer* m_p; \
  131. }
  132. SAFE_MAPI_ARRAY(SPropValue);
  133. SAFE_MAPI_ARRAY(SPropTagArray);
  134. #if defined(WIN32)
  135. template <class ptrtype> class TSafeMAPIPtr
  136. {
  137. ptrtype * m_p;
  138. public:
  139. TSafeMAPIPtr(void) { m_p = NULL; }
  140. TSafeMAPIPtr(ptrtype * p) { m_p = p; }
  141. ~TSafeMAPIPtr(void) { MAPIFreeBuffer((LPVOID) m_p);}
  142. void Transfer(ptrtype ** pp) { *pp = m_p; m_p = NULL; }
  143. operator ptrtype *() { return m_p; }
  144. ptrtype ** operator &() { return &m_p; }
  145. void operator =(ptrtype * p) { m_p = p; }
  146. };
  147. typedef TSafeMAPIPtr<VOID> SpMAPIVOID;
  148. typedef TSafeMAPIPtr<ENTRYID> SpMAPIENTRYID;
  149. #else // !WIN32
  150. #define SAFE_MAPI_PTR(TYPE) \
  151. class SpMAPI##TYPE CLASS_NEW \
  152. { \
  153. public: \
  154. inline SpMAPI##TYPE(TYPE* p) {m_p = p; } \
  155. inline SpMAPI##TYPE(void) { m_p = 0; } \
  156. inline ~SpMAPI##TYPE() { MAPIFreeBuffer((LPVOID)m_p); } \
  157. inline void Transfer(TYPE** p) { *p = m_p; m_p = 0; } \
  158. inline operator TYPE *() { return m_p; } \
  159. inline TYPE** operator &() { return &m_p; } \
  160. inline void operator =(TYPE* pv) { ASSERT((0 == pv) || (m_p == 0)); m_p = pv; } \
  161. private: \
  162. inline void operator =(SpMAPI##TYPE &) {;} \
  163. inline SpMAPI##TYPE(const SpMAPI##TYPE &) {;} \
  164. TYPE* m_p; \
  165. };
  166. SAFE_MAPI_PTR(VOID)
  167. SAFE_MAPI_PTR(ENTRYID)
  168. #endif // WIN32
  169. #if 0
  170. template <class Set> class TSafeSet
  171. {
  172. Set * m_p;
  173. public:
  174. TSafeSet(Set * p) { m_p = p; }
  175. TSafeSet(void) { m_p = NULL; }
  176. ~TSafeSet(void) { FreeSRowSet((LPSRowSet) m_p); }
  177. void Release(void) { FreeSRowSet((LPSRowSet) m_p); m_p = NULL; }
  178. void Transfer(Set ** pp) { *pp = m_p; m_p = NULL; }
  179. SPropValue * GetProp(int iRow, int iCol) {
  180. return &(m_p->aRow[iRow].lpProps[iCol]);
  181. }
  182. operator Set *() { return m_p; }
  183. Set * operator ->() { return m_p; }
  184. Set& operator *() { return *m_p; }
  185. Set ** operator &() { return &m_p; }
  186. void operator =(Set * p) { ASSERT(NULL == p); m_p = p; }
  187. };
  188. typedef TSafeSet<SRowSet> SpSRowSet;
  189. typedef TSafeSet<ADRLIST> Sp_ADRLIST;
  190. #else // !WIN32
  191. class SpSRowSet CLASS_NEW
  192. {
  193. public:
  194. inline SpSRowSet(SRowSet* p) : m_p(p) {;}
  195. inline SpSRowSet(void) { m_p = 0; }
  196. inline ~SpSRowSet() { FreeSRowSet(m_p); }
  197. inline void Transfer(SRowSet** p) { *p = m_p; m_p = 0; }
  198. inline operator SRowSet *() { return m_p; }
  199. inline SRowSet* operator ->() { return m_p; }
  200. inline SRowSet& operator *() { return *m_p; }
  201. inline SRowSet** operator &() { return &m_p; }
  202. inline void operator =(SRowSet* pv) { ASSERT(0 == pv); m_p = pv; }
  203. inline SPropValue * GetProp(int iRow, int iCol) { return &(m_p->aRow[iRow].lpProps[iCol]); }
  204. private:
  205. inline void operator =(SpSRowSet &) {;}
  206. inline SpSRowSet(const SpSRowSet&) {;}
  207. SRowSet* m_p;
  208. };
  209. class Sp_ADRLIST
  210. {
  211. public:
  212. inline Sp_ADRLIST(_ADRLIST* p) : m_p(p) {;}
  213. inline Sp_ADRLIST(void) { m_p = 0; }
  214. inline ~Sp_ADRLIST() { FreeProws((SRowSet *) m_p); }
  215. inline void Release(void) { FreeProws((SRowSet *) m_p); m_p = NULL; }
  216. inline void Transfer(_ADRLIST** p) { *p = m_p; m_p = 0; }
  217. inline operator _ADRLIST *() { return m_p; }
  218. inline _ADRLIST* operator ->() { return m_p; }
  219. inline _ADRLIST& operator *() { return *m_p; }
  220. inline _ADRLIST** operator &() { return &m_p; }
  221. inline void operator =(_ADRLIST* pv) { ASSERT(0 == pv); m_p = pv; }
  222. inline SPropValue * GetProp(int iRow, int iCol) { return &(m_p->aEntries[iRow].rgPropVals[iCol]); }
  223. private:
  224. inline void operator =(Sp_ADRLIST &) {;}
  225. inline Sp_ADRLIST(const Sp_ADRLIST&) {;}
  226. _ADRLIST* m_p;
  227. };
  228. #endif // WIN32
  229. #endif defined(MAPIDEFS_H)
  230. #ifndef _MAC
  231. #define SAFE_MEM_PTR(TYPE) \
  232. class Sp##TYPE \
  233. CLASS_NEW \
  234. { \
  235. public: \
  236. inline Sp##TYPE(TYPE* p) : m_p(p) { ; } \
  237. inline Sp##TYPE(void) { m_p = NULL; } \
  238. inline ~Sp##TYPE(void) { delete [] m_p; } \
  239. inline void Transfer(TYPE** p) { *p = m_p; m_p = 0; } \
  240. \
  241. inline TYPE* operator ++() { return ++m_p; } \
  242. inline TYPE* operator ++(int) { return m_p++; } \
  243. inline TYPE* operator --() { return --m_p; } \
  244. inline TYPE* operator --(int) { return m_p--; } \
  245. inline operator TYPE*() { return m_p; } \
  246. inline TYPE& operator *() { return *m_p; } \
  247. inline TYPE** operator &() { return &m_p; } \
  248. inline void operator =(TYPE* p) { m_p = p; } \
  249. private: \
  250. inline void operator =(Sp##TYPE &) {;} \
  251. inline Sp##TYPE(const Sp##TYPE&) {;} \
  252. TYPE* m_p; \
  253. }
  254. #else // _MAC
  255. #define SAFE_MEM_PTR(TYPE) \
  256. class Sp##TYPE \
  257. CLASS_NEW \
  258. { \
  259. public: \
  260. inline Sp##TYPE(TYPE* p) : m_p(p) { ; } \
  261. inline Sp##TYPE(void) { m_p = NULL; } \
  262. inline ~Sp##TYPE(void) { TdxMacFree(m_p); } \
  263. inline void Transfer(TYPE** p) { *p = m_p; m_p = 0; } \
  264. \
  265. inline TYPE* operator ++() { return ++m_p; } \
  266. inline TYPE* operator ++(int) { return m_p++; } \
  267. inline TYPE* operator --() { return --m_p; } \
  268. inline TYPE* operator --(int) { return m_p--; } \
  269. inline operator TYPE*() { return m_p; } \
  270. inline TYPE& operator *() { return *m_p; } \
  271. inline TYPE** operator &() { return &m_p; } \
  272. inline void operator =(TYPE* p) { m_p = p; } \
  273. private: \
  274. inline void operator =(Sp##TYPE &) {;} \
  275. inline Sp##TYPE(const Sp##TYPE&) {;} \
  276. TYPE* m_p; \
  277. }
  278. #endif // _MAC
  279. SAFE_MEM_PTR(TCHAR);
  280. SAFE_MEM_PTR(int);
  281. SAFE_MEM_PTR(ULONG);
  282. SAFE_MEM_PTR(BYTE);
  283. #ifdef WIN16
  284. typedef char OLECHAR;
  285. #endif // WIN16
  286. SAFE_MEM_PTR(OLECHAR);
  287. SAFE_MEM_PTR(GUID);
  288. class SppTCHAR CLASS_NEW
  289. {
  290. public:
  291. inline SppTCHAR(TCHAR** p) : m_p(p) { ; }
  292. inline ~SppTCHAR() { delete [] m_p; }
  293. inline void Transfer(TCHAR*** p) { *p = m_p; m_p = 0; }
  294. inline TCHAR** operator ++() { return ++m_p; }
  295. inline TCHAR** operator ++(int) { return m_p++; }
  296. inline TCHAR** operator --() { return --m_p; }
  297. inline TCHAR** operator --(int) { return m_p--; }
  298. inline operator TCHAR**() { return m_p; }
  299. inline TCHAR*& operator *() { return *m_p; }
  300. inline TCHAR*** operator &() { return &m_p; }
  301. inline void operator =(TCHAR** p) { m_p = p; }
  302. private:
  303. inline void operator =(SppTCHAR* &) {;}
  304. inline SppTCHAR(const SppTCHAR*&) {;}
  305. TCHAR** m_p;
  306. };
  307. /////////////////////////////////////////////////////////////////////////
  308. #if 0
  309. #ifdef WIN32
  310. template <class handle, fnDelete> class TSafeHandle
  311. {
  312. handle m_h;
  313. public:
  314. TSafeHandle(void) { m_h = NULL; }
  315. ~TSafeHandle(void) { fnDelete(m_h); }
  316. };
  317. #define SAFE_HANDLE(a, b) typedef TSafeHandle<a, b> Sp##a;
  318. SAFE_HANLE(HCRYPTHASH, CryptDestroyHash)
  319. #endif // WIN32
  320. #endif // 0
  321. #endif // __SAFEPNT_H__