Verified Commit 0f28059b authored by Didier WECKMANN's avatar Didier WECKMANN
Browse files

feat(io): allow custom serializers for any object, even when defined outside sight

parent f1dafbf8
Pipeline #181155 failed with stages
in 6 seconds
......@@ -34,7 +34,7 @@
namespace sight::io::session
{
namespace detail::Helper
namespace Helper
{
constexpr static auto s_Value {"Value"};
......@@ -203,6 +203,6 @@ inline static typename T::sptr deserialize(
return casted;
}
} // namespace detail::Helper
} // namespace Helper
} // namespace sight::io
......@@ -45,7 +45,7 @@ public:
/// Constructor
inline SessionReaderImpl(SessionReader* const sessionReader) :
m_SessionReader(sessionReader),
m_sessionReader(sessionReader),
m_password(std::make_unique<PasswordKeeper>()),
m_encryptionPolicy(PasswordKeeper::EncryptionPolicy::DEFAULT)
{
......@@ -57,16 +57,22 @@ public:
/// Read the session from archive.
inline void read()
{
// Create the session and deserialize the root object
detail::SessionDeserializer session;
m_object = session.deserialize(m_SessionReader->getFile(), m_password->getPassword(), m_encryptionPolicy);
// Deserialize the root object
m_object = m_sessionDeserializer.deserialize(
m_sessionReader->getFile(),
m_password->getPassword(),
m_encryptionPolicy
);
}
/// Session deserializer which perform the deserialization
detail::SessionDeserializer m_sessionDeserializer;
/// Use a shared_ptr to keep the object alive as it is the read() return value
core::tools::Object::sptr m_object;
/// Pointer to the public interface
SessionReader* const m_SessionReader;
SessionReader* const m_sessionReader;
/// Keep the password in a vault
const std::unique_ptr<PasswordKeeper> m_password;
......@@ -114,4 +120,18 @@ void SessionReader::setEncryptionPolicy(const PasswordKeeper::EncryptionPolicy p
m_pimpl->m_encryptionPolicy = policy;
}
//------------------------------------------------------------------------------
void SessionReader::setDeserializer(const std::string& className, deserializer_t deserializer)
{
m_pimpl->m_sessionDeserializer.setDeserializer(className, deserializer);
}
//------------------------------------------------------------------------------
void SessionReader::setDefaultDeserializer(const std::string& className, deserializer_t deserializer)
{
detail::SessionDeserializer::setDefaultDeserializer(className, deserializer);
}
} // namespace sight::io::session
......@@ -27,11 +27,25 @@
#include <core/crypto/secure_string.hpp>
#include <core/location/SingleFile.hpp>
#include <data/Object.hpp>
#include <io/base/reader/IObjectReader.hpp>
#include <io/zip/ArchiveReader.hpp>
#include <boost/property_tree/ptree.hpp>
namespace sight::io::session
{
/// The deserializer function signature. This is used to register an object deserialization function.
using deserializer_t = std::function<data::Object::sptr(
zip::ArchiveReader&,
const boost::property_tree::ptree&,
const std::map<std::string, data::Object::sptr>&,
data::Object::sptr,
const core::crypto::secure_string&
)>;
/**
* @brief Session reader.
*
......@@ -79,6 +93,19 @@ public:
/// @param policy the encryption policy: @see sight::io::session::PasswordKeeper::EncryptionPolicy
IO_SESSION_API void setEncryptionPolicy(const PasswordKeeper::EncryptionPolicy policy);
/// Set a deserialization function for an object
/// @param className the name of the object to serialize
/// @param deserializer the function pointer to the deserialization function
IO_SESSION_API void setDeserializer(const std::string& className, deserializer_t deserializer = nullptr);
/// Set a default deserialization function for an object
/// @param className the name of the object to serialize
/// @param deserializer the function pointer to the deserialization function
IO_SESSION_API static void setDefaultDeserializer(
const std::string& className,
deserializer_t deserializer = nullptr
);
private:
/// PImpl
......
......@@ -45,7 +45,7 @@ public:
/// Constructor
inline SessionWriterImpl(SessionWriter* const sessionWriter) :
m_SessionWriter(sessionWriter),
m_sessionWriter(sessionWriter),
m_password(std::make_unique<PasswordKeeper>()),
m_encryptionPolicy(PasswordKeeper::EncryptionPolicy::DEFAULT)
{
......@@ -58,21 +58,23 @@ public:
inline void write()
{
// Retrieve the root object
auto root_object = std::dynamic_pointer_cast<const data::Object>(m_SessionWriter->getObject());
auto root_object = std::dynamic_pointer_cast<const data::Object>(m_sessionWriter->getObject());
SIGHT_FATAL_IF("Root object is null or not a data object.", !root_object);
// Create the session and serialize the root object
detail::SessionSerializer session;
session.serialize(
m_SessionWriter->getFile(),
// Serialize the root object
m_sessionSerializer.serialize(
m_sessionWriter->getFile(),
root_object,
m_password->getPassword(),
m_encryptionPolicy
);
}
/// Session serializer which perform the serialization
detail::SessionSerializer m_sessionSerializer;
/// Pointer to the public interface
SessionWriter* const m_SessionWriter;
SessionWriter* const m_sessionWriter;
/// Keep the password in a vault
const std::unique_ptr<PasswordKeeper> m_password;
......@@ -117,4 +119,18 @@ void SessionWriter::setEncryptionPolicy(const PasswordKeeper::EncryptionPolicy p
m_pimpl->m_encryptionPolicy = policy;
}
//------------------------------------------------------------------------------
void SessionWriter::setSerializer(const std::string& className, serializer_t serializer)
{
m_pimpl->m_sessionSerializer.setSerializer(className, serializer);
}
//------------------------------------------------------------------------------
void SessionWriter::setDefaultSerializer(const std::string& className, serializer_t serializer)
{
detail::SessionSerializer::setDefaultSerializer(className, serializer);
}
} //namespace sight::io::session
......@@ -27,11 +27,25 @@
#include <core/crypto/secure_string.hpp>
#include <core/location/SingleFile.hpp>
#include <data/Object.hpp>
#include <io/base/writer/IObjectWriter.hpp>
#include <io/zip/ArchiveWriter.hpp>
#include <boost/property_tree/ptree.hpp>
namespace sight::io::session
{
/// The serializer function signature. This is used to register an object serialization function.
using serializer_t = std::function<void (
zip::ArchiveWriter&,
boost::property_tree::ptree&,
data::Object::csptr,
std::map<std::string, data::Object::csptr>&,
const core::crypto::secure_string&
)>;
/**
* @brief Session writer.
*
......@@ -80,6 +94,16 @@ public:
/// @param policy the encryption policy: @see sight::io::session::PasswordKeeper::EncryptionPolicy
IO_SESSION_API void setEncryptionPolicy(const PasswordKeeper::EncryptionPolicy policy);
/// Set a serialization function for an object
/// @param className the name of the object to serialize
/// @param serializer the function pointer to the serialization function
IO_SESSION_API void setSerializer(const std::string& className, serializer_t serializer = nullptr);
/// Set a default serialization function for an object
/// @param className the name of the object to serialize
/// @param serializer the function pointer to the serialization function
IO_SESSION_API static void setDefaultSerializer(const std::string& className, serializer_t serializer = nullptr);
private:
/// PImpl
......
......@@ -22,8 +22,8 @@
#pragma once
#include "io/session/config.hpp"
#include "io/session/detail/Helper.hpp"
#include "io/session/detail/Series.hpp"
#include "io/session/Helper.hpp"
#include <data/ActivitySeries.hpp>
#include <data/Composite.hpp>
......
......@@ -22,7 +22,7 @@
#pragma once
#include "io/session/config.hpp"
#include "io/session/detail/Helper.hpp"
#include "io/session/Helper.hpp"
#include <data/Array.hpp>
......
......@@ -22,7 +22,7 @@
#pragma once
#include "io/session/config.hpp"
#include "io/session/detail/Helper.hpp"
#include "io/session/Helper.hpp"
#include <data/CalibrationInfo.hpp>
#include <data/Image.hpp>
......
......@@ -22,7 +22,7 @@
#pragma once
#include "io/session/config.hpp"
#include "io/session/detail/Helper.hpp"
#include "io/session/Helper.hpp"
#include <data/Camera.hpp>
......
......@@ -22,8 +22,8 @@
#pragma once
#include "io/session/config.hpp"
#include "io/session/detail/Helper.hpp"
#include "io/session/detail/Series.hpp"
#include "io/session/Helper.hpp"
#include <data/Camera.hpp>
#include <data/CameraSeries.hpp>
......
......@@ -22,7 +22,7 @@
#pragma once
#include "io/session/config.hpp"
#include "io/session/detail/Helper.hpp"
#include "io/session/Helper.hpp"
#include <data/Color.hpp>
......
......@@ -22,7 +22,7 @@
#pragma once
#include "io/session/config.hpp"
#include "io/session/detail/Helper.hpp"
#include "io/session/Helper.hpp"
#include <data/Composite.hpp>
......
......@@ -22,8 +22,8 @@
#pragma once
#include "io/session/config.hpp"
#include "io/session/detail/Helper.hpp"
#include "io/session/detail/Series.hpp"
#include "io/session/Helper.hpp"
#include <data/DicomSeries.hpp>
......
......@@ -22,7 +22,7 @@
#pragma once
#include "io/session/config.hpp"
#include "io/session/detail/Helper.hpp"
#include "io/session/Helper.hpp"
#include <data/Edge.hpp>
......
......@@ -22,7 +22,7 @@
#pragma once
#include "io/session/config.hpp"
#include "io/session/detail/Helper.hpp"
#include "io/session/Helper.hpp"
#include <data/Equipment.hpp>
......
......@@ -22,7 +22,7 @@
#pragma once
#include "io/session/config.hpp"
#include "io/session/detail/Helper.hpp"
#include "io/session/Helper.hpp"
#include <data/Edge.hpp>
#include <data/Graph.hpp>
......
......@@ -22,7 +22,7 @@
#pragma once
#include "io/session/config.hpp"
#include "io/session/detail/Helper.hpp"
#include "io/session/Helper.hpp"
#include <data/Histogram.hpp>
......
......@@ -22,7 +22,7 @@
#pragma once
#include "io/session/config.hpp"
#include "io/session/detail/Helper.hpp"
#include "io/session/Helper.hpp"
#include <data/Image.hpp>
......
......@@ -22,8 +22,8 @@
#pragma once
#include "io/session/config.hpp"
#include "io/session/detail/Helper.hpp"
#include "io/session/detail/Series.hpp"
#include "io/session/Helper.hpp"
#include <data/ImageSeries.hpp>
......
......@@ -22,7 +22,7 @@
#pragma once
#include "io/session/config.hpp"
#include "io/session/detail/Helper.hpp"
#include "io/session/Helper.hpp"
#include <data/Landmarks.hpp>
#include <data/Point.hpp>
......
Supports Markdown
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