diff --git a/include/mp/type-chrono.h b/include/mp/type-chrono.h index d7154986..f58adddf 100644 --- a/include/mp/type-chrono.h +++ b/include/mp/type-chrono.h @@ -29,6 +29,29 @@ decltype(auto) CustomReadField(TypeList>, Pri { return read_dest.construct(input.get()); } + +//! Overload CustomBuildField and CustomReadField to serialize +//! std::chrono::time_point parameters and return values as integer tick counts. +//! The capnp field type must be an integer type with enough range to hold the +//! time_since_epoch() count for the given Duration (e.g. Int64 for nanoseconds). +template +void CustomBuildField(TypeList>, Priority<1>, InvokeContext& invoke_context, + Value&& value, Output&& output) +{ + using Rep = typename Duration::rep; + static_assert(std::numeric_limits::lowest() <= std::numeric_limits::lowest(), + "capnp type does not have enough range to hold lowest std::chrono::time_point value"); + static_assert(std::numeric_limits::max() >= std::numeric_limits::max(), + "capnp type does not have enough range to hold highest std::chrono::time_point value"); + output.set(value.time_since_epoch().count()); +} + +template +decltype(auto) CustomReadField(TypeList>, Priority<1>, + InvokeContext& invoke_context, Input&& input, ReadDest&& read_dest) +{ + return read_dest.construct(std::chrono::time_point{Duration{input.get()}}); +} } // namespace mp #endif // MP_PROXY_TYPE_CHRONO_H