mirror of
https://github.com/fraillt/bitsery.git
synced 2026-10-06 08:55:37 +00:00
added polymorphism support for raw pointers
This commit is contained in:
@@ -24,309 +24,78 @@
|
||||
#define BITSERY_EXT_POINTER_H
|
||||
|
||||
#include "../traits/core/traits.h"
|
||||
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include "utils/pointer_utils.h"
|
||||
#include "utils/rtti_utils.h"
|
||||
|
||||
namespace bitsery {
|
||||
|
||||
namespace ext {
|
||||
|
||||
//forward declare
|
||||
class PointerLinkingContext;
|
||||
|
||||
enum class PointerType {
|
||||
Nullable,
|
||||
NotNull
|
||||
};
|
||||
|
||||
enum class PointerOwnershipType:uint8_t {
|
||||
//is not responsible for pointer lifetime management.
|
||||
Observer,
|
||||
//only ONE owner is responsible for this pointers creation/destruction
|
||||
Owner,
|
||||
//MANY shared owners is responsible for pointer creation/destruction
|
||||
//requires additional context to manage shared owners themselves.
|
||||
Shared
|
||||
};
|
||||
|
||||
namespace details_pointer {
|
||||
|
||||
//helper functions that creates or destroys pointers
|
||||
//useful, because can be specialized
|
||||
template <typename T>
|
||||
void destroyObject(T &ptr) {
|
||||
if (ptr) {
|
||||
delete ptr;
|
||||
}
|
||||
ptr = nullptr;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void createObject(T &ptr) {
|
||||
using TNonPtr = typename std::remove_pointer<T>::type;
|
||||
if (ptr == nullptr)
|
||||
ptr = new TNonPtr{};
|
||||
}
|
||||
|
||||
|
||||
template <typename S, typename TPtr>
|
||||
void serializeObject(S &s, const TPtr &obj) {
|
||||
using TNonPtr = typename std::remove_pointer<TPtr>::type;
|
||||
static_assert(!std::is_polymorphic<TNonPtr>::value, "Polymorphic types are not supported");
|
||||
serializeImpl(s, *obj, details::IsFundamentalType<TNonPtr>{});
|
||||
}
|
||||
|
||||
template <typename S, typename T>
|
||||
void serializeImpl(S& s, const T& obj, std::true_type) {
|
||||
s.template value<sizeof(T)>(obj);
|
||||
}
|
||||
|
||||
template <typename S, typename T>
|
||||
void serializeImpl(S& s, const T& obj, std::false_type) {
|
||||
s.object(obj);
|
||||
}
|
||||
|
||||
template <typename S, typename TPtr>
|
||||
void deserializeObject(S &s, TPtr &obj) {
|
||||
using TNonPtr = typename std::remove_pointer<TPtr>::type;
|
||||
static_assert(!std::is_polymorphic<TNonPtr>::value, "Polymorphic types are not supported");
|
||||
deserializeImpl(s, *obj, details::IsFundamentalType<TNonPtr>{});
|
||||
}
|
||||
|
||||
template <typename S, typename T>
|
||||
void deserializeImpl(S& s, T& obj, std::true_type) {
|
||||
s.template value<sizeof(T)>(obj);
|
||||
}
|
||||
|
||||
template <typename S, typename T>
|
||||
void deserializeImpl(S& s, T& obj, std::false_type) {
|
||||
s.object(obj);
|
||||
}
|
||||
|
||||
class RawPointerManager {
|
||||
public:
|
||||
template <typename Writer, typename Ser, typename T>
|
||||
static void serializeObject(Writer& , Ser& ser, const T& obj) {
|
||||
|
||||
details_pointer::serializeObject(ser, obj);
|
||||
}
|
||||
|
||||
template <typename Reader, typename Des, typename T>
|
||||
static void deserializeObject(Reader& , Des& des, T& obj) {
|
||||
details_pointer::createObject(obj);
|
||||
details_pointer::deserializeObject(des, obj);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static void destroyObject(T& obj) {
|
||||
details_pointer::destroyObject(obj);
|
||||
}
|
||||
};
|
||||
|
||||
class PointerLinkingContextSerialization {
|
||||
public:
|
||||
explicit PointerLinkingContextSerialization()
|
||||
:_currId{0},
|
||||
_ptrMap{}
|
||||
{}
|
||||
|
||||
PointerLinkingContextSerialization(const PointerLinkingContextSerialization&) = delete;
|
||||
PointerLinkingContextSerialization& operator = (const PointerLinkingContextSerialization&) = delete;
|
||||
|
||||
PointerLinkingContextSerialization(PointerLinkingContextSerialization&&) = default;
|
||||
PointerLinkingContextSerialization& operator = (PointerLinkingContextSerialization&&) = default;
|
||||
~PointerLinkingContextSerialization() = default;
|
||||
|
||||
struct PointerInfo {
|
||||
PointerInfo(size_t id_, PointerOwnershipType ownershipType_)
|
||||
:id{id_},
|
||||
ownershipType{ownershipType_},
|
||||
sharedCount{0}
|
||||
{};
|
||||
size_t id;
|
||||
PointerOwnershipType ownershipType;
|
||||
size_t sharedCount;
|
||||
};
|
||||
|
||||
const PointerInfo& getInfoByPtr(const void *ptr, PointerOwnershipType ptrType) {
|
||||
auto res = _ptrMap.emplace(ptr, PointerInfo{_currId + 1u, ptrType});
|
||||
auto& ptrInfo = res.first->second;
|
||||
if (res.second) {
|
||||
++_currId;
|
||||
return ptrInfo;
|
||||
}
|
||||
//ptr already exists
|
||||
//for observer return success
|
||||
if (ptrType == PointerOwnershipType::Observer)
|
||||
return ptrInfo;
|
||||
//set owner and return success
|
||||
if (ptrInfo.ownershipType == PointerOwnershipType::Observer) {
|
||||
ptrInfo.ownershipType = ptrType;
|
||||
return ptrInfo;
|
||||
}
|
||||
//only shared ownership can get here multiple times
|
||||
assert(ptrType == PointerOwnershipType::Shared);
|
||||
ptrInfo.sharedCount++;
|
||||
return ptrInfo;
|
||||
}
|
||||
|
||||
//valid, when all pointers have owners.
|
||||
//we cannot serialize pointers, if we haven't serialized objects themselves
|
||||
bool isPointerSerializationValid() const {
|
||||
return std::all_of(_ptrMap.begin(), _ptrMap.end(), [](const std::pair<const void*, PointerInfo>& p) {
|
||||
return p.second.ownershipType != PointerOwnershipType::Observer;
|
||||
});
|
||||
}
|
||||
|
||||
private:
|
||||
size_t _currId;
|
||||
std::unordered_map<const void*, PointerInfo> _ptrMap;
|
||||
|
||||
};
|
||||
|
||||
//this class is used to store context for shared ptr owners
|
||||
struct SharedContextBase {
|
||||
virtual ~SharedContextBase() = default;
|
||||
};
|
||||
|
||||
class PointerLinkingContextDeserialization {
|
||||
public:
|
||||
explicit PointerLinkingContextDeserialization()
|
||||
: _idMap{}
|
||||
{}
|
||||
|
||||
PointerLinkingContextDeserialization(const PointerLinkingContextDeserialization&) = delete;
|
||||
PointerLinkingContextDeserialization& operator = (const PointerLinkingContextDeserialization&) = delete;
|
||||
|
||||
PointerLinkingContextDeserialization(PointerLinkingContextDeserialization&&) = default;
|
||||
PointerLinkingContextDeserialization& operator = (PointerLinkingContextDeserialization&&) = default;
|
||||
~PointerLinkingContextDeserialization() = default;
|
||||
|
||||
|
||||
struct PointerInfo {
|
||||
PointerInfo(size_t id_, void* ptr, PointerOwnershipType ownershipType_)
|
||||
:id{id_},
|
||||
ownershipType{ownershipType_},
|
||||
ownerPtr{ptr},
|
||||
observersList{},
|
||||
sharedContext{}
|
||||
{};
|
||||
PointerInfo(const PointerInfo&) = delete;
|
||||
PointerInfo& operator = (const PointerInfo&) = delete;
|
||||
PointerInfo(PointerInfo&&) = default;
|
||||
PointerInfo&operator = (PointerInfo&&) = default;
|
||||
~PointerInfo() = default;
|
||||
|
||||
void processOwner(void* ptr) {
|
||||
ownerPtr = ptr;
|
||||
assert(ownershipType != PointerOwnershipType::Observer);
|
||||
for (auto& o:observersList)
|
||||
o.get() = ptr;
|
||||
observersList.clear();
|
||||
observersList.shrink_to_fit();
|
||||
}
|
||||
void processObserver(void* (&ptr)) {
|
||||
if (ownerPtr) {
|
||||
ptr = ownerPtr;
|
||||
} else {
|
||||
observersList.push_back(ptr);
|
||||
}
|
||||
}
|
||||
size_t id;
|
||||
PointerOwnershipType ownershipType;
|
||||
void* ownerPtr;
|
||||
std::vector<std::reference_wrapper<void*>> observersList;
|
||||
std::unique_ptr<SharedContextBase> sharedContext;
|
||||
};
|
||||
|
||||
PointerInfo& getInfoById(size_t id, PointerOwnershipType ptrType) {
|
||||
auto res = _idMap.emplace(id, PointerInfo{id, nullptr, ptrType});
|
||||
auto& ptrInfo = res.first->second;
|
||||
if (!res.second) {
|
||||
assert(ptrType != PointerOwnershipType::Owner || ptrInfo.ownershipType == PointerOwnershipType::Observer);
|
||||
if (ptrInfo.ownershipType == PointerOwnershipType::Observer)
|
||||
ptrInfo.ownershipType = ptrType;
|
||||
}
|
||||
return ptrInfo;
|
||||
}
|
||||
|
||||
//valid, when all pointers has owners
|
||||
bool isPointerDeserializationValid() const {
|
||||
return std::all_of(_idMap.begin(), _idMap.end(), [](const std::pair<const size_t, PointerInfo>& p) {
|
||||
return p.second.ownershipType != PointerOwnershipType::Observer;
|
||||
});
|
||||
}
|
||||
|
||||
private:
|
||||
std::unordered_map<size_t, PointerInfo> _idMap;
|
||||
};
|
||||
|
||||
template <typename S>
|
||||
PointerLinkingContext& getLinkingContext(S& s) {
|
||||
template<typename S>
|
||||
PointerLinkingContext &getLinkingContext(S &s) {
|
||||
auto res = s.template context<PointerLinkingContext>();
|
||||
assert(res != nullptr);
|
||||
return *res;
|
||||
}
|
||||
|
||||
|
||||
template<typename TObject>
|
||||
struct RawPointerObjectHandler {
|
||||
|
||||
using TPointer = TObject;
|
||||
|
||||
template<typename T>
|
||||
void create(TObject &obj) const {
|
||||
obj = new T{};
|
||||
}
|
||||
|
||||
void destroy(TObject &obj) const {
|
||||
delete obj;
|
||||
obj = nullptr;
|
||||
}
|
||||
|
||||
const TPointer getPtr(const TObject &obj) const {
|
||||
return obj;
|
||||
}
|
||||
|
||||
TPointer getPtr(TObject &obj) const {
|
||||
return obj;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
template <typename TObject>
|
||||
struct RawPointerManagerConfig {
|
||||
using RTTI = bitsery::ext::utils::StandardRTTI;
|
||||
static constexpr PointerOwnershipType OwnershipType = PointerOwnershipType::Owner;
|
||||
|
||||
using Handler = RawPointerObjectHandler<TObject>;
|
||||
|
||||
static std::unique_ptr<utils::PointerSharedContextBase> createSharedContext(TObject &) {
|
||||
return {};
|
||||
}
|
||||
|
||||
static void restoreFromSharedContext(TObject &, utils::PointerSharedContextBase *) {
|
||||
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
//this class is for convenience
|
||||
class PointerLinkingContext:
|
||||
public details_pointer::PointerLinkingContextSerialization,
|
||||
public details_pointer::PointerLinkingContextDeserialization {
|
||||
class PointerOwner : public utils::PointerOwnerManager<details_pointer::RawPointerManagerConfig> {
|
||||
public:
|
||||
bool isValid() {
|
||||
return isPointerSerializationValid() && isPointerDeserializationValid();
|
||||
}
|
||||
};
|
||||
|
||||
class PointerOwner {
|
||||
public:
|
||||
explicit PointerOwner(PointerType ptrType = PointerType::Nullable):_ptrType{ptrType} {}
|
||||
|
||||
template<typename Ser, typename Writer, typename T, typename Fnc>
|
||||
void serialize(Ser &ser, Writer &w, const T &obj, Fnc &&) const {
|
||||
auto& ctx = details_pointer::getLinkingContext(ser);
|
||||
if (obj) {
|
||||
auto& ptrInfo = ctx.getInfoByPtr(obj, PointerOwnershipType::Owner);
|
||||
details::writeSize(w, ptrInfo.id);
|
||||
details_pointer::RawPointerManager::serializeObject(w, ser, obj);
|
||||
} else {
|
||||
assert(_ptrType == PointerType::Nullable);
|
||||
details::writeSize(w, 0);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Des, typename Reader, typename T, typename Fnc>
|
||||
void deserialize(Des &des, Reader &r, T &obj, Fnc &&) const {
|
||||
details_pointer::getLinkingContext(des);
|
||||
size_t id{};
|
||||
details::readSize(r, id, std::numeric_limits<size_t>::max());
|
||||
if (id) {
|
||||
auto& ctx = details_pointer::getLinkingContext(des);
|
||||
auto& ptrInfo = ctx.getInfoById(id, PointerOwnershipType::Owner);
|
||||
details_pointer::RawPointerManager::deserializeObject(r, des, obj);
|
||||
ptrInfo.processOwner(obj);
|
||||
} else {
|
||||
if (_ptrType == PointerType::Nullable)
|
||||
details_pointer::RawPointerManager::destroyObject(obj);
|
||||
else
|
||||
r.setError(ReaderError::InvalidPointer);
|
||||
}
|
||||
}
|
||||
private:
|
||||
PointerType _ptrType;
|
||||
explicit PointerOwner(PointerType ptrType = PointerType::Nullable) : PointerOwnerManager(ptrType) {}
|
||||
};
|
||||
|
||||
class PointerObserver {
|
||||
public:
|
||||
|
||||
explicit PointerObserver(PointerType ptrType = PointerType::Nullable):_ptrType{ptrType} {}
|
||||
explicit PointerObserver(PointerType ptrType = PointerType::Nullable) : _ptrType{ptrType} {}
|
||||
|
||||
template<typename Ser, typename Writer, typename T, typename Fnc>
|
||||
void serialize(Ser &ser, Writer &w, const T &obj, Fnc &&) const {
|
||||
auto& ctx = details_pointer::getLinkingContext(ser);
|
||||
auto &ctx = details_pointer::getLinkingContext(ser);
|
||||
if (obj) {
|
||||
details::writeSize(w, ctx.getInfoByPtr(obj, PointerOwnershipType::Observer).id);
|
||||
} else {
|
||||
@@ -340,8 +109,8 @@ namespace bitsery {
|
||||
size_t id{};
|
||||
details::readSize(r, id, std::numeric_limits<size_t>::max());
|
||||
if (id) {
|
||||
auto& ctx = details_pointer::getLinkingContext(des);
|
||||
ctx.getInfoById(id, PointerOwnershipType::Observer).processObserver(reinterpret_cast<void*&>(obj));
|
||||
auto &ctx = details_pointer::getLinkingContext(des);
|
||||
ctx.getInfoById(id, PointerOwnershipType::Observer).processObserver(reinterpret_cast<void *&>(obj));
|
||||
} else {
|
||||
if (_ptrType == PointerType::Nullable)
|
||||
obj = nullptr;
|
||||
@@ -349,6 +118,7 @@ namespace bitsery {
|
||||
r.setError(ReaderError::InvalidPointer);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
PointerType _ptrType;
|
||||
};
|
||||
@@ -356,18 +126,18 @@ namespace bitsery {
|
||||
class ReferencedByPointer {
|
||||
public:
|
||||
template<typename Ser, typename Writer, typename T, typename Fnc>
|
||||
void serialize(Ser &ser, Writer &w, const T &obj, Fnc && fnc) const {
|
||||
auto& ctx = details_pointer::getLinkingContext(ser);
|
||||
void serialize(Ser &ser, Writer &w, const T &obj, Fnc &&fnc) const {
|
||||
auto &ctx = details_pointer::getLinkingContext(ser);
|
||||
details::writeSize(w, ctx.getInfoByPtr(&obj, PointerOwnershipType::Owner).id);
|
||||
fnc(const_cast<T&>(obj));
|
||||
fnc(const_cast<T &>(obj));
|
||||
}
|
||||
|
||||
template<typename Des, typename Reader, typename T, typename Fnc>
|
||||
void deserialize(Des &des, Reader &r, T &obj, Fnc && fnc) const {
|
||||
void deserialize(Des &des, Reader &r, T &obj, Fnc &&fnc) const {
|
||||
size_t id{};
|
||||
details::readSize(r, id, std::numeric_limits<size_t>::max());
|
||||
if (id) {
|
||||
auto& ctx = details_pointer::getLinkingContext(des);
|
||||
auto &ctx = details_pointer::getLinkingContext(des);
|
||||
fnc(obj);
|
||||
ctx.getInfoById(id, PointerOwnershipType::Owner).processOwner(&obj);
|
||||
} else {
|
||||
@@ -382,7 +152,7 @@ namespace bitsery {
|
||||
namespace traits {
|
||||
|
||||
template<typename T>
|
||||
struct ExtensionTraits<ext::PointerOwner, T*> {
|
||||
struct ExtensionTraits<ext::PointerOwner, T *> {
|
||||
using TValue = T;
|
||||
static constexpr bool SupportValueOverload = true;
|
||||
static constexpr bool SupportObjectOverload = true;
|
||||
@@ -391,7 +161,7 @@ namespace bitsery {
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct ExtensionTraits<ext::PointerObserver, T*> {
|
||||
struct ExtensionTraits<ext::PointerObserver, T *> {
|
||||
//although pointer observer doesn't serialize anything, but we still add value overload support to be consistent with pointer owners
|
||||
using TValue = T;
|
||||
static constexpr bool SupportValueOverload = true;
|
||||
|
||||
319
include/bitsery/ext/utils/pointer_utils.h
Normal file
319
include/bitsery/ext/utils/pointer_utils.h
Normal file
@@ -0,0 +1,319 @@
|
||||
//
|
||||
// Created by fraillt on 17.11.30.
|
||||
//
|
||||
|
||||
#ifndef BITSERY_POINTER_UTILS_H
|
||||
#define BITSERY_POINTER_UTILS_H
|
||||
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
|
||||
#include "polymorphism_utils.h"
|
||||
|
||||
namespace bitsery {
|
||||
namespace ext {
|
||||
|
||||
enum class PointerType {
|
||||
Nullable,
|
||||
NotNull
|
||||
};
|
||||
|
||||
enum class PointerOwnershipType : uint8_t {
|
||||
//is not responsible for pointer lifetime management.
|
||||
Observer,
|
||||
//only ONE owner is responsible for this pointers creation/destruction
|
||||
Owner,
|
||||
//MANY shared owners is responsible for pointer creation/destruction
|
||||
//requires additional context to manage shared owners themselves.
|
||||
Shared
|
||||
};
|
||||
|
||||
//forward declaration
|
||||
class PointerLinkingContext;
|
||||
|
||||
namespace utils {
|
||||
class PointerLinkingContextSerialization {
|
||||
public:
|
||||
explicit PointerLinkingContextSerialization()
|
||||
: _currId{0},
|
||||
_ptrMap{} {}
|
||||
|
||||
PointerLinkingContextSerialization(const PointerLinkingContextSerialization &) = delete;
|
||||
|
||||
PointerLinkingContextSerialization &operator=(const PointerLinkingContextSerialization &) = delete;
|
||||
|
||||
PointerLinkingContextSerialization(PointerLinkingContextSerialization &&) = default;
|
||||
|
||||
PointerLinkingContextSerialization &operator=(PointerLinkingContextSerialization &&) = default;
|
||||
|
||||
~PointerLinkingContextSerialization() = default;
|
||||
|
||||
struct PointerInfo {
|
||||
PointerInfo(size_t id_, PointerOwnershipType ownershipType_)
|
||||
: id{id_},
|
||||
ownershipType{ownershipType_},
|
||||
sharedCount{0} {};
|
||||
size_t id;
|
||||
PointerOwnershipType ownershipType;
|
||||
size_t sharedCount;
|
||||
};
|
||||
|
||||
const PointerInfo &getInfoByPtr(const void *ptr, PointerOwnershipType ptrType) {
|
||||
auto res = _ptrMap.emplace(ptr, PointerInfo{_currId + 1u, ptrType});
|
||||
auto &ptrInfo = res.first->second;
|
||||
if (res.second) {
|
||||
++_currId;
|
||||
return ptrInfo;
|
||||
}
|
||||
//ptr already exists
|
||||
//for observer return success
|
||||
if (ptrType == PointerOwnershipType::Observer)
|
||||
return ptrInfo;
|
||||
//set owner and return success
|
||||
if (ptrInfo.ownershipType == PointerOwnershipType::Observer) {
|
||||
ptrInfo.ownershipType = ptrType;
|
||||
return ptrInfo;
|
||||
}
|
||||
//only shared ownership can get here multiple times
|
||||
assert(ptrType == PointerOwnershipType::Shared);
|
||||
ptrInfo.sharedCount++;
|
||||
return ptrInfo;
|
||||
}
|
||||
|
||||
//valid, when all pointers have owners.
|
||||
//we cannot serialize pointers, if we haven't serialized objects themselves
|
||||
bool isPointerSerializationValid() const {
|
||||
return std::all_of(_ptrMap.begin(), _ptrMap.end(),
|
||||
[](const std::pair<const void *, PointerInfo> &p) {
|
||||
return p.second.ownershipType != PointerOwnershipType::Observer;
|
||||
});
|
||||
}
|
||||
|
||||
private:
|
||||
size_t _currId;
|
||||
std::unordered_map<const void *, PointerInfo> _ptrMap;
|
||||
|
||||
};
|
||||
|
||||
//this class is used to store context for shared ptr owners
|
||||
struct PointerSharedContextBase {
|
||||
virtual ~PointerSharedContextBase() = default;
|
||||
};
|
||||
|
||||
class PointerLinkingContextDeserialization {
|
||||
public:
|
||||
explicit PointerLinkingContextDeserialization()
|
||||
: _idMap{} {}
|
||||
|
||||
PointerLinkingContextDeserialization(const PointerLinkingContextDeserialization &) = delete;
|
||||
|
||||
PointerLinkingContextDeserialization &operator=(const PointerLinkingContextDeserialization &) = delete;
|
||||
|
||||
PointerLinkingContextDeserialization(PointerLinkingContextDeserialization &&) = default;
|
||||
|
||||
PointerLinkingContextDeserialization &operator=(PointerLinkingContextDeserialization &&) = default;
|
||||
|
||||
~PointerLinkingContextDeserialization() = default;
|
||||
|
||||
|
||||
struct PointerInfo {
|
||||
PointerInfo(size_t id_, void *ptr, PointerOwnershipType ownershipType_)
|
||||
: id{id_},
|
||||
ownershipType{ownershipType_},
|
||||
ownerPtr{ptr},
|
||||
observersList{},
|
||||
sharedContext{} {};
|
||||
|
||||
PointerInfo(const PointerInfo &) = delete;
|
||||
|
||||
PointerInfo &operator=(const PointerInfo &) = delete;
|
||||
|
||||
PointerInfo(PointerInfo &&) = default;
|
||||
|
||||
PointerInfo &operator=(PointerInfo &&) = default;
|
||||
|
||||
~PointerInfo() = default;
|
||||
|
||||
void processOwner(void *ptr) {
|
||||
ownerPtr = ptr;
|
||||
assert(ownershipType != PointerOwnershipType::Observer);
|
||||
for (auto &o:observersList)
|
||||
o.get() = ptr;
|
||||
observersList.clear();
|
||||
observersList.shrink_to_fit();
|
||||
}
|
||||
|
||||
void processObserver(void *(&ptr)) {
|
||||
if (ownerPtr) {
|
||||
ptr = ownerPtr;
|
||||
} else {
|
||||
observersList.push_back(ptr);
|
||||
}
|
||||
}
|
||||
|
||||
size_t id;
|
||||
PointerOwnershipType ownershipType;
|
||||
void *ownerPtr;
|
||||
std::vector<std::reference_wrapper<void *>> observersList;
|
||||
std::unique_ptr<PointerSharedContextBase> sharedContext;
|
||||
};
|
||||
|
||||
PointerInfo &getInfoById(size_t id, PointerOwnershipType ptrType) {
|
||||
auto res = _idMap.emplace(id, PointerInfo{id, nullptr, ptrType});
|
||||
auto &ptrInfo = res.first->second;
|
||||
if (!res.second) {
|
||||
assert(ptrType != PointerOwnershipType::Owner ||
|
||||
ptrInfo.ownershipType == PointerOwnershipType::Observer);
|
||||
if (ptrInfo.ownershipType == PointerOwnershipType::Observer)
|
||||
ptrInfo.ownershipType = ptrType;
|
||||
}
|
||||
return ptrInfo;
|
||||
}
|
||||
|
||||
//valid, when all pointers has owners
|
||||
bool isPointerDeserializationValid() const {
|
||||
return std::all_of(_idMap.begin(), _idMap.end(), [](const std::pair<const size_t, PointerInfo> &p) {
|
||||
return p.second.ownershipType != PointerOwnershipType::Observer;
|
||||
});
|
||||
}
|
||||
|
||||
private:
|
||||
std::unordered_map<size_t, PointerInfo> _idMap;
|
||||
};
|
||||
|
||||
template<template<typename> class Config>
|
||||
class PointerOwnerManager {
|
||||
|
||||
template <typename TObject>
|
||||
using Handler = typename Config<TObject>::Handler;
|
||||
|
||||
template<typename TObject>
|
||||
struct HelperTypes {
|
||||
using RTTI = typename Config<TObject>::RTTI;
|
||||
using THandler = Handler<TObject>;
|
||||
using TValue = typename std::remove_pointer<typename THandler::TPointer>::type;
|
||||
};
|
||||
|
||||
template<typename Ser, typename T, typename Fnc>
|
||||
void serializeImpl(PointerLinkingContextSerialization &, Ser &ser, const T &obj, Fnc &&,
|
||||
std::true_type) const {
|
||||
InheritanceTreeSerialize<Ser, T, HelperTypes> tree{};
|
||||
auto handler = tree.getHandler(obj);
|
||||
|
||||
assert(handler.second);
|
||||
ser.object(handler.first);
|
||||
handler.second->process(ser, const_cast<T &>(obj));
|
||||
}
|
||||
|
||||
template<typename Ser, typename T, typename Fnc>
|
||||
void serializeImpl(PointerLinkingContextSerialization &, Ser &, const T &obj, Fnc &&fnc,
|
||||
std::false_type) const {
|
||||
auto handler = Handler<T>{};
|
||||
fnc(*handler.getPtr(const_cast<T &>(obj)));
|
||||
}
|
||||
|
||||
template<typename Des, typename T, typename Fnc, typename Reader>
|
||||
void deserializeImpl(PointerLinkingContextDeserialization &, Des &des, T &obj, Fnc &&,
|
||||
Reader &r, std::true_type) const {
|
||||
InheritanceTreeTypeId id{};
|
||||
des.object(id);
|
||||
|
||||
InheritanceTreeDeserialize<Des, T, HelperTypes> tree{};
|
||||
auto handler = tree.getHandler(id);
|
||||
if (handler) {
|
||||
handler->create(obj);
|
||||
handler->process(des, obj);
|
||||
} else {
|
||||
r.setError(ReaderError::InvalidPointer);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Des, typename T, typename Fnc, typename Reader>
|
||||
void deserializeImpl(PointerLinkingContextDeserialization &, Des &, T &obj, Fnc &&fnc,
|
||||
Reader &, std::false_type) const {
|
||||
using TValue = typename HelperTypes<T>::TValue;
|
||||
auto handler = Handler<T>{};
|
||||
if (auto ptr = handler.getPtr(obj)) {
|
||||
fnc(*ptr);
|
||||
} else {
|
||||
handler.template create<TValue>(obj);
|
||||
fnc(*handler.getPtr(obj));
|
||||
}
|
||||
}
|
||||
|
||||
PointerType _ptrType;
|
||||
public:
|
||||
|
||||
explicit PointerOwnerManager(PointerType ptrType = PointerType::Nullable) : _ptrType{ptrType} {}
|
||||
|
||||
template<typename Ser, typename Writer, typename T, typename Fnc>
|
||||
void serialize(Ser &ser, Writer &w, const T &obj, Fnc &&fnc) const {
|
||||
auto handler = Handler<T>{};
|
||||
auto ptr = handler.getPtr(obj);
|
||||
if (ptr) {
|
||||
auto ctx = ser.template context<PointerLinkingContext>();
|
||||
assert(ctx != nullptr);
|
||||
auto &ptrInfo = ctx->getInfoByPtr(ptr, Config<T>::OwnershipType);
|
||||
details::writeSize(w, ptrInfo.id);
|
||||
if (ptrInfo.sharedCount == 0) {
|
||||
serializeImpl(*ctx, ser, obj, std::forward<Fnc>(fnc),
|
||||
std::is_polymorphic<typename HelperTypes<T>::TValue>{});
|
||||
}
|
||||
} else {
|
||||
assert(_ptrType == PointerType::Nullable);
|
||||
details::writeSize(w, 0);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Des, typename Reader, typename T, typename Fnc>
|
||||
void deserialize(Des &des, Reader &r, T &obj, Fnc &&fnc) const {
|
||||
size_t id{};
|
||||
details::readSize(r, id, std::numeric_limits<size_t>::max());
|
||||
auto handler = Handler<T>{};
|
||||
if (id) {
|
||||
auto ctx = des.template context<PointerLinkingContext>();
|
||||
assert(ctx != nullptr);
|
||||
auto &ptrInfo = ctx->getInfoById(id, Config<T>::OwnershipType);
|
||||
//todo add deserialization checking
|
||||
if (ptrInfo.ownershipType == PointerOwnershipType::Owner) {
|
||||
deserializeImpl(*ctx, des, obj, std::forward<Fnc>(fnc), r,
|
||||
std::is_polymorphic<typename HelperTypes<T>::TValue>{});
|
||||
ptrInfo.processOwner(handler.getPtr(obj));
|
||||
} else {
|
||||
if (!ptrInfo.sharedContext) {
|
||||
deserializeImpl(*ctx, des, obj, std::forward<Fnc>(fnc), r,
|
||||
std::is_polymorphic<typename HelperTypes<T>::TValue>{});
|
||||
ptrInfo.processOwner(handler.getPtr(obj));
|
||||
ptrInfo.sharedContext = Config<T>::createSharedContext(obj);
|
||||
} else {
|
||||
Config<T>::restoreFromSharedContext(obj, ptrInfo.sharedContext.get());
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
if (_ptrType == PointerType::Nullable && handler.getPtr(obj)) {
|
||||
handler.destroy(obj);
|
||||
} else
|
||||
r.setError(ReaderError::InvalidPointer);
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
//this class is for convenience
|
||||
class PointerLinkingContext :
|
||||
public utils::PointerLinkingContextSerialization,
|
||||
public utils::PointerLinkingContextDeserialization {
|
||||
public:
|
||||
bool isValid() {
|
||||
return isPointerSerializationValid() && isPointerDeserializationValid();
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif //BITSERY_POINTER_UTILS_H
|
||||
225
include/bitsery/ext/utils/polymorphism_utils.h
Normal file
225
include/bitsery/ext/utils/polymorphism_utils.h
Normal file
@@ -0,0 +1,225 @@
|
||||
//
|
||||
// Created by fraillt on 17.11.3.
|
||||
//
|
||||
|
||||
#ifndef BITSERY_EXT_POLYMORPHISM_H
|
||||
#define BITSERY_EXT_POLYMORPHISM_H
|
||||
|
||||
#include "../inheritance.h"
|
||||
|
||||
#include <unordered_map>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
|
||||
namespace bitsery {
|
||||
|
||||
namespace ext {
|
||||
namespace details_polymorphism {
|
||||
//stores template types
|
||||
template<typename ...>
|
||||
struct List {
|
||||
};
|
||||
}
|
||||
|
||||
//specialize for your base class by deriving from DerivedClasses with list of derivatives that DIRECTLY inherits from your base class.
|
||||
//e.g.
|
||||
// template <> PolymorphicBase<Animal>: DerivedClasses<Dog, Cat>{};
|
||||
// template <> PolymorphicBase<Dog>: DerivedClasses<Bulldog, GolderRetriever> {};
|
||||
// IMPORTANT !!!
|
||||
// although you can add all derivates to same base like this:
|
||||
// SuperClass<Animal>:DerivedClasses<Dog, Cat, Bulldog, GoldenRetriever>{};
|
||||
// it will not work when you try to serialize Dog*, because it will not find Bulldog and GoldenRetriever
|
||||
template<typename TBase>
|
||||
struct PolymorphicBaseClass {
|
||||
using childs = details_polymorphism::List<>;
|
||||
};
|
||||
|
||||
//derive from this class when specifying childs for your base class, atleast one child must exists, hence T1
|
||||
//e.g.
|
||||
// template <> SuperClass<Animal>: DerivedClasses<Dog, Cat>{};
|
||||
template<typename T1, typename ... Tn>
|
||||
struct DerivedClasses {
|
||||
using childs = details_polymorphism::List<T1, Tn...>;
|
||||
};
|
||||
|
||||
namespace utils {
|
||||
//this object will be used to serialize/deserialize polymorphic type id within inheritance tree from base class
|
||||
struct InheritanceTreeTypeId {
|
||||
uint16_t depth{};
|
||||
uint16_t index{};
|
||||
template <typename S>
|
||||
void serialize(S& s) {
|
||||
s.value2b(depth);
|
||||
s.value2b(index);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename S, typename TObject>
|
||||
struct PolymorphicObjectHandlerInterface {
|
||||
virtual void process(S& s, TObject& obj) const = 0;
|
||||
virtual void create(TObject& obj) const = 0;
|
||||
virtual ~PolymorphicObjectHandlerInterface() = default;
|
||||
};
|
||||
}
|
||||
|
||||
namespace details_polymorphism {
|
||||
|
||||
|
||||
template<typename S, typename TObject, typename TDerived, typename RTTI, typename ObjectHandler>
|
||||
struct PolymorphicObjectHandlerBase : public utils::PolymorphicObjectHandlerInterface<S, TObject> {
|
||||
void process(S &s, TObject &obj) const final {
|
||||
s.object(reinterpret_cast<TDerived &>(*_handler.getPtr(obj)));
|
||||
};
|
||||
|
||||
void create(TObject &obj) const final {
|
||||
auto ptr = _handler.getPtr(obj);
|
||||
if (ptr && RTTI::get(*ptr) != RTTI::template get<TDerived>()) {
|
||||
_handler.destroy(obj);
|
||||
_handler.template create<TDerived>(obj);
|
||||
} else {
|
||||
if (ptr == nullptr)
|
||||
_handler.template create<TDerived>(obj);
|
||||
}
|
||||
}
|
||||
ObjectHandler _handler{};
|
||||
};
|
||||
|
||||
template<typename S, typename TObject, template<typename> class TObjectManager>
|
||||
class PolymorphicHandlersGenerator {
|
||||
public:
|
||||
template<typename Fnc>
|
||||
static void generate(Fnc &&addHandlerFnc) {
|
||||
PolymorphicHandlersGenerator tmp{std::forward<Fnc>(addHandlerFnc)};
|
||||
}
|
||||
|
||||
private:
|
||||
using RTTI = typename TObjectManager<TObject>::RTTI;
|
||||
|
||||
template <typename TDerived>
|
||||
using TPolymorphicObjectHandler = PolymorphicObjectHandlerBase<S, TObject, TDerived, RTTI, typename TObjectManager<TObject>::THandler>;
|
||||
|
||||
template<typename Fnc>
|
||||
explicit PolymorphicHandlersGenerator(Fnc &&addHandlerFnc):_addHandler(std::forward<Fnc>(addHandlerFnc)) {
|
||||
//fill inheritance tree
|
||||
using TBase = typename TObjectManager<TObject>::TValue;
|
||||
add<TBase>();
|
||||
}
|
||||
|
||||
template<typename TClass>
|
||||
void add() {
|
||||
addClass<TClass>();
|
||||
//save current index and increase depth
|
||||
auto saveIndex = index;
|
||||
index = 0;
|
||||
depth++;
|
||||
addChilds<TClass>(typename PolymorphicBaseClass<TClass>::childs{});
|
||||
//restore index and depth
|
||||
depth--;
|
||||
index = saveIndex;
|
||||
}
|
||||
|
||||
template<typename TClassBase, typename T1, typename ... Tn>
|
||||
void addChilds(details_polymorphism::List<T1, Tn...>) {
|
||||
static_assert(std::is_base_of<TClassBase, T1>::value,
|
||||
"PolymorphicBaseClass<TBase> must derive a list of derived classes from TBase.");
|
||||
add<T1>();
|
||||
addChilds<TClassBase>(details_polymorphism::List<Tn...>{});
|
||||
}
|
||||
|
||||
template<typename>
|
||||
void addChilds(details_polymorphism::List<>) {
|
||||
}
|
||||
|
||||
|
||||
template<typename TClass>
|
||||
void addClass() {
|
||||
if (!std::is_abstract<TClass>::value) {
|
||||
utils::InheritanceTreeTypeId tid{};
|
||||
tid.index = index;
|
||||
tid.depth = depth;
|
||||
#if __cplusplus > 201103L
|
||||
auto handler = std::make_unique<TPolymorphicObjectHandler<TClass>>();
|
||||
#else
|
||||
auto handler = std::unique_ptr<TPolymorphicObjectHandler<TClass>>(
|
||||
new TPolymorphicObjectHandler<TClass>{});
|
||||
#endif
|
||||
if (_addHandler(RTTI::template get<TClass>(), tid, std::move(handler)))
|
||||
++index;
|
||||
}
|
||||
}
|
||||
|
||||
std::function<bool(size_t, utils::InheritanceTreeTypeId,
|
||||
std::unique_ptr<utils::PolymorphicObjectHandlerInterface<S, TObject>> &&)> _addHandler;
|
||||
uint16_t depth{};
|
||||
uint16_t index{};
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
namespace utils {
|
||||
|
||||
template<typename Ser, typename TObject, template<typename> class TObjectManager>
|
||||
class InheritanceTreeSerialize {
|
||||
public:
|
||||
|
||||
InheritanceTreeSerialize() {
|
||||
details_polymorphism::PolymorphicHandlersGenerator<Ser, TObject, TObjectManager>::generate(
|
||||
[this](size_t typeHash, InheritanceTreeTypeId id,
|
||||
std::unique_ptr<PolymorphicObjectHandlerInterface<Ser, TObject>> &&handler) {
|
||||
return _map.emplace(std::make_pair(typeHash, std::make_pair(id, std::move(handler)))).second;
|
||||
}
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
const std::pair<InheritanceTreeTypeId, PolymorphicObjectHandlerInterface<Ser, TObject>*> getHandler(const TObject &obj) {
|
||||
using RTTI = typename TObjectManager<TObject>::RTTI;
|
||||
auto handler = typename TObjectManager<TObject>::THandler{};
|
||||
auto it = _map.find(RTTI::get(*handler.getPtr(obj)));
|
||||
if (it != _map.end()) {
|
||||
return std::make_pair(it->second.first, it->second.second.get());
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
private:
|
||||
std::unordered_map<size_t, std::pair<InheritanceTreeTypeId, std::unique_ptr<PolymorphicObjectHandlerInterface<Ser, TObject>>>> _map{};
|
||||
};
|
||||
|
||||
|
||||
template<typename Des, typename TObject, template<typename> class TObjectManager>
|
||||
class InheritanceTreeDeserialize {
|
||||
public:
|
||||
InheritanceTreeDeserialize() {
|
||||
details_polymorphism::PolymorphicHandlersGenerator<Des, TObject, TObjectManager>::generate(
|
||||
[this](size_t, InheritanceTreeTypeId id, std::unique_ptr<PolymorphicObjectHandlerInterface<Des, TObject>> &&handler) {
|
||||
return _map.emplace(std::make_pair(getHashFromId(id), std::move(handler))).second;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
const PolymorphicObjectHandlerInterface<Des, TObject> *getHandler(const InheritanceTreeTypeId &id) {
|
||||
auto it = _map.find(getHashFromId(id));
|
||||
if (it != _map.end())
|
||||
return it->second.get();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
private:
|
||||
size_t getHashFromId(const InheritanceTreeTypeId &id) const {
|
||||
size_t res = id.depth << 16;
|
||||
return res + id.index;
|
||||
}
|
||||
|
||||
std::unordered_map<size_t, std::unique_ptr<PolymorphicObjectHandlerInterface<Des, TObject>>> _map{};
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif //BITSERY_EXT_POLYMORPHISM_H
|
||||
28
include/bitsery/ext/utils/rtti_utils.h
Normal file
28
include/bitsery/ext/utils/rtti_utils.h
Normal file
@@ -0,0 +1,28 @@
|
||||
//
|
||||
// Created by fraillt on 17.11.30.
|
||||
//
|
||||
|
||||
#ifndef BITSERY_RTTI_UTILS_H
|
||||
#define BITSERY_RTTI_UTILS_H
|
||||
|
||||
#include <typeinfo>
|
||||
|
||||
namespace bitsery {
|
||||
namespace ext {
|
||||
namespace utils {
|
||||
struct StandardRTTI {
|
||||
template<typename T>
|
||||
static size_t get() {
|
||||
return typeid(T).hash_code();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
static size_t get(T &&obj) {
|
||||
return typeid(obj).hash_code();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif //BITSERY_RTTI_UTILS_H
|
||||
Reference in New Issue
Block a user