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

  1. #ifndef _IPOATTR_
  2. #define _IPOATTR_
  3.  
  4. /*******************************************************************************
  5. * FILE NAME: IPOATTR.HPP                                                       *
  6. *                                                                              *
  7. * DESCRIPTION:                                                                 *
  8. *   Declaration of the class(es):                                              *
  9. *    Attribute definition for ODBC/CLI generation                              *
  10. *                                                                              *
  11. *******************************************************************************/
  12.  
  13. /***********************************************
  14.  
  15.     Licensed Materials - Property of IBM
  16.  
  17.     5622-880   5801-AAR
  18.  
  19.     (c) Copyright IBM Corp 1991, 1996.
  20.  
  21. ************************************************/
  22.  
  23. #include <ibase.hpp>
  24. #include <istring.hpp>
  25. #include <wcstr.h>
  26.  
  27. //***************************************************************************
  28. // base class for the null flag
  29. class IPOAttributeBase
  30. {
  31.   private:
  32.     Boolean theNull;
  33.   public:
  34.     IPOAttributeBase( Boolean aNull ) : theNull(aNull) {}
  35.     IPOAttributeBase( const IPOAttributeBase& anAttribute ) :
  36.          theNull(anAttribute.theNull) {}
  37.     Boolean isNull() const { return theNull; }
  38.     void setNull( const Boolean aNull = true ) { theNull = aNull; }
  39. };
  40.  
  41.  
  42. //***************************************************************************
  43. // template class for numeric SQL types
  44. // works for built in numeric types
  45. template <class TYPE>
  46. class IPONumericAttribute : public IPOAttributeBase
  47. {
  48.   protected:
  49.     TYPE theValue;
  50.   public:
  51.     IPONumericAttribute() : IPOAttributeBase(true), theValue((TYPE)0) {}
  52.     IPONumericAttribute( const IPONumericAttribute<TYPE>& anAttribute ) :
  53.         IPOAttributeBase(anAttribute), theValue(anAttribute.theValue) {}
  54.     const TYPE get() const { return theValue; }
  55.     void set( const TYPE aValue ) { theValue = aValue; }
  56.     Boolean operator==(const IPONumericAttribute<TYPE>& anAttribute) const
  57.     {
  58.       if ( isNull() != anAttribute.isNull() )
  59.          return false;
  60.       else if ( isNull() )
  61.          return true;
  62.       else
  63.          return get() == anAttribute.get();
  64.     }
  65.     Boolean operator!=(const IPONumericAttribute<TYPE>& anAttribute) const
  66.         { return ! ( *this == anAttribute ); }
  67.     void set( const IString& aString );
  68.     const IString asString() const { return isNull() ? IString("") : IString(theValue); }
  69.     void getFrom( TYPE* pBuffer ) { theValue = *pBuffer; }
  70.     const TYPE* pData() const { return &theValue; }
  71.     long length() const { return sizeof(theValue); }
  72. };
  73.  
  74. // instantiate the template for IString conversions - the only difference
  75. // for the numeric types
  76. inline void IPONumericAttribute<char>::set( const IString& aString )
  77.   { theValue = *((const char*)aString); }
  78. inline void IPONumericAttribute<short>::set( const IString& aString )
  79.   { theValue = aString.asInt(); }
  80. inline void IPONumericAttribute<unsigned short>::set( const IString& aString )
  81.   { theValue = aString.asUnsigned(); }
  82. inline void IPONumericAttribute<int>::set( const IString& aString )
  83.   { theValue = aString.asInt(); }
  84. inline void IPONumericAttribute<unsigned int>::set( const IString& aString )
  85.   { theValue = aString.asUnsigned(); }
  86. inline void IPONumericAttribute<long>::set( const IString& aString )
  87.   { theValue = aString.asInt(); }
  88. inline void IPONumericAttribute<unsigned long>::set( const IString& aString )
  89.   { theValue = aString.asUnsigned(); }
  90. inline void IPONumericAttribute<double>::set( const IString& aString )
  91.   { theValue = aString.asDouble(); }
  92.  
  93. //***************************************************************************
  94. // base class for strings
  95. class IPOStringAttribute : public IPOAttributeBase
  96. {
  97.   protected:
  98.     IString theString;
  99.   public:
  100.     IPOStringAttribute() : IPOAttributeBase(true) {}
  101.     IPOStringAttribute( const IPOStringAttribute& anAttribute ) :
  102.         theString(anAttribute.theString), IPOAttributeBase(anAttribute) {}
  103.     Boolean operator==(const IPOStringAttribute& anAttribute) const
  104.     {
  105.       if ( isNull() != anAttribute.isNull() )
  106.          return false;
  107.       else if ( isNull() )
  108.          return true;
  109.       else
  110.          return theString == anAttribute.theString;
  111.     }
  112.     Boolean operator!=(const IPOStringAttribute& anAttribute) const
  113.         { return ! ( *this == anAttribute ); }
  114.     const IString asString() const { return isNull() ? IString() : theString; }
  115.     void set( const IString& aValue ) { theString = aValue; }
  116.     const IString& get() const { return theString; }
  117.     unsigned putInto( void* pBuffer ) const
  118.     {
  119.       memcpy( pBuffer, (char*) theString, theString.length() );
  120.       return theString.length();
  121.     }
  122.     unsigned putInto( char* pBuffer ) const
  123.     {
  124.       memcpy( pBuffer, (char*) theString, theString.length() );
  125.       pBuffer[theString.length()] = '\0';
  126.       return theString.length();
  127.     }
  128.     void getFrom( const char* pBuffer )
  129.     {
  130.       theString = pBuffer;
  131.     }
  132.     void getFrom( const char* pBuffer, unsigned length )
  133.     {
  134.       theString = IString(pBuffer,length);
  135.     }
  136.     unsigned putInto( wchar_t* pBuffer ) const
  137.     {
  138.       memcpy( pBuffer, (char*) theString, theString.length() );
  139.       // set the last full character to the null terminator
  140.       pBuffer[(theString.length()+1)/sizeof(wchar_t)] = L'\0';
  141.       return theString.length();
  142.     }
  143.     void getFrom( const wchar_t* pBuffer )
  144.     {
  145.       theString = IString( pBuffer, wcslen(pBuffer) );
  146.     }
  147.     void getFrom( const wchar_t* pBuffer, unsigned length )
  148.     {
  149.       theString = IString( pBuffer, length*sizeof(wchar_t) );
  150.     }
  151.     const char* pData() const { return theString; }
  152.     long length() const { return theString.length(); }
  153. };
  154.  
  155. #endif
  156.