Apache Geode Native C++ Reference 1.15.0
CacheableString.hpp
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
28namespace apache {
29namespace geode {
30namespace client {
31
32using internal::DSCode;
33
38class APACHE_GEODE_EXPORT CacheableString
39 : public internal::DataSerializablePrimitive,
40 public CacheableKey {
41 protected:
42 std::string m_str;
43 DSCode m_type;
44 mutable int m_hashcode;
45
46 public:
47 inline explicit CacheableString(DSCode type = DSCode::CacheableASCIIString)
48 : m_str(), m_type(type), m_hashcode(0) {}
49
50 inline explicit CacheableString(const std::string& value)
51 : CacheableString(std::string(value)) {}
52
53 inline explicit CacheableString(std::string&& value)
54 : m_str(std::move(value)), m_hashcode(0) {
55 bool ascii = isAscii(m_str);
56
57 m_type = m_str.length() > (std::numeric_limits<uint16_t>::max)()
58 ? ascii ? DSCode::CacheableASCIIStringHuge
59 : DSCode::CacheableStringHuge
60 : ascii ? DSCode::CacheableASCIIString
61 : DSCode::CacheableString;
62 }
63
64 ~CacheableString() noexcept override = default;
65
66 void operator=(const CacheableString& other) = delete;
67 CacheableString(const CacheableString& other) = delete;
68
69 void toData(DataOutput& output) const override;
70
71 void fromData(DataInput& input) override;
72
73 DSCode getDsCode() const override { return m_type; }
74
76 static std::shared_ptr<Serializable> createDeserializable();
77
79 static std::shared_ptr<Serializable> createDeserializableHuge();
80
82 static std::shared_ptr<Serializable> createUTFDeserializable();
83
85 static std::shared_ptr<Serializable> createUTFDeserializableHuge();
86
88 virtual bool operator==(const CacheableKey& other) const override;
89
91 virtual int32_t hashcode() const override;
92
93 inline static std::shared_ptr<CacheableString> create(
94 const std::string& value) {
95 return std::make_shared<CacheableString>(value);
96 }
97
98 inline static std::shared_ptr<CacheableString> create(std::string&& value) {
99 return std::make_shared<CacheableString>(std::move(value));
100 }
101
102 static std::shared_ptr<CacheableString> create(const std::u16string& value);
103
104 static std::shared_ptr<CacheableString> create(std::u16string&& value);
105
106 static std::shared_ptr<CacheableString> create(const std::u32string& value);
107
108 static std::shared_ptr<CacheableString> create(std::u32string&& value);
109
110 inline static std::shared_ptr<CacheableString> create(
111 const std::wstring& value) {
112 return std::make_shared<CacheableString>(
113 std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t>{}
114 .to_bytes(value));
115 }
116
117 inline static std::shared_ptr<CacheableString> create(std::wstring&& value) {
118 return std::make_shared<CacheableString>(
119 std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t>{}
120 .to_bytes(std::move(value)));
121 }
122
124 inline std::string::size_type length() const { return m_str.length(); }
125
126 inline const std::string& value() const { return m_str; }
127
128 virtual std::string toString() const override;
129
130 virtual size_t objectSize() const override;
131
132 private:
133 static bool isAscii(const std::string& str);
134};
135
136template <>
137inline std::shared_ptr<CacheableKey> CacheableKey::create(std::string value) {
138 return CacheableString::create(value);
139}
140
141template <>
142inline std::shared_ptr<CacheableKey> CacheableKey::create(
143 std::u16string value) {
144 return CacheableString::create(value);
145}
146
147template <>
148inline std::shared_ptr<CacheableKey> CacheableKey::create(
149 std::u32string value) {
150 return CacheableString::create(value);
151}
152
153template <>
154inline std::shared_ptr<CacheableKey> CacheableKey::create(std::wstring value) {
155 return CacheableString::create(value);
156}
157
158template <>
159inline std::shared_ptr<Cacheable> Serializable::create(std::string value) {
160 return CacheableString::create(value);
161}
162
163template <>
164inline std::shared_ptr<Cacheable> Serializable::create(std::u16string value) {
165 return CacheableString::create(value);
166}
167
168template <>
169inline std::shared_ptr<Cacheable> Serializable::create(std::u32string value) {
170 return CacheableString::create(value);
171}
172
173template <>
174inline std::shared_ptr<Cacheable> Serializable::create(std::wstring value) {
175 return CacheableString::create(value);
176}
177
178template <>
179inline std::shared_ptr<CacheableKey> CacheableKey::create(const char* value) {
180 return CacheableString::create(value);
181}
182
183template <>
184inline std::shared_ptr<Cacheable> Serializable::create(const char* value) {
185 return CacheableString::create(value);
186}
187
188template <>
189inline std::shared_ptr<CacheableKey> CacheableKey::create(char* value) {
190 return CacheableString::create(value);
191}
192
193template <>
194inline std::shared_ptr<Cacheable> Serializable::create(char* value) {
195 return CacheableString::create(value);
196}
197
198template <>
199inline std::shared_ptr<CacheableKey> CacheableKey::create(
200 const char16_t* value) {
201 return CacheableString::create(value);
202}
203
204template <>
205inline std::shared_ptr<Cacheable> Serializable::create(const char16_t* value) {
206 return CacheableString::create(value);
207}
208
209template <>
210inline std::shared_ptr<CacheableKey> CacheableKey::create(char16_t* value) {
211 return CacheableString::create(value);
212}
213
214template <>
215inline std::shared_ptr<Cacheable> Serializable::create(char16_t* value) {
216 return CacheableString::create(value);
217}
218
219template <>
220inline std::shared_ptr<CacheableKey> CacheableKey::create(
221 const char32_t* value) {
222 return CacheableString::create(value);
223}
224
225template <>
226inline std::shared_ptr<Cacheable> Serializable::create(const char32_t* value) {
227 return CacheableString::create(value);
228}
229template <>
230inline std::shared_ptr<CacheableKey> CacheableKey::create(char32_t* value) {
231 return CacheableString::create(value);
232}
233
234template <>
235inline std::shared_ptr<Cacheable> Serializable::create(char32_t* value) {
236 return CacheableString::create(value);
237}
238
239template <>
240inline std::shared_ptr<CacheableKey> CacheableKey::create(
241 const wchar_t* value) {
242 return CacheableString::create(value);
243}
244
245template <>
246inline std::shared_ptr<Cacheable> Serializable::create(const wchar_t* value) {
247 return CacheableString::create(value);
248}
249
250template <>
251inline std::shared_ptr<CacheableKey> CacheableKey::create(wchar_t* value) {
252 return CacheableString::create(value);
253}
254
255template <>
256inline std::shared_ptr<Cacheable> Serializable::create(wchar_t* value) {
257 return CacheableString::create(value);
258}
259
260} // namespace client
261} // namespace geode
262} // namespace apache
263
264namespace std {
265
266template <>
267struct hash<apache::geode::client::CacheableString>
268 : hash<apache::geode::client::CacheableKey> {};
269
270} // namespace std
271
272#endif // GEODE_CACHEABLESTRING_H_
Represents a cacheable key.
Definition: CacheableKey.hpp:40
static std::shared_ptr< CacheableKey > create(_T value)
Factory method that creates the key type that matches the type of value.
Implement a immutable C string wrapper that can serve as a distributable key object for caching as we...
Definition: CacheableString.hpp:40
virtual std::string toString() const override
Display this object as 'string', which depends on the implementation in the subclasses.
static std::shared_ptr< Serializable > createUTFDeserializable()
creation function for wide strings
virtual int32_t hashcode() const override
return the hashcode for this key.
static std::shared_ptr< Serializable > createUTFDeserializableHuge()
creation function for wide strings > 64K length in UTF8 encoding
static std::shared_ptr< Serializable > createDeserializableHuge()
creation function for strings > 64K length
std::string::size_type length() const
Return the length of the contained string.
Definition: CacheableString.hpp:124
static std::shared_ptr< Serializable > createDeserializable()
creation function for strings
virtual bool operator==(const CacheableKey &other) const override
return true if this key matches other.
virtual size_t objectSize() const override
return the size in bytes of the instance being serialized.
Provide operations for reading primitive data values, byte arrays, strings, Serializable objects from...
Definition: DataInput.hpp:59
Provide operations for writing primitive data values, byte arrays, strings, Serializable objects to a...
Definition: DataOutput.hpp:48
static std::shared_ptr< Serializable > create(_T value)
Factory method that creates the Serializable object that matches the type of value.

Apache Geode C++ Cache API Documentation