23 #include <boost/type_index.hpp> 26 #include <type_traits> 52 template <
typename SerializedT,
typename ObjectT,
typename... AdditionalArgs>
56 SerializedT
serialize(
const ObjectT &input, AdditionalArgs &&... args)
const 58 std::lock_guard<decltype(mutex_)> lg(
mutex_);
60 auto type_index = boost::typeindex::type_id_runtime(input);
66 return itr->second(input, std::forward<AdditionalArgs>(args)...);
68 ASSERT_LOG(
false,
"Cannot find an appropriate serializer for " +
69 type_index.pretty_name());
77 std::function<SerializedT(
const ObjectT &, AdditionalArgs &&...)>;
82 template <
typename T,
typename Callable>
85 static_assert(std::is_base_of<ObjectT, T>::value,
86 "T is not a subclass of ObjectT");
88 std::lock_guard<decltype(mutex_)> lg(
mutex_);
89 auto type_index = boost::typeindex::type_id<T>();
91 "Already got a serializer for this type of object.");
95 auto wrapper = [callable](
const ObjectT &base_obj,
96 AdditionalArgs &&... args) -> SerializedT {
99 const T &concrete_object =
reinterpret_cast<const T &
>(base_obj);
102 const T &extra_safety =
assert_cast<
const T &>(base_obj);
104 std::addressof(extra_safety),
106 return callable(concrete_object, std::forward<AdditionalArgs>(args)...);
111 template <
typename T>
114 std::lock_guard<decltype(mutex_)> lg(
mutex_);
116 auto type_index = boost::typeindex::type_id<T>();
122 std::map<boost::typeindex::type_index, SerializationCallable>
serializers_;
std::map< boost::typeindex::type_index, SerializationCallable > serializers_
This is the header file for a generated source file, GitSHA1.cpp.
Provides dynamic serializers management for a given object's hierarchy.
std::recursive_mutex mutex_
#define ASSERT_LOG(cond, msg)
std::function< json(const IUpdate &, AdditionalArgs &&...)> SerializationCallable
Callable type for serialization.
void register_serializer(Callable &&callable)
Register a serializer for T.
SerializedT serialize(const ObjectT &input, AdditionalArgs &&... args) const
std::enable_if_t< is_shared_ptr_v< Out >, Out > assert_cast(const std::shared_ptr< In > &in)
void unregister_serializer()