Commit 9a9c7bc6 authored by Matthias Vallentin's avatar Matthias Vallentin Committed by Dominik Charousset

Add a portable timestamp representation

This patch also makes instances of std::chrono::duration serializable.

Closes #510.
parent 2ebb7d6a
Subproject commit 2904d8d37d0442b641a881a01dad46053ea68a7f
Subproject commit f13969fb63a1eb6c6f6c06dc178f18a0bcc7bca2
......@@ -29,6 +29,7 @@
#include "caf/fwd.hpp"
#include "caf/atom.hpp"
#include "caf/error.hpp"
#include "caf/timestamp.hpp"
#include "caf/allowed_unsafe_message_type.hpp"
#include "caf/meta/annotation.hpp"
......@@ -432,6 +433,20 @@ public:
return convert_apply(dref(), x, tmp, assign);
}
template <class Duration>
error apply(std::chrono::time_point<std::chrono::system_clock, Duration>& t) {
if (Derived::reads_state) {
auto dur = t.time_since_epoch();
return apply(dur);
}
if (Derived::writes_state) {
Duration dur;
auto e = apply(dur);
t = std::chrono::time_point<std::chrono::system_clock, Duration>{dur};
return e;
}
}
/// Applies this processor to a raw block of data of size `num_bytes`.
virtual error apply_raw(size_t num_bytes, void* data) = 0;
......
......@@ -21,6 +21,7 @@
#define CAF_DETAIL_TYPE_TRAITS_HPP
#include <tuple>
#include <chrono>
#include <string>
#include <vector>
#include <utility>
......@@ -28,6 +29,7 @@
#include <type_traits>
#include "caf/fwd.hpp"
#include "caf/timestamp.hpp"
#include "caf/detail/type_list.hpp"
......@@ -138,17 +140,24 @@ struct is_one_of<X, X, Ts...> : std::true_type {};
template <class X, typename T0, class... Ts>
struct is_one_of<X, T0, Ts...> : is_one_of<X, Ts...> {};
/// Checks whether `T` is a `std::chrono::duration`.
template <class T>
struct is_duration : std::false_type {};
template <class Period, class Rep>
struct is_duration<std::chrono::duration<Period, Rep>> : std::true_type {};
/// Checks wheter `T` is considered a builtin type.
///
/// Builtin types are: (1) all arithmetic types, (2) string types from the STL,
/// and (3) built-in types such as `actor_ptr`.
/// Builtin types are: (1) all arithmetic types (including time types), (2)
/// string types from the STL, and (3) built-in types such as `actor_ptr`.
template <class T>
struct is_builtin {
// all arithmetic types are considered builtin
static constexpr bool value = std::is_arithmetic<T>::value
|| is_one_of<T, std::string, std::u16string,
std::u32string, atom_value,
message, actor, group,
|| is_duration<T>::value
|| is_one_of<T, timestamp, std::string,
std::u16string, std::u32string,
atom_value, message, actor, group,
node_id>::value;
};
......
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright (C) 2011 - 2016 *
* Dominik Charousset <dominik.charousset (at) haw-hamburg.de> *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#ifndef CAF_TIMESTAMP_HPP
#define CAF_TIMESTAMP_HPP
#include <cstdint>
#include <chrono>
namespace caf {
/// A portable timestamp with nanosecond resolution anchored at the UNIX epoch.
using timestamp = std::chrono::time_point<
std::chrono::system_clock,
std::chrono::duration<int64_t, std::nano>
>;
} // namespace caf
#endif // CAF_TIMESTAMP_HPP
......@@ -28,6 +28,7 @@
#include "caf/fwd.hpp"
#include "caf/atom.hpp"
#include "caf/timestamp.hpp"
#include "caf/detail/type_list.hpp"
#include "caf/detail/squashed_int.hpp"
......@@ -46,6 +47,7 @@ using sorted_builtin_types =
std::vector<char>, // @charbuf
down_msg, // @down
duration, // @duration
timestamp, // @timestamp
error, // @error
exit_msg, // @exit
group, // @group
......
......@@ -36,6 +36,7 @@
#include "caf/logger.hpp"
#include "caf/message.hpp"
#include "caf/duration.hpp"
#include "caf/timestamp.hpp"
#include "caf/actor_cast.hpp"
#include "caf/actor_system.hpp"
#include "caf/actor_factory.hpp"
......@@ -60,6 +61,7 @@ const char* numbered_type_names[] = {
"@charbuf",
"@down",
"@duration",
"@timestamp",
"@error",
"@exit",
"@group",
......
......@@ -141,6 +141,8 @@ struct fixture {
int32_t i32 = -345;
float f32 = 3.45f;
double f64 = 54.3;
timestamp::duration dur = timestamp::duration{1478715821 * 1000000000ll};
timestamp ts = timestamp{dur};
test_enum te = test_enum::b;
string str = "Lorem ipsum dolor sit amet.";
raw_struct rs;
......@@ -195,7 +197,7 @@ struct fixture {
: system(cfg),
context(&system) {
rs.str.assign(string(str.rbegin(), str.rend()));
msg = make_message(i32, te, str, rs);
msg = make_message(i32, dur, ts, te, str, rs);
}
};
......@@ -261,6 +263,20 @@ CAF_TEST(double_values) {
CAF_CHECK_EQUAL(f64, x);
}
CAF_TEST(duration_values) {
auto buf = serialize(dur);
timestamp::duration x;
deserialize(buf, x);
CAF_CHECK_EQUAL(dur, x);
}
CAF_TEST(timestamp_values) {
auto buf = serialize(ts);
timestamp x;
deserialize(buf, x);
CAF_CHECK_EQUAL(ts, x);
}
CAF_TEST(enum_classes) {
auto buf = serialize(te);
test_enum x;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment