NeBuild dev
Loading...
Searching...
No Matches
key.hpp
Go to the documentation of this file.
1//# This file is a part of toml++ and is subject to the the terms of the MIT license.
2//# Copyright (c) Mark Gillard <mark.gillard@outlook.com.au>
3//# See https://github.com/marzer/tomlplusplus/blob/master/LICENSE for the full license text.
4// SPDX-License-Identifier: MIT
5#pragma once
6
7#include "source_region.hpp"
8#include "std_utility.hpp"
9#include "print_to_stream.hpp"
10#include "header_start.hpp"
11
13{
31 class key
32 {
33 private:
34 std::string key_;
35 source_region source_;
36
37 public:
40 key() noexcept = default;
41
44 explicit key(std::string_view k, source_region&& src = {}) //
45 : key_{ k },
46 source_{ std::move(src) }
47 {}
48
51 explicit key(std::string_view k, const source_region& src) //
52 : key_{ k },
53 source_{ src }
54 {}
55
58 explicit key(std::string&& k, source_region&& src = {}) noexcept //
59 : key_{ std::move(k) },
60 source_{ std::move(src) }
61 {}
62
65 explicit key(std::string&& k, const source_region& src) noexcept //
66 : key_{ std::move(k) },
67 source_{ src }
68 {}
69
72 explicit key(const char* k, source_region&& src = {}) //
73 : key_{ k },
74 source_{ std::move(src) }
75 {}
76
79 explicit key(const char* k, const source_region& src) //
80 : key_{ k },
81 source_{ src }
82 {}
83
84#if TOML_ENABLE_WINDOWS_COMPAT
85
90 explicit key(std::wstring_view k, source_region&& src = {}) //
91 : key_{ impl::narrow(k) },
92 source_{ std::move(src) }
93 {}
94
99 explicit key(std::wstring_view k, const source_region& src) //
100 : key_{ impl::narrow(k) },
101 source_{ src }
102 {}
103
104#endif
105
108
111 std::string_view str() const noexcept
112 {
113 return std::string_view{ key_ };
114 }
115
118 /*implicit*/ operator std::string_view() const noexcept
119 {
120 return str();
121 }
122
125 bool empty() const noexcept
126 {
127 return key_.empty();
128 }
129
132 const char* data() const noexcept
133 {
134 return key_.data();
135 }
136
139 size_t length() const noexcept
140 {
141 return key_.length();
142 }
143
145
148
151 const source_region& source() const noexcept
152 {
153 return source_;
154 }
155
157
161
164 friend bool operator==(const key& lhs, const key& rhs) noexcept
165 {
166 return lhs.key_ == rhs.key_;
167 }
168
171 friend bool operator!=(const key& lhs, const key& rhs) noexcept
172 {
173 return lhs.key_ != rhs.key_;
174 }
175
178 friend bool operator<(const key& lhs, const key& rhs) noexcept
179 {
180 return lhs.key_ < rhs.key_;
181 }
182
185 friend bool operator<=(const key& lhs, const key& rhs) noexcept
186 {
187 return lhs.key_ <= rhs.key_;
188 }
189
192 friend bool operator>(const key& lhs, const key& rhs) noexcept
193 {
194 return lhs.key_ > rhs.key_;
195 }
196
199 friend bool operator>=(const key& lhs, const key& rhs) noexcept
200 {
201 return lhs.key_ >= rhs.key_;
202 }
203
206 friend bool operator==(const key& lhs, std::string_view rhs) noexcept
207 {
208 return lhs.key_ == rhs;
209 }
210
213 friend bool operator!=(const key& lhs, std::string_view rhs) noexcept
214 {
215 return lhs.key_ != rhs;
216 }
217
220 friend bool operator<(const key& lhs, std::string_view rhs) noexcept
221 {
222 return lhs.key_ < rhs;
223 }
224
227 friend bool operator<=(const key& lhs, std::string_view rhs) noexcept
228 {
229 return lhs.key_ <= rhs;
230 }
231
234 friend bool operator>(const key& lhs, std::string_view rhs) noexcept
235 {
236 return lhs.key_ > rhs;
237 }
238
241 friend bool operator>=(const key& lhs, std::string_view rhs) noexcept
242 {
243 return lhs.key_ >= rhs;
244 }
245
248 friend bool operator==(std::string_view lhs, const key& rhs) noexcept
249 {
250 return lhs == rhs.key_;
251 }
252
255 friend bool operator!=(std::string_view lhs, const key& rhs) noexcept
256 {
257 return lhs != rhs.key_;
258 }
259
262 friend bool operator<(std::string_view lhs, const key& rhs) noexcept
263 {
264 return lhs < rhs.key_;
265 }
266
269 friend bool operator<=(std::string_view lhs, const key& rhs) noexcept
270 {
271 return lhs <= rhs.key_;
272 }
273
276 friend bool operator>(std::string_view lhs, const key& rhs) noexcept
277 {
278 return lhs > rhs.key_;
279 }
280
283 friend bool operator>=(std::string_view lhs, const key& rhs) noexcept
284 {
285 return lhs >= rhs.key_;
286 }
287
289
292
294 using const_iterator = const char*;
295
297 using iterator = const_iterator;
298
301 const_iterator begin() const noexcept
302 {
303 return key_.data();
304 }
305
308 const_iterator end() const noexcept
309 {
310 return key_.data() + key_.length();
311 }
312
314
316 friend std::ostream& operator<<(std::ostream& lhs, const key& rhs)
317 {
318 impl::print_to_stream(lhs, rhs.key_);
319 return lhs;
320 }
321 };
322
324 template <typename T>
325 inline constexpr bool is_key = std::is_same_v<impl::remove_cvref<T>, toml::key>;
326
329 template <typename T>
330 inline constexpr bool is_key_or_convertible = is_key<T> //
331 || impl::is_constructible_or_convertible<toml::key, T>;
332}
334
335#include "header_end.hpp"
std::basic_ostream< Char > & operator<<(std::basic_ostream< Char > &lhs, node_type rhs)
Pretty-prints the value of a node_type to a stream.
Definition forward_declarations.hpp:256
bool operator==(const json_pointer< RefStringTypeLhs > &lhs, const json_pointer< RefStringTypeRhs > &rhs) noexcept
Definition json.h:14737
bool operator!=(const json_pointer< RefStringTypeLhs > &lhs, const json_pointer< RefStringTypeRhs > &rhs) noexcept
Definition json.h:14762
bool operator<(const json_pointer< RefStringTypeLhs > &lhs, const json_pointer< RefStringTypeRhs > &rhs) noexcept
Definition json.h:14787
TOML_NAMESPACE_START
Definition key.hpp:13
constexpr bool is_key
Metafunction for determining if a type is, or is a reference to, a toml::key.
Definition key.hpp:325
constexpr bool is_key_or_convertible
Metafunction for determining if a type is, or is a reference to, a toml::key, or is implicitly or exp...
Definition key.hpp:330
TOML_NAMESPACE_END
Definition key.hpp:333
#define TOML_NODISCARD_CTOR
Definition preprocessor.hpp:446
#define TOML_PURE_INLINE_GETTER
Definition preprocessor.hpp:479
A source document region.
Definition source_region.hpp:167