Thoth
A functional, expressive, asynchronous C++26 webdev library
Loading...
Searching...
No Matches
Definitions.hpp
Go to the documentation of this file.
1#pragma once
2#include <variant>
3#include <memory>
4#include <vector>
5#include <string>
6#include <optional>
7#include <expected>
8#include <concepts>
9#include <span>
10
11#include <Thoth/Dsa/Cow.hpp>
13
14
15namespace Thoth::Http {
16 struct RequestError;
17}
18
19namespace Thoth::NJson {
20 struct JsonObject;
21 struct Json;
22
23
24 using Null = std::monostate; // null
26 using Number = long double; // number
27 using Bool = bool; // bool
28 using Object = std::unique_ptr<JsonObject>; // {Object}
29 using Array = std::vector<Json>; // [Array]
30
31 namespace Details {
32 struct BufferInfo {
33 std::string_view bufferView;
34 std::shared_ptr<std::string> buffer;
35 };
36
37 static bool ReadString(std::string_view& input, auto& val, const BufferInfo& info);
38 static bool ReadNumber(std::string_view& input, auto& val);
39 static bool ReadObject(std::string_view& input, auto& val, const BufferInfo& info);
40 static bool ReadBool (std::string_view& input, auto& val);
41 static bool ReadNull (std::string_view& input, auto& val);
42 static bool ReadArray (std::string_view& input, auto& val, const BufferInfo& info);
43 }
44
45
46#pragma region Wappers for std::optional and std::expected
48 using CRefValWrapper = const Json*;
49
50 using OptRefValWrapper = std::optional<RefValWrapper>;
51 using OptCRefValWrapper = std::optional<CRefValWrapper>;
52
53 using ExpRefValWrapper = std::expected<RefValWrapper, Http::RequestError>;
54 using ExpCRefValWrapper = std::expected<CRefValWrapper, Http::RequestError>;
55
57 using CValWrapper = const Json;
58
59 using OptValWrapper = std::optional<ValWrapper>;
60 using OptCValWrapper = std::optional<CValWrapper>;
61
62 using ExpValWrapper = std::expected<ValWrapper, Http::RequestError>;
63 using ExpCValWrapper = std::expected<CValWrapper, Http::RequestError>;
64#pragma endregion
65
66
67
68 using JsonObjKey = std::string;
69 using JsonObjKeyRef = std::string_view;
70
71 using Key = std::variant<int, JsonObjKey>;
72 using Keys = std::span<const Key>;
73
74
75 template<class ...T>
76 requires ((std::unsigned_integral<T> || std::convertible_to<T, std::string_view>) &&...)
77 auto MakeKeys(const T&... keys) { return std::array<Key, sizeof...(T)>{ (keys, ...) }; }
78}
Definition Client.hpp:12
Definition Definitions.hpp:19
std::optional< CValWrapper > OptCValWrapper
Definition Definitions.hpp:60
std::vector< Json > Array
Definition Definitions.hpp:29
std::expected< CRefValWrapper, Http::RequestError > ExpCRefValWrapper
Definition Definitions.hpp:54
std::unique_ptr< JsonObject > Object
Definition Definitions.hpp:28
std::expected< ValWrapper, Http::RequestError > ExpValWrapper
Definition Definitions.hpp:62
std::string_view JsonObjKeyRef
Definition Definitions.hpp:69
std::optional< CRefValWrapper > OptCRefValWrapper
Definition Definitions.hpp:51
auto MakeKeys(const T &... keys)
Definition Definitions.hpp:77
std::variant< int, JsonObjKey > Key
Definition Definitions.hpp:71
long double Number
Definition Definitions.hpp:26
std::monostate Null
Definition Definitions.hpp:24
std::span< const Key > Keys
Definition Definitions.hpp:72
std::optional< RefValWrapper > OptRefValWrapper
Definition Definitions.hpp:50
std::string JsonObjKey
Definition Definitions.hpp:68
bool Bool
Definition Definitions.hpp:27
std::expected< CValWrapper, Http::RequestError > ExpCValWrapper
Definition Definitions.hpp:63
std::optional< ValWrapper > OptValWrapper
Definition Definitions.hpp:59
std::expected< RefValWrapper, Http::RequestError > ExpRefValWrapper
Definition Definitions.hpp:53
Like Rust's cow (copy on write).
Definition Cow.hpp:15
Definition Definitions.hpp:32
std::string_view bufferView
Definition Definitions.hpp:33
std::shared_ptr< std::string > buffer
Definition Definitions.hpp:34
Definition Json.hpp:16
Definition JsonObject.hpp:14