metadata 0.2.0
Modern C++23 header-only metadata container (JSON-like)
Loading...
Searching...
No Matches
helpers.hpp
1#pragma once
2
3#include <md/object.hpp>
4#include <md/value.hpp>
5
6#include <string>
7#include <string_view>
8
9namespace md {
10
11// Free-function counterparts to Object methods, for users who prefer the
12// functional style. Each is a thin forward.
13
15[[nodiscard]] inline bool contains(const Object& o, const std::string_view key) {
16 return o.contains(key);
17}
18
20[[nodiscard]] inline Value* find_ptr(Object& o, const std::string_view key) {
21 return o.find_ptr(key);
22}
24[[nodiscard]] inline const Value* find_ptr(const Object& o, const std::string_view key) {
25 return o.find_ptr(key);
26}
27
29inline Value& require(Object& o, const std::string_view key) {
30 return o.require(key);
31}
33inline const Value& require(const Object& o, const std::string_view key) {
34 return o.require(key);
35}
36
38inline std::string& require_string(Object& o, const std::string_view key) {
39 return o.require_string(key);
40}
42inline const std::string& require_string(const Object& o, const std::string_view key) {
43 return o.require_string(key);
44}
45
47inline Array& require_array(Object& o, const std::string_view key) {
48 return o.require_array(key);
49}
51inline const Array& require_array(const Object& o, const std::string_view key) {
52 return o.require_array(key);
53}
54
56inline Object& require_object(Object& o, const std::string_view key) {
57 return o.require_object(key);
58}
60inline const Object& require_object(const Object& o, const std::string_view key) {
61 return o.require_object(key);
62}
63
65[[nodiscard]] inline const std::string* get_string_if(const Object& o, const std::string_view key) {
66 return o.get_string_if(key);
67}
69[[nodiscard]] inline const Array* get_array_if(const Object& o, const std::string_view key) {
70 return o.get_array_if(key);
71}
73[[nodiscard]] inline const Object* get_object_if(const Object& o, const std::string_view key) {
74 return o.get_object_if(key);
75}
76
78inline void merge(Object& dst, const Object& src) {
79 dst.merge(src);
80}
81
82} // namespace md
const Object * get_object_if(const std::string_view key) const
Pointer to the nested object at key, or nullptr if absent or wrong type.
Definition object.hpp:295
Object & require_object(const std::string_view key)
Return the nested object at key; throws on missing key or type mismatch.
Definition object.hpp:264