home *** CD-ROM | disk | FTP | other *** search
- #ifndef __DATAAREA__
- #define __DATAAREA__ 1
-
- #pragma once
-
- #ifndef __HANDLEMANAGER__
- #include "HandleManager.h"
- #endif
-
- #ifndef __MEMORY__
- #include <memory.h>
- #endif
-
- #ifndef __STDDEF__
- #include <stddef.h>
- #endif
-
-
- // Assure outlined inlines go in the correct segment
- #pragma segment DataArea
-
-
- /*µ class DataArea
- ** This class provides in-memory data storage. Data can be written to and
- ** read from a DataArea. The data area expands as required to contain the
- ** data written to it. DataAreas must be explicitly initialized as the
- ** initialization can fail.
- ** The DataArea is built on top of handles. The handles are usually allocated
- ** from the application's heap. However, the handle can be allocated from
- ** the MultiFinder heap. If the MultiFinder heap is used, the application
- ** should be running under System 7.0 as the temporary memory calls it makes
- ** are only valid (or work correctly) under System 7.0
- */
-
-
- class DataArea : public SingleObject {
- public:
- DataArea();
- virtual ~DataArea();
-
- void UseHeap(HandleManagerHeap whichHeap);
- void UseHeap(HandleManager *aHandleManager);
- /*
- ** Indicate which heap to use for memory allocation. The default is
- ** to use the application heap
- */
-
- OSErr IDataArea(const DataArea *anArea);
- OSErr IDataArea(size_t initialSize = 0, size_t increment = 0, Boolean useSize = false);
- /*
- ** Initialize the data area. Returns noErr if successful, otherwise
- ** the error code. Must be called prior to use. Not done in
- ** constructor because initialization can fail. useSize is true if
- ** the DataArea's size should be set to "initialSize" instead of the
- ** default of 0.
- */
-
- void SetIncrement(size_t anIncrement);
- void **GetHandle() const;
- size_t GetSize() const;
- size_t GetCursor() const;
- size_t GetIndex(size_t aSize) const;
- void *GetData() const;
- void *GetData(size_t i) const;
- void *GetData(size_t i, size_t aSize) const;
- // Accessor functions. GetData() yields a volatile pointer to
- // the current position in the data, to the index'th position in
- // the data, or a particular fixed-size element in the data.
-
- virtual OSErr Assign(const DataArea *anArea);
- /*
- ** Set this DataArea from the contents of the other data area.
- */
-
- size_t Read(const void *aPtr, size_t aLen);
- /*
- ** Copy "aLen" byte of data from the current cursor position into
- ** "aPtr", returning the number of bytes read.
- */
-
- size_t Write(void **aHndl);
- size_t Write(const void *aPtr, size_t aLen);
- /*
- ** Write data starting at the current cursor position. Expand the
- ** data area if required. Return the number of bytes written. If
- ** 0, then an error occurred, check MemError()
- */
-
- size_t IncrCursor(size_t aDelta);
- size_t DecrCursor(size_t aDelta);
- size_t SetCursor(size_t aCursor);
- /*
- ** Set the read/write cursor to the location specified. Return the
- ** new location.
- */
-
- size_t Require(size_t amtReqd);
- /*
- ** Assure that there is at least amtReqd bytes of data available
- ** at fCursor. Returns the number of bytes available. Useful for
- ** allocating space.
- */
-
- void HLock();
- void HUnlock();
- void MoveHHi();
- // Handle specific operations
-
- void DeleteFromStart();
- void DeleteFromStart(size_t start);
- void DeleteToEnd();
- void DeleteToEnd(size_t start);
- void Delete(size_t start, size_t end);
- /*
- ** Remove a chunk of data from the DataArea. Space is not necessarily
- ** reclaimed. The data from start to end-1 is removed, fCursor is
- ** updated to the new position.
- */
-
- void Truncate();
- /*
- ** Shrink the DataArea to the cursor position.
- */
-
- size_t AlignCursor(size_t alignment);
- /*
- ** Assure that the cursor is on a multiple of "alignment". Useful
- ** for assuring word and long word alignment of data. Will advance
- ** the cursor and possibly increase the size of the data area to
- ** enforce the alignment criterion. It returns the new cursor
- ** position. If the cursor could not be advanced, it returns 0.
- */
-
-
- private:
- size_t _Write(const void *aPtr, size_t aLen);
- /*
- ** Private version. Called by Write() above after determining that
- ** there is enough space available for the write to succeed
- */
-
-
- protected:
- void **fHandle; // The handle containing the data
-
-
- private:
- size_t fCursor; // The current read/write location
- size_t fSize; // The current size of the handle
- size_t fIncrement; // How much to increment when expanding
- HandleManager *fHandleProc; // How to do handle operations
- };
-
-
-
- //µ DataArea::UseHeap
- inline void DataArea::UseHeap(HandleManager *aHandleManager)
- {
- fHandleProc = aHandleManager;
- }
-
-
- //µ DataArea::UseHeap
- #pragma segment DataArea
- inline void DataArea::UseHeap(HandleManagerHeap whichHeap)
- {
- UseHeap(HandleManager::GetManager(whichHeap));
- }
-
-
- //µ DataArea::SetIncrement
- #pragma segment DataArea
- inline void DataArea::SetIncrement(size_t anIncrement)
- {
- fIncrement = anIncrement;
- }
-
-
- //µ DataArea::GetHandle
- #pragma segment DataArea
- inline void **DataArea::GetHandle() const
- {
- return (fHandle);
- }
-
-
- //µ DataArea::GetSize
- #pragma segment DataArea
- inline size_t DataArea::GetSize() const
- {
- return (fSize);
- }
-
-
- //µ DataArea::GetCursor
- #pragma segment DataArea
- inline size_t DataArea::GetCursor() const
- {
- return (fCursor);
- }
-
-
- //µ DataArea::GetIndex
- #pragma segment DataArea
- inline size_t DataArea::GetIndex(size_t aSize) const
- {
- return (fCursor / aSize);
- }
-
-
- //µ DataArea::GetData
- #pragma segment DataArea
- inline void *DataArea::GetData() const
- {
- return ((char*)*fHandle + fCursor);
- }
-
-
- //µ DataArea::GetData
- #pragma segment DataArea
- inline void *DataArea::GetData(size_t i) const
- {
- return ((char*)*fHandle + i);
- }
-
-
- //µ DataArea::GetData
- #pragma segment DataArea
- inline void *DataArea::GetData(size_t i, size_t aSize) const
- {
- return ((char*)*fHandle + i * aSize);
- }
-
-
- //µ DataArea::IncrCursor
- #pragma segment DataArea
- inline size_t DataArea::IncrCursor(size_t aDelta)
- {
- return (SetCursor(fCursor + aDelta));
- }
-
-
- //µ DataArea::DeleteFromStart
- #pragma segment DataArea
- inline void DataArea::DeleteFromStart()
- {
- Delete(0, fCursor);
- }
-
-
- //µ DataArea::DeleteFromStart
- #pragma segment DataArea
- inline void DataArea::DeleteFromStart(size_t start)
- {
- Delete(0, start);
- }
-
-
- //µ DataArea::DeleteToEnd
- #pragma segment DataArea
- inline void DataArea::DeleteToEnd()
- {
- fSize = fCursor;
- }
-
-
- //µ DataArea::DeleteToEnd
- #pragma segment DataArea
- inline void DataArea::DeleteToEnd(size_t start)
- {
- Delete(start, fSize);
- }
-
-
-
- #endif
-
-
-