home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / sybase / starbuck / hpp.z / WBUFFER.HPP < prev    next >
C/C++ Source or Header  |  1996-10-18  |  6KB  |  246 lines

  1. /*************************************************************************
  2.  *
  3.  * WBuffer
  4.  *
  5.  *    This class encapsulates operations on a buffer of arbitrary data.
  6.  *
  7.  *************************************************************************/
  8.  
  9. #ifndef _WBUFFER_HPP_INCLUDED
  10. #define _WBUFFER_HPP_INCLUDED
  11.  
  12. #ifndef _WNO_PRAGMA_PUSH
  13. #pragma pack(push,8);
  14. #pragma enum int;
  15. #endif
  16.  
  17. #ifndef _WOBJECT_HPP_INCLUDED
  18. #  include "wobject.hpp"
  19. #endif
  20.  
  21. extern "C" {
  22.     #include <ctype.h>
  23.     #include <string.h>
  24.     #include <time.h>
  25. };
  26.  
  27. class WResourceID;
  28. class WModule;   
  29. class WBufferReference;
  30. class WBuffer;
  31. class WString;
  32.  
  33. //
  34. // WBuffer
  35. //
  36.  
  37. class WCMCLASS WBuffer : public WObject {
  38.     WDeclareSubclass( WBuffer, WObject )
  39.  
  40.     public:
  41.  
  42.         //
  43.         // WBufferElement
  44.         //
  45.         //    This class represents an individual byte in a buffer.
  46.         //
  47.  
  48.         class WBufferElement {
  49.  
  50.             friend class WBuffer;
  51.  
  52.             public:
  53.  
  54.                 WBufferElement& operator=( const WByte ch )
  55.                     { buffer.SetByte( index, ch ); return *this; }
  56.  
  57.                 WBufferElement& operator=( const WBufferElement & e )
  58.                     { buffer.SetByte( index,
  59.                                       e.buffer.GetByte( e.index ) );
  60.                       return *this; }
  61.  
  62.                 operator WByte() const
  63.                     { return buffer.GetByte( index ); }
  64.  
  65.             private:
  66.                 WBufferElement( WBuffer *buf ) : buffer( *buf ), index( 0 ) {}
  67.  
  68.                 WBufferElement( const WBufferElement & e );
  69.  
  70.                 ~WBufferElement() {}
  71.  
  72.             private:
  73.                 WBuffer& buffer;
  74.                 WULong   index;
  75.         };
  76.  
  77.         /**********************************************************
  78.          * Constructors and Destructors
  79.          *********************************************************/
  80.  
  81.         WBuffer();
  82.         WBuffer( WULong size, void * buffer, WBool makeCopy=TRUE,
  83.                  WBool selfDeleteData=FALSE );
  84.         WBuffer( const WBuffer & b, WBool makeCopy=FALSE );
  85.         WBuffer( const WString & string, WBool includeNull=TRUE );
  86.  
  87.         ~WBuffer();
  88.  
  89.         /*********************************************************
  90.          * Operators
  91.          *********************************************************/
  92.  
  93.         //
  94.         // [] operator
  95.         //
  96.  
  97.         const WBufferElement& operator[]( int index ) const;
  98.  
  99.         WBufferElement& operator[]( int index );
  100.  
  101.         //
  102.         // casting operators
  103.         //
  104.  
  105.         operator const WByte *() const { return GetBuffer(); }
  106.  
  107.         //
  108.         // = operator
  109.         //
  110.  
  111.         WBuffer & operator=( const WBuffer & s )
  112.             { Create( s ); return *this; }
  113.  
  114.         //
  115.         // == operator
  116.         //
  117.  
  118.         friend int WEXPORT operator==( const WBuffer & a, const WBuffer & b );
  119.  
  120.         //
  121.         // != operator
  122.         //
  123.  
  124.         friend int WEXPORT operator!=( const WBuffer & a, const WBuffer & b );
  125.  
  126.         //
  127.         // += operator
  128.         //
  129.  
  130.         WBuffer& operator+=( const WBuffer & a );
  131.  
  132.         //
  133.         // + operator
  134.         //
  135.  
  136.         WBuffer operator+( const WBuffer & a ) const;
  137.  
  138.         /**********************************************************
  139.          * Properties
  140.          *********************************************************/
  141.  
  142.         // Buffer
  143.         //
  144.         //    Return a pointer to the actual buffer.  Does not
  145.         //    lock the buffer -- if you want to make changes,
  146.         //    use Lock instead.
  147.  
  148.         const WByte *GetBuffer() const;
  149.  
  150.         // Byte
  151.         //
  152.         //    Set/get an individual byte in the buffer.
  153.  
  154.         WByte GetByte( WULong index );
  155.         WBool SetByte( WULong index, WByte ch );
  156.  
  157.         // Null
  158.         //
  159.         //    True if it is a null buffer.
  160.  
  161.         WBool GetNull() const;
  162.  
  163.         // Size
  164.         //
  165.         //    Returns the size of the buffer in bytes.
  166.  
  167.         WULong GetSize() const;
  168.  
  169.         /**********************************************************
  170.          * Methods
  171.          *********************************************************/
  172.  
  173.         // Clear
  174.         //
  175.         //    Frees the buffer.
  176.  
  177.         void Clear();
  178.     
  179.         // Concat
  180.         //
  181.         //    Concatenate a buffer onto another.
  182.  
  183.         WBool Concat( const WBuffer & suffix );
  184.  
  185.         // Create
  186.         //
  187.         //    Various constructors for building a new buffer.
  188.         //    First frees the old buffer.
  189.  
  190.         WBool Create();
  191.         WBool Create( WULong size, void * buffer=NULL, WBool makeCopy=TRUE,
  192.                       WBool selfDeleteData=FALSE );
  193.         WBool Create( const WBuffer & s, WBool makeCopy=FALSE );
  194.         WBool Create( const WString & str, WBool includeNull=TRUE );
  195.  
  196.         // Fill
  197.         //
  198.         //    Fill the buffer with a given byte.
  199.  
  200.         WBool Fill( const WByte byte );
  201.  
  202.         // Lock
  203.         //
  204.         //    Call this if you want to work directly on the buffer
  205.         //    inside the WBuffer.  It will ensure that no one else
  206.         //    is referencing it, that it has a minimum size (in bytes),
  207.         //    and return its pointer.  Call Unlock when you're done.
  208.  
  209.         WByte *Lock( WULong minimumSize=0 );
  210.  
  211.         // Unlock
  212.         //
  213.         //    Call this when done writing directly to the buffer.
  214.  
  215.         WBool Unlock();
  216.  
  217.         /**********************************************************
  218.          * Static Properties
  219.          *********************************************************/
  220.  
  221.         static const WBuffer & GetNullBuffer();
  222.  
  223.         /**********************************************************
  224.          * Private
  225.          *********************************************************/
  226.  
  227.     protected:
  228.  
  229.         WBool  ReallocateRef( WULong minimumSize );
  230.         WBool  GrowTo( WULong size );
  231.  
  232.     protected:
  233.  
  234.         WBufferReference *_ref;
  235.         const WByte      *_bufferData;  // for easier debugging only!
  236.         WBufferElement    _element;
  237. };
  238.     
  239.  
  240. #ifndef _WNO_PRAGMA_PUSH
  241. #pragma enum pop;
  242. #pragma pack(pop);
  243. #endif
  244.  
  245. #endif // _WBUFFER_HPP_INCLUDED
  246.