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 
00017 namespace gemfire {
00018 
00019 #define GF_STRING (int8_t)GemfireTypeIds::CacheableASCIIString
00020 #define GF_STRING_HUGE (int8_t)GemfireTypeIds::CacheableASCIIStringHuge
00021 #define GF_WIDESTRING (int8_t)GemfireTypeIds::CacheableString
00022 #define GF_WIDESTRING_HUGE (int8_t)GemfireTypeIds::CacheableStringHuge
00023 
00028 class CPPCACHE_EXPORT CacheableString
00029   : public CacheableKey
00030 {
00031 protected:
00032 
00033     void* m_str;
00034     int8_t m_type;
00035     uint32_t m_len;
00036 
00037 public:
00038 
00042   virtual void toData( DataOutput& output ) const;
00043 
00049   virtual Serializable* fromData( DataInput& input );
00050 
00054   static Serializable* createDeserializable( );
00055 
00059   static Serializable* createDeserializableHuge( );
00060 
00064   static Serializable* createUTFDeserializable( );
00065 
00069   static Serializable* createUTFDeserializableHuge( );
00070 
00076   virtual int32_t classId( ) const;
00077 
00091   virtual int8_t typeId( ) const;
00092 
00094   virtual bool operator==( const CacheableKey& other ) const;
00095 
00097   virtual uint32_t hashcode( ) const;
00098 
00105   static CacheableStringPtr create( const char* value )
00106   {
00107     return CacheableStringPtr(new CacheableString(value));
00108   }
00109 
00116   static CacheableStringPtr create( const char* value, size_t len )
00117   {
00118     return CacheableStringPtr(new CacheableString(value, len));
00119   }
00120 
00131   static CacheableStringPtr createNoCopy( char* value, size_t len )
00132   {
00133     return CacheableStringPtr(new CacheableString(value, len, true));
00134   }
00135 
00142   static CacheableStringPtr create( const wchar_t* value )
00143   {
00144     return CacheableStringPtr(new CacheableString(value));
00145   }
00146 
00153   static CacheableStringPtr create( const wchar_t* value, size_t len )
00154   {
00155     return CacheableStringPtr(new CacheableString(value, len));
00156   }
00157 
00168   static CacheableStringPtr createNoCopy( wchar_t* value, size_t len )
00169   {
00170     return CacheableStringPtr(new CacheableString(value, len, true));
00171   }
00172 
00173 
00180   const char* asChar( ) const
00181   {
00182     if ( m_type == GF_WIDESTRING || m_type == GF_WIDESTRING_HUGE ) {
00183       throw IllegalStateException( "CacheableString::asChar: the string is a "
00184           "wide character string; use asWChar() to obtain it." );
00185     }
00186     return (const char*)m_str;
00187   }
00188 
00196   const wchar_t* asWChar( ) const
00197   {
00198     if ( m_type == GF_STRING || m_type == GF_STRING_HUGE ) {
00199       throw IllegalStateException( "CacheableString::asWChar: the string is "
00200           "not a wide character string; use asChar() to obtain it." );
00201     }
00202     return (const wchar_t*)m_str;
00203   }
00204 
00206   inline bool isWideString( ) const
00207   {
00208     return ( m_type == GF_WIDESTRING || m_type == GF_WIDESTRING_HUGE );
00209   }
00210 
00212   inline uint32_t length( ) const {
00213     return m_len;
00214   }
00215 
00227   const char* toString( )
00228   {
00229     return (const char*)m_str;
00230   }
00231 
00232   virtual CacheableStringPtr toString() const
00233   {
00234     return CacheableStringPtr(this);
00235   }
00236 
00238   virtual const char* className() const
00239   {
00240     return "CacheableString";
00241   }
00242 
00244   virtual ~CacheableString( );
00245 
00247   virtual size_t logString( char* buffer, size_t maxLength ) const;
00248 
00249   virtual uint32_t objectSize() const;
00250 
00252   void copyString( const char* value, size_t len );
00254   void copyString( const wchar_t* value, size_t len );
00256   CacheableString( const char* value, size_t len = 0 );
00258   CacheableString( char* value, size_t len, bool noCopy );
00260   CacheableString( const wchar_t* value, size_t len = 0 );
00262   CacheableString( wchar_t* value, size_t len, bool noCopy );
00264   inline CacheableString( int8_t type = GF_STRING )
00265     : m_str( NULL ), m_type( type ), m_len( 0 ) { }
00266 
00267 private:
00268 
00269   // never implemented.
00270   void operator=( const CacheableString& other );
00271   CacheableString( const CacheableString& other );
00272 
00273 };
00274 
00276 inline CacheableKeyPtr createKeyArr( const char* value )
00277 {
00278   return CacheableKeyPtr( CacheableString::create( value ).ptr( ) );
00279 }
00280 
00282 inline CacheableKeyPtr createKeyArr( const wchar_t* value )
00283 {
00284   return CacheableKeyPtr( CacheableString::create( value ).ptr( ) );
00285 }
00286 
00288 inline CacheablePtr createValueArr( const char* value )
00289 {
00290   return CacheablePtr( CacheableString::create( value ).ptr( ) );
00291 }
00292 
00294 inline CacheablePtr createValueArr( const wchar_t* value )
00295 {
00296   return CacheablePtr( CacheableString::create( value ).ptr( ) );
00297 }
00298 
00299 }
00300 
00301 #endif
00302 

GemFire C++ Cache API Documentation