From 50e538d6ecd31752669283309b4f7902b60316d4 Mon Sep 17 00:00:00 2001 From: Gaute Lindkvist Date: Thu, 28 Sep 2023 14:01:24 +0200 Subject: [PATCH] Support serialising chrono time points --- Core/cafJsonDataType.h | 12 +++++++ Core/cafJsonSerializer.h | 24 ++++++++++++++ Core/cafPortableDataType.h | 2 ++ Core/cafUuidGenerator.cpp | 32 +++++++++---------- .../cafRestInterfaceBasicTest.cpp | 8 ++--- .../cafRpcClientPassByRefObjectFactory.cpp | 1 + 6 files changed, 59 insertions(+), 20 deletions(-) diff --git a/Core/cafJsonDataType.h b/Core/cafJsonDataType.h index 554d1838..651414a7 100644 --- a/Core/cafJsonDataType.h +++ b/Core/cafJsonDataType.h @@ -21,6 +21,7 @@ #include +#include #include #include #include @@ -107,4 +108,15 @@ struct JsonDataType } }; +template <> +struct JsonDataType +{ + static nlohmann::json type() + { + auto object = nlohmann::json::object(); + object["type"] = "integer"; + return object; + } +}; + } // namespace caffa \ No newline at end of file diff --git a/Core/cafJsonSerializer.h b/Core/cafJsonSerializer.h index acfcb9a2..ba57525a 100644 --- a/Core/cafJsonSerializer.h +++ b/Core/cafJsonSerializer.h @@ -22,6 +22,8 @@ #include +#include +#include #include #include @@ -105,4 +107,26 @@ class JsonSerializer : public Serializer void readObjectFromJson( ObjectHandle* object, const nlohmann::json& jsonObject ) const; void writeObjectToJson( const ObjectHandle* object, nlohmann::json& jsonObject ) const; }; + } // End of namespace caffa + +/** + * Serialiser for chrono time points as regular int64_t nanoseconds since epoch + */ +namespace nlohmann +{ +template +struct adl_serializer> +{ + static void to_json( json& j, const std::chrono::time_point& tp ) + { + j = std::chrono::duration_cast( tp.time_since_epoch() ).count(); + } + + static void from_json( const json& j, std::chrono::time_point& tp ) + { + Duration duration( j.get() ); + tp = std::chrono::steady_clock::time_point( duration ); + } +}; +} // namespace nlohmann diff --git a/Core/cafPortableDataType.h b/Core/cafPortableDataType.h index cb5c74d3..fa3dcb54 100644 --- a/Core/cafPortableDataType.h +++ b/Core/cafPortableDataType.h @@ -19,6 +19,7 @@ #pragma once +#include #include #include #include @@ -105,4 +106,5 @@ CAFFA_DEFINE_PORTABLE_TYPE_NAME( uint64_t, "uint64" ) CAFFA_DEFINE_PORTABLE_TYPE_NAME( int64_t, "int64" ) CAFFA_DEFINE_PORTABLE_TYPE_NAME( std::string, "string" ) CAFFA_DEFINE_PORTABLE_TYPE_NAME( void, "void" ) +CAFFA_DEFINE_PORTABLE_TYPE_NAME( std::chrono::steady_clock::time_point, "timestamp_ns" ) } // namespace caffa \ No newline at end of file diff --git a/Core/cafUuidGenerator.cpp b/Core/cafUuidGenerator.cpp index 2890d48f..94caafea 100644 --- a/Core/cafUuidGenerator.cpp +++ b/Core/cafUuidGenerator.cpp @@ -1,24 +1,24 @@ -//################################################################################################## +// ################################################################################################## // -// Custom Visualization Core library -// Copyright (C) 2022- Kontur AS +// Custom Visualization Core library +// Copyright (C) 2022- Kontur AS // -// This library may be used under the terms of the GNU Lesser General Public License as follows: +// This library may be used under the terms of the GNU Lesser General Public License as follows: // -// GNU Lesser General Public License Usage -// This library is free software; you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation; either version 2.1 of the License, or -// (at your option) any later version. +// GNU Lesser General Public License Usage +// This library is free software; you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. // -// This library is distributed in the hope that it will be useful, but WITHOUT ANY -// WARRANTY; without even the implied warranty of MERCHANTABILITY or -// FITNESS FOR A PARTICULAR PURPOSE. +// This library is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. // -// See the GNU Lesser General Public License at <> -// for more details. +// See the GNU Lesser General Public License at <> +// for more details. // -//################################################################################################## +// ################################################################################################## #include "cafUuidGenerator.h" #include "cafLogger.h" @@ -38,7 +38,7 @@ std::string UuidGenerator::generate() std::scoped_lock lock( s_mutex ); if ( !s_uuidGenerator ) { - auto now = system_clock::now(); + auto now = steady_clock::now(); auto nanoseconds_since_epoch = now.time_since_epoch(); auto seconds_since_epoch = duration_cast( nanoseconds_since_epoch ); unsigned nanoseconds_since_last_second = static_cast( diff --git a/RestInterface/RestInterface_UnitTests/cafRestInterfaceBasicTest.cpp b/RestInterface/RestInterface_UnitTests/cafRestInterfaceBasicTest.cpp index f002e3e4..2fe374b8 100644 --- a/RestInterface/RestInterface_UnitTests/cafRestInterfaceBasicTest.cpp +++ b/RestInterface/RestInterface_UnitTests/cafRestInterfaceBasicTest.cpp @@ -904,9 +904,9 @@ TEST_F( RestTest, LocalResponseTimeAndDataTransfer ) { serverDocument->demoObject->floatVector = { 42.0f }; - auto start_time = std::chrono::system_clock::now(); + auto start_time = std::chrono::steady_clock::now(); auto clientVector = clientDocument->demoObject->floatVector(); - auto end_time = std::chrono::system_clock::now(); + auto end_time = std::chrono::steady_clock::now(); auto duration = std::chrono::duration_cast( end_time - start_time ).count(); CAFFA_INFO( "Getting single float vector took " << duration << "µs" ); ASSERT_EQ( serverDocument->demoObject->floatVector(), clientDocument->demoObject->floatVector() ); @@ -924,9 +924,9 @@ TEST_F( RestTest, LocalResponseTimeAndDataTransfer ) serverDocument->demoObject->floatVector = serverVector; { - auto start_time = std::chrono::system_clock::now(); + auto start_time = std::chrono::steady_clock::now(); auto clientVector = clientDocument->demoObject->floatVector(); - auto end_time = std::chrono::system_clock::now(); + auto end_time = std::chrono::steady_clock::now(); auto duration = std::chrono::duration_cast( end_time - start_time ).count(); size_t KB = numberOfFloats * sizeof( float ) / ( 1024u ); CAFFA_INFO( "Transferred " << numberOfFloats << " floats for a total of " << KB << " KB" ); diff --git a/RpcBase/cafRpcClientPassByRefObjectFactory.cpp b/RpcBase/cafRpcClientPassByRefObjectFactory.cpp index 9d91be5b..7eea95f4 100644 --- a/RpcBase/cafRpcClientPassByRefObjectFactory.cpp +++ b/RpcBase/cafRpcClientPassByRefObjectFactory.cpp @@ -154,6 +154,7 @@ void ClientPassByRefObjectFactory::registerAllBasicAccessorCreators() registerBasicAccessorCreators(); registerBasicAccessorCreators(); registerBasicAccessorCreators(); + registerBasicAccessorCreators(); } } // namespace caffa::rpc