Skip to content

Commit 8de13fa

Browse files
committed
Some API cleanups
1 parent ecf25cf commit 8de13fa

File tree

2 files changed

+50
-75
lines changed

2 files changed

+50
-75
lines changed

include/json2cpp/constexpr_json.hpp

Lines changed: 49 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,14 @@ template<typename T> struct span
5050

5151
constexpr span() : begin_{ nullptr }, end_{ nullptr } {}
5252

53-
constexpr const T *begin() const { return begin_; }
53+
[[nodiscard]] constexpr const T *begin() const noexcept { return begin_; }
5454

55-
constexpr const T *end() const { return end_; }
55+
[[nodiscard]] constexpr const T *end() const noexcept { return end_; }
5656

57-
constexpr std::size_t size() const { return static_cast<std::size_t>(std::distance(begin_, end_)); }
57+
[[nodiscard]] constexpr std::size_t size() const noexcept
58+
{
59+
return static_cast<std::size_t>(std::distance(begin_, end_));
60+
}
5861

5962
const T *begin_;
6063
const T *end_;
@@ -188,10 +191,11 @@ template<typename CharType> struct basic_json
188191

189192
struct iterator
190193
{
191-
constexpr explicit iterator(const basic_json &value, std::size_t index = 0) : parent_value_(&value), index_{ index }
194+
constexpr explicit iterator(const basic_json &value, std::size_t index = 0) noexcept
195+
: parent_value_(&value), index_{ index }
192196
{}
193197

194-
constexpr const basic_json &operator*() const
198+
constexpr const basic_json &operator*() const noexcept
195199
{
196200
if (parent_value_->is_array()) {
197201
return (*parent_value_)[index_];
@@ -202,11 +206,11 @@ template<typename CharType> struct basic_json
202206
}
203207
}
204208

205-
constexpr const basic_json *operator->() const { return &(*(*this)); }
209+
constexpr const basic_json *operator->() const noexcept { return &(*(*this)); }
206210

207-
constexpr std::size_t index() const { return index_; }
211+
constexpr std::size_t index() const noexcept { return index_; }
208212

209-
constexpr const basic_json &value() const { return *(*this); }
213+
constexpr const basic_json &value() const noexcept { return *(*this); }
210214

211215

212216
constexpr std::basic_string_view<CharType> key() const
@@ -218,53 +222,52 @@ template<typename CharType> struct basic_json
218222
}
219223
}
220224

221-
constexpr bool operator==(const iterator &other) const
225+
constexpr bool operator==(const iterator &other) const noexcept
222226
{
223227
return other.parent_value_ == parent_value_ && other.index_ == index_;
224228
}
225-
constexpr bool operator!=(const iterator &other) const { return !(*this == other); }
229+
constexpr bool operator!=(const iterator &other) const noexcept { return !(*this == other); }
226230

227231

228-
constexpr bool operator<(const iterator &other) const
232+
constexpr bool operator<(const iterator &other) const noexcept
229233
{
230-
assert(other.parent_value_ == parent_value_);
231-
return index_ < other.index_;
234+
return other.parent_value_ == parent_value_ && index_ < other.index_;
232235
}
233236

234-
constexpr iterator &operator--()
237+
constexpr iterator &operator--() noexcept
235238
{
236239
--index_;
237240
return *this;
238241
}
239242

240-
constexpr iterator operator--(int)
243+
[[nodiscard]] constexpr iterator operator--(int) noexcept
241244
{
242245
iterator result{ *this };
243246
index_--;
244247
return result;
245248
}
246249

247-
constexpr iterator &operator++()
250+
constexpr iterator &operator++() noexcept
248251
{
249252
++index_;
250253
return *this;
251254
}
252255

253-
constexpr iterator operator++(int)
256+
[[nodiscard]] constexpr iterator operator++(int) noexcept
254257
{
255258
iterator result{ *this };
256259
index_++;
257260
return result;
258261
}
259262

260-
constexpr iterator &operator+=(const std::ptrdiff_t value)
263+
constexpr iterator &operator+=(const std::ptrdiff_t value) noexcept
261264
{
262265
index_ = static_cast<std::size_t>(static_cast<std::ptrdiff_t>(index_) + value);
263266
return *this;
264267
}
265268

266269

267-
constexpr iterator &operator+=(const std::size_t value)
270+
constexpr iterator &operator+=(const std::size_t value) noexcept
268271
{
269272
index_ += value;
270273
return *this;
@@ -276,25 +279,24 @@ template<typename CharType> struct basic_json
276279

277280
using const_iterator = iterator;
278281

279-
constexpr iterator begin() const { return iterator{ *this }; }
282+
[[nodiscard]] constexpr iterator begin() const noexcept { return iterator{ *this }; }
280283

281-
constexpr iterator end() const { return iterator{ *this, size() }; }
284+
[[nodiscard]] constexpr iterator end() const noexcept { return iterator{ *this, size() }; }
282285

283-
constexpr iterator cbegin() const { return begin(); }
286+
[[nodiscard]] constexpr iterator cbegin() const noexcept { return begin(); }
284287

285-
constexpr iterator cend() const { return end(); }
288+
[[nodiscard]] constexpr iterator cend() const noexcept { return end(); }
286289

287-
constexpr std::size_t size() const noexcept
290+
[[nodiscard]] constexpr std::size_t size() const noexcept
288291
{
289292
if (is_null()) { return 0; }
290293
if (is_object()) { return data.get_if_object()->size(); }
291-
292294
if (is_array()) { return data.get_if_array()->size(); }
293295

294296
return 1;
295297
}
296298

297-
constexpr const basic_json &operator[](const std::size_t idx) const
299+
[[nodiscard]] constexpr const basic_json &operator[](const std::size_t idx) const
298300
{
299301
if (const auto &children = array_data(); idx < children.size()) {
300302
return *std::next(children.begin(), static_cast<std::ptrdiff_t>(idx));
@@ -303,7 +305,7 @@ template<typename CharType> struct basic_json
303305
}
304306
}
305307

306-
constexpr iterator find(const std::basic_string_view<CharType> key) const
308+
[[nodiscard]] constexpr iterator find(const std::basic_string_view<CharType> key) const noexcept
307309
{
308310
for (auto itr = begin(); itr != end(); ++itr) {
309311
if (itr.key() == key) { return itr; }
@@ -312,7 +314,7 @@ template<typename CharType> struct basic_json
312314
return end();
313315
}
314316

315-
constexpr const basic_json &operator[](const std::basic_string_view<CharType> key) const
317+
[[nodiscard]] constexpr const basic_json &operator[](const std::basic_string_view<CharType> key) const
316318
{
317319
const auto &children = object_data();
318320

@@ -359,7 +361,7 @@ template<typename CharType> struct basic_json
359361
constexpr static basic_json object() { return basic_json{ data_t{ basic_object_t<CharType>{} } }; }
360362
constexpr static basic_json array() { return basic_json{ data_t{ basic_array_t<CharType>{} } }; }
361363

362-
template<typename Type> constexpr Type get() const
364+
template<typename Type> [[nodiscard]] constexpr Type get() const
363365
{
364366
if constexpr (std::is_same_v<Type, std::uint64_t> || std::is_same_v<Type, std::int64_t>) {
365367
if (const auto *uint_value = data.get_if_uinteger(); uint_value != nullptr) {
@@ -381,59 +383,32 @@ template<typename CharType> struct basic_json
381383
throw std::runtime_error("Incorrect type for get()");
382384
}
383385

384-
constexpr bool is_object() const noexcept { return data.selected == data_t::selected_type::object; }
385-
386-
constexpr bool is_array() const noexcept { return data.selected == data_t::selected_type::array; }
387-
388-
constexpr bool is_string() const noexcept { return data.selected == data_t::selected_type::string; }
389-
390-
constexpr bool is_boolean() const noexcept { return data.selected == data_t::selected_type::boolean; }
391-
392-
constexpr bool is_structured() const noexcept { return is_object() || is_array(); }
393-
394-
constexpr bool is_number() const noexcept { return is_number_integer() || is_number_float(); }
395-
396-
constexpr double as_number_float() const
386+
[[nodiscard]] constexpr bool is_object() const noexcept { return data.selected == data_t::selected_type::object; }
387+
[[nodiscard]] constexpr bool is_array() const noexcept { return data.selected == data_t::selected_type::array; }
388+
[[nodiscard]] constexpr bool is_string() const noexcept { return data.selected == data_t::selected_type::string; }
389+
[[nodiscard]] constexpr bool is_boolean() const noexcept { return data.selected == data_t::selected_type::boolean; }
390+
[[nodiscard]] constexpr bool is_structured() const noexcept { return is_object() || is_array(); }
391+
[[nodiscard]] constexpr bool is_number() const noexcept { return is_number_integer() || is_number_float(); }
392+
[[nodiscard]] constexpr bool is_number_integer() const noexcept { return is_number_signed() || is_number_unsigned(); }
393+
[[nodiscard]] constexpr bool is_null() const noexcept { return data.selected == data_t::selected_type::empty; }
394+
[[nodiscard]] constexpr bool is_binary() const noexcept { return data.selected == data_t::selected_type::binary; }
395+
396+
[[nodiscard]] constexpr bool is_number_signed() const noexcept
397397
{
398-
if (const double *value = data.get_if_floating_point(); value != nullptr) {
399-
return *value;
400-
} else {
401-
throw std::runtime_error("Not a float type");
402-
}
398+
return data.selected == data_t::selected_type::integer;
403399
}
404-
constexpr double as_boolean() const
400+
401+
[[nodiscard]] constexpr bool is_number_unsigned() const noexcept
405402
{
406-
if (const bool *value = data.get_if_boolean(); value != nullptr) {
407-
return *value;
408-
} else {
409-
throw std::runtime_error("Not a boolean type");
410-
}
403+
return data.selected == data_t::selected_type::uinteger;
411404
}
412405

413-
constexpr auto as_string() const
406+
[[nodiscard]] constexpr bool is_number_float() const noexcept
414407
{
415-
if (const auto *value = data.get_if_string(); value != nullptr) {
416-
return *value;
417-
} else {
418-
throw std::runtime_error("Not a string type");
419-
}
408+
return data.selected == data_t::selected_type::floating_point;
420409
}
421410

422-
constexpr operator double() const { return as_number_float(); }
423-
424-
constexpr bool is_number_integer() const noexcept { return is_number_signed() || is_number_unsigned(); }
425-
426-
constexpr bool is_number_signed() const noexcept { return data.selected == data_t::selected_type::integer; }
427-
428-
constexpr bool is_number_unsigned() const noexcept { return data.selected == data_t::selected_type::uinteger; }
429-
430-
constexpr bool is_number_float() const noexcept { return data.selected == data_t::selected_type::floating_point; }
431-
432-
constexpr bool is_null() const noexcept { return data.selected == data_t::selected_type::empty; }
433-
434-
constexpr bool is_binary() const noexcept { return data.selected == data_t::selected_type::binary; }
435-
436-
constexpr bool is_primitive() const noexcept
411+
[[nodiscard]] constexpr bool is_primitive() const noexcept
437412
{
438413
return is_null() || is_string() || is_boolean() || is_number() || is_binary();
439414
}

test/constexpr_schema_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ TEST_CASE("Basic test of very large energyplus JSON schema")
1010

1111
STATIC_REQUIRE(document["properties"sv]["Version"sv]["patternProperties"sv][".*"sv]["properties"sv]
1212
["version_identifier"sv]["default"sv]
13-
.as_string()
13+
.get<std::string_view>()
1414
== "22.1"sv);
1515
}

0 commit comments

Comments
 (0)