VMware GemFire Native C++ Reference  9.1
CacheableString.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #ifndef GEODE_CACHEABLESTRING_H_
4 #define GEODE_CACHEABLESTRING_H_
5 
6 /*
7  * Licensed to the Apache Software Foundation (ASF) under one or more
8  * contributor license agreements. See the NOTICE file distributed with
9  * this work for additional information regarding copyright ownership.
10  * The ASF licenses this file to You under the Apache License, Version 2.0
11  * (the "License"); you may not use this file except in compliance with
12  * the License. You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  */
22 
23 #include "geode_globals.hpp"
24 #include "geode_types.hpp"
25 #include "CacheableKey.hpp"
26 #include "GeodeTypeIds.hpp"
27 #include "ExceptionTypes.hpp"
28 
32 namespace apache {
33 namespace geode {
34 namespace client {
35 
36 #define GF_STRING (int8_t) GeodeTypeIds::CacheableASCIIString
37 #define GF_STRING_HUGE (int8_t) GeodeTypeIds::CacheableASCIIStringHuge
38 #define GF_WIDESTRING (int8_t) GeodeTypeIds::CacheableString
39 #define GF_WIDESTRING_HUGE (int8_t) GeodeTypeIds::CacheableStringHuge
40 
46  protected:
47  void* m_str;
48  int8_t m_type;
49  uint32_t m_len;
50  mutable int m_hashcode;
51 
52  public:
56  virtual void toData(DataOutput& output) const;
57 
63  virtual Serializable* fromData(DataInput& input);
64 
66  static Serializable* createDeserializable();
67 
69  static Serializable* createDeserializableHuge();
70 
72  static Serializable* createUTFDeserializable();
73 
75  static Serializable* createUTFDeserializableHuge();
76 
82  virtual int32_t classId() const;
83 
99  virtual int8_t typeId() const;
100 
102  virtual bool operator==(const CacheableKey& other) const;
103 
105  virtual int32_t hashcode() const;
106 
113  static CacheableStringPtr create(const char* value, int32_t len = 0) {
114  CacheableStringPtr str = NULLPTR;
115  if (value != NULL) {
116  str = new CacheableString();
117  str->initString(value, len);
118  }
119  return str;
120  }
121 
132  static CacheableStringPtr createNoCopy(char* value, int32_t len = 0) {
133  CacheableStringPtr str = NULLPTR;
134  if (value != NULL) {
135  str = new CacheableString();
136  str->initStringNoCopy(value, len);
137  }
138  return str;
139  }
140 
147  static CacheableStringPtr create(const wchar_t* value, int32_t len = 0) {
148  CacheableStringPtr str = NULLPTR;
149  if (value != NULL) {
150  str = new CacheableString();
151  str->initString(value, len);
152  }
153  return str;
154  }
155 
166  static CacheableStringPtr createNoCopy(wchar_t* value, int32_t len = 0) {
167  CacheableStringPtr str = NULLPTR;
168  if (value != NULL) {
169  str = new CacheableString();
170  str->initStringNoCopy(value, len);
171  }
172  return str;
173  }
174 
176  inline bool isCString() const {
177  return (m_type == GF_STRING || m_type == GF_STRING_HUGE);
178  }
179 
181  inline bool isWideString() const {
182  return (m_type == GF_WIDESTRING || m_type == GF_WIDESTRING_HUGE);
183  }
184 
194  const char* asChar() const {
195  if (isWideString()) {
196  throw IllegalStateException(
197  "CacheableString::asChar: the string is a "
198  "wide character string; use asWChar() to obtain it.");
199  }
200  return reinterpret_cast<const char*>(m_str);
201  }
202 
212  const wchar_t* asWChar() const {
213  if (isCString()) {
214  throw IllegalStateException(
215  "CacheableString::asWChar: the string is "
216  "not a wide character string; use asChar() to obtain it.");
217  }
218  return reinterpret_cast<const wchar_t*>(m_str);
219  }
220 
222  inline uint32_t length() const { return m_len; }
223 
235  const char* toString() { return reinterpret_cast<const char*>(m_str); }
236 
237  virtual CacheableStringPtr toString() const {
238  return CacheableStringPtr(this);
239  }
240 
242  virtual const char* className() const { return "CacheableString"; }
243 
245  virtual ~CacheableString();
246 
248  virtual int32_t logString(char* buffer, int32_t maxLength) const;
249 
250  virtual uint32_t objectSize() const;
251 
252  protected:
254  void copyString(const char* value, int32_t len);
256  void copyString(const wchar_t* value, int32_t len);
258  void initString(const char* value, int32_t len);
263  void initStringNoCopy(char* value, int32_t len);
265  void initString(const wchar_t* value, int32_t len);
270  void initStringNoCopy(wchar_t* value, int32_t len);
272  char* getASCIIString(const wchar_t* value, int32_t& len, int32_t& encodedLen);
274  inline CacheableString(int8_t type = GF_STRING)
275  : m_str(NULL), m_type(type), m_len(0), m_hashcode(0) {}
276 
277  private:
278  // never implemented.
279  void operator=(const CacheableString& other);
280  CacheableString(const CacheableString& other);
281 };
282 
284 inline CacheableKeyPtr createKeyArr(const char* value) {
285  return (value != NULL ? CacheableKeyPtr(CacheableString::create(value).ptr())
286  : NULLPTR);
287 }
288 
290 inline CacheableKeyPtr createKeyArr(const wchar_t* value) {
291  return (value != NULL ? CacheableKeyPtr(CacheableString::create(value).ptr())
292  : NULLPTR);
293 }
294 
296 inline CacheablePtr createValueArr(const char* value) {
297  return (value != NULL ? CacheablePtr(CacheableString::create(value).ptr())
298  : NULLPTR);
299 }
300 
302 inline CacheablePtr createValueArr(const wchar_t* value) {
303  return (value != NULL ? CacheablePtr(CacheableString::create(value).ptr())
304  : NULLPTR);
305 }
306 } // namespace client
307 } // namespace geode
308 } // namespace apache
309 
310 #endif // GEODE_CACHEABLESTRING_H_
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:166
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:147
bool isCString() const
Returns true if the underlying string is a normal C string.
Definition: CacheableString.hpp:176
CacheablePtr createValueArr(const char *value)
overload of apache::geode::client::createValueArr to pass char*
Definition: CacheableString.hpp:296
Each enum represents a predefined RegionAttributes in a Cache.
Definition: Assert.hpp:31
virtual const char * className() const
get the name of the class of this object for logging purpose
Definition: CacheableString.hpp:242
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:113
#define CPPCACHE_EXPORT
Defines a Geode CPPCACHE export.
Definition: geode_base.hpp:58
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:132
const char * toString()
Display this object as c string.
Definition: CacheableString.hpp:235
const char * asChar() const
Return the string that backs this CacheableString as a char *.
Definition: CacheableString.hpp:194
const wchar_t * asWChar() const
Return the string that backs this CacheableString as a wchar_t *.
Definition: CacheableString.hpp:212
Provide operations for writing primitive data values, byte arrays, strings, Serializable objects to a...
Definition: DataOutput.hpp:74
CacheableString(int8_t type=(int8_t) GeodeTypeIds::CacheableASCIIString)
Default constructor.
Definition: CacheableString.hpp:274
Implement a immutable C string wrapper that can serve as a distributable key object for caching as we...
Definition: CacheableString.hpp:45
bool isWideString() const
Returns true if the underlying string is a wide-character string.
Definition: CacheableString.hpp:181
void initStringNoCopy(char *value, int32_t len)
Initialize the string without making a copy, given a C string and length.
virtual CacheableStringPtr toString() const
Display this object as &#39;string&#39;, which depends on the implementation in the subclasses.
Definition: CacheableString.hpp:237
This abstract base class is the superclass of all user objects in the cache that can be serialized...
Definition: Serializable.hpp:53
Provide operations for reading primitive data values, byte arrays, strings, Serializable objects from...
Definition: DataInput.hpp:56
Represents a cacheable key.
Definition: CacheableKey.hpp:36
uint32_t length() const
Return the length of the contained string.
Definition: CacheableString.hpp:222
CacheableKeyPtr createKeyArr(const char *value)
overload of apache::geode::client::createKeyArr to pass char*
Definition: CacheableString.hpp:284
This namespace contains all the Geode C++ API classes, enumerations and globals.
void initString(const char *value, int32_t len)
initialize the string, given a value and length.

Pivotal GemFire C++ Cache API Documentation