home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / include / irecord.hpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-22  |  14.4 KB  |  267 lines

  1. #ifndef _IRecord_
  2. #define _IRecord_
  3. /*******************************************************************************
  4. * FILE NAME: irecord.hpp                                                       *
  5. *                                                                              *
  6. * DESCRIPTION:                                                                 *
  7. *   Declaration of the class(es):                                              *
  8. *     IRecord - Base class for all record classes.                             *
  9. *                                                                              *
  10. * COPYRIGHT:                                                                   *
  11. *   IBM(R) VisualAge(TM) for C++                                               *
  12. *   (C) Copyright International Business Machines Corporation 1991, 1996       *
  13. *   Licensed Material - Program-Property of IBM - All Rights Reserved.         *
  14. *   US Government Users Restricted Rights - Use, duplication, or disclosure    *
  15. *   restricted by GSA ADP Schedule Contract with IBM Corp.                     *
  16. *                                                                              *
  17. *   This program will not run in DOS mode.                                     *
  18. *                                                                              *
  19. * DISCLAIMER OF WARRANTIES:                                                    *
  20. *   The following [enclosed] code is sample code created by IBM                *
  21. *   Corporation.  This sample code is not part of any standard IBM product     *
  22. *   and is provided to you solely for the purpose of assisting you in the      *
  23. *   development of your applications.  The code is provided "AS IS",           *
  24. *   without warranty of any kind.  IBM shall not be liable for any damages     *
  25. *   arising out of your use of the sample code, even if they have been         *
  26. *   advised of the possibility of such damages.                                *
  27. *******************************************************************************/
  28. #ifndef _IVBASE_
  29.   #include <ivbase.hpp>
  30. #endif
  31.  
  32. #ifndef _ISTRING_
  33.   #include <istring.hpp>
  34. #endif
  35.  
  36. /*-------------------------- Pragma Library Support --------------------------*/
  37. #ifndef __NO_DEFAULT_LIBS__
  38.   #ifdef __OS2__
  39.     #ifdef __IMPORTLIB__
  40.        #pragma library("CPPOV03I.LIB")
  41.     #else
  42.        #pragma library("CPPOV03.LIB")
  43.     #endif
  44.   #endif
  45.   #ifdef __WINDOWS__
  46.     #ifdef __IMPORTLIB__
  47.        #pragma library("CPPWV03I.LIB")
  48.     #else
  49.        #pragma library("CPPWV03.LIB")
  50.     #endif
  51.   #endif
  52. #endif
  53.  
  54. // Align classes on four byte boundary.
  55. #pragma pack(4)
  56.  
  57. class IRecord : public IVBase {
  58. /*******************************************************************************
  59. *
  60. * This class is the base class for all record classes.  A record class is
  61. * generally used to wrapper a data structure required for a non-OO legacy
  62. * system.  The record may be share between the OO and the legacy systems
  63. * via communications, files, or any other medium of data exchange or sharing.
  64. *
  65. * This class also contains an IString that provides the storage for the
  66. * data in the record.  The data muat always be maintained in this IString
  67. * in the structure expected by the legacy system.  This way, with the
  68. * exception of a data conversion step, it is always ready for exchange
  69. * or sharing.
  70. *
  71. * The subclasses of IRecord should provide accessor functions for the
  72. * fields they wish to expose to the OO code.  The IRecord class provides
  73. * protected helper functions to aid in the writing of these functions.
  74. * When using the member functions, remember that the first byte in the
  75. * record is offset 1 (just like IString).
  76. *
  77. *******************************************************************************/
  78.  
  79. public:
  80. /*------------------------- Constructors/Destructor ----------------------------
  81. | You can contruct an instance of this class as follows:                       |
  82. |   - Constructs an empty record of size 0.                                    |
  83. |   - Constructs a record containing the 'recordData'.  The record size is     |
  84. |     determined from the size of the IString.                                 |
  85. |   - Copies the record data from the argument record.                         |
  86. |-----------------------------------------------------------------------------*/
  87. IRecord ( );
  88.  
  89. IRecord ( const IString & recordData );
  90.  
  91. IRecord ( const IRecord & aRecord );
  92.  
  93. virtual ~IRecord ( );
  94.  
  95. /*--------------------------------- Testing ------------------------------------
  96. | The following functions test for record properties:                          |
  97. |   isOrdered - Returns false.                                                 |
  98. ------------------------------------------------------------------------------*/
  99. virtual IBoolean isOrdered ( ) const;
  100.  
  101. /*------------------------------- Conversion -----------------------------------
  102. | The following functions permit the conversion of a record to various other   |
  103. | data types.                                                                  |
  104. |   asString - Returns the the IRecord contents as an IString.  A conversion   |
  105. |              operator to IString is not provided to avoid ambiguity with the |
  106. |              IString comparison operators (see IOrderedRecord).  Whenever an |
  107. |              IOrderedRecord is involved in a comparison with an IString, we  |
  108. |              want to make sure the IOrderedRecord operator is used.          |
  109. ------------------------------------------------------------------------------*/
  110. IString asString ( ) const;
  111.  
  112. /*--------------------------- Assignment Operator ------------------------------
  113. | The following operators allow the record's contents to be assigned from      |
  114. | another record or an IString.                                                |
  115. |   operator = - Replaces the contents of the record.  The target IRecord      |
  116. |                size will be the source IRecord or IString size.              |
  117. |-----------------------------------------------------------------------------*/
  118. IRecord & operator = ( const IRecord & aRecord );
  119. IRecord & operator = ( const IString & aRecord );
  120.  
  121. /*-------------------------------- Accessors -----------------------------------
  122. | size   - returns the size of the record in bytes.                            |
  123. |-----------------------------------------------------------------------------*/
  124. unsigned long size ( ) const;
  125.  
  126. protected:
  127. /*--------------------------- Protected Accessors ------------------------------
  128. | setSize - Subclass default constructors, and constructors and assignment     |
  129. |           operators that take an IString, must use this function in their    |
  130. |           function body.  The default constructors must then set the fields  |
  131. |           to their default values.  This function will pad the record with   |
  132. |           '\x00' or truncate the record to the specified size.  A subclass   |
  133. |           must only add to the size of its superclass.  It must never try to |
  134. |           remove fields.  Using this technique will improve performance as   |
  135. |           the fields are subsequently filled and ensure that size() always   |
  136. |           returns the correct value.  The set size is returned.              |
  137. |-----------------------------------------------------------------------------*/
  138. unsigned long setSize ( unsigned long size );
  139.  
  140. /*------------------------ Accessor Helper Functions ---------------------------
  141. | field    - There are four flavors of this function.                          |
  142. |            1. Takes a pointer to a block of memory of at least the specified |
  143. |               field size and copies size bytes from recordOffset.  If the    |
  144. |               end of the record is encountered before size bytes are copied, |
  145. |               the result is padded to size with padByte.  The same pointer   |
  146. |               as the first parameter is returned from the function.          |
  147. |            2. Takes an IString and copies size bytes from recordOffset.      |
  148. |               If the end of the record is encountered before size bytes are  |
  149. |               copied, the result is padded to size with padByte.  The same   |
  150. |               IString as the first parameter is returned from the function.  |
  151. |            3. Takes a pointer to memory (size + 1 bytes) for a null          |
  152. |               terminated character string and copies size bytes from         |
  153. |               recordOffset.  If the end of the record is encountered before  |
  154. |               size bytes are copied, the result is padded to size with       |
  155. |               padByte.  A terminating null is ALWAYS placed in the extra     |
  156. |               byte provided in the buffer pointed to by the first parameter. |
  157. |               The same pointer as the first parameter is returned from the   |
  158. |               function.                                                      |
  159. |            4. Takes a fundamental data type and copies the implied size      |
  160. |               bytes from recordOffset.  If the end of the record is          |
  161. |               encountered before size bytes are copied, the result is padded |
  162. |               to size with padByte.  The result of the same type as the      |
  163. |               first parameter is returned from the function.  The value of   |
  164. |               the first parameter is not used.  It is needed to              |
  165. |               differentiate the overloaded functions.  You can receive the   |
  166. |               results into the same variable you pass as the first paramter, |
  167. |               for example:                                                   |
  168. |                   x = field( x, 20 );                                        |
  169. |                                                                              |
  170. | setField - There are four flavors of this function.  In all cases, if the    |
  171. |            specified record offset is greater than the current size of the   |
  172. |            record, the record is padded with '\x00' up to the offset.  The   |
  173. |            padding of the field (if any) is as explained for each flavor.    |
  174. |            1. Takes a pointer to a block of memory of at least the specified |
  175. |               field size and copies size bytes to recordOffset.  The same    |
  176. |               pointer as the first parameter is returned from the function.  |
  177. |            2. Takes an IString and copies size bytes to recordOffset.        |
  178. |               If (IString::size() < size), the field is padded to size with  |
  179. |               padByte.  The same IString as the first parameter is returned  |
  180. |               from the function.                                             |
  181. |            3. Takes a pointer to a null terminated character string and      |
  182. |               copies size bytes to recordOffset.  If (strlen(pData) < size), |
  183. |               the field is padded to size with padByte.  The terminating     |
  184. |               null byte is NOT copied.  The same pointer as the first        |
  185. |               parameter is returned from the function.                       |
  186. |            4. Takes a fundamental data type and copies the implied size      |
  187. |               bytes to recordOffset.  The result of the same type as the     |
  188. |               first parameter is returned from the function.  It will not    |
  189. |               be changed.  You can receive the results into the same         |
  190. |               variable you pass as the first paramter, for example:          |
  191. |                   x = setField( x, 20 );                                     |
  192. |-----------------------------------------------------------------------------*/
  193. void * field ( void * pBuffer,
  194.                unsigned long recordOffset,
  195.                unsigned long size,
  196.                unsigned char padByte = '\x00' ) const;
  197.  
  198. IString & field ( IString & buffer,
  199.                   unsigned long recordOffset,
  200.                   unsigned long size,
  201.                   unsigned char padByte = '\x00' ) const;
  202.  
  203. char * field ( char * pBuffer,
  204.                unsigned long recordOffset,
  205.                unsigned long size,
  206.                unsigned char padByte = '\x00' ) const;
  207.  
  208. char field ( char buffer,
  209.              unsigned long recordOffset,
  210.              unsigned char padByte = '\x00' ) const;
  211. unsigned short field ( unsigned short buffer,
  212.                        unsigned long recordOffset,
  213.                        unsigned char padByte = '\x00' ) const;
  214. unsigned long field ( unsigned long buffer,
  215.                       unsigned long recordOffset,
  216.                       unsigned char padByte = '\x00' ) const;
  217. float field ( float buffer,
  218.               unsigned long recordOffset,
  219.               unsigned char padByte = '\x00' ) const;
  220. double field ( double buffer,
  221.                unsigned long recordOffset,
  222.                unsigned char padByte = '\x00' ) const;
  223. long double field ( long double buffer,
  224.                     unsigned long recordOffset,
  225.                     unsigned char padByte = '\x00' ) const;
  226.  
  227.  
  228. const void * setField ( const void * pData,
  229.                         unsigned long recordOffset,
  230.                         unsigned long size );
  231.  
  232. const IString & setField ( const IString & data,
  233.                            unsigned long recordOffset,
  234.                            unsigned long size,
  235.                            unsigned char padByte = '\x00' );
  236.  
  237. const char * setField ( const char * pData,
  238.                         unsigned long recordOffset,
  239.                         unsigned long size,
  240.                         unsigned char padByte = '\x00' );
  241.  
  242. const char setField ( const char data,
  243.                       unsigned long recordOffset );
  244. const unsigned short setField ( const unsigned short data,
  245.                                 unsigned long recordOffset );
  246. const unsigned long setField ( const unsigned long data,
  247.                                unsigned long recordOffset );
  248. const float setField ( const float data,
  249.                        unsigned long recordOffset );
  250. const double setField ( const double data,
  251.                         unsigned long recordOffset );
  252. const long double setField ( const long double data,
  253.                              unsigned long recordOffset );
  254.  
  255. private:
  256. friend class IOrderedRecord;
  257.  
  258. /*--------------------------- Private State Data -----------------------------*/
  259. IString sRecordData;
  260.  
  261. }; // IRecord
  262.  
  263. // Resume compiler default packing.
  264. #pragma pack()
  265.  
  266. #endif // _IRecord_
  267.