Bitcoin Core 29.99.0
P2P Digital Currency
type-number.h
Go to the documentation of this file.
1// Copyright (c) 2025 The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#ifndef MP_PROXY_TYPE_NUMBER_H
6#define MP_PROXY_TYPE_NUMBER_H
7
8#include <mp/util.h>
9
10namespace mp {
11template <typename LocalType, typename Value>
12LocalType BuildPrimitive(InvokeContext& invoke_context,
13 const Value& value,
15 typename std::enable_if<std::is_enum<Value>::value>::type* enable = nullptr)
16{
17 using E = std::make_unsigned_t<std::underlying_type_t<Value>>;
18 using T = std::make_unsigned_t<LocalType>;
19 static_assert(std::numeric_limits<T>::max() >= std::numeric_limits<E>::max(), "mismatched integral/enum types");
20 return static_cast<LocalType>(value);
21}
22
23template <typename LocalType, typename Value>
24LocalType BuildPrimitive(InvokeContext& invoke_context,
25 const Value& value,
27 typename std::enable_if<std::is_integral<Value>::value, int>::type* enable = nullptr)
28{
29 static_assert(
30 std::numeric_limits<LocalType>::lowest() <= std::numeric_limits<Value>::lowest(), "mismatched integral types");
31 static_assert(
32 std::numeric_limits<LocalType>::max() >= std::numeric_limits<Value>::max(), "mismatched integral types");
33 return value;
34}
35
36template <typename LocalType, typename Value>
37LocalType BuildPrimitive(InvokeContext& invoke_context,
38 const Value& value,
40 typename std::enable_if<std::is_floating_point<Value>::value>::type* enable = nullptr)
41{
42 static_assert(std::is_same<Value, LocalType>::value,
43 "mismatched floating point types. please fix message.capnp type declaration to match wrapped interface");
44 return value;
45}
46
47template <typename LocalType, typename Input, typename ReadDest>
50 InvokeContext& invoke_context,
51 Input&& input,
52 ReadDest&& read_dest,
53 typename std::enable_if<std::is_enum<LocalType>::value>::type* enable = 0)
54{
55 return read_dest.construct(static_cast<LocalType>(input.get()));
56}
57
58template <typename LocalType, typename Input, typename ReadDest>
61 InvokeContext& invoke_context,
62 Input&& input,
63 ReadDest&& read_dest,
64 typename std::enable_if<std::is_integral<LocalType>::value>::type* enable = nullptr)
65{
66 auto value = input.get();
67 if (value < std::numeric_limits<LocalType>::min() || value > std::numeric_limits<LocalType>::max()) {
68 throw std::range_error("out of bound int received");
69 }
70 return read_dest.construct(static_cast<LocalType>(value));
71}
72
73template <typename LocalType, typename Input, typename ReadDest>
76 InvokeContext& invoke_context,
77 Input&& input,
78 ReadDest&& read_dest,
79 typename std::enable_if<std::is_floating_point<LocalType>::value>::type* enable = 0)
80{
81 auto value = input.get();
82 static_assert(std::is_same<LocalType, decltype(value)>::value, "floating point type mismatch");
83 return read_dest.construct(value);
84}
85} // namespace mp
86
87#endif // MP_PROXY_TYPE_NUMBER_H
#define T(expected, seed, data)
Functions to serialize / deserialize common bitcoin types.
Definition: common-types.h:57
decltype(auto) CustomReadField(TypeList< LocalType >, Priority< 1 >, InvokeContext &invoke_context, Input &&input, ReadDest &&read_dest)
Overload multiprocess library's CustomReadField hook to allow any object with an Unserialize method t...
Definition: common-types.h:83
LocalType BuildPrimitive(InvokeContext &invoke_context, const Value &value, TypeList< LocalType >, typename std::enable_if< std::is_enum< Value >::value >::type *enable=nullptr)
Definition: type-number.h:12
Function parameter type for prioritizing overloaded function calls that would otherwise be ambiguous.
Definition: util.h:109
Generic utility functions used by capnp code.
Definition: util.h:33
#define E
Definition: util_tests.cpp:546