Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
Helpers.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2022, the clio developers.
5
6 Permission to use, copy, modify, and distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#pragma once
21
22#include <cstdint>
23#include <optional>
24#include <queue>
25#include <stdexcept>
26#include <string>
27#include <typeinfo>
28
29namespace util::impl {
30
34struct KeyException : public ::std::logic_error {
35 KeyException(::std::string msg) : ::std::logic_error{msg}
36 {
37 }
38};
39
43struct StoreException : public ::std::logic_error {
44 StoreException(::std::string msg) : ::std::logic_error{msg}
45 {
46 }
47};
48
55template <typename KeyType, char Separator>
56class Tokenizer final {
57 using opt_key_t = std::optional<KeyType>;
58 KeyType key_;
59 KeyType token_{};
60 std::queue<KeyType> tokens_{};
61
62public:
63 explicit Tokenizer(KeyType key) : key_{key}
64 {
65 if (key.empty())
66 throw KeyException("Empty key");
67
68 for (auto const& c : key) {
69 if (c == Separator) {
70 saveToken();
71 } else {
72 token_ += c;
73 }
74 }
75
76 saveToken();
77 }
78
79 [[nodiscard]] opt_key_t
80 next()
81 {
82 if (tokens_.empty())
83 return std::nullopt;
84 auto token = tokens_.front();
85 tokens_.pop();
86 return std::make_optional(std::move(token));
87 }
88
89private:
90 void
91 saveToken()
92 {
93 if (token_.empty())
94 throw KeyException("Empty token in key '" + key_ + "'.");
95 tokens_.push(std::move(token_));
96 token_ = {};
97 }
98};
99
100template <typename T>
101static constexpr char const*
102typeName()
103{
104 return typeid(T).name();
105}
106
107template <>
108constexpr char const*
109typeName<uint64_t>()
110{
111 return "uint64_t";
112}
113
114template <>
115constexpr char const*
116typeName<int64_t>()
117{
118 return "int64_t";
119}
120
121template <>
122constexpr char const*
123typeName<uint32_t>()
124{
125 return "uint32_t";
126}
127
128template <>
129constexpr char const*
130typeName<int32_t>()
131{
132 return "int32_t";
133}
134
135template <>
136constexpr char const*
137typeName<bool>()
138{
139 return "bool";
140}
141
142template <>
143constexpr char const*
144typeName<std::string>()
145{
146 return "std::string";
147}
148
149template <>
150constexpr char const*
151typeName<char const*>()
152{
153 return "const char*";
154}
155
156template <>
157constexpr char const*
158typeName<double>()
159{
160 return "double";
161}
162
163template <>
164constexpr char const*
165typeName<float>()
166{
167 return "float";
168}
169
170}; // namespace util::impl
Simple string tokenizer. Used by Config.
Definition Helpers.hpp:56
Thrown when a KeyPath related error occurs.
Definition Helpers.hpp:34
Thrown when a Store (config's storage) related error occurs.
Definition Helpers.hpp:43