Pivotal GemFire Native C++ Reference  10.1.3
CacheableString.hpp
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #pragma once
19 
20 #ifndef GEODE_CACHEABLESTRING_H_
21 #define GEODE_CACHEABLESTRING_H_
22 
23 #include "CacheableKey.hpp"
24 #include "internal/DSCode.hpp"
25 #include "internal/DataSerializablePrimitive.hpp"
26 #include "internal/geode_globals.hpp"
27 
31 namespace apache {
32 namespace geode {
33 namespace client {
34 
35 using internal::DSCode;
36 
41 class APACHE_GEODE_EXPORT CacheableString
42  : public internal::DataSerializablePrimitive,
43  public CacheableKey {
44  protected:
45  std::string m_str;
46  DSCode m_type;
47  mutable int m_hashcode;
48 
49  public:
50  inline explicit CacheableString(DSCode type = DSCode::CacheableASCIIString)
51  : m_str(), m_type(type), m_hashcode(0) {}
52 
53  inline explicit CacheableString(const std::string& value)
54  : CacheableString(std::string(value)) {}
55 
56  inline explicit CacheableString(std::string&& value)
57  : m_str(std::move(value)), m_hashcode(0) {
58  bool ascii = isAscii(m_str);
59 
60  m_type =
61  m_str.length() > std::numeric_limits<uint16_t>::max()
62  ? ascii ? DSCode::CacheableASCIIStringHuge
63  : DSCode::CacheableStringHuge
64  : ascii ? DSCode::CacheableASCIIString : DSCode::CacheableString;
65  }
66 
67  ~CacheableString() noexcept override = default;
68 
69  void operator=(const CacheableString& other) = delete;
70  CacheableString(const CacheableString& other) = delete;
71 
72  void toData(DataOutput& output) const override;
73 
74  void fromData(DataInput& input) override;
75 
76  DSCode getDsCode() const override { return m_type; }
77 
79  static std::shared_ptr<Serializable> createDeserializable();
80 
82  static std::shared_ptr<Serializable> createDeserializableHuge();
83 
85  static std::shared_ptr<Serializable> createUTFDeserializable();
86 
88  static std::shared_ptr<Serializable> createUTFDeserializableHuge();
89 
91  virtual bool operator==(const CacheableKey& other) const override;
92 
94  virtual int32_t hashcode() const override;
95 
96  inline static std::shared_ptr<CacheableString> create(
97  const std::string& value) {
98  return std::make_shared<CacheableString>(value);
99  }
100 
101  inline static std::shared_ptr<CacheableString> create(std::string&& value) {
102  return std::make_shared<CacheableString>(std::move(value));
103  }
104 
105  static std::shared_ptr<CacheableString> create(const std::u16string& value);
106 
107  static std::shared_ptr<CacheableString> create(std::u16string&& value);
108 
109  static std::shared_ptr<CacheableString> create(const std::u32string& value);
110 
111  static std::shared_ptr<CacheableString> create(std::u32string&& value);
112 
113  inline static std::shared_ptr<CacheableString> create(
114  const std::wstring& value) {
115  return std::make_shared<CacheableString>(
116  std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t>{}
117  .to_bytes(value));
118  }
119 
120  inline static std::shared_ptr<CacheableString> create(std::wstring&& value) {
121  return std::make_shared<CacheableString>(
122  std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t>{}
123  .to_bytes(std::move(value)));
124  }
125 
127  inline std::string::size_type length() const { return m_str.length(); }
128 
129  inline const std::string& value() const { return m_str; }
130 
131  virtual std::string toString() const override;
132 
133  virtual size_t objectSize() const override;
134 
135  private:
136  static bool isAscii(const std::string& str);
137 };
138 
139 template <>
140 inline std::shared_ptr<CacheableKey> CacheableKey::create(std::string value) {
141  return CacheableString::create(value);
142 }
143 
144 template <>
145 inline std::shared_ptr<CacheableKey> CacheableKey::create(
146  std::u16string value) {
147  return CacheableString::create(value);
148 }
149 
150 template <>
151 inline std::shared_ptr<CacheableKey> CacheableKey::create(
152  std::u32string value) {
153  return CacheableString::create(value);
154 }
155 
156 template <>
157 inline std::shared_ptr<CacheableKey> CacheableKey::create(std::wstring value) {
158  return CacheableString::create(value);
159 }
160 
161 template <>
162 inline std::shared_ptr<Cacheable> Serializable::create(std::string value) {
163  return CacheableString::create(value);
164 }
165 
166 template <>
167 inline std::shared_ptr<Cacheable> Serializable::create(std::u16string value) {
168  return CacheableString::create(value);
169 }
170 
171 template <>
172 inline std::shared_ptr<Cacheable> Serializable::create(std::u32string value) {
173  return CacheableString::create(value);
174 }
175 
176 template <>
177 inline std::shared_ptr<Cacheable> Serializable::create(std::wstring value) {
178  return CacheableString::create(value);
179 }
180 
181 template <>
182 inline std::shared_ptr<CacheableKey> CacheableKey::create(const char* value) {
183  return CacheableString::create(value);
184 }
185 
186 template <>
187 inline std::shared_ptr<Cacheable> Serializable::create(const char* value) {
188  return CacheableString::create(value);
189 }
190 
191 template <>
192 inline std::shared_ptr<CacheableKey> CacheableKey::create(char* value) {
193  return CacheableString::create(value);
194 }
195 
196 template <>
197 inline std::shared_ptr<Cacheable> Serializable::create(char* value) {
198  return CacheableString::create(value);
199 }
200 
201 template <>
202 inline std::shared_ptr<CacheableKey> CacheableKey::create(
203  const char16_t* value) {
204  return CacheableString::create(value);
205 }
206 
207 template <>
208 inline std::shared_ptr<Cacheable> Serializable::create(const char16_t* value) {
209  return CacheableString::create(value);
210 }
211 
212 template <>
213 inline std::shared_ptr<CacheableKey> CacheableKey::create(char16_t* value) {
214  return CacheableString::create(value);
215 }
216 
217 template <>
218 inline std::shared_ptr<Cacheable> Serializable::create(char16_t* value) {
219  return CacheableString::create(value);
220 }
221 
222 template <>
223 inline std::shared_ptr<CacheableKey> CacheableKey::create(
224  const char32_t* value) {
225  return CacheableString::create(value);
226 }
227 
228 template <>
229 inline std::shared_ptr<Cacheable> Serializable::create(const char32_t* value) {
230  return CacheableString::create(value);
231 }
232 template <>
233 inline std::shared_ptr<CacheableKey> CacheableKey::create(char32_t* value) {
234  return CacheableString::create(value);
235 }
236 
237 template <>
238 inline std::shared_ptr<Cacheable> Serializable::create(char32_t* value) {
239  return CacheableString::create(value);
240 }
241 
242 template <>
243 inline std::shared_ptr<CacheableKey> CacheableKey::create(
244  const wchar_t* value) {
245  return CacheableString::create(value);
246 }
247 
248 template <>
249 inline std::shared_ptr<Cacheable> Serializable::create(const wchar_t* value) {
250  return CacheableString::create(value);
251 }
252 
253 template <>
254 inline std::shared_ptr<CacheableKey> CacheableKey::create(wchar_t* value) {
255  return CacheableString::create(value);
256 }
257 
258 template <>
259 inline std::shared_ptr<Cacheable> Serializable::create(wchar_t* value) {
260  return CacheableString::create(value);
261 }
262 
263 } // namespace client
264 } // namespace geode
265 } // namespace apache
266 
267 namespace std {
268 
269 template <>
270 struct hash<apache::geode::client::CacheableString>
271  : hash<apache::geode::client::CacheableKey> {};
272 
273 } // namespace std
274 
275 #endif // GEODE_CACHEABLESTRING_H_
apache::geode::client::CacheableString
Implement a immutable C string wrapper that can serve as a distributable key object for caching as we...
Definition: CacheableString.hpp:41
CacheableKey.hpp
apache::geode::client::DataOutput
Provide operations for writing primitive data values, byte arrays, strings, Serializable objects to a...
Definition: DataOutput.hpp:48
apache::geode::client::CacheableKey
Represents a cacheable key.
Definition: CacheableKey.hpp:40
apache::geode::client::CacheableKey::create
static std::shared_ptr< CacheableKey > create(_T value)
Factory method that creates the key type that matches the type of value.
apache::geode::client::Serializable::create
static std::shared_ptr< Serializable > create(_T value)
Factory method that creates the Serializable object that matches the type of value.
apache::geode::client::DataInput
Provide operations for reading primitive data values, byte arrays, strings, Serializable objects from...
Definition: DataInput.hpp:59
apache::geode::client::CacheableString::length
std::string::size_type length() const
Return the length of the contained string.
Definition: CacheableString.hpp:127

Apache Geode C++ Cache API Documentation