added inheritance extension, and ability to have internal contexts within serializer/deserializer

This commit is contained in:
fraillt
2017-11-06 11:30:14 +02:00
committed by Mindaugas Vinkelis
parent be9ccf08d9
commit f6d02aba38
27 changed files with 1178 additions and 311 deletions

View File

@@ -1,3 +1,24 @@
# [4.2.0](https://github.com/fraillt/bitsery/compare/v4.1.0...v4.2.0) (2017-11-12)
### Features
* serializer/deserializer can now have **internal context(s)** via configuration.
It is convenient way to pass context, when it doesn't convey useful information outside of serializer/deserializer and is default constructable.
* added **contextOrNull\<T\>()** overload to *BasicSerializer/BasicDeserializer*.
Difference between *contextOrNull\<T\>()* and *context\<T\>()* is, that using *context\<T\>()* code doesn't compile if T doesn't exists at all, while using *contextOrNull\<T\>()* code compiles, but returns *nullptr* at runtime.
* added inheritance support via extensions.
In order to correctly manage virtual inheritance two extensions was created in **<bitsery/ext/inheritance.h>** header:
* **BaseClass\<TBase\>** - use when inheriting from objects without virtual inheritance.
* **VirtualBaseClass\<TBase\>** - ensures that only one copy of each virtual base class is serialized.
To keep track of virtual base classes **InheritanceContext** is required, but it is optional if no virtual bases exists in serialization flow.
I.e. if context is not defined, code will not compile only if virtual inheritance is used.
See [inheritance](examples/inheritance.cpp) for usage example.
### Improvements
* added optional ctor parameter for *PointerOwner* and *PointerObserver* - **PointerType**, which specifies if pointer can be null or not.
Default is **Nullable**.
# [4.1.0](https://github.com/fraillt/bitsery/compare/v4.0.1...v4.1.0) (2017-10-27)
### Features
@@ -12,9 +33,9 @@ In order to correctly manage pointer ownership, three extensions was created in
*Currently polimorphism and std::shared_ptr, std::unique_ptr is not supported.*
* added **context\<T\>()** overload to *BasicSerializer/BasicDeserializer* and now serializers is typesafe.
Also for better pointers support, added posibility to have multiple types in context with *std::tuple*.
E.g. when using pointers together with your custom context, you can define your context as *std::tuple\<PointerLinkingContext, MyContext\>* and in serialization function you can correctly get your data via *context\<MyContext\>()*.
* added **context\<T\>()** overload to *BasicSerializer/BasicDeserializer* and now they became typesafe.
For better extensions support, added posibility to have multiple types in context with *std::tuple*.
E.g. when using multiple extensions, that requires specific contexts, together with your custom context, you can define your context as *std::tuple\<PointerLinkingContext, MyContext\>* and in serialization function you can correctly get your data via *context\<MyContext\>()*.
### Improvements

View File

@@ -10,6 +10,7 @@ Library design:
* `errors handling`
* `forward/backward compatibility via Growable extension`
* `pointers`
* `inheritance`
Core Serializer/Deserializer functions (alphabetical order):
@@ -18,11 +19,14 @@ Core Serializer/Deserializer functions (alphabetical order):
* `container`
* `ext`
* `context`
* `context<T>`
* `contextOrNull<T>`
* `object`
* `text`
* `value`
Serializer/Deserializer extensions via `ext` method (alphabetical order):
* `BaseClass`
* `Entropy`
* `Growable`
* `PointerOwner`
@@ -34,6 +38,7 @@ Serializer/Deserializer extensions via `ext` method (alphabetical order):
* `StdSet`
* `StdStack`
* `ValueRange`
* `VirtualBaseClass`
AdapterWriter/Reader functions:
* `writeBits/readBits`
@@ -62,9 +67,6 @@ Output adapters (buffer and stream) functions:
Tips and tricks:
* if you're getting static assert "please define 'serialize' function", most likely it is because your **serialize** function is not defined in same namespace as object.
Limitations:
* max **text** or **container** size can be 2^(n-2) (where n = sizeof(std::size_t) * 8) for 32-bit systems it is 1073741823 (0x3FFFFFF).
Other:
* [Contributing](../CONTRIBUTING.md)
* [Change log](../CHANGELOG.md)

117
examples/inheritance.cpp Normal file
View File

@@ -0,0 +1,117 @@
//
//this example coverls all the corner cases that can happen using inherintace
//in reality virtual inherintance is usually avoided, so your code would look much simpler.
//
#include <bitsery/bitsery.h>
#include <bitsery/adapter/buffer.h>
#include <bitsery/traits/vector.h>
//include inheritance extension
//this header contains two extensions, that specifies inheritance type of base class
// BaseClass - normal inheritance
// VirtualBaseClass - when virtual inheritance is used
//in order for virtual inheritance to work, InheritanceContext is required.
//it can be created either internally (via configuration) or externally (pointer to context).
#include <bitsery/ext/inheritance.h>
using bitsery::ext::BaseClass;
using bitsery::ext::VirtualBaseClass;
struct Base {
uint8_t x{};
//Base doesn't have to be polymorphic class, inheritance works at compile-time.
};
template <typename S>
void serialize(S& s, Base& o) {
s.value1b(o.x);
}
struct Derive1:virtual Base {// virtually inherits from base
uint8_t y1{};
};
template <typename S>
void serialize(S& s, Derive1& o) {
//define virtual inheritance, it will not compile if InheritanceContext is not defined in serializer/deserialzer
s.ext(o, VirtualBaseClass<Base>{});
s.value1b(o.y1);
}
//to make it more interesting, serialize private member
struct Derived2:virtual Base {
explicit Derived2(uint8_t y):y2{y} {}
uint8_t getY2() const {
return y2;
};
private:
friend bitsery::Access;
uint8_t y2{};
template <typename S>
void serialize(S& s) {
//notice virtual inheritance
s.ext(*this, VirtualBaseClass<Base>{});
s.value1b(y2);
}
};
struct MultipleInheritance: Derive1, Derived2 {
explicit MultipleInheritance(uint8_t y2):Derived2{y2} {}
uint8_t z{};
};
template <typename S>
void serialize(S& s, MultipleInheritance& o) {
//has two bases, serialize them separately
s.ext(o, BaseClass<Derive1>{});
s.ext(o, BaseClass<Derived2>{});
s.value1b(o.z);
}
namespace bitsery {
// call to serialize function with Derived2 and MultipleInheritance is ambiguous,
// it matches two serialize functions: Base classes non-member fnc and Derived2 member fnc
// we need explicitly select which function to use
template <>
struct SelectSerializeFnc<Derived2>:UseMemberFnc {};
//multiple inheritance has non-member serialize function defined
template <>
struct SelectSerializeFnc<MultipleInheritance>:UseNonMemberFnc {};
}
using namespace bitsery;
// since in this example we're using virtual inheritance we need InheritanceContext as well.
// InheritanceContext is default constructable and has no useful information outside of serializer/deserializer
// lets create it internally, via configuration
struct ConfigWithContext:DefaultConfig {
//always add internal contexts to tuple
using InternalContext = std::tuple<ext::InheritanceContext>;
};
//some helper types
using Buffer = std::vector<uint8_t>;
using Writer = AdapterWriter<OutputBufferAdapter<Buffer>, ConfigWithContext>;
using Reader = AdapterReader<InputBufferAdapter<Buffer>, ConfigWithContext>;
int main() {
MultipleInheritance data{98};
data.x = 254;
data.y1 = 47;
data.z = 1;
Buffer buf{};
BasicSerializer<Writer> ser{OutputBufferAdapter<Buffer>{buf}, nullptr};
ser.object(data);
auto writtenSize = AdapterAccess::getWriter(ser).writtenBytesCount();
MultipleInheritance res{0};
BasicDeserializer<Reader> des{InputBufferAdapter<Buffer>{buf.begin(), writtenSize}};
des.object(res);
assert(AdapterAccess::getReader(des).error() == ReaderError::NoError && AdapterAccess::getReader(des).isCompletedSuccessfully());
assert(data.x == res.x && data.y1 == res.y1 && data.getY2() == res.getY2() && data.z == res.z);
assert(writtenSize == 4);//base is serialized once, because it is inherited virtually
}

View File

@@ -11,6 +11,7 @@
using bitsery::ext::ReferencedByPointer;
using bitsery::ext::PointerObserver;
using bitsery::ext::PointerOwner;
using bitsery::ext::PointerType ;
enum class MyEnum:uint16_t { V1,V2,V3 };
struct MyStruct {
@@ -71,8 +72,8 @@ private:
});
//observer
s.ext(po1, PointerObserver{});
//owner
s.ext4b(pi1, PointerOwner{});
//owner, mark it as not null
s.ext4b(pi1, PointerOwner{PointerType::NotNull});
}
};

View File

@@ -21,8 +21,8 @@
//SOFTWARE.
#ifndef BITSERY_ADAPTERS_INPUT_BUFFER_ADAPTER_H
#define BITSERY_ADAPTERS_INPUT_BUFFER_ADAPTER_H
#ifndef BITSERY_ADAPTER_BUFFER_H
#define BITSERY_ADAPTER_BUFFER_H
#include "../details/adapter_common.h"
#include "../traits/core/traits.h"
@@ -204,4 +204,4 @@ namespace bitsery {
}
#endif //BITSERY_ADAPTERS_INPUT_BUFFER_ADAPTER_H
#endif //BITSERY_ADAPTER_BUFFER_H

View File

@@ -21,8 +21,8 @@
//SOFTWARE.
#ifndef BITSERY_ADAPTERS_DYNAMIC_STREAM_H
#define BITSERY_ADAPTERS_DYNAMIC_STREAM_H
#ifndef BITSERY_ADAPTER_STREAM_H
#define BITSERY_ADAPTER_STREAM_H
#include "../details/adapter_common.h"
#include "../traits/array.h"
@@ -224,4 +224,4 @@ namespace bitsery {
using OutputBufferedStreamAdapter = BasicBufferedOutputStreamAdapter<char, std::char_traits<char>>;
}
#endif //BITSERY_ADAPTERS_DYNAMIC_STREAM_H
#endif //BITSERY_ADAPTER_STREAM_H

View File

@@ -22,8 +22,8 @@
#ifndef BITSERY_BASIC_READER_H
#define BITSERY_BASIC_READER_H
#ifndef BITSERY_ADAPTER_READER_H
#define BITSERY_ADAPTER_READER_H
#include "details/sessions.h"
#include <algorithm>
@@ -39,7 +39,7 @@ namespace bitsery {
struct AdapterReader {
//this is required by deserializer
static constexpr bool BitPackingEnabled = false;
using TConfig = Config;
using TValue = typename InputAdapter::TValue;
static_assert(details::IsDefined<TValue>::value, "Please define adapter traits or include from <bitsery/traits/...>");
@@ -154,6 +154,7 @@ namespace bitsery {
public:
//this is required by deserializer
static constexpr bool BitPackingEnabled = true;
using TConfig = typename TReader::TConfig;
//make TValue unsigned for bitpacking
using UnsignedValue = typename std::make_unsigned<typename TReader::TValue>::type;
using ScratchType = typename details::ScratchType<UnsignedValue>::type;
@@ -268,4 +269,4 @@ namespace bitsery {
};
}
#endif //BITSERY_BUFFER_READER_H
#endif //BITSERY_ADAPTER_READER_H

View File

@@ -22,8 +22,8 @@
#ifndef BITSERY_BASIC_WRITER_H
#define BITSERY_BASIC_WRITER_H
#ifndef BITSERY_ADAPTER_WRITER_H
#define BITSERY_ADAPTER_WRITER_H
#include "details/sessions.h"
@@ -33,10 +33,12 @@
namespace bitsery {
struct MeasureSize {
template <typename Config>
struct BasicMeasureSize {
//measure class is bit-packing enabled, no need to create wrapper for it
static constexpr bool BitPackingEnabled = true;
using TConfig = Config;
template<size_t SIZE, typename T>
void writeBytes(const T &) {
static_assert(std::is_integral<T>(), "");
@@ -95,6 +97,9 @@ namespace bitsery {
size_t _sessionsBytesCount{};
};
//helper type for default config
using MeasureSize = BasicMeasureSize<DefaultConfig>;
template <typename TWriter>
class AdapterWriterBitPackingWrapper;
@@ -103,7 +108,7 @@ namespace bitsery {
struct AdapterWriter {
//this is required by serializer
static constexpr bool BitPackingEnabled = false;
using TConfig = Config;
using TValue = typename OutputAdapter::TValue;
static_assert(details::IsDefined<TValue>::value, "Please define adapter traits or include from <bitsery/traits/...>");
@@ -207,6 +212,7 @@ namespace bitsery {
public:
//this is required by serializer
static constexpr bool BitPackingEnabled = true;
using TConfig = typename TWriter::TConfig;
//make TValue unsigned for bit packing
using UnsignedType = typename std::make_unsigned<typename TWriter::TValue>::type;
@@ -334,4 +340,4 @@ namespace bitsery {
};
}
#endif //BITSERY_BASIC_WRITER_H
#endif //BITSERY_ADAPTER_WRITER_H

View File

@@ -25,7 +25,7 @@
#define BITSERY_BITSERY_H
#define BITSERY_MAJOR_VERSION 4
#define BITSERY_MINOR_VERSION 1
#define BITSERY_MINOR_VERSION 2
#define BITSERY_PATCH_VERSION 0
#define BITSERY_QUOTE_MACRO(name) #name

View File

@@ -24,6 +24,8 @@
#ifndef BITSERY_COMMON_H
#define BITSERY_COMMON_H
#include <tuple>
namespace bitsery {
/*
@@ -41,7 +43,10 @@ namespace bitsery {
//this functionality allows to support backward/forward compatibility
//however reading from streams is not supported, because this functionality requires random access to buffer.
static constexpr bool BufferSessionsEnabled = false;
//list of contexts that will be instanciated internally within serializer/deserializer.
//contexts must be default constructable.
//internal context has priority, if external context with the same type exists.
using InternalContext = std::tuple<>;
};
}

View File

@@ -40,11 +40,14 @@ namespace bitsery {
using BPEnabledType = BasicDeserializer<typename std::conditional<TAdapterReader::BitPackingEnabled,
TAdapterReader, AdapterReaderBitPackingWrapper<TAdapterReader>>::type, TContext>;
static_assert(details::IsSpecializationOf<typename TReader::TConfig::InternalContext, std::tuple>::value,
"Config::InternalContext must be std::tuple");
template <typename ReaderParam>
explicit BasicDeserializer(ReaderParam&& r, TContext* context = nullptr)
: _reader{std::forward<ReaderParam>(r)},
_context{context}
_context{context},
_internalContext{}
{
}
@@ -66,7 +69,12 @@ namespace bitsery {
template <typename T>
T* context(){
return details::getContext<T>(_context);
return details::getContext<T>(_context, _internalContext);
}
template <typename T>
T* contextOrNull(){
return details::getContextIfTypeExists<T>(_context, _internalContext);
}
/*
@@ -326,6 +334,8 @@ namespace bitsery {
TAdapterReader _reader;
TContext* _context;
typename TReader::TConfig::InternalContext _internalContext;
//process value types
//false_type means that we must process all elements individually

View File

@@ -20,8 +20,8 @@
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.
#ifndef BITSERY_DETAILS_BUFFER_COMMON_H
#define BITSERY_DETAILS_BUFFER_COMMON_H
#ifndef BITSERY_DETAILS_ADAPTER_COMMON_H
#define BITSERY_DETAILS_ADAPTER_COMMON_H
#include <algorithm>
#include <utility>
@@ -124,4 +124,4 @@ namespace bitsery {
}
#endif //BITSERY_DETAILS_BUFFER_COMMON_H
#endif //BITSERY_DETAILS_ADAPTER_COMMON_H

View File

@@ -20,8 +20,8 @@
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.
#ifndef BITSERY_DETAILS_BOTH_COMMON_H
#define BITSERY_DETAILS_BOTH_COMMON_H
#ifndef BITSERY_DETAILS_ADAPTER_UTILS_H
#define BITSERY_DETAILS_ADAPTER_UTILS_H
#include <cassert>
#include <cstdint>
@@ -84,4 +84,4 @@ namespace bitsery {
}
}
#endif //BITSERY_DETAILS_BOTH_COMMON_H
#endif //BITSERY_DETAILS_ADAPTER_UTILS_H

View File

@@ -21,8 +21,8 @@
//SOFTWARE.
#ifndef BITSERY_TRAITS_CORE_NOT_DEFINED_TYPE_H
#define BITSERY_TRAITS_CORE_NOT_DEFINED_TYPE_H
#ifndef BITSERY_DETAILS_NOT_DEFINED_TYPE_H
#define BITSERY_DETAILS_NOT_DEFINED_TYPE_H
#include <iterator>
@@ -76,4 +76,4 @@ namespace std {
using iterator_category = std::random_access_iterator_tag;
};
}
#endif //BITSERY_TRAITS_CORE_NOT_DEFINED_TYPE_H
#endif //BITSERY_DETAILS_NOT_DEFINED_TYPE_H

View File

@@ -41,18 +41,33 @@ namespace bitsery {
}
};
//when call to serialize function is ambiguous (member and non-member serialize function exists for a type)
//specialize this class, by inheriting from either UseNonMemberFnc or UseMemberFnc
//e.g.
//template <> struct SelectSerializeFnc<MyDerivedClass>:UseMemberFnc {};
template<typename T>
struct SelectSerializeFnc : std::integral_constant<int, 0> {
};
//types you need to inherit from when specializing SelectSerializeFnc class
struct UseNonMemberFnc : std::integral_constant<int, 1> {
};
struct UseMemberFnc : std::integral_constant<int, 2> {
};
//serializer/deserializer, does not public interface to get underlying writer/reader
//to prevent users from using writer/reader directly, because they have different interface
//and they cannot be used describing serialization flows.: use extensions for this reason.
//this class allows to get underlying adapter writer/reader, and only should be used outside serialization functions.
struct AdapterAccess {
template <typename Serializer>
static typename Serializer::TWriter& getWriter(Serializer& s) {
template<typename Serializer>
static typename Serializer::TWriter &getWriter(Serializer &s) {
return s._writer;
}
template <typename Deserializer>
static typename Deserializer::TReader& getReader(Deserializer& s) {
template<typename Deserializer>
static typename Deserializer::TReader &getReader(Deserializer &s) {
return s._reader;
}
};
@@ -60,101 +75,104 @@ namespace bitsery {
namespace details {
//helper types for error handling
template <typename T>
struct IsContainerTraitsDefined:public IsDefined<typename traits::ContainerTraits<T>::TValue> {
template<typename T>
struct IsContainerTraitsDefined : public IsDefined<typename traits::ContainerTraits<T>::TValue> {
};
template <typename T>
struct IsTextTraitsDefined:public IsDefined<typename traits::TextTraits<T>::TValue> {
template<typename T>
struct IsTextTraitsDefined : public IsDefined<typename traits::TextTraits<T>::TValue> {
};
template <typename Ext, typename T>
struct IsExtensionTraitsDefined:public IsDefined<typename traits::ExtensionTraits<Ext, T>::TValue> {
template<typename Ext, typename T>
struct IsExtensionTraitsDefined : public IsDefined<typename traits::ExtensionTraits<Ext, T>::TValue> {
};
#ifdef _MSC_VER
//helper types for HasSerializeFunction
template <typename S, typename T>
using TrySerializeFunction = decltype(serialize(std::declval<S &>(), std::declval<T &>()));
//helper types for HasSerializeFunction
template <typename S, typename T>
using TrySerializeFunction = decltype(serialize(std::declval<S &>(), std::declval<T &>()));
template <typename S, typename T>
struct HasSerializeFunctionHelper {
template <typename Q, typename R, typename = TrySerializeFunction<Q, R>>
static std::true_type tester(Q&&, R&&);
static std::false_type tester(...);
using type = decltype(tester(std::declval<S>(), std::declval<T>()));
};
template <typename S, typename T>
struct HasSerializeFunction :HasSerializeFunctionHelper<S, T>::type {};
template <typename S, typename T>
struct HasSerializeFunctionHelper {
template <typename Q, typename R, typename = TrySerializeFunction<Q, R>>
static std::true_type tester(Q&&, R&&);
static std::false_type tester(...);
using type = decltype(tester(std::declval<S>(), std::declval<T>()));
};
template <typename S, typename T>
struct HasSerializeFunction :HasSerializeFunctionHelper<S, T>::type {};
//helper types for HasSerializeMethod
template <typename S, typename T>
using TrySerializeMethod = decltype(Access::serialize(std::declval<S &>(), std::declval<T &>()));
//helper types for HasSerializeMethod
template <typename S, typename T>
using TrySerializeMethod = decltype(Access::serialize(std::declval<S &>(), std::declval<T &>()));
template <typename S, typename T>
struct HasSerializeMethodHelper {
template <typename Q, typename R, typename = TrySerializeMethod<Q, R>>
static std::true_type tester(Q&&, R&&);
static std::false_type tester(...);
using type = decltype(tester(std::declval<S>(), std::declval<T>()));
};
template <typename S, typename T>
struct HasSerializeMethod :HasSerializeMethodHelper<S, T>::type {};
template <typename S, typename T>
struct HasSerializeMethodHelper {
template <typename Q, typename R, typename = TrySerializeMethod<Q, R>>
static std::true_type tester(Q&&, R&&);
static std::false_type tester(...);
using type = decltype(tester(std::declval<S>(), std::declval<T>()));
};
template <typename S, typename T>
struct HasSerializeMethod :HasSerializeMethodHelper<S, T>::type {};
//helper types for IsFlexibleIncluded
template <typename S, typename T>
using TryArchiveProcess = decltype(archiveProcess(std::declval<S &>(), std::declval<T &&>()));
//helper types for IsFlexibleIncluded
template <typename S, typename T>
using TryArchiveProcess = decltype(archiveProcess(std::declval<S &>(), std::declval<T &&>()));
template <typename S, typename T>
struct IsFlexibleIncludedHelper {
template <typename Q, typename R, typename = TryArchiveProcess<Q, R>>
static std::true_type tester(Q&&, R&&);
static std::false_type tester(...);
using type = decltype(tester(std::declval<S>(), std::declval<T>()));
};
template <typename S, typename T>
struct IsFlexibleIncludedHelper {
template <typename Q, typename R, typename = TryArchiveProcess<Q, R>>
static std::true_type tester(Q&&, R&&);
static std::false_type tester(...);
using type = decltype(tester(std::declval<S>(), std::declval<T>()));
};
template <typename S, typename T>
struct IsFlexibleIncluded :IsFlexibleIncludedHelper<S, T>::type {};
template <typename S, typename T>
struct IsFlexibleIncluded :IsFlexibleIncludedHelper<S, T>::type {};
#else
//helper metafunction, that is added to c++17
template<typename... Ts>
struct make_void {
typedef void type;
};
template<typename... Ts>
using void_t = typename make_void<Ts...>::type;
//helper metafunction, that is added to c++17
template<typename... Ts>
struct make_void {
typedef void type;
};
template<typename... Ts>
using void_t = typename make_void<Ts...>::type;
template <typename, typename, typename = void>
struct HasSerializeFunction :std::false_type {};
template<typename, typename, typename = void>
struct HasSerializeFunction : std::false_type {
};
template <typename S, typename T>
struct HasSerializeFunction<S, T,
void_t<decltype(serialize(std::declval<S &>(), std::declval<T &>()))>
> : std::true_type {};
template<typename S, typename T>
struct HasSerializeFunction<S, T,
void_t<decltype(serialize(std::declval<S &>(), std::declval<T &>()))>
> : std::true_type {
};
template <typename, typename, typename = void>
struct HasSerializeMethod :std::false_type {};
template<typename, typename, typename = void>
struct HasSerializeMethod : std::false_type {
};
template <typename S, typename T>
struct HasSerializeMethod<S, T,
void_t<decltype(Access::serialize(std::declval<S &>(), std::declval<T &>()))>
> : std::true_type {};
template<typename S, typename T>
struct HasSerializeMethod<S, T,
void_t<decltype(Access::serialize(std::declval<S &>(), std::declval<T &>()))>
> : std::true_type {
};
//this solution doesn't work with visual studio, but is more elegant
template <typename, typename, typename = void>
struct IsFlexibleIncluded :std::false_type {};
//this solution doesn't work with visual studio, but is more elegant
template<typename, typename, typename = void>
struct IsFlexibleIncluded : std::false_type {
};
template <typename S, typename T>
struct IsFlexibleIncluded<S, T,
void_t<decltype(archiveProcess(std::declval<S &>(), std::declval<T &&>()))>
> : std::true_type {};
template<typename S, typename T>
struct IsFlexibleIncluded<S, T,
void_t<decltype(archiveProcess(std::declval<S &>(), std::declval<T &&>()))>
> : std::true_type {
};
#endif
//used for extensions, when extension TValue = void
struct DummyType {
};
@@ -201,23 +219,34 @@ namespace bitsery {
struct SerializeFunction {
static void invoke(S &s, T &v) {
static_assert(HasSerializeFunction<S,T>::value || HasSerializeMethod<S,T>::value,
static_assert(HasSerializeFunction<S, T>::value || HasSerializeMethod<S, T>::value,
"\nPlease define 'serialize' function for your type (inside or outside of class):\n"
" template<typename S>\n"
" void serialize(S& s)\n"
" {\n"
" ...\n"
" }\n");
static_assert(!(HasSerializeFunction<S,T>::value && HasSerializeMethod<S,T>::value),
"\nPlease define only one 'serialize' function (member OR free), not both.");
internalInvoke(s,v, HasSerializeMethod<S,T>{});
using TDecayed = typename std::decay<T>::type;
selectSerializeFnc(s, v, SelectSerializeFnc<TDecayed>{});
}
private:
static void internalInvoke(S& s, T& v,std::true_type) {
Access::serialize(s,v);
static void selectSerializeFnc(S &s, T &v, std::integral_constant<int, 0>) {
static_assert(!(HasSerializeFunction<S, T>::value && HasSerializeMethod<S, T>::value),
"\nPlease define only one 'serialize' function (member OR free).\n"
"If serialization function is inherited from base class, then explicitly select correct function for your type e.g.:\n"
" template <>\n"
" struct SelectSerializeFnc<DerivedClass>:UseMemberFnc {};\n");
selectSerializeFnc(s, v, std::integral_constant<int,
HasSerializeFunction<S, T>::value ? 1 : 2>{});
}
static void internalInvoke(S& s, T& v,std::false_type) {
serialize(s,v);
static void selectSerializeFnc(S &s, T &v, std::integral_constant<int, 1>) {
serialize(s, v);
}
static void selectSerializeFnc(S &s, T &v, std::integral_constant<int, 2>) {
Access::serialize(s, v);
}
};
@@ -228,7 +257,7 @@ namespace bitsery {
template<typename S, typename T, typename Enabled = void>
struct ArchiveFunction {
static void invoke(S &s, T&& obj) {
static void invoke(S &s, T &&obj) {
static_assert(IsFlexibleIncluded<S, T>::value,
"\nPlease include '<bitsery/flexible.h>' to use 'archive' function:\n");
@@ -237,71 +266,135 @@ namespace bitsery {
};
/*
* function for getting context from serializer/deserializer
* has different behaviour when context is tuple
* helper function for getting context from serializer/deserializer
*/
template <typename T, template <typename...> class Template>
struct IsSpecializationOf : std::false_type
{};
template<typename T, template<typename...> class Template>
struct IsSpecializationOf : std::false_type {
};
template <template <typename...> class Template, typename... Args>
struct IsSpecializationOf<Template<Args...>, Template> : std::true_type
{};
template<template<typename...> class Template, typename... Args>
struct IsSpecializationOf<Template<Args...>, Template> : std::true_type {
};
//helper types for better error messages
template <typename Find, typename ... TList>
struct GetTypeIndex:std::integral_constant<size_t, 0>
{};
template<typename Find, typename ... TList>
struct GetTypeIndex : std::integral_constant<size_t, 0> {
};
//found it
template <typename Find, typename ... Tail>
struct GetTypeIndex<Find, Find, Tail...> :std::integral_constant<size_t, 0>
{};
template<typename Find, typename ... Tail>
struct GetTypeIndex<Find, Find, Tail...> : std::integral_constant<size_t, 0> {
};
//iteratates over types
template <typename Find, typename Head, typename ... Tail>
struct GetTypeIndex<Find, Head, Tail...> :std::integral_constant<size_t, 1 + GetTypeIndex<Find, Tail...>::value>
{};
template<typename Find, typename Head, typename ... Tail>
struct GetTypeIndex<Find, Head, Tail...> : std::integral_constant<size_t,
1 + GetTypeIndex<Find, Tail...>::value> {
};
template <typename Find, typename ... TList>
struct HasType: std::integral_constant<bool, (GetTypeIndex<Find, TList...>::value < (sizeof ... (TList)))>
{};
template<typename Find, typename ... TList>
struct HasType : std::integral_constant<bool, (GetTypeIndex<Find, TList...>::value<(sizeof ... (TList)))> {
};
template<typename TCast, typename Tuple>
struct HasContext : std::is_same<TCast, Tuple> {
};
//when context is tuple, then get object from tuple
#if __cplusplus >= 201402L
template <typename TCast, typename ... Args>
TCast* getContextImpl(std::tuple<Args...>* ctx, std::true_type) {
static_assert(HasType<TCast, Args...>::value, "Invalid context cast from tuple. Type doesn't exists.");
return std::addressof(std::get<TCast>(*ctx));
}
#else
//c++11 doesn't have std::get<type> overload for getting tuple element, so we need to write our selves
template<typename TCast, typename ... Args>
struct HasContext<TCast, std::tuple<Args...>> : HasType<TCast, Args...> {
};
template <typename TCast, typename ... Args>
TCast* getContextImpl(std::tuple<Args...>* ctx, std::true_type) {
/*
* get context, and static assert if type doesn't exists
*/
template<typename TCast, typename ... Args>
TCast *getContextImpl(std::tuple<Args...> *ctx, std::true_type) {
using TCastIndex = GetTypeIndex<TCast, Args...>;
static_assert(HasType<TCast, Args...>::value, "Invalid context cast from tuple. Type doesn't exists.");
static_assert(HasType<TCast, Args...>::value, "Invalid context cast. Type doesn't exists.");
return std::addressof(std::get<TCastIndex::value>(*ctx));
}
#endif
template <typename TCast, typename TContext>
TCast* getContextImpl(TContext* ctx, std::false_type) {
static_assert(std::is_convertible<TContext*, TCast*>::value, "Invalid context cast");
return static_cast<TCast*>(ctx);
template<typename TCast, typename TContext>
TCast *getContextImpl(TContext *ctx, std::false_type) {
static_assert(std::is_convertible<TContext *, TCast *>::value, "Invalid context cast. Type doesn't exists.");
return static_cast<TCast *>(ctx);
}
template <typename TCast, typename TContext>
TCast* getContext(TContext* ctx) {
//get local ctx
template<typename TCast, typename TContext, typename TInternalContext>
TCast *chooseInternalOrExternalContext(TContext *, TInternalContext &internalCtx, std::true_type) {
return getContextImpl<TCast>(&internalCtx, std::true_type{});
}
//get external ctx
template<typename TCast, typename TContext, typename TInternalContext>
TCast *chooseInternalOrExternalContext(TContext *ctx, TInternalContext &, std::false_type) {
return ctx
? getContextImpl<TCast>(ctx, IsSpecializationOf<TContext, std::tuple>{})
: nullptr;
}
}
template<typename TCast, typename TContext, typename TInternalContext>
TCast *getContext(TContext *ctx, TInternalContext &internalCtx) {
return chooseInternalOrExternalContext<TCast>(ctx, internalCtx, HasContext<TCast, TInternalContext>{});
}
/*
* get context, if type doesn't exists then do not static_assert but return null instead
*/
template<typename TCast, typename TContext>
TCast *getContextFromTypeIfExists(TContext *ctx, std::true_type) {
return static_cast<TCast *>(ctx);
}
template<typename TCast, typename TContext>
TCast *getContextFromTypeIfExists(TContext *, std::false_type) {
return nullptr;
}
template<typename TCast, typename TContext>
TCast *getContextImplIfExists(TContext *ctx, std::false_type) {
return getContextFromTypeIfExists<TCast>(ctx, std::is_convertible<TContext *, TCast *>{});
}
template<typename TCast, typename TContext>
TCast *getContextFromTupleIfExists(TContext *ctx, std::true_type tmp) {
return getContextImpl<TCast>(ctx, tmp);
}
template<typename TCast, typename TContext>
TCast *getContextFromTupleIfExists(TContext *, std::false_type) {
return nullptr;
}
template<typename TCast, typename TContext>
TCast *getContextImplIfExists(TContext *ctx, std::true_type) {
return getContextFromTupleIfExists<TCast>(ctx, HasContext<TCast, TContext>{});
}
//get local ctx
template<typename TCast, typename TContext, typename TInternalContext>
TCast *chooseInternalOrExternalContextIfExists(TContext *, TInternalContext &internalCtx, std::true_type) {
return getContextImplIfExists<TCast>(&internalCtx, std::true_type{});
}
//get external ctx
template<typename TCast, typename TContext, typename TInternalContext>
TCast *chooseInternalOrExternalContextIfExists(TContext *ctx, TInternalContext &, std::false_type) {
return ctx
? getContextImplIfExists<TCast>(ctx, IsSpecializationOf<TContext, std::tuple>{})
: nullptr;
}
template<typename TCast, typename TContext, typename TInternalContext>
TCast *getContextIfTypeExists(TContext *ctx, TInternalContext &internalCtx) {
return chooseInternalOrExternalContextIfExists<TCast>(ctx, internalCtx, HasContext<TCast, TInternalContext>{});
}
}
}
#endif //BITSERY_DETAILS_SERIALIZATION_COMMON_H

View File

@@ -2,8 +2,8 @@
// Created by fraillt on 17.10.5.
//
#ifndef BITSERY_BUFFER_SESSIONS_H
#define BITSERY_BUFFER_SESSIONS_H
#ifndef BITSERY_DETAILS_SESSIONS_H
#define BITSERY_DETAILS_SESSIONS_H
#include <vector>
#include <stack>
@@ -225,4 +225,4 @@ namespace bitsery {
}
}
#endif //BITSERY_BUFFER_SESSIONS_H
#endif //BITSERY_DETAILS_SESSIONS_H

View File

@@ -23,6 +23,8 @@
#ifndef BITSERY_EXT_GROWABLE_H
#define BITSERY_EXT_GROWABLE_H
#include "../traits/core/traits.h"
namespace bitsery {
namespace ext {

View File

@@ -0,0 +1,156 @@
//MIT License
//
//Copyright (c) 2017 Mindaugas Vinkelis
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.
#ifndef BITSERY_EXT_INHERITANCE_H
#define BITSERY_EXT_INHERITANCE_H
#include <unordered_set>
#include "../traits/core/traits.h"
namespace bitsery {
namespace ext {
//required when virtual inheritance (ext::VirtualBaseClass) exists in serialization flow.
//for standard inheritance (ext::BaseClass) it is optional.
class InheritanceContext {
public:
InheritanceContext() = default;
InheritanceContext(const InheritanceContext&) = delete;
InheritanceContext&operator = (const InheritanceContext&) = delete;
InheritanceContext(InheritanceContext&&) = default;
InheritanceContext& operator = (InheritanceContext&&) = default;
template <typename TDerived, typename TBase>
void beginBase(const TDerived &derived, const TBase &) {
if (_depth == 0) {
const void* ptr = std::addressof(derived);
if ( _parentPtr != ptr)
_virtualBases.clear();
_parentPtr = ptr;
}
++_depth;
}
template <typename TDerived, typename TBase>
bool beginVirtualBase(const TDerived &derived, const TBase &base) {
beginBase(derived, base);
return _virtualBases.emplace(std::addressof(base)).second;
}
void end() {
--_depth;
}
private:
//these members are required to know when we can clear _virtualBases
size_t _depth{};
const void* _parentPtr{};
//add virtual bases to the list, as long as we're on the same parent
std::unordered_set<const void*> _virtualBases{};
};
template <typename TBase>
class BaseClass {
public:
template<typename Ser, typename Writer, typename T, typename Fnc>
void serialize(Ser &ser, Writer &, const T &obj, Fnc &&fnc) const {
auto& resObj = static_cast<const TBase&>(obj);
if (auto ctx = ser.template contextOrNull<InheritanceContext>()) {
ctx->beginBase(obj, resObj);
fnc(const_cast<TBase&>(resObj));
ctx->end();
} else {
fnc(const_cast<TBase&>(resObj));
}
}
template<typename Des, typename Reader, typename T, typename Fnc>
void deserialize(Des &des, Reader &, T &obj, Fnc &&fnc) const {
auto& resObj = static_cast<TBase&>(obj);
if (auto ctx = des.template contextOrNull<InheritanceContext>()) {
ctx->beginBase(obj, resObj);
fnc(resObj);
ctx->end();
} else {
fnc(resObj);
}
}
};
//requires InheritanceContext
template <typename TBase>
class VirtualBaseClass {
public:
template<typename Ser, typename Writer, typename T, typename Fnc>
void serialize(Ser &ser, Writer &, const T &obj, Fnc &&fnc) const {
auto ctx = ser.template context<InheritanceContext>();
auto& resObj = static_cast<const TBase&>(obj);
if (ctx->beginVirtualBase(obj, resObj))
fnc(const_cast<TBase&>(resObj));
ctx->end();
}
template<typename Des, typename Reader, typename T, typename Fnc>
void deserialize(Des &des, Reader &, T &obj, Fnc &&fnc) const {
auto ctx = des.template context<InheritanceContext>();
auto& resObj = static_cast<TBase&>(obj);
if (ctx->beginVirtualBase(obj, resObj))
fnc(resObj);
ctx->end();
}
};
}
namespace traits {
template<typename TBase, typename T>
struct ExtensionTraits<ext::BaseClass<TBase>, T> {
static_assert(std::is_base_of<TBase, T>::value, "Invalid base class");
using TValue = TBase;
static constexpr bool SupportValueOverload = false;
static constexpr bool SupportObjectOverload = true;
static constexpr bool SupportLambdaOverload = true;
};
template<typename TBase, typename T>
struct ExtensionTraits<ext::VirtualBaseClass<TBase>, T> {
static_assert(std::is_base_of<TBase, T>::value, "Invalid base class");
using TValue = TBase;
static constexpr bool SupportValueOverload = false;
static constexpr bool SupportObjectOverload = true;
//disable lambda overload, when serializing virtually inherited base class.
//Only one instance of virtual base will be serialized, when using multiple inheritance
//and it will be undefined behaviour if derived classes would have different virtual base class serialization flow.
static constexpr bool SupportLambdaOverload = false;
};
}
}
#endif //BITSERY_EXT_INHERITANCE_H

View File

@@ -23,6 +23,8 @@
#ifndef BITSERY_EXT_POINTER_H
#define BITSERY_EXT_POINTER_H
#include "../traits/core/traits.h"
#include <unordered_map>
#include <vector>
#include <memory>
@@ -34,21 +36,93 @@ namespace bitsery {
//forward declare
class PointerLinkingContext;
enum PointerType {
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 {
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
//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 {
@@ -65,36 +139,37 @@ namespace bitsery {
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;
};
size_t createId(const void *ptr, PointerOwnershipType ptrType) {
if (ptr == nullptr)
return 0u;
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.id;
return ptrInfo;
}
//ptr already exists
//for observer return success
if (ptrType == PointerOwnershipType::Observer)
return ptrInfo.id;
return ptrInfo;
//set owner and return success
if (ptrInfo.ownershipType == PointerOwnershipType::Observer) {
ptrInfo.ownershipType = ptrType;
return ptrInfo.id;
return ptrInfo;
}
//only shared ownership can get here multiple times
assert(ptrType == PointerOwnershipType::Shared);
return ptrInfo.id;
}
template <typename S, typename TPtr>
void serialize(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>{});
ptrInfo.sharedCount++;
return ptrInfo;
}
//valid, when all pointers have owners.
@@ -106,25 +181,6 @@ namespace bitsery {
}
private:
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);
}
struct PointerInfo {
PointerInfo(size_t id_, PointerOwnershipType ownershipType_)
:id{id_},
ownershipType{ownershipType_}
{};
size_t id;
PointerOwnershipType ownershipType;
};
size_t _currId;
std::unordered_map<const void*, PointerInfo> _ptrMap;
@@ -135,23 +191,6 @@ namespace bitsery {
virtual ~SharedContextBase() = default;
};
//helper functions that creates or destroys pointers
//useful, because can be specialized
template <typename T>
void destroyPointer(T& ptr) {
if (ptr) {
delete ptr;
}
ptr = nullptr;
}
template <typename T>
void createPointer(T& ptr) {
using TNonPtr = typename std::remove_pointer<T>::type;
if (ptr == nullptr)
ptr = new TNonPtr{};
}
class PointerLinkingContextDeserialization {
public:
explicit PointerLinkingContextDeserialization()
@@ -165,36 +204,52 @@ namespace bitsery {
PointerLinkingContextDeserialization& operator = (PointerLinkingContextDeserialization&&) = default;
~PointerLinkingContextDeserialization() = default;
void processOwnerPtr(size_t id, void* ptr, PointerOwnershipType ptrType) {
assert(id != 0 && ptr != nullptr && ptrType != PointerOwnershipType::Observer);
auto res = _idMap.emplace(id, PointerInfo{id, ptr, ptrType});
auto& ptrInfo = res.first->second;
if (!res.second) {
assert(ptrInfo.ownershipType != PointerOwnershipType::Owner);
//if already exists, then process observer list
if (ptrInfo.ownershipType == PointerOwnershipType::Observer) {
ptrInfo.ptrAddress = ptr;
ptrInfo.ownershipType = ptrType;
processObserverList(ptrInfo.observersList, ptr);
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;
};
void processObserverPtr(size_t id, void* (&ptr)) {
auto res = _idMap.emplace(id, PointerInfo{id, nullptr, PointerOwnershipType::Observer});
PointerInfo& getInfoById(size_t id, PointerOwnershipType ptrType) {
auto res = _idMap.emplace(id, PointerInfo{id, nullptr, ptrType});
auto& ptrInfo = res.first->second;
if (ptrInfo.ptrAddress)
ptr = ptrInfo.ptrAddress;
else
ptrInfo.observersList.push_back(ptr);
}
template <typename S, typename TPtr>
void deserialize(S& s, TPtr& obj) {
using TNonPtr = typename std::remove_pointer<TPtr>::type;
static_assert(!std::is_polymorphic<TNonPtr>::value, "Polymorphic types are not supported");
createPointer(obj);
deserializeImpl(s, *obj, details::IsFundamentalType<TNonPtr>{});
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
@@ -205,45 +260,6 @@ namespace bitsery {
}
private:
struct PointerInfo {
PointerInfo(size_t id_, void* ptr, PointerOwnershipType ownershipType_)
:id{id_},
ownershipType{ownershipType_},
ptrAddress{ptr},
observersList{},
sharedContext{}
{};
PointerInfo(const PointerInfo&) = delete;
PointerInfo& operator = (const PointerInfo&) = delete;
PointerInfo(PointerInfo&&) = default;
PointerInfo&operator = (PointerInfo&&) = default;
~PointerInfo() = default;
size_t id;
PointerOwnershipType ownershipType;
void* ptrAddress;
std::vector<std::reference_wrapper<void*>> observersList;
std::unique_ptr<SharedContextBase> sharedContext;
};
void processObserverList(std::vector<std::reference_wrapper<void*>>& observers, void* ptr) {
for (auto& o:observers)
o.get() = ptr;
observers.clear();
observers.shrink_to_fit();
}
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);
}
std::unordered_map<size_t, PointerInfo> _idMap;
};
@@ -265,7 +281,6 @@ namespace bitsery {
}
};
class PointerOwner {
public:
explicit PointerOwner(PointerType ptrType = PointerType::Nullable):_ptrType{ptrType} {}
@@ -273,11 +288,14 @@ namespace bitsery {
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 id = ctx.createId(obj, details_pointer::PointerOwnershipType::Owner);
assert(id || _ptrType == PointerType::Nullable);
details::writeSize(w, id);
if (id)
ctx.serialize(ser, obj);
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>
@@ -287,11 +305,12 @@ namespace bitsery {
details::readSize(r, id, std::numeric_limits<size_t>::max());
if (id) {
auto& ctx = details_pointer::getLinkingContext(des);
ctx.deserialize(des, obj);
ctx.processOwnerPtr(id, obj, details_pointer::PointerOwnershipType::Owner);
auto& ptrInfo = ctx.getInfoById(id, PointerOwnershipType::Owner);
details_pointer::RawPointerManager::deserializeObject(r, des, obj);
ptrInfo.processOwner(obj);
} else {
if (_ptrType == PointerType::Nullable)
details_pointer::destroyPointer(obj);
details_pointer::RawPointerManager::destroyObject(obj);
else
r.setError(ReaderError::InvalidPointer);
}
@@ -308,9 +327,12 @@ namespace bitsery {
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 id = ctx.createId(obj, details_pointer::PointerOwnershipType::Observer);
assert(id || _ptrType == PointerType::Nullable);
details::writeSize(w, id);
if (obj) {
details::writeSize(w, ctx.getInfoByPtr(obj, PointerOwnershipType::Observer).id);
} else {
assert(_ptrType == PointerType::Nullable);
details::writeSize(w, 0);
}
}
template<typename Des, typename Reader, typename T, typename Fnc>
@@ -319,7 +341,7 @@ namespace bitsery {
details::readSize(r, id, std::numeric_limits<size_t>::max());
if (id) {
auto& ctx = details_pointer::getLinkingContext(des);
ctx.processObserverPtr(id, reinterpret_cast<void*&>(obj));
ctx.getInfoById(id, PointerOwnershipType::Observer).processObserver(reinterpret_cast<void*&>(obj));
} else {
if (_ptrType == PointerType::Nullable)
obj = nullptr;
@@ -336,7 +358,7 @@ namespace bitsery {
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);
details::writeSize(w, ctx.createId(&obj, details_pointer::PointerOwnershipType::Owner));
details::writeSize(w, ctx.getInfoByPtr(&obj, PointerOwnershipType::Owner).id);
fnc(const_cast<T&>(obj));
}
@@ -347,7 +369,7 @@ namespace bitsery {
if (id) {
auto& ctx = details_pointer::getLinkingContext(des);
fnc(obj);
ctx.processOwnerPtr(id, &obj, details_pointer::PointerOwnershipType::Owner);
ctx.getInfoById(id, PointerOwnershipType::Owner).processOwner(&obj);
} else {
//cannot be null for references
r.setError(ReaderError::InvalidPointer);

View File

@@ -23,6 +23,7 @@
#ifndef BITSERY_EXT_STD_MAP_H
#define BITSERY_EXT_STD_MAP_H
#include "../traits/core/traits.h"
#include "../details/adapter_utils.h"
namespace bitsery {

View File

@@ -33,6 +33,7 @@
// using optional = experimental::optional<T>;
//}
#include <type_traits>
#include "../traits/core/traits.h"
namespace bitsery {
namespace ext {

View File

@@ -27,6 +27,7 @@
#include "../details/adapter_utils.h"
//we need this, so we could
#include <unordered_set>
#include "../traits/core/traits.h"
namespace bitsery {
namespace ext {

View File

@@ -39,10 +39,14 @@ namespace bitsery {
using BPEnabledType = BasicSerializer<typename std::conditional<TAdapterWriter::BitPackingEnabled,
TAdapterWriter, AdapterWriterBitPackingWrapper<TAdapterWriter>>::type, TContext>;
static_assert(details::IsSpecializationOf<typename TWriter::TConfig::InternalContext, std::tuple>::value,
"Config::InternalContext must be std::tuple");
template <typename WriterParam>
explicit BasicSerializer(WriterParam&& w, TContext* context = nullptr)
: _writer{std::forward<WriterParam>(w)},
_context{context}
_context{context},
_internalContext{}
{
}
@@ -64,7 +68,12 @@ namespace bitsery {
template <typename T>
T* context() {
return details::getContext<T>(_context);
return details::getContext<T>(_context, _internalContext);
}
template <typename T>
T* contextOrNull() {
return details::getContextIfTypeExists<T>(_context, _internalContext);
}
/*
@@ -322,6 +331,7 @@ namespace bitsery {
TAdapterWriter _writer;
TContext* _context;
typename TWriter::TConfig::InternalContext _internalContext;
//process value types
//false_type means that we must process all elements individually

View File

@@ -20,8 +20,8 @@
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.
#ifndef BITSERY_TRAITS_HELPER_STD_DEFAULTS_H
#define BITSERY_TRAITS_HELPER_STD_DEFAULTS_H
#ifndef BITSERY_TRAITS_CORE_STD_DEFAULTS_H
#define BITSERY_TRAITS_CORE_STD_DEFAULTS_H
#include "traits.h"
#include <iostream>
@@ -82,4 +82,4 @@ namespace bitsery {
}
}
#endif //BITSERY_TRAITS_HELPER_STD_DEFAULTS_H
#endif //BITSERY_TRAITS_CORE_STD_DEFAULTS_H

View File

@@ -26,6 +26,19 @@
using testing::Eq;
template <typename ... Args>
struct ConfigWithContext: bitsery::DefaultConfig {
using InternalContext = std::tuple<Args...>;
};
template <typename Context, typename ... Args>
using SerializerConfigWithContext = bitsery::BasicSerializer<
bitsery::AdapterWriter<bitsery::OutputBufferAdapter<Buffer>, ConfigWithContext<Args...>>, Context>;
template <typename Context, typename ... Args>
using DeserializerConfigWithContext = bitsery::BasicDeserializer<
bitsery::AdapterReader<bitsery::InputBufferAdapter<Buffer>, ConfigWithContext<Args...>>, Context>;
template <typename Context>
using MySerializer = bitsery::BasicSerializer<Writer, Context>;
@@ -66,4 +79,55 @@ TEST(SerializationContext, WhenContextIsNotTupleThenContextCastOverloadReturnSam
SingleTypeContext ctx{};
MySerializer<SingleTypeContext> ser1{buf, &ctx};
EXPECT_THAT(ser1.context<SingleTypeContext>(), Eq(&ctx));
}
TEST(SerializationContext, SerializerDeserializerCanHaveInternalContextViaConfig) {
Buffer buf{};
SerializerConfigWithContext<void, float, int> ser{buf};
EXPECT_THAT(ser.context<int>(), ::testing::NotNull());
EXPECT_THAT(*ser.context<int>(), Eq(0));
*ser.context<int>() = 10;
EXPECT_THAT(*ser.context<int>(), Eq(10));
DeserializerConfigWithContext<void, char> des{InputAdapter{buf.begin(), 1}};
EXPECT_THAT(des.context<char>(), ::testing::NotNull());
EXPECT_THAT(*des.context<char>(), Eq(0));
*des.context<char>() = 10;
EXPECT_THAT(*des.context<char>(), Eq(10));
//new instance has new context
SerializerConfigWithContext<void, float, int> ser2{buf};
EXPECT_THAT(ser2.context<int>(), ::testing::NotNull());
EXPECT_THAT(*ser2.context<int>(), Eq(0));
}
TEST(SerializationContext, WhenInternalAndExternalContextIsTheSamePriorityGoesToInternalContext) {
Buffer buf{};
int externalCtx = 5;
SerializerConfigWithContext<int, float, int> ser{buf, &externalCtx};
EXPECT_THAT(ser.context<int>(), ::testing::NotNull());
EXPECT_THAT(*ser.context<int>(), Eq(0));
*ser.context<int>() = 2;
DeserializerConfigWithContext<int, int, char> des{InputAdapter{buf.begin(), 1}, &externalCtx};
EXPECT_THAT(des.context<char>(), ::testing::NotNull());
EXPECT_THAT(*des.context<char>(), Eq(0));
*des.context<int>() = 3;
EXPECT_THAT(externalCtx, Eq(5));
}
TEST(SerializationContext, ContextIfExistsReturnsNullWhenTypeDoesntExists) {
Buffer buf{};
std::tuple<double, short> extCtx1{};
SerializerConfigWithContext<std::tuple<double, short>, float, int> ser{buf, &extCtx1};
EXPECT_THAT(ser.contextOrNull<int>(), ::testing::NotNull());
EXPECT_THAT(ser.contextOrNull<char>(), ::testing::IsNull());
double extCtx2{};
DeserializerConfigWithContext<double, int, char> des{InputAdapter{buf.begin(), 1}, &extCtx2};
EXPECT_THAT(des.contextOrNull<double>(), ::testing::NotNull());
EXPECT_THAT(des.contextOrNull<float>(), ::testing::IsNull());
}

View File

@@ -0,0 +1,354 @@
//MIT License
//
//Copyright (c) 2017 Mindaugas Vinkelis
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.
#include <gmock/gmock.h>
#include "serialization_test_utils.h"
#include <bitsery/ext/inheritance.h>
using bitsery::ext::BaseClass;
using bitsery::ext::VirtualBaseClass;
struct ConfigWithInheritanceCtx:bitsery::DefaultConfig {
using InternalContext = std::tuple<bitsery::ext::InheritanceContext>;
};
using SerContext = BasicSerializationContext<ConfigWithInheritanceCtx, void>;
using testing::Eq;
/*
* base class
*/
struct Base {
uint8_t x{};
virtual ~Base() = default;
};
template <typename S>
void serialize(S& s, Base& o) {
s.value1b(o.x);
}
/*
* non virtual inheritance from base
*/
struct Derive1NonVirtually:Base {
uint8_t y1{};
};
template <typename S>
void serialize(S& s, Derive1NonVirtually& o) {
s.ext(o, BaseClass<Base>{});
s.value1b(o.y1);
}
struct Derive2NonVirtually:Base {
uint8_t y2{};
};
template <typename S>
void serialize(S& s, Derive2NonVirtually& o) {
//use lambda to serialize base
s.ext(o, BaseClass<Base>{}, [&s](Base& b) {
s.object(b);
});
s.value1b(o.y2);
}
struct MultipleInheritanceNonVirtualBase: Derive1NonVirtually, Derive2NonVirtually {
uint8_t z{};
};
template <typename S>
void serialize(S& s, MultipleInheritanceNonVirtualBase& o) {
s.ext(o, BaseClass<Derive1NonVirtually>{});
s.ext(o, BaseClass<Derive2NonVirtually>{});
s.value1b(o.z);
}
/*
* virtual inheritance from base
*/
struct Derive1Virtually:virtual Base {
uint8_t y1{};
};
template <typename S>
void serialize(S& s, Derive1Virtually& o) {
s.ext(o, VirtualBaseClass<Base>{});
s.value1b(o.y1);
}
struct Derive2Virtually:virtual Base {
uint8_t y2{};
};
template <typename S>
void serialize(S& s, Derive2Virtually& o) {
s.ext(o, VirtualBaseClass<Base>{});
s.value1b(o.y2);
}
struct MultipleInheritanceVirtualBase: Derive1Virtually, Derive2Virtually {
uint8_t z{};
MultipleInheritanceVirtualBase() = default;
MultipleInheritanceVirtualBase(uint8_t x_, uint8_t y1_, uint8_t y2_, uint8_t z_) {
x = x_;
y1 = y1_;
y2 = y2_;
z = z_;
}
template <typename S>
void serialize(S& s) {
s.ext(*this, BaseClass<Derive1Virtually>{});
s.ext(*this, BaseClass<Derive2Virtually>{});
s.value1b(z);
}
};
bool operator == (const MultipleInheritanceVirtualBase& lhs, const MultipleInheritanceVirtualBase& rhs) {
return std::tie(lhs.x, lhs.y1, lhs.y2, lhs.z) == std::tie(rhs.x, rhs.y1, rhs.y2, rhs.z);
}
TEST(SerializeExtensionInheritance, BaseClass) {
Derive1NonVirtually d1{};
d1.x = 187;
d1.y1 = 74;
Derive1NonVirtually rd1{};
SerContext ctx{};
ctx.createSerializer().object(d1);
ctx.createDeserializer().object(rd1);
EXPECT_THAT(rd1.x, Eq(d1.x));
EXPECT_THAT(rd1.y1, Eq(d1.y1));
EXPECT_THAT(ctx.getBufferSize(), Eq(2));
}
TEST(SerializeExtensionInheritance, VirtualBaseClass) {
Derive1Virtually d1{};
d1.x = 15;
d1.y1 = 87;
Derive1Virtually rd1{};
SerContext ctx{};
ctx.createSerializer().object(d1);
ctx.createDeserializer().object(rd1);
EXPECT_THAT(rd1.x, Eq(d1.x));
EXPECT_THAT(rd1.y1, Eq(d1.y1));
EXPECT_THAT(ctx.getBufferSize(), Eq(2));
}
TEST(SerializeExtensionInheritance, MultipleBasesWithoutVirtualInheritance) {
MultipleInheritanceNonVirtualBase md{};
//x is ambiguous because we don't derive virtually
static_cast<Derive1NonVirtually&>(md).x = 1;
static_cast<Derive2NonVirtually&>(md).x = 2;
md.y1 = 4;
md.z = 5;
md.y2 = 6;
MultipleInheritanceNonVirtualBase res{};
SerContext ctx{};
ctx.createSerializer().object(md);
ctx.createDeserializer().object(res);
EXPECT_THAT(static_cast<Derive1NonVirtually&>(res).x, Eq(static_cast<Derive1NonVirtually&>(md).x));
EXPECT_THAT(static_cast<Derive2NonVirtually&>(res).x, Eq(static_cast<Derive2NonVirtually&>(md).x));
EXPECT_THAT(res.y1, Eq(md.y1));
EXPECT_THAT(res.y2, Eq(md.y2));
EXPECT_THAT(res.z, Eq(md.z));
EXPECT_THAT(ctx.getBufferSize(), Eq(5)); //5 because two bases
}
TEST(SerializeExtensionInheritance, WhenNoVirtualInheritanceExistsThenInheritanceContextIsNotRequired) {
MultipleInheritanceNonVirtualBase md{};
//x is ambiguous because we don't derive virtually
static_cast<Derive1NonVirtually&>(md).x = 1;
static_cast<Derive2NonVirtually&>(md).x = 2;
md.y1 = 4;
md.z = 5;
md.y2 = 6;
MultipleInheritanceNonVirtualBase res{};
//without InheritanceContext
SerializationContext ctx{};
ctx.createSerializer().object(md);
ctx.createDeserializer().object(res);
EXPECT_THAT(static_cast<Derive1NonVirtually&>(res).x, Eq(static_cast<Derive1NonVirtually&>(md).x));
EXPECT_THAT(static_cast<Derive2NonVirtually&>(res).x, Eq(static_cast<Derive2NonVirtually&>(md).x));
EXPECT_THAT(res.y1, Eq(md.y1));
EXPECT_THAT(res.y2, Eq(md.y2));
EXPECT_THAT(res.z, Eq(md.z));
EXPECT_THAT(ctx.getBufferSize(), Eq(5)); //5 because two bases
}
TEST(SerializeExtensionInheritance, MultipleBasesWithVirtualInheritance) {
MultipleInheritanceVirtualBase md{3,7,5,15};
MultipleInheritanceVirtualBase res{};
SerContext ctx{};
ctx.createSerializer().object(md);
ctx.createDeserializer().object(res);
EXPECT_THAT(res, Eq(md));
EXPECT_THAT(ctx.getBufferSize(), Eq(4)); //4 because virtual base
}
TEST(SerializeExtensionInheritance, MultipleBasesWithVirtualInheritanceMultipleObjects) {
std::vector<MultipleInheritanceVirtualBase> data;
data.emplace_back(4,8,7,9);
data.emplace_back(1,2,3,4);
data.emplace_back(8,7,15,97);
data.emplace_back(54,132,45,84);
data.emplace_back(27,85,41,2);
std::vector<MultipleInheritanceVirtualBase> res{};
SerContext ctx{};
ctx.createSerializer().container(data, 10);
ctx.createDeserializer().container(res, 10);
EXPECT_THAT(res, ::testing::ContainerEq(data));
EXPECT_THAT(ctx.getBufferSize(), Eq(1 + 4 * data.size())); //1 container size + 4 because virtual base * elements
}
//
class BasePrivateSerialize {
public:
explicit BasePrivateSerialize(uint8_t v):_v{v} {}
uint8_t getX() const { return _v; }
private:
uint8_t _v;
friend bitsery::Access;
template <typename S>
void serialize(S& s) {
s.value1b(_v);
}
};
class DerivedPrivateBase: public BasePrivateSerialize {
public:
explicit DerivedPrivateBase(uint8_t v) : BasePrivateSerialize(v) {}
uint8_t z{};
};
template <typename S>
void serialize(S& s, DerivedPrivateBase& o) {
//use lambda for base serialization
s.ext(o, BaseClass<BasePrivateSerialize>{}, [&s](BasePrivateSerialize& b) {
s.object(b);
});
s.value1b(o.z);
}
struct BaseNonMemberSerialize {
uint8_t x{};
};
template <typename S>
void serialize(S& s, BaseNonMemberSerialize& o) {
s.value1b(o.x);
}
struct DerivedMemberSerialize: public BaseNonMemberSerialize {
uint8_t z{};
template <typename S>
void serialize(S& s) {
s.ext(*this, BaseClass<BaseNonMemberSerialize>{});
s.value1b(z);
}
};
//explicitly select serialize functions, for types that has ambiguous serialize functions
namespace bitsery {
template <>
struct SelectSerializeFnc<DerivedPrivateBase>:UseNonMemberFnc {};
template <>
struct SelectSerializeFnc<DerivedMemberSerialize>:UseMemberFnc {};
}
TEST(SerializeExtensionInheritance, WhenDerivedClassHasAmbiguousSerializeFunctionThenExplicitlySelectSpecialization) {
DerivedPrivateBase data1{43};
data1.z = 87;
DerivedMemberSerialize data2{};
data2.x = 71;
data2.z = 22;
DerivedPrivateBase res1{0};
DerivedMemberSerialize res2{};
SerContext ctx{};
ctx.createSerializer().object(data1);
ctx.createSerializer().object(data2);
ctx.createDeserializer().object(res1);
ctx.createDeserializer().object(res2);
EXPECT_THAT(res1.getX(), Eq(data1.getX()));
EXPECT_THAT(res1.z, Eq(data1.z));
EXPECT_THAT(res2.x, Eq(data2.x));
EXPECT_THAT(res2.z, Eq(data2.z));
}
struct AbstractBase {
uint8_t x{};
virtual void exec() = 0;
virtual ~AbstractBase() = default;
template <typename S>
void serialize(S& s) {
s.value1b(x);
}
};
struct ImplementedBase:AbstractBase {
uint8_t y{};
void exec() override {}
template <typename S>
void serialize(S& s) {
s.ext(*this, BaseClass<AbstractBase>{});
s.value1b(y);
}
};
TEST(SerializeExtensionInheritance, CanSerializeAbstractClass) {
ImplementedBase data{};
data.x = 4;
data.y = 2;
ImplementedBase res{};
SerContext ctx{};
ctx.createSerializer().object(data);
ctx.createDeserializer().object(res);
EXPECT_THAT(res.x, Eq(data.x));
EXPECT_THAT(res.y, Eq(data.y));
}

View File

@@ -92,9 +92,9 @@ TEST(SerializeExtensionPointer, PointerLinkingContextAcceptsMultipleSharedOwners
MyStruct1* sharedPtr = &data;
//linking context
PointerLinkingContext plctx1{};
EXPECT_THAT(plctx1.createId(sharedPtr, bitsery::ext::details_pointer::PointerOwnershipType::Shared), Eq(1));
EXPECT_THAT(plctx1.createId(sharedPtr, bitsery::ext::details_pointer::PointerOwnershipType::Shared), Eq(1));
EXPECT_THAT(plctx1.createId(sharedPtr, bitsery::ext::details_pointer::PointerOwnershipType::Shared), Eq(1));
EXPECT_THAT(plctx1.getInfoByPtr(sharedPtr, bitsery::ext::PointerOwnershipType::Shared).id, Eq(1));
EXPECT_THAT(plctx1.getInfoByPtr(sharedPtr, bitsery::ext::PointerOwnershipType::Shared).id, Eq(1));
EXPECT_THAT(plctx1.getInfoByPtr(sharedPtr, bitsery::ext::PointerOwnershipType::Shared).id, Eq(1));
}
TEST_F(SerializeExtensionPointerSerialization, WhenPointersAreNullThenIsValid) {