mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-11 22:50:59 +01:00
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:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user