Squashed 'src/univalue/' changes from a44caf65fe..6c19d050a9

6c19d050a9 Merge bitcoin-core/univalue-subtree#33: Add getInt<Integral>() helper
09e4a930fc Add getInt helper
10619e0d9a Merge bitcoin-core/univalue#32: refactor: include-what-you-use
431cdf5d27 refactor: use constexpr where appropriate
64fc881fa4 refactor: cleanup headers for iwyu
9c35bf38eb Merge bitcoin-core/univalue-subtree#30: doc: note that our API has diverged from upstream
09b65facb9 doc: note that our API has diverged from upstream

git-subtree-dir: src/univalue
git-subtree-split: 6c19d050a9bcb2be216121db0df57c930a9ee12e
This commit is contained in:
MacroFake
2022-05-12 11:51:51 +02:00
parent 9b49ed656f
commit f403531f97
12 changed files with 78 additions and 100 deletions

View File

@@ -6,13 +6,14 @@
#ifndef __UNIVALUE_H__
#define __UNIVALUE_H__
#include <stdint.h>
#include <string.h>
#include <string>
#include <vector>
#include <charconv>
#include <cstdint>
#include <cstring>
#include <map>
#include <cassert>
#include <stdexcept>
#include <string>
#include <type_traits>
#include <vector>
class UniValue {
public:
@@ -168,10 +169,24 @@ public:
// value is of unexpected type
const std::vector<std::string>& getKeys() const;
const std::vector<UniValue>& getValues() const;
template <typename Int>
auto getInt() const
{
static_assert(std::is_integral<Int>::value);
if (typ != VNUM) {
throw std::runtime_error("JSON value is not an integer as expected");
}
Int result;
const auto [first_nonmatching, error_condition] = std::from_chars(val.data(), val.data() + val.size(), result);
if (first_nonmatching != val.data() + val.size() || error_condition != std::errc{}) {
throw std::runtime_error("JSON integer out of range");
}
return result;
}
bool get_bool() const;
const std::string& get_str() const;
int get_int() const;
int64_t get_int64() const;
auto get_int() const { return getInt<int>(); };
auto get_int64() const { return getInt<int64_t>(); };
double get_real() const;
const UniValue& get_obj() const;
const UniValue& get_array() const;