Bitcoin Core 32.99.0
P2P Digital Currency
Loading...
Searching...
No Matches
type-chrono.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_CHRONO_H
6#define MP_PROXY_TYPE_CHRONO_H
7
8#include <mp/util.h>
9
10#include <chrono>
11
12namespace mp {
15template <class Rep, class Period, typename Value, typename Output>
16void CustomBuildField(TypeList<std::chrono::duration<Rep, Period>>, Priority<1>, InvokeContext&, Value&& value,
17 Output&& output)
18{
19 static_assert(safe_less_equal(std::numeric_limits<decltype(output.get())>::lowest(), std::numeric_limits<Rep>::lowest()),
20 "capnp type does not have enough range to hold lowest std::chrono::duration value");
21 static_assert(safe_greater_equal(std::numeric_limits<decltype(output.get())>::max(), std::numeric_limits<Rep>::max()),
22 "capnp type does not have enough range to hold highest std::chrono::duration value");
23 output.set(value.count());
24}
25
26template <class Rep, class Period, typename Input, typename ReadDest>
27decltype(auto) CustomReadField(TypeList<std::chrono::duration<Rep, Period>>, Priority<1>, InvokeContext& invoke_context,
28 Input&& input, ReadDest&& read_dest)
29{
30 return read_dest.construct(input.get());
31}
32
37template <class Clock, class Duration, typename Value, typename Output>
38void CustomBuildField(TypeList<std::chrono::time_point<Clock, Duration>>, Priority<1>, InvokeContext&,
39 Value&& value, Output&& output)
40{
41 using Rep = typename Duration::rep;
42 static_assert(safe_less_equal(std::numeric_limits<decltype(output.get())>::lowest(), std::numeric_limits<Rep>::lowest()),
43 "capnp type does not have enough range to hold lowest std::chrono::time_point value");
44 static_assert(safe_greater_equal(std::numeric_limits<decltype(output.get())>::max(), std::numeric_limits<Rep>::max()),
45 "capnp type does not have enough range to hold highest std::chrono::time_point value");
46 output.set(value.time_since_epoch().count());
47}
48
49template <class Clock, class Duration, typename Input, typename ReadDest>
50decltype(auto) CustomReadField(TypeList<std::chrono::time_point<Clock, Duration>>, Priority<1>,
52{
53 return read_dest.construct(std::chrono::time_point<Clock, Duration>{Duration{input.get()}});
54}
55} // namespace mp
56
57#endif // MP_PROXY_TYPE_CHRONO_H
Functions to serialize / deserialize common bitcoin types.
constexpr bool safe_less_equal(A a, B b)
Generic utility functions used by capnp code.
Definition util.h:39
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...
constexpr bool safe_greater_equal(A a, B b)
Definition util.h:45
void CustomBuildField(TypeList< LocalType >, Priority< 1 >, InvokeContext &invoke_context, Value &&value, Output &&output)
Overload multiprocess library's CustomBuildField hook to allow any serializable object to be stored i...
Function parameter type for prioritizing overloaded function calls that would otherwise be ambiguous.
Definition util.h:143
Type holding a list of types.
Definition util.h:57
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition time.h:82