@@ -126,6 +126,8 @@ template<typename CharType> struct data_variant
126126 // cppcheck-suppress noExplicitConstructor
127127 constexpr data_variant (std::basic_string_view<CharType> s) : value{ s }, selected{ selected_type::string } {}
128128
129+ [[nodiscard]] constexpr bool is_boolean () const noexcept { return selected == selected_type::boolean; }
130+
129131 [[nodiscard]] constexpr const bool *get_if_boolean () const noexcept
130132 {
131133 if (selected == selected_type::boolean) {
@@ -135,51 +137,68 @@ template<typename CharType> struct data_variant
135137 }
136138 }
137139
140+ [[nodiscard]] constexpr bool is_array () const noexcept { return selected == selected_type::array; }
141+
138142 [[nodiscard]] constexpr const basic_array_t <CharType> *get_if_array () const noexcept
139143 {
140- if (selected == selected_type::array ) {
144+ if (is_array () ) {
141145 return &value.array_ ;
142146 } else {
143147 return nullptr ;
144148 }
145149 }
150+
151+ [[nodiscard]] constexpr bool is_object () const noexcept { return selected == selected_type::object; }
152+
146153 [[nodiscard]] constexpr const basic_object_t <CharType> *get_if_object () const noexcept
147154 {
148- if (selected == selected_type::object ) {
155+ if (is_object () ) {
149156 return &value.object_ ;
150157 } else {
151158 return nullptr ;
152159 }
153160 }
161+
162+ [[nodiscard]] constexpr bool is_integer () const noexcept { return selected == selected_type::integer; }
163+
154164 [[nodiscard]] constexpr const std::int64_t *get_if_integer () const noexcept
155165 {
156- if (selected == selected_type::integer ) {
166+ if (is_integer () ) {
157167 return &value.int64_t_ ;
158168 } else {
159169 return nullptr ;
160170 }
161171 }
172+
173+ [[nodiscard]] constexpr bool is_uinteger () const noexcept { return selected == selected_type::uinteger; }
174+
162175 [[nodiscard]] constexpr const std::uint64_t *get_if_uinteger () const noexcept
163176 {
164- if (selected == selected_type::uinteger ) {
177+ if (is_uinteger () ) {
165178 return &value.uint64_t_ ;
166179 } else {
167180 return nullptr ;
168181 }
169182 }
170183
184+
185+ [[nodiscard]] constexpr bool is_floating_point () const noexcept { return selected == selected_type::floating_point; }
186+
187+
171188 [[nodiscard]] constexpr const double *get_if_floating_point () const noexcept
172189 {
173- if (selected == selected_type::floating_point ) {
190+ if (is_floating_point () ) {
174191 return &value.double_ ;
175192 } else {
176193 return nullptr ;
177194 }
178195 }
179196
197+ [[nodiscard]] constexpr bool is_string () const noexcept { return selected == selected_type::string; }
198+
180199 [[nodiscard]] constexpr const std::basic_string_view<CharType> *get_if_string () const noexcept
181200 {
182- if (selected == selected_type::string ) {
201+ if (is_string () ) {
183202 return &value.string_view_ ;
184203 } else {
185204 return nullptr ;
@@ -199,7 +218,7 @@ template<typename CharType> struct basic_json
199218 : parent_value_(&value), index_{ index }
200219 {}
201220
202- constexpr const basic_json &operator *() const noexcept
221+ constexpr const basic_json &operator *() const
203222 {
204223 if (parent_value_->is_array ()) {
205224 return (*parent_value_)[index_];
@@ -340,7 +359,7 @@ template<typename CharType> struct basic_json
340359 }
341360 }
342361
343- template <typename Key>[[nodiscard]] constexpr std::size_t count (const Key &key) const noexcept
362+ template <typename Key> [[nodiscard]] constexpr std::size_t count (const Key &key) const
344363 {
345364 if (is_object ()) {
346365 const auto found = find (key);
@@ -353,7 +372,7 @@ template<typename CharType> struct basic_json
353372 return 0 ;
354373 }
355374
356- [[nodiscard]] constexpr iterator find (const std::basic_string_view<CharType> key) const noexcept
375+ [[nodiscard]] constexpr iterator find (const std::basic_string_view<CharType> key) const
357376 {
358377 for (auto itr = begin (); itr != end (); ++itr) {
359378 if (itr.key () == key) { return itr; }
@@ -369,17 +388,17 @@ template<typename CharType> struct basic_json
369388
370389 constexpr const auto &array_data () const
371390 {
372- if (const auto *result = data.get_if_array (); result != nullptr ) {
373- return *result ;
391+ if (data.is_array () ) {
392+ return *data. get_if_array () ;
374393 } else {
375394 throw std::runtime_error (" value is not an array type" );
376395 }
377396 }
378397
379398 constexpr const auto &object_data () const
380399 {
381- if (const auto *result = data.get_if_object (); result != nullptr ) {
382- return *result ;
400+ if (data.is_object () ) {
401+ return *data. get_if_object () ;
383402 } else {
384403 throw std::runtime_error (" value is not an object type" );
385404 }
@@ -388,35 +407,37 @@ template<typename CharType> struct basic_json
388407 constexpr static basic_json object () { return basic_json{ data_t { basic_object_t <CharType>{} } }; }
389408 constexpr static basic_json array () { return basic_json{ data_t { basic_array_t <CharType>{} } }; }
390409
391- template <typename Type>[[nodiscard]] constexpr auto get () const
410+ template <typename Type> [[nodiscard]] constexpr auto get () const
392411 {
393412 // I don't like this level of implicit conversions in the `get()` function,
394413 // but it's necessary for API compatibility with nlohmann::json
395- if constexpr (std::is_same_v<Type, std::uint64_t > || std::is_same_v<Type, std::int64_t > || std::is_same_v<Type, double >) {
396- if (const auto *uint_value = data.get_if_uinteger (); uint_value != nullptr ) {
397- return Type (*uint_value);
398- } else if (const auto *value = data.get_if_integer (); value != nullptr ) {
399- return Type (*value);
400- } else if (const auto *fpvalue = data.get_if_floating_point (); fpvalue != nullptr ) {
401- return Type (*fpvalue);
414+ if constexpr (std::is_same_v<Type,
415+ std::uint64_t > || std::is_same_v<Type, std::int64_t > || std::is_same_v<Type, double >) {
416+ if (data.is_uinteger ()) {
417+ return Type (*data.get_if_uinteger ());
418+ } else if (data.is_integer ()) {
419+ return Type (*data.get_if_integer ());
420+ } else if (data.is_floating_point ()) {
421+ return Type (*data.get_if_floating_point ());
402422 } else {
403423 throw std::runtime_error (" Unexpected type: number requested" );// + ss.str() );
404424 }
405425 } else if constexpr (std::is_same_v<Type,
406426 std::basic_string_view<CharType>> || std::is_same_v<Type, std::basic_string<CharType>>) {
407- if (const auto *value = data.get_if_string (); value != nullptr ) { return *value; }
408- else {
427+ if (data.is_string ()) {
428+ return *data.get_if_string ();
429+ } else {
409430 throw std::runtime_error (" Unexpected type: string-like requested" );
410431 }
411432 } else if constexpr (std::is_same_v<Type, bool >) {
412- if (const auto *value = data.get_if_boolean (); value != nullptr ) { return *value; }
413- else {
433+ if (data.is_boolean ()) {
434+ return *data.get_if_boolean ();
435+ } else {
414436 throw std::runtime_error (" Unexpected type: bool requested" );
415437 }
416438 } else {
417439 throw std::runtime_error (" Unexpected type for get()" );
418440 }
419-
420441 }
421442
422443 [[nodiscard]] constexpr bool is_object () const noexcept { return data.selected == data_t ::selected_type::object; }
0 commit comments