Apache Ignite C++ Client
Loading...
Searching...
No Matches
primitive.h
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#include "big_decimal.h"
21#include "big_integer.h"
22#include "ignite_date.h"
23#include "ignite_date_time.h"
24#include "ignite_duration.h"
25#include "ignite_error.h"
26#include "ignite_period.h"
27#include "ignite_time.h"
28#include "ignite_timestamp.h"
29#include "ignite_type.h"
30#include "uuid.h"
31
32#include <cstdint>
33#include <optional>
34#include <type_traits>
35#include <variant>
36#include <vector>
37
38namespace ignite {
39
43class primitive {
44public:
45 // Default
46 primitive() = default;
47
51 primitive(std::nullptr_t) {} // NOLINT(google-explicit-constructor)
52
56 primitive(std::nullopt_t) {} // NOLINT(google-explicit-constructor)
57
63 primitive(bool value) // NOLINT(google-explicit-constructor)
64 : m_value(value) {}
65
71 primitive(std::int8_t value) // NOLINT(google-explicit-constructor)
72 : m_value(value) {}
73
79 primitive(std::int16_t value) // NOLINT(google-explicit-constructor)
80 : m_value(value) {}
81
87 primitive(std::int32_t value) // NOLINT(google-explicit-constructor)
88 : m_value(value) {}
89
95 primitive(std::int64_t value) // NOLINT(google-explicit-constructor)
96 : m_value(value) {}
97
103 primitive(float value) // NOLINT(google-explicit-constructor)
104 : m_value(value) {}
105
111 primitive(double value) // NOLINT(google-explicit-constructor)
112 : m_value(value) {}
113
119 primitive(uuid value) // NOLINT(google-explicit-constructor)
120 : m_value(value) {}
121
127 primitive(std::string value) // NOLINT(google-explicit-constructor)
128 : m_value(std::move(value)) {}
129
135 primitive(std::string_view value) // NOLINT(google-explicit-constructor)
136 : m_value(std::string(value)) {}
137
143 primitive(const char *value) // NOLINT(google-explicit-constructor)
144 : m_value(std::string(value)) {}
145
151 primitive(std::vector<std::byte> value) // NOLINT(google-explicit-constructor)
152 : m_value(std::move(value)) {}
153
160 primitive(std::byte *buf, std::size_t len)
161 : m_value(std::vector<std::byte>(buf, buf + len)) {}
162
168 primitive(big_decimal value) // NOLINT(google-explicit-constructor)
169 : m_value(std::move(value)) {}
170
176 primitive(ignite_date value) // NOLINT(google-explicit-constructor)
177 : m_value(value) {}
178
184 primitive(ignite_date_time value) // NOLINT(google-explicit-constructor)
185 : m_value(value) {}
186
192 primitive(ignite_time value) // NOLINT(google-explicit-constructor)
193 : m_value(value) {}
194
200 primitive(ignite_timestamp value) // NOLINT(google-explicit-constructor)
201 : m_value(value) {}
202
208 primitive(ignite_period value) // NOLINT(google-explicit-constructor)
209 : m_value(value) {}
210
216 primitive(ignite_duration value) // NOLINT(google-explicit-constructor)
217 : m_value(value) {}
218
226 template<typename T>
227 [[nodiscard]] const T &get() const {
228 if constexpr (std::is_same_v<T, bool> // Bool
229 || std::is_same_v<T, std::int8_t> // Int8
230 || std::is_same_v<T, std::int16_t> // Int16
231 || std::is_same_v<T, std::int32_t> // Int32
232 || std::is_same_v<T, std::int64_t> // Int64
233 || std::is_same_v<T, float> // Float
234 || std::is_same_v<T, double> // Double
235 || std::is_same_v<T, uuid> // Uuid
236 || std::is_same_v<T, std::string> // String
237 || std::is_same_v<T, std::vector<std::byte>> // Bytes
238 || std::is_same_v<T, big_decimal> // Decimal
239 || std::is_same_v<T, ignite_date> // Date
240 || std::is_same_v<T, ignite_date_time> // DateTime
241 || std::is_same_v<T, ignite_time> // Time
242 || std::is_same_v<T, ignite_timestamp> // Timestamp
243 || std::is_same_v<T, ignite_period> // Period
244 || std::is_same_v<T, ignite_duration> // Duration
245 ) {
246 return std::get<T>(m_value);
247 } else {
248 static_assert(sizeof(T) == 0, "Type is not an Ignite primitive type or is not yet supported");
249 }
250 }
251
257 [[nodiscard]] bool is_null() const noexcept { return m_value.index() == 0; }
258
264 [[nodiscard]] ignite_type get_type() const noexcept {
265 auto type_idx = m_value.index();
266 type_idx = type_idx <= 13 ? type_idx : type_idx + 1;
267 return static_cast<ignite_type>(type_idx);
268 }
269
277 friend constexpr bool operator==(const primitive &lhs, const primitive &rhs) noexcept {
278 return lhs.m_value == rhs.m_value;
279 }
280
288 friend constexpr bool operator!=(const primitive &lhs, const primitive &rhs) noexcept {
289 return lhs.m_value != rhs.m_value;
290 }
291
292private:
294 typedef std::variant<std::nullptr_t, // Null = 0
295 bool, // Bool = 1
296 std::int8_t, // Int8 = 2
297 std::int16_t, // Int16 = 3
298 std::int32_t, // Int32 = 4
299 std::int64_t, // Int64 = 5
300 float, // Float = 6
301 double, // Double = 7
302 big_decimal, // Decimal = 8
303 ignite_date, // Date = 9
304 ignite_time, // Time = 10
305 ignite_date_time, // Datetime = 11
306 ignite_timestamp, // Timestamp = 12
307 uuid, // UUID = 13
308 std::string, // String = 15
309 std::vector<std::byte>, // Bytes = 16
310 ignite_period, // Period = 17
311 ignite_duration // Duration = 18
312 >
313 value_type;
314
316 value_type m_value;
317};
318
319} // namespace ignite
Definition big_decimal.h:35
A date together with time of day with nanosecond precision.
Definition ignite_date_time.h:32
A date.
Definition ignite_date.h:29
A time-based amount of time.
Definition ignite_duration.h:29
A date-based amount of time.
Definition ignite_period.h:29
A time of day with nanosecond precision.
Definition ignite_time.h:29
A moment of time.
Definition ignite_timestamp.h:29
primitive(std::int8_t value)
Definition primitive.h:71
bool is_null() const noexcept
Definition primitive.h:257
primitive(std::string value)
Definition primitive.h:127
primitive(uuid value)
Definition primitive.h:119
primitive(std::int64_t value)
Definition primitive.h:95
ignite_type get_type() const noexcept
Definition primitive.h:264
primitive(ignite_timestamp value)
Definition primitive.h:200
const T & get() const
Definition primitive.h:227
primitive(ignite_date_time value)
Definition primitive.h:184
primitive(std::int32_t value)
Definition primitive.h:87
primitive(big_decimal value)
Definition primitive.h:168
primitive(std::nullopt_t)
Definition primitive.h:56
primitive(ignite_period value)
Definition primitive.h:208
primitive(double value)
Definition primitive.h:111
primitive(ignite_date value)
Definition primitive.h:176
friend constexpr bool operator==(const primitive &lhs, const primitive &rhs) noexcept
Comparison operator.
Definition primitive.h:277
primitive(std::nullptr_t)
Definition primitive.h:51
primitive(std::string_view value)
Definition primitive.h:135
primitive(std::byte *buf, std::size_t len)
Definition primitive.h:160
primitive(const char *value)
Definition primitive.h:143
primitive(ignite_duration value)
Definition primitive.h:216
primitive(float value)
Definition primitive.h:103
primitive(bool value)
Definition primitive.h:63
primitive(ignite_time value)
Definition primitive.h:192
friend constexpr bool operator!=(const primitive &lhs, const primitive &rhs) noexcept
Comparison operator.
Definition primitive.h:288
primitive(std::vector< std::byte > value)
Definition primitive.h:151
primitive(std::int16_t value)
Definition primitive.h:79
Universally unique identifier (UUID).
Definition uuid.h:32