bugfix in enableBitPacking

This commit is contained in:
Mindaugas Vinkelis
2019-06-27 10:06:16 +03:00
committed by Mindaugas Vinkelis
parent aca3139600
commit 03f2c3c8b5
10 changed files with 82 additions and 6 deletions

View File

@@ -1,3 +1,11 @@
# [4.6.1](https://github.com/fraillt/bitsery/compare/v4.6.0...v4.6.1) (2019-06-27)
### Features
* flexible syntax now also supports `cereal` like serialization interface (thanks to [Luli2020](https://github.com/Luli2020))
### Bug fixes
* when using `enableBitPacking`, internal context (i.e. context<T>()) in serializer/deserializer was not copied to/from new bitpacking enabled instance.
# [4.6.0](https://github.com/fraillt/bitsery/compare/v4.5.1...v4.6.0) (2019-03-12)
### Features

View File

@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.1)
project(bitsery
LANGUAGES CXX
VERSION 4.6.0)
VERSION 4.6.1)
#======== build options ===================================
option(BITSERY_BUILD_EXAMPLES "Build examples" OFF)

View File

@@ -15,7 +15,9 @@ Library design:
Core Serializer/Deserializer functions (alphabetical order):
* `operator()` (4.6.1) (when flexible syntax is enabled)
* `align` (1.0.0)
* `archive` (4.0.0) (when flexible syntax is enabled)
* `boolValue` (4.0.0)
* `container` (1.0.0)
* `ext` (2.0.0)

View File

@@ -19,11 +19,12 @@ struct MyStruct {
void serialize(S& s) {
//now we can use flexible syntax with
s.archive(i, e, fs);
// flexible syntax also supports `cereal` like serialization interface by calling operator()
// s(i, e, fs);
}
};
using namespace bitsery;
//some helper types

View File

@@ -258,6 +258,20 @@ namespace bitsery {
}
};
namespace details {
// used in "making friends" with non-wrapped deserializer type
template <typename TReader>
struct GetNonWrappedAdapterReader {
using Reader = TReader;
};
template <typename TWrapped>
struct GetNonWrappedAdapterReader<AdapterReaderBitPackingWrapper<TWrapped>> {
using Reader = TWrapped;
};
}
}
#endif //BITSERY_ADAPTER_READER_H

View File

@@ -330,6 +330,19 @@ namespace bitsery {
TWriter& _writer;
};
namespace details {
// used in "making friends" with non-wrapped serializer type
template <typename TWriter>
struct GetNonWrappedAdapterWriter {
using Writer = TWriter;
};
template <typename TWrapped>
struct GetNonWrappedAdapterWriter<AdapterWriterBitPackingWrapper<TWrapped>> {
using Writer = TWrapped;
};
}
}
#endif //BITSERY_ADAPTER_WRITER_H

View File

@@ -26,7 +26,7 @@
#define BITSERY_MAJOR_VERSION 4
#define BITSERY_MINOR_VERSION 6
#define BITSERY_PATCH_VERSION 0
#define BITSERY_PATCH_VERSION 1
#define BITSERY_QUOTE_MACRO(name) #name
#define BITSERY_BUILD_VERSION_STR(major,minor, patch) \

View File

@@ -104,9 +104,7 @@ namespace bitsery {
template <typename T, typename... TArgs>
BasicDeserializer &operator()(T &&head, TArgs &&... tail) {
//serialize object
details::ArchiveFunction<BasicDeserializer, T>::invoke(*this, std::forward<T>(head));
//expand other elements
archive(std::forward<TArgs>(tail)...);
return *this;
}
@@ -340,6 +338,8 @@ namespace bitsery {
private:
friend AdapterAccess;
// this is required when creating bitpacking serializer, to access internal context
friend class BasicDeserializer<typename details::GetNonWrappedAdapterReader<TAdapterReader>::Reader, TContext>;
TAdapterReader _reader;
TContext* _context;
@@ -415,7 +415,10 @@ namespace bitsery {
void procEnableBitPacking(const Fnc& fnc, std::false_type) {
//create serializer using bitpacking wrapper
BPEnabledType tmp(_reader, _context);
// move internal context to and from of bitpacking enabled serializer
tmp._internalContext = std::move(_internalContext);
fnc(tmp);
_internalContext = std::move(tmp._internalContext);
}
//these are dummy functions for extensions that have TValue = void

View File

@@ -335,11 +335,14 @@ namespace bitsery {
private:
friend AdapterAccess;
// this is required when creating bitpacking serializer, to access internal context
friend class BasicSerializer<typename details::GetNonWrappedAdapterWriter<TAdapterWriter>::Writer, TContext>;
TAdapterWriter _writer;
TContext* _context;
typename TWriter::TConfig::InternalContext _internalContext;
//process value types
//false_type means that we must process all elements individually
template<size_t VSIZE, typename It>
@@ -404,7 +407,10 @@ namespace bitsery {
void procEnableBitPacking(const Fnc& fnc, std::false_type) {
//create serializer using bitpacking wrapper
BPEnabledType tmp(_writer, _context);
// move internal context to and from of bitpacking enabled serializer
tmp._internalContext = std::move(_internalContext);
fnc(tmp);
_internalContext = std::move(tmp._internalContext);
}
//these are dummy functions for extensions that have TValue = void
@@ -423,6 +429,7 @@ namespace bitsery {
};
//helper type
template <typename Adapter>
using Serializer = BasicSerializer<AdapterWriter<Adapter, DefaultConfig>>;

View File

@@ -130,4 +130,32 @@ TEST(SerializationContext, ContextIfExistsReturnsNullWhenTypeDoesntExists) {
DeserializerConfigWithContext<double, int, char> des{InputAdapter{buf.begin(), 1}, &extCtx2};
EXPECT_THAT(des.contextOrNull<double>(), ::testing::NotNull());
EXPECT_THAT(des.contextOrNull<float>(), ::testing::IsNull());
}
}
TEST(SerializationContext, WhenBitPackingIsEnabledThenInternalContextIsMovedToNewInstanceAndMovedBackAfterwards) {
Buffer buf{};
using Ser = SerializerConfigWithContext<void, int>;
using BPSer = typename Ser::BPEnabledType;
using Des = DeserializerConfigWithContext<void, int>;
using BPDes = typename Des::BPEnabledType;
Ser ser{buf, nullptr};
*ser.context<int>() = 1;
EXPECT_THAT(*ser.context<int>(), Eq(1));
ser.enableBitPacking([](BPSer& s) {
EXPECT_THAT(*s.context<int>(), Eq(1));
*s.context<int>() = 2;
});
EXPECT_THAT(*ser.context<int>(), Eq(2));
Des des{InputAdapter{buf.begin(), 1}, nullptr};
*des.context<int>() = 3;
EXPECT_THAT(*des.context<int>(), Eq(3));
des.enableBitPacking([](BPDes& d) {
EXPECT_THAT(*d.context<int>(), Eq(3));
*d.context<int>() = 4;
});
EXPECT_THAT(*des.context<int>(), Eq(4));
}