Switchboard SDK
Loading...
Searching...
No Matches
SBAny.hpp
1//
2// SBAny.hpp
3// SwitchboardSDK
4//
5// Copyright © 2026 Synervoz. All rights reserved.
6//
7
8#pragma once
9
10#include "switchboard/export.h"
11
12#include <any>
13#include <cstdint>
14#include <functional>
15#include <initializer_list>
16#include <map>
17#include <optional>
18#include <string>
19#include <utility>
20#include <variant>
21#include <vector>
22
23namespace switchboard {
24
25template <typename T>
26class Result;
27
28class SBAnyMap;
29class SBAnyVector;
30
32enum class SBAnyType : uint32_t {
33 Null,
34 Bool,
35 Int,
36 Float,
37 Double,
38 String,
39 Vector,
40 Map,
41 Function,
42 Pointer
43};
44
52class SWITCHBOARDSDK_EXPORT SBAny final {
53public:
54 // Default constructor - Null type
55 SBAny();
56 SBAny(std::monostate);
57
58 // Boolean type
59 SBAny(bool);
60
61 // Integer types
62 SBAny(int32_t);
63 SBAny(int64_t);
64 SBAny(uint32_t);
65 SBAny(uint64_t);
66
67 // SFINAE to avoid ambiguity with int32_t and int64_t
68 static constexpr bool int_is_distinct = !std::is_same<int, int32_t>::value && !std::is_same<int, int64_t>::value;
69 template <bool Enable = int_is_distinct, typename std::enable_if<Enable, int>::type = 0>
70 SBAny(int value) : SBAny(static_cast<int64_t>(value)) {}
71
72 static constexpr bool uint_is_distinct =
73 !std::is_same<unsigned int, uint32_t>::value && !std::is_same<unsigned int, uint64_t>::value;
74 template <bool Enable = uint_is_distinct, typename std::enable_if<Enable, int>::type = 0>
75 SBAny(unsigned int value) : SBAny(static_cast<uint64_t>(value)) {}
76
77 // Floating point types
78 SBAny(float);
79 SBAny(double);
80
81 // String types
82 SBAny(const char* utf8);
83 SBAny(const std::string& str);
84
85 // Map type
86 SBAny(std::initializer_list<std::pair<std::string, SBAny>> init);
87 SBAny(const SBAnyMap& init);
88
89 // Vector type
90 // SBAny(std::vector<SBAny> init);
91 SBAny(const SBAnyVector& init);
92 explicit SBAny(std::initializer_list<SBAny> init);
93
94 // Function wrapper constructors
95 SBAny(std::function<Result<SBAny>()> init); // Property Getter
96 SBAny(std::function<SBAny()> init); // Property Getter (no failure)
97 SBAny(std::function<Result<void>(const SBAny&)> init); // Property Setter
98 SBAny(std::function<void(const SBAny&)> init); // Property Setter (no failure)
99 SBAny(std::function<Result<SBAny>(const SBAnyMap&)> init); // Action
100
101 // Pointers
102 SBAny(float* init);
103
104 // Copy constructor and assignment
105 SBAny(const SBAny&);
106 SBAny& operator=(const SBAny&);
107
108 ~SBAny();
109
115 SBAnyType type() const noexcept;
116
122 std::any toAny() const;
123
129 void* toPtr() const;
130
139 template <typename T>
140 static T convert(const SBAny& value);
141
142private:
143 struct Impl;
144 Impl* pimpl;
145};
146
150class SWITCHBOARDSDK_EXPORT SBAnyVector final {
151public:
156
163
172
179 template <typename Iterator>
180 SBAnyVector(Iterator first, Iterator last);
181
190 template <typename T>
191 explicit SBAnyVector(const std::vector<T>& vec) : SBAnyVector() {
192 reserve(vec.size());
193 for (const auto& item : vec) {
194 emplace_back(SBAny(item));
195 }
196 }
197
203 explicit SBAnyVector(std::initializer_list<SBAny> init);
204
209
215 void reserve(size_t newCapacity);
216
222 [[nodiscard]] size_t size() const;
223
230 void emplace_back(const SBAny& value);
231
238 void push_back(const SBAny& value);
239
247 SBAny& operator[](size_t index);
248
256 const SBAny& operator[](size_t index) const;
257
258 // Iterators
259 using iterator = std::vector<SBAny>::iterator;
260 using const_iterator = std::vector<SBAny>::const_iterator;
261
262 iterator begin();
263 iterator end();
264 const_iterator begin() const;
265 const_iterator end() const;
266
267private:
268 struct Impl;
269 Impl* pimpl;
270};
271
275class SWITCHBOARDSDK_EXPORT SBAnyMap {
276public:
281
286 SBAnyMap(const std::string& jsonString);
287
293 SBAnyMap(const SBAnyMap& other);
294
303
309 SBAnyMap(std::initializer_list<std::pair<std::string, SBAny>> init);
310
315
323 [[nodiscard]] bool hasKey(const std::string& key) const;
324
330 [[nodiscard]] bool empty() const;
331
337 [[nodiscard]] size_t size() const;
338
346 [[nodiscard]] SBAny getAny(const std::string& key) const;
347
355 [[nodiscard]] SBAny at(const std::string& key) const;
356
365 template <typename T>
366 [[nodiscard]] T get(const std::string& key) const;
367
377 template <typename T>
378 [[nodiscard]] T get(const std::string& key, std::optional<T> defaultValue) const;
379
386 void set(const std::string& key, const SBAny& value);
387
393 void erase(const std::string& key);
394
407 template <typename T>
408 static T get(const SBAnyMap& map, const std::string& key, std::optional<T> defaultValue = std::nullopt);
409
417 static SBAnyMap jsonToMap(const std::string& jsonString);
418
426 static std::string mapToJson(const SBAnyMap& map);
427
428 // Iterators
429 using iterator = std::map<std::string, SBAny>::iterator;
430 using const_iterator = std::map<std::string, SBAny>::const_iterator;
431
432 iterator begin();
433 iterator end();
434 const_iterator begin() const;
435 const_iterator end() const;
436
437 // Operator overloads
438 SBAny& operator[](const std::string& key);
439 const SBAny& operator[](const std::string& key) const;
440
441private:
442 struct Impl;
443 Impl* pimpl;
444};
445
446}
Represents the result of an operation that can either succeed or fail.
Definition Result.hpp:39
A versatile container class that can hold values of various types.
Definition SBAny.hpp:52
void * toPtr() const
SBAnyType type() const noexcept
std::any toAny() const
static T convert(const SBAny &value)
Map class to store and retrieve values.
Definition SBAny.hpp:275
bool hasKey(const std::string &key) const
static T get(const SBAnyMap &map, const std::string &key, std::optional< T > defaultValue=std::nullopt)
SBAny getAny(const std::string &key) const
SBAnyMap(const std::string &jsonString)
size_t size() const
SBAnyMap(const SBAnyMap &other)
void set(const std::string &key, const SBAny &value)
void erase(const std::string &key)
SBAny at(const std::string &key) const
T get(const std::string &key) const
SBAnyMap(std::initializer_list< std::pair< std::string, SBAny > > init)
T get(const std::string &key, std::optional< T > defaultValue) const
SBAnyMap & operator=(const SBAnyMap &other)
static std::string mapToJson(const SBAnyMap &map)
static SBAnyMap jsonToMap(const std::string &jsonString)
Vector of SBAny values, used for representing lists or arrays of values.
Definition SBAny.hpp:150
SBAnyVector(const SBAnyVector &init)
Copy constructor for SBAnyVector.
void push_back(const SBAny &value)
Adds a new SBAny value to the end of the vector.
void emplace_back(const SBAny &value)
Adds a new SBAny value to the end of the vector.
const SBAny & operator[](size_t index) const
Accesses an element in the vector by index (const version).
SBAnyVector(const std::vector< T > &vec)
Constructor for SBAnyVector from a std::vector of arbitrary type T.
Definition SBAny.hpp:191
SBAnyVector()
Default constructor for SBAnyVector, initializes an empty vector.
size_t size() const
Gets the number of elements in the vector.
SBAny & operator[](size_t index)
Accesses an element in the vector by index.
~SBAnyVector()
Destructor for SBAnyVector, cleans up any resources used by the vector.
void reserve(size_t newCapacity)
Reserves space in the vector for a specified number of elements.
SBAnyVector(Iterator first, Iterator last)
Constructor for SBAnyVector from a range of iterators.
SBAnyVector(std::initializer_list< SBAny > init)
Constructor for SBAnyVector from an initializer list of SBAny values.
SBAnyVector & operator=(const SBAnyVector &other)
Assignment operator for SBAnyVector.