/src/boost/boost/json/impl/value_ref.ipp
Line | Count | Source (jump to first uncovered line) |
1 | | // |
2 | | // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com) |
3 | | // |
4 | | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
5 | | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
6 | | // |
7 | | // Official repository: https://github.com/boostorg/json |
8 | | // |
9 | | |
10 | | #ifndef BOOST_JSON_IMPL_VALUE_REF_IPP |
11 | | #define BOOST_JSON_IMPL_VALUE_REF_IPP |
12 | | |
13 | | #include <boost/json/value_ref.hpp> |
14 | | #include <boost/json/array.hpp> |
15 | | #include <boost/json/value.hpp> |
16 | | |
17 | | namespace boost { |
18 | | namespace json { |
19 | | |
20 | | value_ref:: |
21 | | operator |
22 | | value() const |
23 | 0 | { |
24 | 0 | return make_value({}); |
25 | 0 | } |
26 | | |
27 | | value |
28 | | value_ref:: |
29 | | from_init_list( |
30 | | void const* p, |
31 | | storage_ptr sp) |
32 | 0 | { |
33 | 0 | return make_value( |
34 | 0 | *reinterpret_cast< |
35 | 0 | init_list const*>(p), |
36 | 0 | std::move(sp)); |
37 | 0 | } |
38 | | |
39 | | bool |
40 | | value_ref:: |
41 | | is_key_value_pair() const noexcept |
42 | 0 | { |
43 | 0 | if(what_ != what::ini) |
44 | 0 | return false; |
45 | 0 | if(arg_.init_list_.size() != 2) |
46 | 0 | return false; |
47 | 0 | auto const& e = |
48 | 0 | *arg_.init_list_.begin(); |
49 | 0 | if( e.what_ != what::str && |
50 | 0 | e.what_ != what::strfunc) |
51 | 0 | return false; |
52 | 0 | return true; |
53 | 0 | } |
54 | | |
55 | | bool |
56 | | value_ref:: |
57 | | maybe_object( |
58 | | std::initializer_list< |
59 | | value_ref> init) noexcept |
60 | 0 | { |
61 | 0 | for(auto const& e : init) |
62 | 0 | if(! e.is_key_value_pair()) |
63 | 0 | return false; |
64 | 0 | return true; |
65 | 0 | } |
66 | | |
67 | | string_view |
68 | | value_ref:: |
69 | | get_string() const noexcept |
70 | 0 | { |
71 | 0 | BOOST_ASSERT( |
72 | 0 | what_ == what::str || |
73 | 0 | what_ == what::strfunc); |
74 | 0 | if (what_ == what::strfunc) |
75 | 0 | return *static_cast<const string*>(f_.p); |
76 | 0 | return arg_.str_; |
77 | 0 | } |
78 | | |
79 | | value |
80 | | value_ref:: |
81 | | make_value( |
82 | | storage_ptr sp) const |
83 | 0 | { |
84 | 0 | switch(what_) |
85 | 0 | { |
86 | 0 | default: |
87 | 0 | case what::str: |
88 | 0 | return string( |
89 | 0 | arg_.str_, |
90 | 0 | std::move(sp)); |
91 | | |
92 | 0 | case what::ini: |
93 | 0 | return make_value( |
94 | 0 | arg_.init_list_, |
95 | 0 | std::move(sp)); |
96 | | |
97 | 0 | case what::func: |
98 | 0 | return f_.f(f_.p, |
99 | 0 | std::move(sp)); |
100 | | |
101 | 0 | case what::strfunc: |
102 | 0 | return f_.f(f_.p, |
103 | 0 | std::move(sp)); |
104 | | |
105 | 0 | case what::cfunc: |
106 | 0 | return cf_.f(cf_.p, |
107 | 0 | std::move(sp)); |
108 | 0 | } |
109 | 0 | } |
110 | | |
111 | | value |
112 | | value_ref:: |
113 | | make_value( |
114 | | std::initializer_list< |
115 | | value_ref> init, |
116 | | storage_ptr sp) |
117 | 0 | { |
118 | 0 | if(maybe_object(init)) |
119 | 0 | return make_object( |
120 | 0 | init, std::move(sp)); |
121 | 0 | return make_array( |
122 | 0 | init, std::move(sp)); |
123 | 0 | } |
124 | | |
125 | | object |
126 | | value_ref:: |
127 | | make_object( |
128 | | std::initializer_list<value_ref> init, |
129 | | storage_ptr sp) |
130 | 0 | { |
131 | 0 | object obj(std::move(sp)); |
132 | 0 | obj.reserve(init.size()); |
133 | 0 | for(auto const& e : init) |
134 | 0 | obj.emplace( |
135 | 0 | e.arg_.init_list_.begin()[0].get_string(), |
136 | 0 | e.arg_.init_list_.begin()[1].make_value( |
137 | 0 | obj.storage())); |
138 | 0 | return obj; |
139 | 0 | } |
140 | | |
141 | | array |
142 | | value_ref:: |
143 | | make_array( |
144 | | std::initializer_list< |
145 | | value_ref> init, |
146 | | storage_ptr sp) |
147 | 0 | { |
148 | 0 | array arr(std::move(sp)); |
149 | 0 | arr.reserve(init.size()); |
150 | 0 | for(auto const& e : init) |
151 | 0 | arr.emplace_back( |
152 | 0 | e.make_value( |
153 | 0 | arr.storage())); |
154 | 0 | return arr; |
155 | 0 | } |
156 | | |
157 | | void |
158 | | value_ref:: |
159 | | write_array( |
160 | | value* dest, |
161 | | std::initializer_list< |
162 | | value_ref> init, |
163 | | storage_ptr const& sp) |
164 | 0 | { |
165 | 0 | struct undo |
166 | 0 | { |
167 | 0 | value* const base; |
168 | 0 | value* pos; |
169 | 0 | ~undo() |
170 | 0 | { |
171 | 0 | if(pos) |
172 | 0 | while(pos > base) |
173 | 0 | (--pos)->~value(); |
174 | 0 | } |
175 | 0 | }; |
176 | 0 | undo u{dest, dest}; |
177 | 0 | for(auto const& e : init) |
178 | 0 | { |
179 | 0 | ::new(u.pos) value( |
180 | 0 | e.make_value(sp)); |
181 | 0 | ++u.pos; |
182 | 0 | } |
183 | 0 | u.pos = nullptr; |
184 | 0 | } |
185 | | |
186 | | } // namespace json |
187 | | } // namespace boost |
188 | | |
189 | | #endif |