H:/gfcppsancout/product/include/gfcpp/CacheableString.hpp

Go to the documentation of this file.
00001 #ifndef _GEMFIRE_CACHEABLESTRING_HPP_
00002 #define _GEMFIRE_CACHEABLESTRING_HPP_
00003 /*=========================================================================
00004  * (c) Copyright 2002-2007, GemStone Systems, Inc. All Rights Reserved.  
00005  * 1260 NW Waterhouse Ave., Suite 200,  Beaverton, OR 97006
00006  *=========================================================================
00007  */
00008 #include "gfcpp_globals.hpp"
00009 #include "gf_types.hpp"
00010 #include "CacheableKey.hpp"
00011 #include "GemfireTypeIds.hpp"
00012 #include "ExceptionTypes.hpp"
00013 
00014 
00015 #ifndef CACHEABLESTRING_MAX_LENGTH
00016   #define CACHEABLESTRING_MAX_LENGTH 64*1024
00017 #endif
00018 
00022 namespace gemfire {
00023 
00024 #define GF_STRING (int8_t)GemfireTypeIds::CacheableASCIIString
00025 #define GF_WIDESTRING (int8_t)GemfireTypeIds::CacheableString
00026 #define GF_NULLSTRING (int8_t)GemfireTypeIds::CacheableNullString
00027 
00032 class CPPCACHE_EXPORT CacheableString
00033   : public CacheableKey
00034 {
00035   protected:
00036 
00037     void* m_str;
00038     int8_t m_type;
00039     uint16_t m_len; // K64
00040 
00041   public:
00042 
00046   virtual void toData( DataOutput& output ) const;
00047 
00053   virtual Serializable* fromData( DataInput& input );
00054   
00058   static Serializable* createNullDeserializable( );
00059 
00063   static Serializable* createDeserializable( );
00064 
00068   static Serializable* createUTFDeserializable( );
00069 
00075   virtual int8_t classId( ) const;
00076 
00088   virtual int8_t typeId( ) const;
00089 
00091   virtual bool operator==( const CacheableKey& other ) const;
00092 
00094   virtual uint32_t hashcode( ) const;
00095 
00102   static CacheableStringPtr create( const char* value )
00103   {
00104     return new CacheableString( value );
00105   }
00106 
00113   static CacheableStringPtr create( const char* value, size_t len )
00114   {
00115     return new CacheableString( value, len );
00116   }
00117 
00128   static CacheableStringPtr createNoCopy( char* value, size_t len )
00129   {
00130     return new CacheableString( value, len, true );
00131   }
00132 
00139   static CacheableStringPtr create( const wchar_t* value )
00140   {
00141     return new CacheableString( value );
00142   }
00143 
00150   static CacheableStringPtr create( const wchar_t* value, size_t len )
00151   {
00152     return new CacheableString( value, len );
00153   }
00154 
00165   static CacheableStringPtr createNoCopy( wchar_t* value, size_t len )
00166   {
00167     return new CacheableString( value, len, true );
00168   }
00169 
00170 
00177   const char* asChar( ) const
00178   {
00179     if ( m_type == GF_WIDESTRING ) {
00180       throw IllegalStateException( "CacheableString::asChar: the string is a "
00181           "wide character string; use asWChar() to obtain it." );
00182     }
00183     return (const char*)m_str;
00184   }
00185 
00193   const wchar_t* asWChar( ) const
00194   {
00195     if ( m_type == GF_STRING ) {
00196       throw IllegalStateException( "CacheableString::asWChar: the string is "
00197           "not a wide character string; use asChar() to obtain it." );
00198     }
00199     return (const wchar_t*)m_str;
00200   }
00201 
00203   inline bool isWideString( ) const
00204   {
00205     return ( m_type == GF_WIDESTRING );
00206   }
00207 
00209   inline uint16_t length( ) const { // 64K
00210     return m_len;
00211   }
00212 
00224   const char* toString( )
00225   {
00226     return (const char*)m_str;
00227   }
00228 
00229   virtual CacheableStringPtr toString( ) const
00230   {
00231     return this;
00232   }
00233 
00235   virtual ~CacheableString( );
00236 
00238   virtual size_t logString( char* buffer, size_t maxLength ) const;
00239   
00240   virtual uint32_t objectSize() const;
00241   
00242   protected:
00243 
00245   void checkSize( const char* value, size_t& len );
00247   void checkSize( const wchar_t* value, size_t& len );
00249   void copyString( const char* value, size_t len );
00251   void copyString( const wchar_t* value, size_t len );
00253   CacheableString( const char* value, size_t len = 0 );
00255   CacheableString( char* value, size_t len, bool noCopy );
00257   CacheableString( const wchar_t* value, size_t len = 0 );
00259   CacheableString( wchar_t* value, size_t len, bool noCopy );
00261   inline CacheableString( int8_t type = GF_NULLSTRING )
00262     : m_str( NULL ), m_type( type ), m_len( 0 ) { }
00263 
00264   private:
00265 
00266   // never implemented.
00267   void operator=( const CacheableString& other );
00268   CacheableString( const CacheableString& other );
00269 
00270 };
00271 
00273 inline CacheableKeyPtr createKeyArr( const char* value )
00274 {
00275   return CacheableKeyPtr( CacheableString::create( value ).ptr( ) );
00276 }
00277 
00279 inline CacheableKeyPtr createKeyArr( const wchar_t* value )
00280 {
00281   return CacheableKeyPtr( CacheableString::create( value ).ptr( ) );
00282 }
00283 
00285 inline CacheablePtr createValueArr( const char* value )
00286 {
00287   return CacheablePtr( CacheableString::create( value ).ptr( ) );
00288 }
00289 
00291 inline CacheablePtr createValueArr( const wchar_t* value )
00292 {
00293   return CacheablePtr( CacheableString::create( value ).ptr( ) );
00294 }
00295 
00296 }
00297 
00298 #endif
00299 

GemFire C++ Cache API Documentation