Skip to content

Commit

Permalink
Merge branch 'master' into misc-fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Destroyerrrocket authored Jul 21, 2024
2 parents f6308f6 + 4dcdd59 commit c1e1a44
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 13 deletions.
4 changes: 1 addition & 3 deletions examples/forward_backward_compatibility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ struct Weapon
template<typename S>
void serialize(S& s)
{
// forward/backward compatibility for monsters
// forward/backward compatibility for weapons
s.ext(*this, bitsery::ext::Growable{}, [](S& s, Weapon& o1) {
s.text1b(o1.name, 20);
s.value2b(o1.damage);
Expand Down Expand Up @@ -103,8 +103,6 @@ main()

// create buffer to store data to
Buffer buffer{};
// since we're using different configuration, we cannot use quickSerialization
// function.
auto writtenSize = bitsery::quickSerialization<OutputAdapter>(buffer, data);

MyTypes::Monster res{};
Expand Down
19 changes: 13 additions & 6 deletions include/bitsery/adapter/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,11 @@ class OutputBufferAdapter

void maybeResize(size_t newOffset, std::true_type)
{
if (newOffset > _bufferSize) {
traits::BufferAdapterTraits<Buffer>::increaseBufferSize(
*_buffer, _currOffset, newOffset);
_beginIt = std::begin(*_buffer);
_bufferSize = traits::ContainerTraits<Buffer>::size(*_buffer);
}
if (newOffset > _bufferSize)
BITSERY_UNLIKELY
{
doResize(newOffset);
}
}

void maybeResize(BITSERY_MAYBE_UNUSED size_t newOffset, std::false_type)
Expand All @@ -289,6 +288,14 @@ class OutputBufferAdapter
std::copy_n(data, size, _beginIt + static_cast<diff_t>(_currOffset));
_currOffset = newOffset;
}

BITSERY_NOINLINE void doResize(size_t newOffset)
{
traits::BufferAdapterTraits<Buffer>::increaseBufferSize(
*_buffer, _currOffset, newOffset);
_beginIt = std::begin(*_buffer);
_bufferSize = traits::ContainerTraits<Buffer>::size(*_buffer);
}
};

}
Expand Down
66 changes: 64 additions & 2 deletions include/bitsery/bitsery.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,74 @@
BITSERY_BUILD_VERSION_STR( \
BITSERY_MAJOR_VERSION, BITSERY_MINOR_VERSION, BITSERY_PATCH_VERSION)

#if __cplusplus > 201402L
#define BITSERY_MAYBE_UNUSED [[maybe_unused]]
#define BITSERY_DO_PRAGMA(x) _Pragma(#x)
#ifdef __GNUC__
#define BITSERY_DISABLE_WARNINGS(...) \
BITSERY_DO_PRAGMA(GCC diagnostic push) \
BITSERY_DO_PRAGMA(GCC diagnostic ignored __VA_ARGS__)
#define BITSERY_ENABLE_WARNINGS() BITSERY_DO_PRAGMA(GCC diagnostic pop)
#elif defined(_MSC_VER)
#define BITSERY_DISABLE_WARNINGS(...) \
BITSERY_DO_PRAGMA(GCC diagnostic push) \
BITSERY_DO_PRAGMA(GCC diagnostic ignored __VA_ARGS__) \
BITSERY_DO_PRAGMA(GCC diagnostic pop)
#define BITSERY_ENABLE_WARNINGS() BITSERY_DO_PRAGMA(GCC diagnostic pop)
#else
#define BITSERY_DISABLE_WARNINGS(...)
#define BITSERY_ENABLE_WARNINGS()
#endif

#ifdef __clang__
#define BITSERY_ATTRIBUTE(...) \
BITSERY_DISABLE_WARNINGS("-Wfuture-attribute-extensions") \
[[__VA_ARGS__]] BITSERY_ENABLE_WARNINGS()
#elif defined(__GNUC__)
#define BITSERY_ATTRIBUTE(...) [[__VA_ARGS__]]
#elif defined(_MSC_VER)
#define BITSERY_ATTRIBUTE(...) [[__VA_ARGS__]]
#else
#define BITSERY_ATTRIBUTE(...) [[__VA_ARGS__]]
#endif

#if __has_cpp_attribute(likely)
#define BITSERY_LIKELY BITSERY_ATTRIBUTE(likey)
#else
#define BITSERY_LIKELY
#endif

#if __has_cpp_attribute(unlikely)
#define BITSERY_UNLIKELY BITSERY_ATTRIBUTE(unlikely)
#else
#define BITSERY_UNLIKELY
#endif

#if __has_cpp_attribute(maybe_unused)
#define BITSERY_MAYBE_UNUSED BITSERY_ATTRIBUTE(maybe_unused)
#else
#define BITSERY_MAYBE_UNUSED
#endif


#if __GNUC__
#define BITSERY_NOINLINE __attribute__((noinline))
#elif defined(_MSC_VER)
#define BITSERY_NOINLINE __declspec(noinline)
#else
#define BITSERY_NOINLINE
#endif

#if __GNUC__
#define BITSERY_ASSUME(cond) \
do { \
if (!(cond)) \
__builtin_unreachable(); \
} while (0)
#elif defined(_MSC_VER)
#define BITSERY_ASSUME(cond) __assume(cond)
#else
#define BITSERY_ASSUME(cond)
#endif

#include "deserializer.h"
#include "serializer.h"

Expand Down
36 changes: 36 additions & 0 deletions include/bitsery/brief_syntax/bitset.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// MIT License
//
// Copyright (c) 2018 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_BRIEF_SYNTAX_TYPE_STD_BITSET_H
#define BITSERY_BRIEF_SYNTAX_TYPE_STD_BITSET_H

#include "../ext/std_bitset.h"

namespace bitsery {
template<typename S, size_t N>
void
serialize(S& s, std::bitset<N>& obj)
{
s.ext(obj, ext::StdBitset{});
}
}
#endif
36 changes: 36 additions & 0 deletions include/bitsery/brief_syntax/optional.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// MIT License
//
// Copyright (c) 2018 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_BRIEF_SYNTAX_TYPE_STD_OPTIONAL_H
#define BITSERY_BRIEF_SYNTAX_TYPE_STD_OPTIONAL_H

#include "../ext/std_optional.h"

namespace bitsery {
template<typename S, typename Ts>
void
serialize(S& s, std::optional<Ts>& obj)
{
s.ext(obj, ext::StdOptional{});
}
} // namespace bitsery
#endif
8 changes: 6 additions & 2 deletions include/bitsery/traits/core/std_defaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#ifndef BITSERY_TRAITS_CORE_STD_DEFAULTS_H
#define BITSERY_TRAITS_CORE_STD_DEFAULTS_H

#include "../../bitsery.h"
#include "../../details/serialization_common.h"
#include "traits.h"

Expand Down Expand Up @@ -103,8 +104,11 @@ struct StdContainerForBufferAdapter<T, true>
static_cast<size_t>(static_cast<double>(container.size()) * 1.5) + 128;
// make data cache friendly
newSize -= newSize % 64; // 64 is cache line size
container.resize(
(std::max)(newSize > minSize ? newSize : minSize, container.capacity()));
auto resize =
(std::max)(newSize > minSize ? newSize : minSize, container.capacity());
BITSERY_ASSUME(resize >= container.size());
BITSERY_ASSUME(resize >= container.capacity());
container.resize(resize);
}
};

Expand Down
14 changes: 14 additions & 0 deletions tests/brief_syntax.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@
#include <bitsery/brief_syntax.h>
#include <bitsery/brief_syntax/array.h>
#include <bitsery/brief_syntax/atomic.h>
#include <bitsery/brief_syntax/bitset.h>
#include <bitsery/brief_syntax/chrono.h>
#include <bitsery/brief_syntax/deque.h>
#include <bitsery/brief_syntax/forward_list.h>
#include <bitsery/brief_syntax/list.h>
#include <bitsery/brief_syntax/map.h>
#include <bitsery/brief_syntax/memory.h>
#include <bitsery/brief_syntax/optional.h>
#include <bitsery/brief_syntax/queue.h>
#include <bitsery/brief_syntax/set.h>
#include <bitsery/brief_syntax/stack.h>
Expand Down Expand Up @@ -473,6 +475,12 @@ TEST(BriefSyntax, StdAtomic)
0x1337);
}

TEST(BriefSyntax, StdBitset)
{
std::bitset<17> bits{ 0b10101010101010101 };
EXPECT_TRUE(procBriefSyntax(bits) == bits);
}

#if __cplusplus > 201402L

TEST(BriefSyntax, StdTuple)
Expand All @@ -490,6 +498,12 @@ TEST(BriefSyntax, StdVariant)
EXPECT_TRUE(procBriefSyntax(t1) == t1);
}

TEST(BriefSyntax, StdOptional)
{
std::optional<uint32_t> opt{ 54654 };
EXPECT_TRUE(procBriefSyntax(opt) == opt);
}

#endif

TEST(BriefSyntax, NestedTypes)
Expand Down

0 comments on commit c1e1a44

Please sign in to comment.