|
| 1 | +// Copyright (c) 2023 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 BITCOIN_IPC_CAPNP_COMMON_TYPES_H |
| 6 | +#define BITCOIN_IPC_CAPNP_COMMON_TYPES_H |
| 7 | + |
| 8 | +#include <clientversion.h> |
| 9 | +#include <streams.h> |
| 10 | + |
| 11 | +#include <cstddef> |
| 12 | +#include <mp/proxy-types.h> |
| 13 | +#include <type_traits> |
| 14 | +#include <utility> |
| 15 | + |
| 16 | +namespace ipc { |
| 17 | +namespace capnp { |
| 18 | +//! Use SFINAE to define Serializeable<T> trait which is true if type T has a |
| 19 | +//! Serialize(stream) method, false otherwise. |
| 20 | +template <typename T> |
| 21 | +struct Serializable { |
| 22 | +private: |
| 23 | + template <typename C> |
| 24 | + static std::true_type test(decltype(std::declval<C>().Serialize(std::declval<std::nullptr_t&>()))*); |
| 25 | + template <typename> |
| 26 | + static std::false_type test(...); |
| 27 | + |
| 28 | +public: |
| 29 | + static constexpr bool value = decltype(test<T>(nullptr))::value; |
| 30 | +}; |
| 31 | + |
| 32 | +//! Use SFINAE to define Unserializeable<T> trait which is true if type T has |
| 33 | +//! an Unserialize(stream) method, false otherwise. |
| 34 | +template <typename T> |
| 35 | +struct Unserializable { |
| 36 | +private: |
| 37 | + template <typename C> |
| 38 | + static std::true_type test(decltype(std::declval<C>().Unserialize(std::declval<std::nullptr_t&>()))*); |
| 39 | + template <typename> |
| 40 | + static std::false_type test(...); |
| 41 | + |
| 42 | +public: |
| 43 | + static constexpr bool value = decltype(test<T>(nullptr))::value; |
| 44 | +}; |
| 45 | +} // namespace capnp |
| 46 | +} // namespace ipc |
| 47 | + |
| 48 | +//! Functions to serialize / deserialize common bitcoin types. |
| 49 | +namespace mp { |
| 50 | +//! Overload multiprocess library's CustomBuildField hook to allow any |
| 51 | +//! serializable object to be stored in a capnproto Data field or passed to a |
| 52 | +//! canproto interface. Use Priority<1> so this hook has medium priority, and |
| 53 | +//! higher priority hooks could take precedence over this one. |
| 54 | +template <typename LocalType, typename Value, typename Output> |
| 55 | +void CustomBuildField( |
| 56 | + TypeList<LocalType>, Priority<1>, InvokeContext& invoke_context, Value&& value, Output&& output, |
| 57 | + // Enable if serializeable and if LocalType is not cv or reference |
| 58 | + // qualified. If LocalType is cv or reference qualified, it is important to |
| 59 | + // fall back to lower-priority Priority<0> implementation of this function |
| 60 | + // that strips cv references, to prevent this CustomBuildField overload from |
| 61 | + // taking precedence over more narrow overloads for specific LocalTypes. |
| 62 | + std::enable_if_t<ipc::capnp::Serializable<LocalType>::value && |
| 63 | + std::is_same_v<LocalType, std::remove_cv_t<std::remove_reference_t<LocalType>>>>* enable = nullptr) |
| 64 | +{ |
| 65 | + DataStream stream; |
| 66 | + value.Serialize(stream); |
| 67 | + auto result = output.init(stream.size()); |
| 68 | + memcpy(result.begin(), stream.data(), stream.size()); |
| 69 | +} |
| 70 | + |
| 71 | +//! Overload multiprocess library's CustomReadField hook to allow any object |
| 72 | +//! with an Unserialize method to be read from a capnproto Data field or |
| 73 | +//! returned from canproto interface. Use Priority<1> so this hook has medium |
| 74 | +//! priority, and higher priority hooks could take precedence over this one. |
| 75 | +template <typename LocalType, typename Input, typename ReadDest> |
| 76 | +decltype(auto) |
| 77 | +CustomReadField(TypeList<LocalType>, Priority<1>, InvokeContext& invoke_context, Input&& input, ReadDest&& read_dest, |
| 78 | + std::enable_if_t<ipc::capnp::Unserializable<LocalType>::value>* enable = nullptr) |
| 79 | +{ |
| 80 | + return read_dest.update([&](auto& value) { |
| 81 | + if (!input.has()) return; |
| 82 | + auto data = input.get(); |
| 83 | + SpanReader stream({data.begin(), data.end()}); |
| 84 | + value.Unserialize(stream); |
| 85 | + }); |
| 86 | +} |
| 87 | +} // namespace mp |
| 88 | + |
| 89 | +#endif // BITCOIN_IPC_CAPNP_COMMON_TYPES_H |
0 commit comments