Skip to content

Commit 798f4b5

Browse files
committed
1 parent a595a0b commit 798f4b5

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ set(MP_PUBLIC_HEADERS
4646
include/mp/proxy-types.h
4747
include/mp/proxy.h
4848
include/mp/type-char.h
49+
include/mp/type-chrono.h
4950
include/mp/type-context.h
5051
include/mp/type-decay.h
5152
include/mp/type-exception.h

include/mp/type-chrono.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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_CHRONO_H
6+
#define MP_PROXY_TYPE_CHRONO_H
7+
8+
#include <mp/util.h>
9+
10+
#include <chrono>
11+
12+
namespace mp {
13+
//! Overload CustomBuildField and CustomReadField to serialize std::chrono
14+
//! parameters and return values as numbers.
15+
template <class Rep, class Period, typename Value, typename Output>
16+
void CustomBuildField(TypeList<std::chrono::duration<Rep, Period>>, Priority<1>, InvokeContext& invoke_context, Value&& value,
17+
Output&& output)
18+
{
19+
static_assert(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(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+
26+
template <class Rep, class Period, typename Input, typename ReadDest>
27+
decltype(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+
} // namespace mp
33+
34+
#endif // MP_PROXY_TYPE_CHRONO_H

0 commit comments

Comments
 (0)