Branch data Line data Source code
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 : 0 : LocalType BuildPrimitive(InvokeContext& invoke_context,
13 : : const Value& value,
14 : : TypeList<LocalType>,
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 : 0 : return static_cast<LocalType>(value);
21 : : }
22 : :
23 : : template <typename LocalType, typename Value>
24 : 3 : LocalType BuildPrimitive(InvokeContext& invoke_context,
25 : : const Value& value,
26 : : TypeList<LocalType>,
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 : 3 : return value;
34 : : }
35 : :
36 : : template <typename LocalType, typename Value>
37 : : LocalType BuildPrimitive(InvokeContext& invoke_context,
38 : : const Value& value,
39 : : TypeList<LocalType>,
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 [ # # ]: 0 : 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 [ # # ]: 0 : return read_dest.construct(static_cast<LocalType>(input.get()));
62 : : }
63 : :
64 : : template <typename LocalType, typename Input, typename ReadDest>
65 [ + - ]: 3 : 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 : 3 : 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 [ - + ]: 3 : 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
|