build-artifacts/win/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  * Copyright (c) 2002-2014 Pivotal Software, Inc. All Rights Reserved.
00006  * This product is protected by U.S. and international copyright
00007  * and intellectual property laws. Pivotal products are covered by
00008  * more patents listed at http://www.pivotal.io/patents.
00009  *=========================================================================
00010  */
00011 
00012 #include "gfcpp_globals.hpp"
00013 #include "gf_types.hpp"
00014 #include "CacheableKey.hpp"
00015 #include "GemfireTypeIds.hpp"
00016 #include "ExceptionTypes.hpp"
00017 
00021 namespace gemfire
00022 {
00023 
00024 #define GF_STRING (int8_t)GemfireTypeIds::CacheableASCIIString
00025 #define GF_STRING_HUGE (int8_t)GemfireTypeIds::CacheableASCIIStringHuge
00026 #define GF_WIDESTRING (int8_t)GemfireTypeIds::CacheableString
00027 #define GF_WIDESTRING_HUGE (int8_t)GemfireTypeIds::CacheableStringHuge
00028 
00033   class CPPCACHE_EXPORT CacheableString: public CacheableKey
00034   {
00035   protected:
00036     void* m_str;
00037     int8_t m_type;
00038     uint32_t m_len;
00039     mutable int m_hashcode;
00040 
00041   public:
00045     virtual void toData(DataOutput& output) const;
00046 
00052     virtual Serializable* fromData(DataInput& input);
00053 
00055     static Serializable* createDeserializable();
00056 
00058     static Serializable* createDeserializableHuge();
00059 
00061     static Serializable* createUTFDeserializable();
00062 
00064     static Serializable* createUTFDeserializableHuge();
00065 
00071     virtual int32_t classId() const;
00072 
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, int32_t len = 0)
00103     {
00104       CacheableStringPtr str = NULLPTR;
00105       if (value != NULL) {
00106         str = new CacheableString();
00107         str->initString(value, len);
00108       }
00109       return str;
00110     }
00111 
00122     static CacheableStringPtr createNoCopy(char* value, int32_t len = 0)
00123     {
00124       CacheableStringPtr str = NULLPTR;
00125       if (value != NULL) {
00126         str = new CacheableString();
00127         str->initStringNoCopy(value, len);
00128       }
00129       return str;
00130     }
00131 
00138     static CacheableStringPtr create(const wchar_t* value, int32_t len = 0)
00139     {
00140       CacheableStringPtr str = NULLPTR;
00141       if (value != NULL) {
00142         str = new CacheableString();
00143         str->initString(value, len);
00144       }
00145       return str;
00146     }
00147 
00158     static CacheableStringPtr createNoCopy(wchar_t* value, int32_t len = 0)
00159     {
00160       CacheableStringPtr str = NULLPTR;
00161       if (value != NULL) {
00162         str = new CacheableString();
00163         str->initStringNoCopy(value, len);
00164       }
00165       return str;
00166     }
00167 
00169     inline bool isCString() const
00170     {
00171       return (m_type == GF_STRING || m_type == GF_STRING_HUGE);
00172     }
00173 
00175     inline bool isWideString() const
00176     {
00177       return (m_type == GF_WIDESTRING || m_type == GF_WIDESTRING_HUGE);
00178     }
00179 
00189     const char* asChar() const
00190     {
00191       if (isWideString()) {
00192         throw IllegalStateException("CacheableString::asChar: the string is a "
00193           "wide character string; use asWChar() to obtain it.");
00194       }
00195       return (const char*) m_str;
00196     }
00197 
00207     const wchar_t* asWChar() const
00208     {
00209       if (isCString()) {
00210         throw IllegalStateException("CacheableString::asWChar: the string is "
00211           "not a wide character string; use asChar() to obtain it.");
00212       }
00213       return (const wchar_t*) m_str;
00214     }
00215 
00217     inline uint32_t length() const
00218     {
00219       return m_len;
00220     }
00221 
00233     const char* toString()
00234     {
00235       return (const char*) m_str;
00236     }
00237 
00238     virtual CacheableStringPtr toString() const
00239     {
00240       return CacheableStringPtr(this);
00241     }
00242 
00244     virtual const char* className() const
00245     {
00246       return "CacheableString";
00247     }
00248 
00250     virtual ~CacheableString();
00251 
00253     virtual int32_t logString(char* buffer, int32_t maxLength) const;
00254 
00255     virtual uint32_t objectSize() const;
00256 
00257   protected:
00258 
00260     void copyString(const char* value, int32_t len);
00262     void copyString(const wchar_t* value, int32_t len);
00264     void initString(const char* value, int32_t len);
00269     void initStringNoCopy(char* value, int32_t len);
00271     void initString(const wchar_t* value, int32_t len);
00276     void initStringNoCopy(wchar_t* value, int32_t len);
00278     char* getASCIIString(const wchar_t* value, int32_t& len,
00279         int32_t& encodedLen);
00281     inline CacheableString(int8_t type = GF_STRING) :
00282       m_str(NULL), m_type(type), m_len(0), m_hashcode(0)
00283     {
00284     }
00285 
00286   private:
00287     // never implemented.
00288     void operator=(const CacheableString& other);
00289     CacheableString(const CacheableString& other);
00290   };
00291 
00293   inline CacheableKeyPtr createKeyArr(const char* value)
00294   {
00295     return (value != NULL ? CacheableKeyPtr(CacheableString::create(
00296         value).ptr()) : NULLPTR);
00297   }
00298 
00300   inline CacheableKeyPtr createKeyArr(const wchar_t* value)
00301   {
00302     return (value != NULL ? CacheableKeyPtr(CacheableString::create(
00303         value).ptr()) : NULLPTR);
00304   }
00305 
00307   inline CacheablePtr createValueArr(const char* value)
00308   {
00309     return (value != NULL ? CacheablePtr(CacheableString::create(
00310         value).ptr()) : NULLPTR);
00311   }
00312 
00314   inline CacheablePtr createValueArr(const wchar_t* value)
00315   {
00316     return (value != NULL ? CacheablePtr(CacheableString::create(
00317         value).ptr()) : NULLPTR);
00318   }
00319 }
00320 
00321 
00322 #endif

GemFire C++ Cache API Documentation