Skip to content

Commit

Permalink
Support serialising chrono time points
Browse files Browse the repository at this point in the history
  • Loading branch information
lindkvis committed Sep 28, 2023
1 parent 321af28 commit 50e538d
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 20 deletions.
12 changes: 12 additions & 0 deletions Core/cafJsonDataType.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include <nlohmann/json.hpp>

#include <chrono>
#include <concepts>
#include <string>
#include <typeinfo>
Expand Down Expand Up @@ -107,4 +108,15 @@ struct JsonDataType<std::string>
}
};

template <>
struct JsonDataType<std::chrono::steady_clock::time_point>
{
static nlohmann::json type()
{
auto object = nlohmann::json::object();
object["type"] = "integer";
return object;
}
};

} // namespace caffa
24 changes: 24 additions & 0 deletions Core/cafJsonSerializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

#include <nlohmann/json.hpp>

#include <chrono>
#include <fstream>
#include <set>
#include <string>

Expand Down Expand Up @@ -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 <typename Clock, typename Duration>
struct adl_serializer<std::chrono::time_point<Clock, Duration>>
{
static void to_json( json& j, const std::chrono::time_point<Clock, Duration>& tp )
{
j = std::chrono::duration_cast<Duration>( tp.time_since_epoch() ).count();
}

static void from_json( const json& j, std::chrono::time_point<Clock, Duration>& tp )
{
Duration duration( j.get<int64_t>() );
tp = std::chrono::steady_clock::time_point( duration );
}
};
} // namespace nlohmann
2 changes: 2 additions & 0 deletions Core/cafPortableDataType.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#pragma once

#include <chrono>
#include <string>
#include <typeinfo>
#include <vector>
Expand Down Expand Up @@ -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
32 changes: 16 additions & 16 deletions Core/cafUuidGenerator.cpp
Original file line number Diff line number Diff line change
@@ -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 <<http://www.gnu.org/licenses/lgpl-2.1.html>>
// for more details.
// See the GNU Lesser General Public License at <<http://www.gnu.org/licenses/lgpl-2.1.html>>
// for more details.
//
//##################################################################################################
// ##################################################################################################
#include "cafUuidGenerator.h"

#include "cafLogger.h"
Expand All @@ -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<seconds>( nanoseconds_since_epoch );
unsigned nanoseconds_since_last_second = static_cast<unsigned>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::chrono::microseconds>( end_time - start_time ).count();
CAFFA_INFO( "Getting single float vector took " << duration << "µs" );
ASSERT_EQ( serverDocument->demoObject->floatVector(), clientDocument->demoObject->floatVector() );
Expand All @@ -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<std::chrono::milliseconds>( end_time - start_time ).count();
size_t KB = numberOfFloats * sizeof( float ) / ( 1024u );
CAFFA_INFO( "Transferred " << numberOfFloats << " floats for a total of " << KB << " KB" );
Expand Down
1 change: 1 addition & 0 deletions RpcBase/cafRpcClientPassByRefObjectFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ void ClientPassByRefObjectFactory::registerAllBasicAccessorCreators()
registerBasicAccessorCreators<uint64_t>();
registerBasicAccessorCreators<bool>();
registerBasicAccessorCreators<std::string>();
registerBasicAccessorCreators<std::chrono::steady_clock::time_point>();
}

} // namespace caffa::rpc

0 comments on commit 50e538d

Please sign in to comment.