VMware GemFire Native Client Cache Reference  9.0.6
CacheableString.hpp
Go to the documentation of this file.
1 #ifndef _GEMFIRE_CACHEABLESTRING_HPP_
2 #define _GEMFIRE_CACHEABLESTRING_HPP_
3 
4 /*=========================================================================
5  * Copyright (c) 2002-2014 Pivotal Software, Inc. All Rights Reserved.
6  * This product is protected by U.S. and international copyright
7  * and intellectual property laws. Pivotal products are covered by
8  * more patents listed at http://www.pivotal.io/patents.
9  *=========================================================================
10  */
11 
12 #include "gfcpp_globals.hpp"
13 #include "gf_types.hpp"
14 #include "CacheableKey.hpp"
15 #include "GemfireTypeIds.hpp"
16 #include "ExceptionTypes.hpp"
17 
21 namespace gemfire
22 {
23 
24 #define GF_STRING (int8_t)GemfireTypeIds::CacheableASCIIString
25 #define GF_STRING_HUGE (int8_t)GemfireTypeIds::CacheableASCIIStringHuge
26 #define GF_WIDESTRING (int8_t)GemfireTypeIds::CacheableString
27 #define GF_WIDESTRING_HUGE (int8_t)GemfireTypeIds::CacheableStringHuge
28 
34  {
35  protected:
36  void* m_str;
37  int8_t m_type;
38  uint32_t m_len;
39  mutable int m_hashcode;
40 
41  public:
45  virtual void toData(DataOutput& output) const;
46 
52  virtual Serializable* fromData(DataInput& input);
53 
55  static Serializable* createDeserializable();
56 
58  static Serializable* createDeserializableHuge();
59 
61  static Serializable* createUTFDeserializable();
62 
64  static Serializable* createUTFDeserializableHuge();
65 
71  virtual int32_t classId() const;
72 
88  virtual int8_t typeId() const;
89 
91  virtual bool operator==(const CacheableKey& other) const;
92 
94  virtual uint32_t hashcode() const;
95 
102  static CacheableStringPtr create(const char* value, int32_t len = 0)
103  {
104  CacheableStringPtr str = NULLPTR;
105  if (value != NULL) {
106  str = new CacheableString();
107  str->initString(value, len);
108  }
109  return str;
110  }
111 
122  static CacheableStringPtr createNoCopy(char* value, int32_t len = 0)
123  {
124  CacheableStringPtr str = NULLPTR;
125  if (value != NULL) {
126  str = new CacheableString();
127  str->initStringNoCopy(value, len);
128  }
129  return str;
130  }
131 
138  static CacheableStringPtr create(const wchar_t* value, int32_t len = 0)
139  {
140  CacheableStringPtr str = NULLPTR;
141  if (value != NULL) {
142  str = new CacheableString();
143  str->initString(value, len);
144  }
145  return str;
146  }
147 
158  static CacheableStringPtr createNoCopy(wchar_t* value, int32_t len = 0)
159  {
160  CacheableStringPtr str = NULLPTR;
161  if (value != NULL) {
162  str = new CacheableString();
163  str->initStringNoCopy(value, len);
164  }
165  return str;
166  }
167 
169  inline bool isCString() const
170  {
171  return (m_type == GF_STRING || m_type == GF_STRING_HUGE);
172  }
173 
175  inline bool isWideString() const
176  {
177  return (m_type == GF_WIDESTRING || m_type == GF_WIDESTRING_HUGE);
178  }
179 
189  const char* asChar() const
190  {
191  if (isWideString()) {
192  throw IllegalStateException("CacheableString::asChar: the string is a "
193  "wide character string; use asWChar() to obtain it.");
194  }
195  return (const char*) m_str;
196  }
197 
207  const wchar_t* asWChar() const
208  {
209  if (isCString()) {
210  throw IllegalStateException("CacheableString::asWChar: the string is "
211  "not a wide character string; use asChar() to obtain it.");
212  }
213  return (const wchar_t*) m_str;
214  }
215 
217  inline uint32_t length() const
218  {
219  return m_len;
220  }
221 
233  const char* toString()
234  {
235  return (const char*) m_str;
236  }
237 
238  virtual CacheableStringPtr toString() const
239  {
240  return CacheableStringPtr(this);
241  }
242 
244  virtual const char* className() const
245  {
246  return "CacheableString";
247  }
248 
250  virtual ~CacheableString();
251 
253  virtual int32_t logString(char* buffer, int32_t maxLength) const;
254 
255  virtual uint32_t objectSize() const;
256 
257  protected:
258 
260  void copyString(const char* value, int32_t len);
262  void copyString(const wchar_t* value, int32_t len);
264  void initString(const char* value, int32_t len);
269  void initStringNoCopy(char* value, int32_t len);
271  void initString(const wchar_t* value, int32_t len);
276  void initStringNoCopy(wchar_t* value, int32_t len);
278  char* getASCIIString(const wchar_t* value, int32_t& len,
279  int32_t& encodedLen);
281  inline CacheableString(int8_t type = GF_STRING) :
282  m_str(NULL), m_type(type), m_len(0), m_hashcode(0)
283  {
284  }
285 
286  private:
287  // never implemented.
288  void operator=(const CacheableString& other);
289  CacheableString(const CacheableString& other);
290  };
291 
293  inline CacheableKeyPtr createKeyArr(const char* value)
294  {
295  return (value != NULL ? CacheableKeyPtr(CacheableString::create(
296  value).ptr()) : NULLPTR);
297  }
298 
300  inline CacheableKeyPtr createKeyArr(const wchar_t* value)
301  {
302  return (value != NULL ? CacheableKeyPtr(CacheableString::create(
303  value).ptr()) : NULLPTR);
304  }
305 
307  inline CacheablePtr createValueArr(const char* value)
308  {
309  return (value != NULL ? CacheablePtr(CacheableString::create(
310  value).ptr()) : NULLPTR);
311  }
312 
314  inline CacheablePtr createValueArr(const wchar_t* value)
315  {
316  return (value != NULL ? CacheablePtr(CacheableString::create(
317  value).ptr()) : NULLPTR);
318  }
319 }
320 
321 
322 #endif
uint32_t length() const
Return the length of the contained string.
Definition: CacheableString.hpp:217
Provide operations for writing primitive data values, byte arrays, strings, Serializable objects to a...
Definition: DataOutput.hpp:62
CacheableString(int8_t type=(int8_t) GemfireTypeIds::CacheableASCIIString)
Default constructor.
Definition: CacheableString.hpp:281
virtual const char * className() const
get the name of the class of this object for logging purpose
Definition: CacheableString.hpp:244
virtual CacheableStringPtr toString() const
Display this object as 'string', which depends on the implementation in the subclasses.
Definition: CacheableString.hpp:238
void initStringNoCopy(char *value, int32_t len)
Initialize the string without making a copy, given a C string and length.
static CacheableStringPtr createNoCopy(char *value, int32_t len=0)
Factory method for creating an instance of CacheableString from a C string of given length by taking ...
Definition: CacheableString.hpp:122
Represents a cacheable key.
Definition: CacheableKey.hpp:23
static CacheableStringPtr create(const char *value, int32_t len=0)
Factory method for creating an instance of CacheableString from a null terminated C string optionally...
Definition: CacheableString.hpp:102
const char * asChar() const
Return the string that backs this CacheableString as a char *.
Definition: CacheableString.hpp:189
This namespace contains all the GemFire C++ API classes, enumerations and globals.
Definition: Assert.hpp:19
Implement a immutable C string wrapper that can serve as a distributable key object for caching as we...
Definition: CacheableString.hpp:33
CacheableKeyPtr createKeyArr(const char *value)
overload of gemfire::createKeyArr to pass char*
Definition: CacheableString.hpp:293
bool isWideString() const
Returns true if the underlying string is a wide-character string.
Definition: CacheableString.hpp:175
void initString(const char *value, int32_t len)
initialize the string, given a value and length.
bool isCString() const
Returns true if the underlying string is a normal C string.
Definition: CacheableString.hpp:169
This abstract base class is the superclass of all user objects in the cache that can be serialized...
Definition: Serializable.hpp:39
static CacheableStringPtr createNoCopy(wchar_t *value, int32_t len=0)
Factory method for creating an instance of CacheableString from a wide-character C string of given le...
Definition: CacheableString.hpp:158
Provide operations for reading primitive data values, byte arrays, strings, Serializable objects from...
Definition: DataInput.hpp:44
#define CPPCACHE_EXPORT
Defines a GemFire CPPCACHE export.
Definition: gf_base.hpp:51
const wchar_t * asWChar() const
Return the string that backs this CacheableString as a wchar_t *.
Definition: CacheableString.hpp:207
const char * toString()
Display this object as c string.
Definition: CacheableString.hpp:233
CacheablePtr createValueArr(const char *value)
overload of gemfire::createValueArr to pass char*
Definition: CacheableString.hpp:307
static CacheableStringPtr create(const wchar_t *value, int32_t len=0)
Factory method for creating an instance of CacheableString from a wide-character null terminated C st...
Definition: CacheableString.hpp:138

GemFire C++ Cache API Documentation