Skip to content

Commit 8a00183

Browse files
author
Mindaugas Vinkelis
committed
release 5.2.0
1 parent 8a5e12a commit 8a00183

File tree

8 files changed

+286
-6
lines changed

8 files changed

+286
-6
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
# [5.2.0](https://github.com/fraillt/bitsery/compare/v5.1.0...v5.2.0) (2020-11-09)
2+
3+
### Features
4+
5+
* new extension **StdBitset**.
6+
7+
### Improvements
8+
* removed unused variable warnings in release build, where `max_size` variable during serialization is ignored.
9+
* removed unknown pragmas warnings for GCC/Clang (thanks to [Mmpuskas](https://github.com/Mmpuskas)).
10+
111
# [5.1.0](https://github.com/fraillt/bitsery/compare/v5.0.3...v5.1.0) (2020-06-08)
212

313
### Features

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
cmake_minimum_required(VERSION 3.1)
22
project(bitsery
33
LANGUAGES CXX
4-
VERSION 5.1.0)
4+
VERSION 5.2.0)
55

66
#======== build options ===================================
77
option(BITSERY_BUILD_EXAMPLES "Build examples" OFF)

include/bitsery/bitsery.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#define BITSERY_BITSERY_H
2626

2727
#define BITSERY_MAJOR_VERSION 5
28-
#define BITSERY_MINOR_VERSION 1
28+
#define BITSERY_MINOR_VERSION 2
2929
#define BITSERY_PATCH_VERSION 0
3030

3131
#define BITSERY_QUOTE_MACRO(name) #name

include/bitsery/ext/std_bitset.h

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
//MIT License
2+
//
3+
//Copyright (c) 2020 Mindaugas Vinkelis
4+
//
5+
//Permission is hereby granted, free of charge, to any person obtaining a copy
6+
//of this software and associated documentation files (the "Software"), to deal
7+
//in the Software without restriction, including without limitation the rights
8+
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
//copies of the Software, and to permit persons to whom the Software is
10+
//furnished to do so, subject to the following conditions:
11+
//
12+
//The above copyright notice and this permission notice shall be included in all
13+
//copies or substantial portions of the Software.
14+
//
15+
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
//SOFTWARE.
22+
23+
#ifndef BITSERY_EXT_STD_BITSET_H
24+
#define BITSERY_EXT_STD_BITSET_H
25+
26+
#include "../traits/core/traits.h"
27+
#include <bitset>
28+
29+
namespace bitsery {
30+
namespace ext {
31+
32+
class StdBitset {
33+
public:
34+
35+
template<typename Ser, typename Fnc, size_t N>
36+
void serialize(Ser &ser, const std::bitset<N> &obj, Fnc &&) const {
37+
constexpr size_t BYTES = N / 8;
38+
constexpr size_t LEFTOVER = N % 8;
39+
if (BYTES > sizeof(unsigned long long)) {
40+
for(size_t i = 0u; i < BYTES; ++i) {
41+
size_t offset = i * 8;
42+
auto data = obj[offset + 0] +
43+
(obj[offset + 1] << 1) +
44+
(obj[offset + 2] << 2) +
45+
(obj[offset + 3] << 3) +
46+
(obj[offset + 4] << 4) +
47+
(obj[offset + 5] << 5) +
48+
(obj[offset + 6] << 6) +
49+
(obj[offset + 7] << 7);
50+
ser.value1b(static_cast<uint8_t>(data));
51+
}
52+
53+
} else {
54+
// more performant way
55+
auto data = obj.to_ullong();
56+
for(size_t i = 0u; i < BYTES; ++i) {
57+
ser.value1b(static_cast<uint8_t>(data & 0xFF));
58+
data >>= 1;
59+
}
60+
}
61+
if (LEFTOVER > 0) {
62+
serializeLeftover(ser.adapter(), obj, N - LEFTOVER, N);
63+
}
64+
}
65+
66+
template<typename Des, typename Fnc, size_t N>
67+
void deserialize(Des &des, std::bitset<N> &obj, Fnc &&) const {
68+
constexpr size_t BYTES = N / 8;
69+
constexpr size_t LEFTOVER = N % 8;
70+
for(size_t i = 0u; i < BYTES; ++i) {
71+
size_t offset = i * 8;
72+
uint8_t data = 0;
73+
des.value1b(data);
74+
obj[offset + 0] = data & 0x01u;
75+
obj[offset + 1] = data & 0x02u;
76+
obj[offset + 2] = data & 0x04u;
77+
obj[offset + 3] = data & 0x08u;
78+
obj[offset + 4] = data & 0x10u;
79+
obj[offset + 5] = data & 0x20u;
80+
obj[offset + 6] = data & 0x40u;
81+
obj[offset + 7] = data & 0x80u;
82+
}
83+
if (LEFTOVER > 0) {
84+
deserializeLeftover(des.adapter(), obj, N - LEFTOVER, N);
85+
}
86+
}
87+
88+
private:
89+
template<typename Writer, size_t N>
90+
void serializeLeftover(Writer& w, const std::bitset<N> &obj, size_t from, size_t to) const {
91+
serializeLeftoverImpl(w, obj, from, to, std::integral_constant<bool, Writer::BitPackingEnabled> {});
92+
}
93+
94+
template<typename Writer, size_t N>
95+
void serializeLeftoverImpl(Writer& w, const std::bitset<N> &obj, size_t from, size_t to, std::integral_constant<bool, false>) const {
96+
auto data = 0;
97+
for (auto i = from; i < to; ++i) {
98+
data += obj[i] << (i - from);
99+
}
100+
w.template writeBytes<1>(static_cast<uint8_t>(data));
101+
}
102+
103+
template<typename Writer, size_t N>
104+
void serializeLeftoverImpl(Writer& w, const std::bitset<N> &obj, size_t from, size_t to, std::integral_constant<bool, true>) const {
105+
for (auto i = from; i < to; ++i) {
106+
w.writeBits(obj[i], 1);
107+
}
108+
}
109+
110+
template<typename Reader, size_t N>
111+
void deserializeLeftover(Reader& r, std::bitset<N> &obj, size_t from, size_t to) const {
112+
deserializeLeftoverImpl(r, obj, from, to, std::integral_constant<bool, Reader::BitPackingEnabled> {});
113+
}
114+
115+
template<typename Reader, size_t N>
116+
void deserializeLeftoverImpl(Reader& r, std::bitset<N> &obj, size_t from, size_t to, std::integral_constant<bool, false>) const {
117+
uint8_t data = 0u;
118+
r.template readBytes<1>(data);
119+
for (auto i = from; i < to; ++i) {
120+
obj[i] = data & (1u << (i - from));
121+
}
122+
}
123+
124+
template<typename Reader, size_t N>
125+
void deserializeLeftoverImpl(Reader& r, std::bitset<N> &obj, size_t from, size_t to, std::integral_constant<bool, true>) const {
126+
for (auto i = from; i < to; ++i) {
127+
uint8_t res = 0u;
128+
r.readBits(res, 1);
129+
obj[i] = res;
130+
}
131+
}
132+
133+
};
134+
}
135+
136+
namespace traits {
137+
template<size_t N>
138+
struct ExtensionTraits<ext::StdBitset, std::bitset<N>> {
139+
using TValue = void;
140+
static constexpr bool SupportValueOverload = false;
141+
static constexpr bool SupportObjectOverload = true;
142+
static constexpr bool SupportLambdaOverload = false;
143+
};
144+
}
145+
}
146+
147+
148+
#endif //BITSERY_EXT_STD_BITSET_H

include/bitsery/ext/utils/pointer_utils.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -376,12 +376,12 @@ namespace bitsery {
376376
std::true_type, OwnershipType<PointerOwnershipType::Owner>) const {
377377
const auto& ctx = des.template context<TPolymorphicContext<RTTI>>();
378378
ctx.deserialize(des, TPtrManager<T>::getPtr(obj),
379-
[&obj, this, memResource](
379+
[&obj, memResource](
380380
const std::shared_ptr<PolymorphicHandlerBase>& handler) {
381381
TPtrManager<T>::createPolymorphic(obj, memResource, handler);
382382
return TPtrManager<T>::getPtr(obj);
383383
},
384-
[&obj, memResource, this](const std::shared_ptr<PolymorphicHandlerBase>& handler) {
384+
[&obj, memResource](const std::shared_ptr<PolymorphicHandlerBase>& handler) {
385385
TPtrManager<T>::destroyPolymorphic(obj, memResource, handler);
386386
});
387387
ptrInfo.processOwner(TPtrManager<T>::getPtr(obj));
@@ -414,7 +414,7 @@ namespace bitsery {
414414
obj, memResource, handler);
415415
return TPtrManager<T>::getPtr(obj);
416416
},
417-
[&obj, memResource, this](const std::shared_ptr<PolymorphicHandlerBase>& handler) {
417+
[&obj, memResource](const std::shared_ptr<PolymorphicHandlerBase>& handler) {
418418
TPtrManager<T>::destroyPolymorphic(obj, memResource, handler);
419419
});
420420
if (!ptrInfo.sharedState)

include/bitsery/serializer.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ namespace bitsery {
289289
static_assert(traits::ContainerTraits<T>::isResizable,
290290
"use container(const T&, Fnc) overload without `maxSize` for static containers");
291291
auto size = traits::ContainerTraits<T>::size(obj);
292+
(void)maxSize; // unused in release
292293
assert(size <= maxSize);
293294
details::writeSize(this->_adapter, size);
294295
procContainer(std::begin(obj), std::end(obj), std::forward<Fnc>(fnc));
@@ -302,6 +303,7 @@ namespace bitsery {
302303
"use container(const T&) overload without `maxSize` for static containers");
303304
static_assert(VSIZE > 0, "");
304305
auto size = traits::ContainerTraits<T>::size(obj);
306+
(void)maxSize; // unused in release
305307
assert(size <= maxSize);
306308
details::writeSize(this->_adapter, size);
307309

@@ -315,6 +317,7 @@ namespace bitsery {
315317
static_assert(traits::ContainerTraits<T>::isResizable,
316318
"use container(const T&) overload without `maxSize` for static containers");
317319
auto size = traits::ContainerTraits<T>::size(obj);
320+
(void)maxSize; // unused in release
318321
assert(size <= maxSize);
319322
details::writeSize(this->_adapter, size);
320323
procContainer(std::begin(obj), std::end(obj));
@@ -453,6 +456,7 @@ namespace bitsery {
453456
template<size_t VSIZE, typename T>
454457
void procText(const T& str, size_t maxSize) {
455458
const size_t length = traits::TextTraits<T>::length(str);
459+
(void)maxSize; // unused in release
456460
assert((length + (traits::TextTraits<T>::addNUL ? 1u : 0u)) <= maxSize);
457461
details::writeSize(this->_adapter, length);
458462
auto begin = std::begin(str);

tests/serialization_container.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,6 @@ TYPED_TEST(SerializeContainerDynamicSizeCompositeTypes, DefaultSerializeFunction
166166

167167
TYPED_TEST(SerializeContainerDynamicSizeCompositeTypes, CustomFunctionThatDoNothing) {
168168
SerializationContext ctx{};
169-
using TValue = typename TestFixture::TValue;
170169

171170
ctx.createSerializer().container(this->src, 1000, EmptyFtor{});
172171
ctx.createDeserializer().container(this->res, 1000, EmptyFtor{});
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
//MIT License
2+
//
3+
//Copyright (c) 2020 Mindaugas Vinkelis
4+
//
5+
//Permission is hereby granted, free of charge, to any person obtaining a copy
6+
//of this software and associated documentation files (the "Software"), to deal
7+
//in the Software without restriction, including without limitation the rights
8+
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
//copies of the Software, and to permit persons to whom the Software is
10+
//furnished to do so, subject to the following conditions:
11+
//
12+
//The above copyright notice and this permission notice shall be included in all
13+
//copies or substantial portions of the Software.
14+
//
15+
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
//SOFTWARE.
22+
23+
#include <bitsery/ext/std_bitset.h>
24+
#include <bitsery/ext/value_range.h>
25+
26+
#include <gmock/gmock.h>
27+
#include "serialization_test_utils.h"
28+
29+
using StdBitset = bitsery::ext::StdBitset;
30+
using ValueRange = bitsery::ext::ValueRange<int>;
31+
32+
using testing::Eq;
33+
34+
TEST(SerializeExtensionStdBitset, BitsetSmallerThanULongLong) {
35+
SerializationContext ctx;
36+
37+
std::bitset<9> data;
38+
data[2] = true;
39+
data[8] = true;
40+
std::bitset<9> res;
41+
42+
ctx.createSerializer().ext(data, StdBitset{});
43+
ctx.createDeserializer().ext(res, StdBitset{});
44+
EXPECT_THAT(res, Eq(data));
45+
}
46+
47+
TEST(SerializeExtensionStdBitset, BitsetSmallerThanULongLong2) {
48+
SerializationContext ctx;
49+
50+
std::bitset<9> data;
51+
data.set();
52+
std::bitset<9> res;
53+
54+
ctx.createSerializer().ext(data, StdBitset{});
55+
ctx.createDeserializer().ext(res, StdBitset{});
56+
EXPECT_THAT(res, Eq(data));
57+
}
58+
59+
60+
TEST(SerializeExtensionStdBitset, BitsetLargerThanULongLong) {
61+
SerializationContext ctx;
62+
63+
std::bitset<200> data;
64+
data[1] = true;
65+
data[100] = true;
66+
data[191] = true;
67+
std::bitset<200> res;
68+
69+
ctx.createSerializer().ext(data, StdBitset{});
70+
ctx.createDeserializer().ext(res, StdBitset{});
71+
EXPECT_THAT(res, Eq(data));
72+
}
73+
74+
TEST(SerializeExtensionStdBitset, BitsetSmallerThanULongLongBitPackingEnabled) {
75+
SerializationContext ctx;
76+
77+
std::bitset<12> data;
78+
int other_data = 1001;
79+
data[2] = true;
80+
data[9] = true;
81+
std::bitset<12> res{};
82+
int other_res{};
83+
84+
ctx.createSerializer().enableBitPacking([&data, &other_data](SerializationContext::TSerializerBPEnabled& sbp) {
85+
sbp.ext(data, StdBitset{});
86+
sbp.ext(other_data, ValueRange{1000,1015});
87+
});
88+
ctx.createDeserializer().enableBitPacking([&res, &other_res](SerializationContext::TDeserializerBPEnabled& dbp) {
89+
dbp.ext(res, StdBitset{});
90+
dbp.ext(other_res, ValueRange{1000,1015});
91+
});
92+
EXPECT_THAT(res, Eq(data));
93+
EXPECT_THAT(other_res, Eq(other_data));
94+
EXPECT_THAT(ctx.getBufferSize(), Eq(2));
95+
}
96+
97+
TEST(SerializeExtensionStdBitset, BitsetLargerThanULongLongBitPackingEnabled) {
98+
SerializationContext ctx;
99+
100+
std::bitset<204> data;
101+
int other_data = 1001;
102+
data[1] = true;
103+
data[100] = true;
104+
data[191] = true;
105+
std::bitset<204> res{};
106+
int other_res{};
107+
108+
ctx.createSerializer().enableBitPacking([&data, &other_data](SerializationContext::TSerializerBPEnabled& sbp) {
109+
sbp.ext(data, StdBitset{});
110+
sbp.ext(other_data, ValueRange{1000,1015});
111+
});
112+
ctx.createDeserializer().enableBitPacking([&res, &other_res](SerializationContext::TDeserializerBPEnabled& dbp) {
113+
dbp.ext(res, StdBitset{});
114+
dbp.ext(other_res, ValueRange{1000,1015});
115+
});
116+
EXPECT_THAT(res, Eq(data));
117+
EXPECT_THAT(other_res, Eq(other_data));
118+
EXPECT_THAT(ctx.getBufferSize(), Eq(26));
119+
}

0 commit comments

Comments
 (0)