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 /*=========================================================================
00005  * (c) Copyright 2002-2007, GemStone Systems, Inc. All Rights Reserved.
00006  * 1260 NW Waterhouse Ave., Suite 200,  Beaverton, OR 97006
00007  *=========================================================================
00008  */
00009 
00010 #include "gfcpp_globals.hpp"
00011 #include "gf_types.hpp"
00012 #include "CacheableKey.hpp"
00013 #include "GemfireTypeIds.hpp"
00014 #include "ExceptionTypes.hpp"
00015 
00019 namespace gemfire
00020 {
00021 
00022 #define GF_STRING (int8_t)GemfireTypeIds::CacheableASCIIString
00023 #define GF_STRING_HUGE (int8_t)GemfireTypeIds::CacheableASCIIStringHuge
00024 #define GF_WIDESTRING (int8_t)GemfireTypeIds::CacheableString
00025 #define GF_WIDESTRING_HUGE (int8_t)GemfireTypeIds::CacheableStringHuge
00026 
00031   class CPPCACHE_EXPORT CacheableString: public CacheableKey
00032   {
00033   protected:
00034     void* m_str;
00035     int8_t m_type;
00036     uint32_t m_len;
00037     mutable int m_hashcode;
00038 
00039   public:
00043     virtual void toData(DataOutput& output) const;
00044 
00050     virtual Serializable* fromData(DataInput& input);
00051 
00053     static Serializable* createDeserializable();
00054 
00056     static Serializable* createDeserializableHuge();
00057 
00059     static Serializable* createUTFDeserializable();
00060 
00062     static Serializable* createUTFDeserializableHuge();
00063 
00069     virtual int32_t classId() const;
00070 
00086     virtual int8_t typeId() const;
00087 
00089     virtual bool operator==(const CacheableKey& other) const;
00090 
00092     virtual uint32_t hashcode() const;
00093 
00100     static CacheableStringPtr create(const char* value, int32_t len = 0)
00101     {
00102       CacheableStringPtr str = NULLPTR;
00103       if (value != NULL) {
00104         str = new CacheableString();
00105         str->initString(value, len);
00106       }
00107       return str;
00108     }
00109 
00120     static CacheableStringPtr createNoCopy(char* value, int32_t len = 0)
00121     {
00122       CacheableStringPtr str = NULLPTR;
00123       if (value != NULL) {
00124         str = new CacheableString();
00125         str->initStringNoCopy(value, len);
00126       }
00127       return str;
00128     }
00129 
00136     static CacheableStringPtr create(const wchar_t* value, int32_t len = 0)
00137     {
00138       CacheableStringPtr str = NULLPTR;
00139       if (value != NULL) {
00140         str = new CacheableString();
00141         str->initString(value, len);
00142       }
00143       return str;
00144     }
00145 
00156     static CacheableStringPtr createNoCopy(wchar_t* value, int32_t len = 0)
00157     {
00158       CacheableStringPtr str = NULLPTR;
00159       if (value != NULL) {
00160         str = new CacheableString();
00161         str->initStringNoCopy(value, len);
00162       }
00163       return str;
00164     }
00165 
00167     inline bool isCString() const
00168     {
00169       return (m_type == GF_STRING || m_type == GF_STRING_HUGE);
00170     }
00171 
00173     inline bool isWideString() const
00174     {
00175       return (m_type == GF_WIDESTRING || m_type == GF_WIDESTRING_HUGE);
00176     }
00177 
00187     const char* asChar() const
00188     {
00189       if (isWideString()) {
00190         throw IllegalStateException("CacheableString::asChar: the string is a "
00191           "wide character string; use asWChar() to obtain it.");
00192       }
00193       return (const char*) m_str;
00194     }
00195 
00205     const wchar_t* asWChar() const
00206     {
00207       if (isCString()) {
00208         throw IllegalStateException("CacheableString::asWChar: the string is "
00209           "not a wide character string; use asChar() to obtain it.");
00210       }
00211       return (const wchar_t*) m_str;
00212     }
00213 
00215     inline uint32_t length() const
00216     {
00217       return m_len;
00218     }
00219 
00231     const char* toString()
00232     {
00233       return (const char*) m_str;
00234     }
00235 
00236     virtual CacheableStringPtr toString() const
00237     {
00238       return CacheableStringPtr(this);
00239     }
00240 
00242     virtual const char* className() const
00243     {
00244       return "CacheableString";
00245     }
00246 
00248     virtual ~CacheableString();
00249 
00251     virtual int32_t logString(char* buffer, int32_t maxLength) const;
00252 
00253     virtual uint32_t objectSize() const;
00254 
00255   protected:
00256 
00258     void copyString(const char* value, int32_t len);
00260     void copyString(const wchar_t* value, int32_t len);
00262     void initString(const char* value, int32_t len);
00267     void initStringNoCopy(char* value, int32_t len);
00269     void initString(const wchar_t* value, int32_t len);
00274     void initStringNoCopy(wchar_t* value, int32_t len);
00276     char* getASCIIString(const wchar_t* value, int32_t& len,
00277         int32_t& encodedLen);
00279     inline CacheableString(int8_t type = GF_STRING) :
00280       m_str(NULL), m_type(type), m_len(0), m_hashcode(0)
00281     {
00282     }
00283 
00284   private:
00285     // never implemented.
00286     void operator=(const CacheableString& other);
00287     CacheableString(const CacheableString& other);
00288   };
00289 
00291   inline CacheableKeyPtr createKeyArr(const char* value)
00292   {
00293     return (value != NULL ? CacheableKeyPtr(CacheableString::create(
00294         value).ptr()) : NULLPTR);
00295   }
00296 
00298   inline CacheableKeyPtr createKeyArr(const wchar_t* value)
00299   {
00300     return (value != NULL ? CacheableKeyPtr(CacheableString::create(
00301         value).ptr()) : NULLPTR);
00302   }
00303 
00305   inline CacheablePtr createValueArr(const char* value)
00306   {
00307     return (value != NULL ? CacheablePtr(CacheableString::create(
00308         value).ptr()) : NULLPTR);
00309   }
00310 
00312   inline CacheablePtr createValueArr(const wchar_t* value)
00313   {
00314     return (value != NULL ? CacheablePtr(CacheableString::create(
00315         value).ptr()) : NULLPTR);
00316   }
00317 }
00318 
00319 
00320 #endif

GemFire C++ Cache API Documentation