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.

672 lines
16 KiB

  1. //--------------------------------------------------------------------
  2. // Microsoft DART Utilities
  3. //
  4. // Copyright 1994 Microsoft Corporation. All Rights Reserved.
  5. //
  6. // @doc
  7. //
  8. // @module auto.h | Definition of <c CAutoRg> and <c CAutoP>
  9. //
  10. // @devnote None
  11. //
  12. // @rev 0 | 24-Oct-94 | matthewb | Created
  13. // @rev 1 | 01-May-95 | rossbu | Updated and consolidated interface
  14. // @rev 2 | 23-May-95 | eugenez | Added support for TaskAlloc'ed pointers
  15. //
  16. extern IMalloc * g_pIMalloc;
  17. //--------------------------------------------------------------------
  18. //
  19. // @class CAutoBase | This template class is a base class, used to give dynamic memory
  20. // local (auto) scope within a function. For instance a large character
  21. // buffer can be allocated from a memory object, but be cleanup up as if were
  22. // allocated on the stack. An additional feature is the ability to
  23. // 'unhook' the object from the local scope using the PvReturn(). For
  24. // instance, you may want to return a newly allocated object to the
  25. // caller, but still have the benefit of error clean up any error
  26. // scenario. <c CAutoRg> is a derived class which cleans up arrays allocated
  27. // with new[]. <c CAutoP> is an analagous but works for single objects
  28. // rather than arrays (allocated with new).
  29. //
  30. // @tcarg class | T | Type of auto object
  31. //
  32. // @ex This declaration would allocate a 100 char buffer from pmem, and
  33. // automatically free the buffer when rgbBuf goes out of scope. |
  34. //
  35. // CAutoRg<lt>char<gt> rgbBuf(New(pmem) char[100]);
  36. //
  37. // @xref <c CAutoRg>
  38. //
  39. // @ex This CAutoP example allocates a CFoo object and returns it if there
  40. // are no errors. |
  41. //
  42. // /* inilize pfoo */
  43. // CAutoP<lt>CFoo<gt> pfoo(New(pmem) CFoo);
  44. // /* do stuff */
  45. // /* call pfoo methods */
  46. // pfoo->Bar();
  47. // /* return w/o destroying foo */
  48. // return pfoo .PvReturn;
  49. //
  50. // @xref <c CAutoP>
  51. //
  52. // ************************ CAutoBase - base class for all AutoPointers *****************************
  53. template <class T>
  54. class CAutoBase
  55. {
  56. public: // @access public
  57. inline CAutoBase(T* pt);
  58. inline ~CAutoBase();
  59. inline T* operator= (T*);
  60. inline operator T* (void);
  61. inline operator const T* (void)const;
  62. inline T ** operator & (void);
  63. inline T* PvReturn(void);
  64. protected: // @access protected
  65. T* m_pt;
  66. private: // Never-to-use
  67. inline CAutoBase& operator= (CAutoBase&);
  68. CAutoBase(CAutoBase&);
  69. };
  70. //--------------------------------------------------------------------
  71. // @mfunc Create a CAutoBase giving the array of objects pointed to by pt
  72. // auto scope.
  73. // @side Allows NULL to be passed in
  74. // @rdesc None
  75. template <class T>
  76. inline CAutoBase<T>::CAutoBase(T *pt)
  77. {
  78. m_pt = pt;
  79. }
  80. //--------------------------------------------------------------------
  81. // @mfunc CAutoBase destructor. Asserts that the object has been free'd
  82. // (set to NULL). Setting to NULL does not happen in the retail build
  83. // @side None
  84. // @rdesc None
  85. //
  86. template <class T>
  87. inline CAutoBase<T>::~CAutoBase()
  88. {
  89. // _ASSERT(NULL == m_pt);
  90. }
  91. //--------------------------------------------------------------------
  92. // @mfunc Assigns to variable after construction. May be dangerous
  93. // so it assert's that the variable is NULL
  94. // @side None
  95. // @rdesc None
  96. //
  97. // @ex Assign CAutoBase variable after construction. |
  98. //
  99. // CAutoBase<lt>char<gt> rgb;
  100. // /* ... */
  101. // rgb(NewG char[100]);
  102. //
  103. template <class T>
  104. inline T* CAutoBase<T>::operator=(T* pt)
  105. {
  106. _ASSERT(m_pt == NULL);
  107. m_pt = pt;
  108. return pt;
  109. }
  110. //--------------------------------------------------------------------
  111. // @mfunc Cast operator used to "unwrap" the pointed object
  112. // as if the CAutoBase variable were a pointer of type T.
  113. // In many situations this is enough for an autopointer to
  114. // look exactly like an ordinary pointer.
  115. // @side None
  116. // @rdesc None
  117. //
  118. template <class T>
  119. inline CAutoBase<T>::operator T*(void)
  120. {
  121. return m_pt;
  122. }
  123. template <class T>
  124. inline CAutoBase<T>::operator const T*(void)const
  125. {
  126. return m_pt;
  127. }
  128. //--------------------------------------------------------------------
  129. // @mfunc Address-of operator is used to make the autopointer even more
  130. // similar to an ordinary pointer. When you take an address of an
  131. // autopointer, you actually get an address of the wrapped
  132. // pointer.
  133. // @side None
  134. // @rdesc None
  135. template <class T>
  136. inline T ** CAutoBase<T>::operator & ()
  137. {
  138. return & m_pt;
  139. }
  140. //--------------------------------------------------------------------
  141. // @mfunc Returns the object(s) pointed to by the CAutoBase variable.
  142. // In addition this method 'unhooks' the object(s), such that
  143. // the scope of the object(s) are no longer local.
  144. //
  145. // See <c CAutoBase> for an example.
  146. // @side None
  147. // @rdesc None
  148. //
  149. template <class T>
  150. inline T * CAutoBase<T>::PvReturn(void)
  151. {
  152. T *ptT = m_pt;
  153. m_pt = NULL;
  154. return ptT;
  155. }
  156. //************************* CAutoRg - autopointers to arrays ******************************
  157. //--------------------------------------------------------------------
  158. // @class This derived class is primarily used to implement the
  159. // vector deleting destructor. Should only be used on objects allocated
  160. // with new[]
  161. //
  162. template <class T>
  163. class CAutoRg :
  164. public CAutoBase<T>
  165. {
  166. public: // @access public
  167. inline CAutoRg(T *pt);
  168. inline ~CAutoRg();
  169. inline T* operator= (T*);
  170. private: // Never-to-use
  171. inline CAutoRg& operator= (CAutoRg&);
  172. CAutoRg(CAutoRg&);
  173. };
  174. //--------------------------------------------------------------------
  175. // @mfunc Create a CAutoRg giving the array of objects pointed to by pt
  176. // auto scope.
  177. // @side Allows NULL to be passed in
  178. // @rdesc None
  179. template <class T>
  180. inline CAutoRg<T>::CAutoRg(T *pt)
  181. : CAutoBase<T>(pt)
  182. {
  183. }
  184. //--------------------------------------------------------------------
  185. // @mfunc CAutoRg destructor. When an object of class CAutoRg goes out
  186. // of scope, free the associated object (if any).
  187. // @side calls the vector delete method
  188. // @rdesc None
  189. //
  190. template <class T>
  191. inline CAutoRg<T>::~CAutoRg()
  192. {
  193. delete [] m_pt;
  194. }
  195. //--------------------------------------------------------------------
  196. // @mfunc Assigns to variable after construction. May be dangerous
  197. // so it assert's that the variable is NULL
  198. // @side None
  199. // @rdesc None
  200. //
  201. // @ex Assign CAutoRg variable after construction. |
  202. //
  203. // CAutoRg<lt>char<gt> rgb;
  204. // /* ... */
  205. // rgb(NewG char[100]);
  206. //
  207. template <class T>
  208. inline T* CAutoRg<T>::operator=(T* pt)
  209. {
  210. return ((CAutoBase<T> *) this)->operator= (pt);
  211. }
  212. //*************************** CAutoP - autopointers to scalars **************
  213. //--------------------------------------------------------------------
  214. // @class This is analagous to <c CAutoRg> but calls scalar delete
  215. // of an object rather than arrays.
  216. //
  217. // @xref <c CAutoRg>
  218. template <class T>
  219. class CAutoP :
  220. public CAutoBase<T>
  221. {
  222. public: // @access public
  223. inline CAutoP(T *pt);
  224. inline ~CAutoP();
  225. inline T* operator= (T*);
  226. inline T* operator->(void);
  227. private: // Never-to-use
  228. inline CAutoP& operator= (CAutoP&);
  229. CAutoP(CAutoP&);
  230. };
  231. //--------------------------------------------------------------------
  232. // @mfunc Create a CAutoP giving the object pointed to by pt
  233. // auto scope.
  234. // @side Allows NULL to be passed in
  235. // @rdesc None
  236. template <class T>
  237. inline CAutoP<T>::CAutoP(T *pt)
  238. : CAutoBase<T>(pt)
  239. {
  240. }
  241. //--------------------------------------------------------------------
  242. // @mfunc Delete the object pointed by CAutoP variable if any.
  243. // @side None
  244. // @rdesc None
  245. //
  246. template <class T>
  247. inline CAutoP<T>::~CAutoP()
  248. {
  249. delete m_pt;
  250. }
  251. //--------------------------------------------------------------------
  252. // @mfunc Assigns to variable after construction. May be dangerous
  253. // so it assert's that the variable is NULL.
  254. // Assign operator is not inherited, so it has to be written
  255. // again. Just calls base class assignment.
  256. // @side None
  257. // @rdesc None
  258. //
  259. template <class T>
  260. inline T* CAutoP<T>::operator=(T* pt)
  261. {
  262. return ((CAutoBase<T> *) this)->operator= (pt);
  263. }
  264. //--------------------------------------------------------------------
  265. // @mfunc The 'follow' operator on the CAutoP allows an CAutoP variable
  266. // to act like a pointer of type T. This overloading generally makes using
  267. // a CAutoP simple as using a regular T pointer.
  268. //
  269. // See <c CAutoRg> example.
  270. // @side None
  271. // @rdesc None
  272. template <class T>
  273. inline T * CAutoP<T>::operator->()
  274. {
  275. _ASSERT(m_pt != NULL);
  276. return m_pt;
  277. }
  278. //******************** CAutoTask - autopointers to TaskAlloc'ed memory ***************
  279. //--------------------------------------------------------------------
  280. // @class CAutoTask is an autopointer to an area allocated using TaskAlloc.
  281. // May be used for scalars or vectors alike, but beware: object destructors
  282. // are not called by the autopointer, just the memory gets released.
  283. //
  284. template <class T>
  285. class CAutoTask :
  286. public CAutoBase<T>
  287. {
  288. public: // @access public
  289. inline CAutoTask (T *pt);
  290. inline ~CAutoTask ();
  291. inline T* operator= (T*);
  292. private: // Never-to-use
  293. inline CAutoTask& operator= (CAutoTask&);
  294. CAutoTask(CAutoTask&);
  295. };
  296. //--------------------------------------------------------------------
  297. // @mfunc Constructor simply calls the constructor for CAutoBase<lt>T<gt>.
  298. // @side None
  299. // @rdesc None
  300. template <class T>
  301. inline CAutoTask<T>::CAutoTask(T *pt)
  302. : CAutoBase<T>(pt)
  303. {
  304. }
  305. //--------------------------------------------------------------------
  306. // @mfunc Free the memory pointed to by CAutoTask variable.
  307. // @side None
  308. // @rdesc None
  309. //
  310. template <class T>
  311. inline CAutoTask<T>::~CAutoTask()
  312. {
  313. if (m_pt)
  314. g_pIMalloc->Free(m_pt);
  315. }
  316. //--------------------------------------------------------------------
  317. // @mfunc Assigns to variable after construction. May be dangerous
  318. // so it assert's that the variable is NULL.
  319. // Assign operator is not inherited, so it has to be written
  320. // again. Just calls base class assignment.
  321. // @side None
  322. // @rdesc None
  323. //
  324. template <class T>
  325. inline T* CAutoTask<T>::operator=(T* pt)
  326. {
  327. return ((CAutoBase<T> *) this)->operator= (pt);
  328. }
  329. //************************* CAutoUnivRg - universal autopointers to arrays ******************************
  330. //--------------------------------------------------------------------
  331. // @class CAutoUnivRg and CAutoUnivP are "universal" autopointer classes.
  332. // They can handle those rare occasions when the "auto-scoped" pointer
  333. // might have been allocated by either New or TaskAlloc, depending on
  334. // the circumstances. You have to always know however just how it was
  335. // allocated this time, and pass this knowledge to the CAutoUniv object
  336. // at construction time.
  337. //
  338. // CAutoUniv objects have the additional construction parameter of type
  339. // BOOL. It is used in fact as a BOOL flag: TRUE means that the
  340. // pointer is allocated by TaskAlloc, and FALSE means NewG.
  341. //
  342. template <class T>
  343. class CAutoUnivRg :
  344. public CAutoRg<T>
  345. {
  346. public: // @access public
  347. inline CAutoUnivRg (T *pt, BOOL fIsTaskAlloc);
  348. inline ~CAutoUnivRg ();
  349. inline T* operator= (T*);
  350. private:
  351. BOOL m_fTaskAlloc;
  352. private: // Never-to-use
  353. inline CAutoUnivRg& operator= (CAutoUnivRg&);
  354. CAutoUnivRg(CAutoUnivRg&);
  355. };
  356. //--------------------------------------------------------------------
  357. // @mfunc Create a CAutoUnivRg giving the array of objects pointed to by pt
  358. // auto scope. Takes a pointer to a memory object, NULL indicates global
  359. // IMalloc (not a global memory object!).
  360. // @side Allows NULL to be passed in
  361. // @rdesc None
  362. template <class T>
  363. inline CAutoUnivRg<T>::CAutoUnivRg (T *pt, BOOL fIsTaskAlloc)
  364. : CAutoRg<T>(pt)
  365. {
  366. m_fTaskAlloc = fIsTaskAlloc;
  367. }
  368. //--------------------------------------------------------------------
  369. // @mfunc CAutoUnivRg destructor. When an object of class CAutoUnivRg goes out
  370. // of scope, free the associated object (if any).
  371. // @side calls the vector delete method
  372. // @rdesc None
  373. //
  374. template <class T>
  375. inline CAutoUnivRg<T>::~CAutoUnivRg()
  376. {
  377. if (m_fTaskAlloc)
  378. {
  379. // m_pt->~T(); // Awaits VC++ 3.0...
  380. g_pIMalloc->Free(m_pt);
  381. }
  382. else
  383. delete [] m_pt;
  384. }
  385. //--------------------------------------------------------------------
  386. // @mfunc Assigns to variable after construction. May be dangerous
  387. // so it assert's that the variable is NULL
  388. // @side None
  389. // @rdesc None
  390. //
  391. template <class T>
  392. inline T* CAutoUnivRg<T>::operator=(T* pt)
  393. {
  394. return ((CAutoBase<T> *) this)->operator= (pt);
  395. }
  396. //*************************** CAutoUnivP - universal autopointers to scalars **************
  397. //--------------------------------------------------------------------
  398. // @class This is analagous to <c CAutoUnivRg> but calls scalar delete
  399. // of an object rather than arrays.
  400. //
  401. template <class T>
  402. class CAutoUnivP :
  403. public CAutoP<T>
  404. {
  405. public: // @access public
  406. inline CAutoUnivP(T *pt, BOOL fIsTaskAlloc);
  407. inline ~CAutoUnivP();
  408. inline T* operator= (T*);
  409. inline T* operator->(void);
  410. private:
  411. BOOL m_fTaskAlloc;
  412. private: // Never-to-use
  413. inline CAutoUnivP& operator= (CAutoUnivP&);
  414. CAutoUnivP(CAutoUnivP&);
  415. };
  416. //--------------------------------------------------------------------
  417. // @mfunc Constructor
  418. // @side None
  419. // @rdesc None
  420. template <class T>
  421. inline CAutoUnivP<T>::CAutoUnivP(T *pt, BOOL fIsTaskAlloc)
  422. : CAutoBase<T>(pt)
  423. {
  424. m_fTaskAlloc = fIsTaskAlloc;
  425. }
  426. //--------------------------------------------------------------------
  427. // @mfunc Delete the object pointed by CAutoUnivP variable if any.
  428. // @side None
  429. // @rdesc None
  430. //
  431. template <class T>
  432. inline CAutoUnivP<T>::~CAutoUnivP()
  433. {
  434. if (m_fTaskAlloc)
  435. {
  436. // m_pt->~T(); // Awaits VC++ 3.0...
  437. g_pIMalloc->Free(m_pt);
  438. }
  439. else
  440. delete m_pt;
  441. }
  442. //--------------------------------------------------------------------
  443. // @mfunc Assigns to variable after construction. May be dangerous
  444. // so it assert's that the variable is NULL.
  445. // Assign operator is not inherited, so it has to be written
  446. // again. Just calls base class assignment.
  447. // @side None
  448. // @rdesc None
  449. //
  450. template <class T>
  451. inline T* CAutoUnivP<T>::operator=(T* pt)
  452. {
  453. return ((CAutoBase<T> *) this)->operator= (pt);
  454. }
  455. //--------------------------------------------------------------------
  456. // @mfunc The 'follow' operator on the CAutoUnivP allows an CAutoUnivP variable
  457. // to act like a pointer of type T. This overloading generally makes using
  458. // a CAutoUnivP simple as using a regular T pointer.
  459. //
  460. // @side None
  461. // @rdesc None
  462. template <class T>
  463. inline T * CAutoUnivP<T>::operator->()
  464. {
  465. _ASSERT(m_pt != NULL);
  466. return m_pt;
  467. }
  468. //------------------------------------------------------------------
  469. // @class auto handle class
  470. //
  471. class CAutoHandle
  472. {
  473. public:
  474. // @cmember constructor
  475. inline CAutoHandle(HANDLE h) : m_handle(h)
  476. {
  477. }
  478. inline CAutoHandle() :
  479. m_handle(INVALID_HANDLE_VALUE)
  480. {
  481. }
  482. // @cmember destructor
  483. inline ~CAutoHandle()
  484. {
  485. if (m_handle != INVALID_HANDLE_VALUE)
  486. CloseHandle(m_handle);
  487. }
  488. // coercion to handle value
  489. inline operator HANDLE (void)
  490. {
  491. return m_handle;
  492. }
  493. inline HANDLE PvReturn(void)
  494. {
  495. HANDLE h = m_handle;
  496. m_handle = INVALID_HANDLE_VALUE;
  497. return h;
  498. }
  499. private:
  500. // @cmember handle value
  501. HANDLE m_handle;
  502. };
  503. //----------------------------------------------------------------------
  504. // @class auto class for registry keys
  505. //
  506. class CAutoHKEY
  507. {
  508. public:
  509. // @cmember constructor
  510. inline CAutoHKEY(HKEY hkey) : m_hkey(hkey)
  511. {
  512. }
  513. // @cmember destructor
  514. inline ~CAutoHKEY()
  515. {
  516. if (m_hkey != NULL)
  517. RegCloseKey(m_hkey);
  518. }
  519. inline operator HKEY(void)
  520. {
  521. return m_hkey;
  522. }
  523. inline HKEY PvReturn(void)
  524. {
  525. HKEY hkey = m_hkey;
  526. m_hkey = NULL;
  527. return hkey;
  528. }
  529. private:
  530. HKEY m_hkey;
  531. };
  532. //------------------------------------------------------------------
  533. // @class automatically unmap view of file on function exit
  534. //
  535. class CAutoUnmapViewOfFile
  536. {
  537. public:
  538. // @cmember constructor
  539. inline CAutoUnmapViewOfFile(PVOID pv) : m_pv(pv)
  540. {
  541. }
  542. // @cmember destructor
  543. inline ~CAutoUnmapViewOfFile()
  544. {
  545. if (m_pv != NULL)
  546. UnmapViewOfFile(m_pv);
  547. }
  548. // @cmember indicate that region should not be unmapped by destructor
  549. inline PVOID PvReturn()
  550. {
  551. PVOID pv = m_pv;
  552. m_pv = NULL;
  553. return pv;
  554. }
  555. private:
  556. // @cmember handle value
  557. PVOID m_pv;
  558. };