home *** CD-ROM | disk | FTP | other *** search
/ Quake 'em / QUAKEEM.BIN / doom_i / program / reject10.exe / SOURCE.ZIP / MEMHANDL.HPP < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-26  |  4.5 KB  |  151 lines

  1. #ifndef __INC_MEMHANDL_HPP__  
  2. #define __INC_MEMHANDL_HPP__  
  3.  
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. /*********************************************************************************
  8. **
  9. **  CLASS: MemoryBlock.
  10. **
  11. **     This is a private class internally by the MemHandle class. It is this
  12. **     class which has custody of a block of memory. The constructor allocates
  13. **     a block of memory of the specified size. When this object is destroyed
  14. **     block of memory it is responsible for is also deleted.
  15. **
  16. *********************************************************************************/
  17.  
  18. class MemoryBlock
  19. {
  20.      friend class MemHandle;                 // only MemHandle objects can use this class
  21.      private:
  22.           MemoryBlock (size_t size);         // CTOR
  23.           ~MemoryBlock ();                   // DTOR
  24.           unsigned char *buffer;             // pointer to the block of memory. 
  25.           size_t szbuffer;                   // the size of the block of memory.
  26.           int count;                         // the number of times this block is
  27.                                              // referenced by MemHandle Objects
  28. };
  29.  
  30.  
  31.  
  32. /*********************************************************************************
  33. **
  34. **  CLASS: MemHandle
  35. **
  36. **
  37. **
  38. **
  39. **
  40. **
  41. **
  42. *********************************************************************************/
  43.  
  44. class MemHandle
  45. {
  46.      private:
  47.           MemoryBlock *mem; 
  48.  
  49.      public:
  50.           MemHandle (const MemHandle &mh);
  51.           MemHandle (size_t size);
  52.           MemHandle (size_t size, unsigned char filler);
  53.           ~MemHandle ();
  54.  
  55.           const char* Buffer() const;
  56.           MemHandle Clone ()  const;
  57.           size_t Size ()      const;
  58.           int Valid ()        const;
  59.           int RefCount ()     const;
  60.           void MemSet (int c);
  61.           operator char * ();
  62.           MemHandle& operator= (const MemHandle& m);
  63.           void Shrink (size_t);
  64. };
  65.  
  66. //*********************************************************************************
  67. //**      INLINE FUNCTIONS
  68. //*********************************************************************************
  69.  
  70. inline const char* MemHandle::Buffer() const
  71. {
  72.      return (char*)(mem->buffer);
  73. }
  74.  
  75.  
  76. /*********************************************************************************
  77. **
  78. **  FUNCTION: MemHandle::Size () const  
  79. **
  80. **  RETURNS:  the size (in bytes) of the memory block held by the MemHandle object
  81. **
  82. *********************************************************************************/
  83.  
  84. inline size_t MemHandle::Size () const
  85. {
  86.      return (mem->szbuffer);
  87. }
  88.  
  89. /*********************************************************************************
  90. **
  91. **  FUNCTION: MemHandle::Size () const  
  92. **
  93. **  RETURNS:  the size (in bytes) of the memory block held by the MemHandle object
  94. **
  95. *********************************************************************************/
  96.  
  97. inline int MemHandle::RefCount () const
  98. {
  99.      return (mem->count);
  100. }
  101.  
  102. /*********************************************************************************
  103. **
  104. **  FUNCTION: MemHandle::Valid () const
  105. **
  106. **            Tests the validity of a MemHandle object
  107. **            You _should_ test any MemHandle object before 
  108. **            you use it. (except for anonomous temporary objects 
  109. **            which you cant test. ho hum)
  110. **
  111. **  RETURNS: TRUE if the object is valid else FALSE
  112. **
  113. *********************************************************************************/
  114.  
  115. inline int MemHandle::Valid () const
  116. {
  117.      return mem && (mem->buffer);
  118. }
  119.  
  120. /*********************************************************************************
  121. **
  122. **  FUNCTIONS: operator MemHandle::char * () 
  123. **
  124. **            Conversion operator to convert a MemHandle object to a pointer
  125. **
  126. **  RETURNS:  char * to the memory block held by the MemHandle object
  127. **
  128. *********************************************************************************/
  129.  
  130. inline MemHandle::operator char * ()
  131. {
  132.      return (char*)(mem->buffer);
  133. }
  134.                              
  135.  
  136. /*********************************************************************************
  137. **
  138. **  FUNCTION: MemHandle::MemSet (int c)   
  139. **
  140. **   sets the whole buffer to the value c.
  141. **
  142. *********************************************************************************/
  143.                              
  144. inline void MemHandle::MemSet (int c)
  145. {
  146.      memset (mem->buffer, c, mem->szbuffer);
  147. }
  148.  
  149.  
  150. #endif                  
  151.