Bitcoin Core  31.0.0
P2P Digital Currency
type-number.h
Go to the documentation of this file.
1 // Copyright (c) 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 
10 namespace mp {
11 template <typename LocalType, typename Value>
12 LocalType 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 
23 template <typename LocalType, typename Value>
24 LocalType 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 
36 template <typename LocalType, typename Value>
37 LocalType 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 
47 template <typename LocalType, typename Input, typename ReadDest>
48 decltype(auto) CustomReadField(TypeList<LocalType>,
49  Priority<1>,
50  InvokeContext& invoke_context,
51  Input&& input,
52  ReadDest&& read_dest,
53  typename std::enable_if<std::is_enum<LocalType>::value>::type* enable = nullptr)
54 {
55  // Disable clang-tidy out-of-range enum value check which triggers when
56  // using an enum type that does not have a 0 value. The check correctly
57  // triggers when it detects that Cap'n Proto returns 0 when reading an
58  // integer field that is unset. But the warning is spurious because the
59  // corresponding BuildField call should never leave the field unset.
60  // NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange)
61  return read_dest.construct(static_cast<LocalType>(input.get()));
62 }
63 
64 template <typename LocalType, typename Input, typename ReadDest>
65 decltype(auto) CustomReadField(TypeList<LocalType>,
66  Priority<1>,
67  InvokeContext& invoke_context,
68  Input&& input,
69  ReadDest&& read_dest,
70  typename std::enable_if<std::is_integral<LocalType>::value>::type* enable = nullptr)
71 {
72  auto value = input.get();
73  if (value < std::numeric_limits<LocalType>::min() || value > std::numeric_limits<LocalType>::max()) {
74  throw std::range_error("out of bound int received");
75  }
76  return read_dest.construct(static_cast<LocalType>(value));
77 }
78 
79 template <typename LocalType, typename Input, typename ReadDest>
80 decltype(auto) CustomReadField(TypeList<LocalType>,
81  Priority<1>,
82  InvokeContext& invoke_context,
83  Input&& input,
84  ReadDest&& read_dest,
85  typename std::enable_if<std::is_floating_point<LocalType>::value>::type* enable = 0)
86 {
87  auto value = input.get();
88  static_assert(std::is_same<LocalType, decltype(value)>::value, "floating point type mismatch");
89  return read_dest.construct(value);
90 }
91 } // namespace mp
92 
93 #endif // MP_PROXY_TYPE_NUMBER_H
Function parameter type for prioritizing overloaded function calls that would otherwise be ambiguous...
Definition: util.h:108
Definition: common.h:29
Generic utility functions used by capnp code.
Definition: util.h:32
#define E
Definition: util_tests.cpp:562
Functions to serialize / deserialize common bitcoin types.
Definition: common-types.h:57
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
decltype(auto) CustomReadField(TypeList< LocalType >, Priority< 1 >, InvokeContext &invoke_context, Input &&input, ReadDest &&read_dest) requires Unserializable< LocalType
Overload multiprocess library&#39;s CustomReadField hook to allow any object with an Unserialize method t...
Definition: common-types.h:100
#define T(expected, seed, data)