home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / Prog / B-C / C++Source Code Fmtr Folder / Src / DataArea.h < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-27  |  6.3 KB  |  279 lines  |  [TEXT/MPS ]

  1. #ifndef __DATAAREA__
  2. #define __DATAAREA__    1
  3.  
  4. #pragma once
  5.  
  6. #ifndef __HANDLEMANAGER__
  7. #include "HandleManager.h"
  8. #endif
  9.  
  10. #ifndef __MEMORY__
  11. #include <memory.h>
  12. #endif
  13.  
  14. #ifndef __STDDEF__
  15. #include <stddef.h>
  16. #endif
  17.  
  18.  
  19. // Assure outlined inlines go in the correct segment
  20. #pragma segment DataArea
  21.  
  22.  
  23. /*µ class DataArea
  24. **    This class provides in-memory data storage.  Data can be written to and
  25. ** read from a DataArea.  The data area expands as required to contain the
  26. ** data written to it.  DataAreas must be explicitly initialized as the
  27. ** initialization can fail.
  28. **    The DataArea is built on top of handles.  The handles are usually allocated
  29. ** from the application's heap.  However, the handle can be allocated from
  30. ** the MultiFinder heap.  If the MultiFinder heap is used, the application
  31. ** should be running under System 7.0 as the temporary memory calls it makes
  32. ** are only valid (or work correctly) under System 7.0
  33. */
  34.  
  35.  
  36. class DataArea : public SingleObject {
  37. public:
  38.     DataArea();
  39.     virtual ~DataArea();
  40.  
  41.     void UseHeap(HandleManagerHeap whichHeap);
  42.     void UseHeap(HandleManager *aHandleManager);
  43.     /*
  44.     ** Indicate which heap to use for memory allocation.  The default is
  45.     ** to use the application heap
  46.     */
  47.  
  48.     OSErr IDataArea(const DataArea *anArea);
  49.     OSErr IDataArea(size_t initialSize = 0, size_t increment = 0, Boolean useSize = false);
  50.     /*
  51.     ** Initialize the data area.  Returns noErr if successful, otherwise
  52.     ** the error code.  Must be called prior to use.  Not done in
  53.     ** constructor because initialization can fail.  useSize is true if
  54.     ** the DataArea's size should be set to "initialSize" instead of the
  55.     ** default of 0.
  56.     */
  57.  
  58.     void SetIncrement(size_t anIncrement);
  59.     void **GetHandle() const;
  60.     size_t GetSize() const;
  61.     size_t GetCursor() const;
  62.     size_t GetIndex(size_t aSize) const;
  63.     void *GetData() const;
  64.     void *GetData(size_t i) const;
  65.     void *GetData(size_t i, size_t aSize) const;
  66.     // Accessor functions.  GetData() yields a volatile pointer to
  67.     // the current position in the data, to the index'th position in
  68.     // the data, or a particular fixed-size element in the data.
  69.  
  70.     virtual OSErr Assign(const DataArea *anArea);
  71.     /*
  72.     ** Set this DataArea from the contents of the other data area.
  73.     */
  74.  
  75.     size_t Read(const void *aPtr, size_t aLen);
  76.     /*
  77.     ** Copy "aLen" byte of data from the current cursor position into
  78.     ** "aPtr", returning the number of bytes read.
  79.     */
  80.  
  81.     size_t Write(void **aHndl);
  82.     size_t Write(const void *aPtr, size_t aLen);
  83.     /*
  84.     ** Write data starting at the current cursor position.  Expand the
  85.     ** data area if required.  Return the number of bytes written.  If
  86.     ** 0, then an error occurred, check MemError()
  87.     */
  88.  
  89.     size_t IncrCursor(size_t aDelta);
  90.     size_t DecrCursor(size_t aDelta);
  91.     size_t SetCursor(size_t aCursor);
  92.     /*
  93.     ** Set the read/write cursor to the location specified.  Return the
  94.     ** new location.
  95.     */
  96.  
  97.     size_t Require(size_t amtReqd);
  98.     /*
  99.     ** Assure that there is at least amtReqd bytes of data available
  100.     ** at fCursor.  Returns the number of bytes available.  Useful for
  101.     ** allocating space.
  102.     */
  103.  
  104.     void HLock();
  105.     void HUnlock();
  106.     void MoveHHi();
  107.     // Handle specific operations
  108.  
  109.     void DeleteFromStart();
  110.     void DeleteFromStart(size_t start);
  111.     void DeleteToEnd();
  112.     void DeleteToEnd(size_t start);
  113.     void Delete(size_t start, size_t end);
  114.     /*
  115.     ** Remove a chunk of data from the DataArea.  Space is not necessarily
  116.     ** reclaimed.  The data from start to end-1 is removed, fCursor is
  117.     ** updated to the new position.
  118.     */
  119.  
  120.     void Truncate();
  121.     /*
  122.     ** Shrink the DataArea to the cursor position.
  123.     */
  124.  
  125.     size_t AlignCursor(size_t alignment);
  126.     /*
  127.     ** Assure that the cursor is on a multiple of "alignment".  Useful
  128.     ** for assuring word and long word alignment of data.  Will advance
  129.     ** the cursor and possibly increase the size of the data area to
  130.     ** enforce the alignment criterion.  It returns the new cursor
  131.     ** position.  If the cursor could not be advanced, it returns 0.
  132.     */
  133.  
  134.  
  135. private:
  136.     size_t _Write(const void *aPtr, size_t aLen);
  137.     /*
  138.     ** Private version.  Called by Write() above after determining that
  139.     ** there is enough space available for the write to succeed
  140.     */
  141.  
  142.  
  143. protected:
  144.     void **fHandle;                                // The handle containing the data
  145.  
  146.  
  147. private:
  148.     size_t fCursor;                                // The current read/write location
  149.     size_t fSize;                                // The current size of the handle
  150.     size_t fIncrement;                            // How much to increment when expanding
  151.     HandleManager *fHandleProc;                    // How to do handle operations
  152. };
  153.  
  154.  
  155.  
  156. //µ   DataArea::UseHeap
  157. inline void DataArea::UseHeap(HandleManager *aHandleManager)
  158. {
  159.     fHandleProc = aHandleManager;
  160. }
  161.  
  162.  
  163. //µ   DataArea::UseHeap
  164. #pragma segment DataArea
  165. inline void DataArea::UseHeap(HandleManagerHeap whichHeap)
  166. {
  167.     UseHeap(HandleManager::GetManager(whichHeap));
  168. }
  169.  
  170.  
  171. //µ   DataArea::SetIncrement
  172. #pragma segment DataArea
  173. inline void DataArea::SetIncrement(size_t anIncrement)
  174. {
  175.     fIncrement = anIncrement;
  176. }
  177.  
  178.  
  179. //µ   DataArea::GetHandle
  180. #pragma segment DataArea
  181. inline void **DataArea::GetHandle() const
  182. {
  183.     return (fHandle);
  184. }
  185.  
  186.  
  187. //µ   DataArea::GetSize
  188. #pragma segment DataArea
  189. inline size_t DataArea::GetSize() const
  190. {
  191.     return (fSize);
  192. }
  193.  
  194.  
  195. //µ   DataArea::GetCursor
  196. #pragma segment DataArea
  197. inline size_t DataArea::GetCursor() const
  198. {
  199.     return (fCursor);
  200. }
  201.  
  202.  
  203. //µ   DataArea::GetIndex
  204. #pragma segment DataArea
  205. inline size_t DataArea::GetIndex(size_t aSize) const
  206. {
  207.     return (fCursor / aSize);
  208. }
  209.  
  210.  
  211. //µ   DataArea::GetData
  212. #pragma segment DataArea
  213. inline void *DataArea::GetData() const
  214. {
  215.     return ((char*)*fHandle + fCursor);
  216. }
  217.  
  218.  
  219. //µ   DataArea::GetData
  220. #pragma segment DataArea
  221. inline void *DataArea::GetData(size_t i) const
  222. {
  223.     return ((char*)*fHandle + i);
  224. }
  225.  
  226.  
  227. //µ   DataArea::GetData
  228. #pragma segment DataArea
  229. inline void *DataArea::GetData(size_t i, size_t aSize) const
  230. {
  231.     return ((char*)*fHandle + i * aSize);
  232. }
  233.  
  234.  
  235. //µ   DataArea::IncrCursor
  236. #pragma segment DataArea
  237. inline size_t DataArea::IncrCursor(size_t aDelta)
  238. {
  239.     return (SetCursor(fCursor + aDelta));
  240. }
  241.  
  242.  
  243. //µ   DataArea::DeleteFromStart
  244. #pragma segment DataArea
  245. inline void DataArea::DeleteFromStart()
  246. {
  247.     Delete(0, fCursor);
  248. }
  249.  
  250.  
  251. //µ   DataArea::DeleteFromStart
  252. #pragma segment DataArea
  253. inline void DataArea::DeleteFromStart(size_t start)
  254. {
  255.     Delete(0, start);
  256. }
  257.  
  258.  
  259. //µ   DataArea::DeleteToEnd
  260. #pragma segment DataArea
  261. inline void DataArea::DeleteToEnd()
  262. {
  263.     fSize = fCursor;
  264. }
  265.  
  266.  
  267. //µ   DataArea::DeleteToEnd
  268. #pragma segment DataArea
  269. inline void DataArea::DeleteToEnd(size_t start)
  270. {
  271.     Delete(start, fSize);
  272. }
  273.  
  274.  
  275.  
  276. #endif
  277.  
  278.  
  279.