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#include <initializer_list>
12#include <utility>
13#include <string>
14#include <any>
15#include <map>
16#include <optional>
17#include <string>
18#include <variant>
19#include <vector>
20#include <functional>
21#include <cstdint>
22
23
24namespace switchboard {
25
26template <typename T>
27class Result;
28
29class SBAnyMap;
30class SBAnyVector;
31
33enum class SBAnyType : uint32_t {
34 Null,
35 Bool,
36 Int,
37 Float,
38 Double,
39 String,
40 Vector,
41 Map,
42 Function,
43 Pointer
44};
45
53class SWITCHBOARDSDK_EXPORT SBAny final {
54public:
55 // Default constructor - Null type
56 SBAny();
57 SBAny(std::monostate);
58
59 // Boolean type
60 SBAny(bool);
61
62 // Integer types
63 SBAny(int32_t);
64 SBAny(int64_t);
65 SBAny(uint32_t);
66 SBAny(uint64_t);
67
68 // SFINAE to avoid ambiguity with int32_t and int64_t
69 static constexpr bool int_is_distinct =
70 !std::is_same<int, int32_t>::value &&
71 !std::is_same<int, int64_t>::value;
72 template<bool Enable = int_is_distinct,
73 typename std::enable_if<Enable, int>::type = 0>
74 SBAny(int value) : SBAny(static_cast<int64_t>(value)) {}
75
76 static constexpr bool uint_is_distinct =
77 !std::is_same<unsigned int, uint32_t>::value &&
78 !std::is_same<unsigned int, uint64_t>::value;
79 template<bool Enable = uint_is_distinct,
80 typename std::enable_if<Enable, int>::type = 0>
81 SBAny(unsigned int value) : SBAny(static_cast<uint64_t>(value)) {}
82
83 // Floating point types
84 SBAny(float);
85 SBAny(double);
86
87 // String types
88 SBAny(const char* utf8);
89 SBAny(const std::string& str);
90
91 // Map type
92 SBAny(std::initializer_list<std::pair<std::string, SBAny>> init);
93 SBAny(const SBAnyMap& init);
94
95 // Vector type
96 // SBAny(std::vector<SBAny> init);
97 SBAny(const SBAnyVector& init);
98 explicit SBAny(std::initializer_list<SBAny> init);
99
100 // Function wrapper constructors
101 SBAny(std::function<Result<SBAny>()> init); // Property Getter
102 SBAny(std::function<SBAny()> init); // Property Getter (no failure)
103 SBAny(std::function<Result<void>(const SBAny&)> init); // Property Setter
104 SBAny(std::function<void(const SBAny&)> init); // Property Setter (no failure)
105 SBAny(std::function<Result<SBAny>(const SBAnyMap&)> init); // Action
106
107 // Pointers
108 SBAny(float* init);
109
110 // Copy constructor and assignment
111 SBAny(const SBAny&);
112 SBAny& operator=(const SBAny&);
113
114 ~SBAny();
115
121 SBAnyType type() const noexcept;
122
128 std::any toAny() const;
129
135 void* toPtr() const;
136
145 template <typename T>
146 static T convert(const SBAny& value);
147
148private:
149 struct Impl;
150 Impl* pimpl;
151};
152
156class SWITCHBOARDSDK_EXPORT SBAnyVector final {
157public:
162
169
178
185 template<typename Iterator>
186 SBAnyVector(Iterator first, Iterator last);
187
196 template<typename T>
197 explicit SBAnyVector(const std::vector<T>& vec) : SBAnyVector() {
198 reserve(vec.size());
199 for (const auto& item : vec) {
200 emplace_back(SBAny(item));
201 }
202 }
203
209 explicit SBAnyVector(std::initializer_list<SBAny> init);
210
215
221 void reserve(size_t newCapacity);
222
228 [[nodiscard]] size_t size() const;
229
236 void emplace_back(const SBAny& value);
237
244 void push_back(const SBAny& value);
245
253 SBAny& operator[](size_t index);
254
262 const SBAny& operator[](size_t index) const;
263
264 // Iterators
265 using iterator = std::vector<SBAny>::iterator;
266 using const_iterator = std::vector<SBAny>::const_iterator;
267
268 iterator begin();
269 iterator end();
270 const_iterator begin() const;
271 const_iterator end() const;
272
273private:
274 struct Impl;
275 Impl* pimpl;
276};
277
281class SWITCHBOARDSDK_EXPORT SBAnyMap {
282public:
287
292 SBAnyMap(const std::string& jsonString);
293
299 SBAnyMap(const SBAnyMap& other);
300
309
315 SBAnyMap(std::initializer_list<std::pair<std::string, SBAny>> init);
316
321
329 [[nodiscard]] bool hasKey(const std::string& key) const;
330
336 [[nodiscard]] bool empty() const;
337
343 [[nodiscard]] size_t size() const;
344
352 [[nodiscard]] SBAny getAny(const std::string& key) const;
353
361 [[nodiscard]] SBAny at(const std::string& key) const;
362
371 template <typename T>
372 [[nodiscard]] T get(const std::string& key) const;
373
380 void set(const std::string& key, const SBAny& value);
381
387 void erase(const std::string& key);
388
399 template <typename T>
400 static T get(const SBAnyMap& map, const std::string& key, std::optional<T> defaultValue = std::nullopt);
401
409 static SBAnyMap jsonToMap(const std::string& jsonString);
410
418 static std::string mapToJson(const SBAnyMap& map);
419
420 // Iterators
421 using iterator = std::map<std::string, SBAny>::iterator;
422 using const_iterator = std::map<std::string, SBAny>::const_iterator;
423
424 iterator begin();
425 iterator end();
426 const_iterator begin() const;
427 const_iterator end() const;
428
429 // Operator overloads
430 SBAny& operator[](const std::string& key);
431 const SBAny& operator[](const std::string& key) const;
432
433private:
434 struct Impl;
435 Impl* pimpl;
436};
437
438}
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:53
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:281
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)
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:156
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:197
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.