1
0
mirror of https://github.com/Rogiel/PacketBuffer synced 2025-12-07 17:02:56 +00:00

Initial commit

This commit is contained in:
2017-05-30 14:44:14 -03:00
commit dff846799a
28 changed files with 3203 additions and 0 deletions

View File

@@ -0,0 +1,127 @@
/*
* Copyright (c) 2017, Rogiel Sulzbach
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of Rogiel Sulzbach nor the names of contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef PACKETBUFFER_SERIALIZER_STD_ARRAY_H
#define PACKETBUFFER_SERIALIZER_STD_ARRAY_H
#include "PacketBuffer/ObjectSerializer.h"
#include <array>
namespace PacketBuffer {
/**
* A ObjectSerializer for a statically sized array of type <tt>T</tt>
* with size of <tt>S</tt>.
*
* @note This serializer does not include the size of the original array
* when serialized. Further changing the size of the array will cause a
* change in the binary format of the packet.
*
* @tparam T the array type
* @tparam S the array fixed size
*/
template<typename T, size_t S>
class ObjectSerializer<T[S], typename std::enable_if<sizeof(T) != 1>::type> {
public:
template<typename Packer>
static inline void pack(Packer& packer, T const array[S]) {
for(int i = 0; i < S; i++) {
packer(array[i]);
}
}
template<typename Unpacker>
static inline void unpack(Unpacker& unpacker, T array[S]) {
for(int i = 0; i < S; i++) {
unpacker(array[i]);
}
}
};
/**
* A ObjectSerializer for a statically sized array of type <tt>T</tt>
* with size of <tt>S</tt>.
*
* This implementation is specialized for object whose size is 1 byte.
*
* @note This serializer does not include the size of the original array
* when serialized. Further changing the size of the array will cause a
* change in the binary format of the packet.
*
* @tparam T the array type
* @tparam S the array fixed size
*/
template<typename T, size_t S>
class ObjectSerializer<T[S], typename std::enable_if<sizeof(T) == 1>::type> {
public:
template<typename Packer>
static inline void pack(Packer& packer, T const array[S]) {
packer.pack(reinterpret_cast<const char*>(array), S);
}
template<typename Unpacker>
static inline void unpack(Unpacker& unpacker, T array[S]) {
unpacker.unpack(reinterpret_cast<char*>(array), S);
}
};
/**
* A ObjectSerializer for a statically sized array of type <tt>T</tt>
* with size of <tt>S</tt>.
*
* @note This serializer does not include the size of the original array
* when serialized. Further changing the size of the array will cause a
* change in the binary format of the packet.
*
* @tparam T the array type
* @tparam S the array fixed size
*/
template<typename T, size_t S>
class ObjectSerializer<std::array<T, S>> {
public:
template<typename Packer>
static inline void pack(Packer& packer, const std::array<T, S>& array) {
for(int i = 0; i < S; i++) {
packer(array[i]);
}
}
template<typename Unpacker>
static inline void unpack(Unpacker& unpacker, std::array<T, S>& array) {
for(int i = 0; i < S; i++) {
unpacker(array[i]);
}
}
};
}
#endif //PACKETBUFFER_SERIALIZER_STD_ARRAY_H

View File

@@ -0,0 +1,87 @@
/*
* Copyright (c) 2017, Rogiel Sulzbach
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of Rogiel Sulzbach nor the names of contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef PACKETBUFFER_SERIALIZER_STD_CHRONO_H
#define PACKETBUFFER_SERIALIZER_STD_CHRONO_H
#include "PacketBuffer/ObjectSerializer.h"
#include <chrono>
namespace PacketBuffer {
/**
* A ObjectSerializer for std::chrono::duration values with representation
* of type <tt>R</tt> and period type of <tt>P</tt>.
*
* @tparam R the duration representation type
* @tparam P the duration period type
*/
template<typename R, typename P>
class ObjectSerializer<std::chrono::duration<R, P>> {
public:
template<typename Packer>
static inline void pack(Packer& packer, const std::chrono::duration<R, P>& duration) {
packer((int64_t) duration.count());
}
template<typename Unpacker>
static inline void unpack(Unpacker& unpacker, std::chrono::duration<R, P>& duration) {
int64_t r;
unpacker(r);
duration = std::chrono::duration<R, P>(r);
}
};
/**
* A ObjectSerializer for std::chrono::time_point values with clock
* of type <tt>Clock</tt> and duration type of <tt>Duration</tt>.
*
* @tparam Clock the time_point clock type
* @tparam Duration the time_point duration type
*/
template<typename Clock, typename Duration>
class ObjectSerializer<std::chrono::time_point<Clock, Duration>> {
public:
template<typename Packer>
static inline void pack(Packer& packer, const std::chrono::time_point<Clock, Duration>& point) {
packer(point.time_since_epoch());
}
template<typename Unpacker>
static inline void unpack(Unpacker& unpacker, std::chrono::time_point<Clock, Duration>& point) {
Duration d;
unpacker(d);
point = std::chrono::time_point<Clock, Duration>(d);
}
};
}
#endif //PACKETBUFFER_SERIALIZER_STD_CHRONO_H

View File

@@ -0,0 +1,35 @@
/*
* Copyright (c) 2017, Rogiel Sulzbach
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of Rogiel Sulzbach nor the names of contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef PACKETBUFFER_SERIALIZER_STD_EXPERIMENTAL_H
#define PACKETBUFFER_SERIALIZER_STD_EXPERIMENTAL_H
#include "Experimental/Optional.h"
#endif //PACKETBUFFER_SERIALIZER_STD_EXPERIMENTAL_H

View File

@@ -0,0 +1,73 @@
/*
* Copyright (c) 2017, Rogiel Sulzbach
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of Rogiel Sulzbach nor the names of contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef PACKETBUFFER_SERIALIZER_STD_EXPERIMENTAL_OPTIONAL_H
#define PACKETBUFFER_SERIALIZER_STD_EXPERIMENTAL_OPTIONAL_H
#include "PacketBuffer/ObjectSerializer.h"
#if __has_include(<experimental/optional>)
#include <experimental/optional>
namespace PacketBuffer {
/**
* A ObjectSerializer for std::optional of type <tt>T</tt>.
*
* @tparam T the optional type
*/
template<typename T>
class ObjectSerializer<std::experimental::optional<T>> {
public:
template<typename Packer>
static inline void pack(Packer& packer, const std::experimental::optional<T>& optional) {
if(optional) {
packer(true);
packer(*optional);
} else {
packer(false);
}
}
template<typename Unpacker>
static inline void unpack(Unpacker& unpacker, std::experimental::optional<T>& optional) {
bool hasValue;
unpacker(hasValue);
if(hasValue) {
unpacker(*optional);
}
}
};
}
#endif
#endif //PACKETBUFFER_SERIALIZER_STD_EXPERIMENTAL_OPTIONAL_H

View File

@@ -0,0 +1,74 @@
/*
* Copyright (c) 2017, Rogiel Sulzbach
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of Rogiel Sulzbach nor the names of contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef PACKETBUFFER_SERIALIZER_STD_LIST_H
#define PACKETBUFFER_SERIALIZER_STD_LIST_H
#include "PacketBuffer/ObjectSerializer.h"
#include <list>
#include <forward_list>
namespace PacketBuffer {
/**
* A ObjectSerializer for std::list with elements of type <tt>R</tt>
* using an allocator of type <tt>Allocator</tt>.
*
* @tparam T the list element type
* @tparam Allocator the list allocator type
*/
template<typename T, typename Allocator>
class ObjectSerializer<std::list<T, Allocator>> {
public:
template<typename Packer>
static inline void pack(Packer& packer, const std::list<T, Allocator>& list) {
auto items = static_cast<uint64_t>(list.size());
packer(items);
for(auto& entry : list) {
packer(entry);
}
}
template<typename Unpacker>
static inline void unpack(Unpacker& unpacker, std::list<T, Allocator>& list) {
uint64_t items;
unpacker(items);
for(int i = 0; i < items; i++) {
T v;
unpacker(v);
list.push_back(v);
}
}
};
}
#endif //PACKETBUFFER_SERIALIZER_STD_LIST_H

View File

@@ -0,0 +1,113 @@
/*
* Copyright (c) 2017, Rogiel Sulzbach
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of Rogiel Sulzbach nor the names of contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef PACKETBUFFER_SERIALIZER_STD_MAP_H
#define PACKETBUFFER_SERIALIZER_STD_MAP_H
#include "PacketBuffer/ObjectSerializer.h"
#include "PacketBuffer/Serializer/Std/Pair.h"
#include <map>
#include <unordered_map>
namespace PacketBuffer {
/**
* A ObjectSerializer for std::map with keys of type <tt>K</tt>,
* values of type <tt>T</tt>, comparison functor of type
* <tt>Compare</tt> and using an allocator of type <tt>Allocator</tt>.
*
* @tparam K the map element key
* @tparam V the map element value
* @tparam Compare the map comparison functor
* @tparam Allocator the map allocator type
*/
template<typename K, typename V, typename Compare, typename Allocator>
class ObjectSerializer<std::map<K, V, Compare, Allocator>> {
public:
template<typename Packer>
static inline void pack(Packer& packer, const std::map<K, V, Compare, Allocator>& map) {
auto items = static_cast<uint64_t>(map.size());
packer(items);
for(const std::pair<K, V>& entry : map) {
packer(entry);
}
}
template<typename Unpacker>
static inline void unpack(Unpacker& unpacker, std::map<K, V, Compare, Allocator>& map) {
uint64_t items;
unpacker(items);
for(int i = 0; i < items; i++) {
std::pair<K, V> entry;
unpacker(entry);
map.insert(std::move(entry));
}
}
};
/**
* A ObjectSerializer for std::unordered_map with keys of type <tt>K</tt>,
* values of type <tt>T</tt>, comparison functor of type <tt>Compare</tt>
* and using an allocator of type <tt>Allocator</tt>.
*
* @tparam K the map element key
* @tparam V the map element value
* @tparam Compare the map comparison functor
* @tparam Allocator the map allocator type
*/
template<typename K, typename V, class Hash, class Predicate, typename Allocator>
class ObjectSerializer<std::unordered_map<K, V, Hash, Predicate, Allocator>> {
public:
template<typename Packer>
static inline void pack(Packer& packer, const std::unordered_map<K, V, Hash, Predicate, Allocator>& map) {
auto items = static_cast<uint64_t>(map.size());
packer(items);
for(const std::pair<K, V>& entry : map) {
packer(entry);
}
}
template<typename Unpacker>
static inline void unpack(Unpacker& unpacker, std::unordered_map<K, V, Hash, Predicate, Allocator>& map) {
uint64_t items;
unpacker(items);
for(int i = 0; i < items; i++) {
std::pair<K, V> entry;
unpacker(entry);
map.insert(std::move(entry));
}
}
};
}
#endif //PACKETBUFFER_SERIALIZER_STD_MAP_H

View File

@@ -0,0 +1,62 @@
/*
* Copyright (c) 2017, Rogiel Sulzbach
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of Rogiel Sulzbach nor the names of contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef PACKETBUFFER_SERIALIZER_STD_PAIR_H
#define PACKETBUFFER_SERIALIZER_STD_PAIR_H
#include "PacketBuffer/ObjectSerializer.h"
#include <utility>
namespace PacketBuffer {
/**
* A ObjectSerializer for std::pair of types <tt>T1</tt> and <tt>T2</tt>.
*
* @tparam T1 the pair first type
* @tparam T2 the pair second type
*/
template<typename T1, typename T2>
class ObjectSerializer<std::pair<T1, T2>> {
public:
template<typename Packer>
static inline void pack(Packer& packer, const std::pair<T1, T2>& pair) {
packer(pair.first, pair.second);
}
template<typename Unpacker>
static inline void unpack(Unpacker& unpacker, std::pair<T1, T2>& pair) {
unpacker(pair.first, pair.second);
}
};
}
#endif //PACKETBUFFER_SERIALIZER_STD_PAIR_H

View File

@@ -0,0 +1,112 @@
/*
* Copyright (c) 2017, Rogiel Sulzbach
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of Rogiel Sulzbach nor the names of contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef PACKETBUFFER_SERIALIZER_STD_SET_H
#define PACKETBUFFER_SERIALIZER_STD_SET_H
#include "PacketBuffer/ObjectSerializer.h"
#include <set>
#include <unordered_set>
namespace PacketBuffer {
/**
* A ObjectSerializer for std::set with elements of type <tt>T</tt>,
* comparison functor of type <tt>Compare</tt> and using an allocator
* of type <tt>Allocator</tt>.
*
* @tparam T the set element key
* @tparam Compare the set comparison functor
* @tparam Allocator the set allocator type
*/
template<typename T, typename Compare, typename Allocator>
class ObjectSerializer<std::set<T, Compare, Allocator>> {
public:
template<typename Packer>
static inline void pack(Packer& packer, const std::set<T, Compare, Allocator>& set) {
auto items = static_cast<uint64_t>(set.size());
packer(items);
for(auto& entry : set) {
packer(entry);
}
}
template<typename Unpacker>
static inline void unpack(Unpacker& unpacker, std::set<T, Compare, Allocator>& set) {
uint64_t items;
unpacker(items);
for(int i = 0; i < items; i++) {
T v;
unpacker(v);
set.insert(v);
}
}
};
/**
* A ObjectSerializer for std::unordered_set with elements of type
* <tt>T</tt>, hash functor of type <tt>Hash</tt>, predicate of type
* <tt>Predicate</tt> and using an allocator of type <tt>Allocator</tt>.
*
* @tparam T the set element key
* @tparam Hash the set hash functor
* @tparam Predicate the set predicate functor
* @tparam Allocator the set allocator type
*/
template<typename T, typename Hash, typename Predicate, typename Allocator>
class ObjectSerializer<std::unordered_set<T, Hash, Predicate, Allocator>> {
public:
template<typename Packer>
static inline void pack(Packer& packer, const std::unordered_set<T, Hash, Predicate, Allocator>& set) {
auto items = static_cast<uint64_t>(set.size());
packer(items);
for(auto& entry : set) {
packer(entry);
}
}
template<typename Unpacker>
static inline void unpack(Unpacker& unpacker, std::unordered_set<T, Hash, Predicate, Allocator>& set) {
uint64_t items;
unpacker(items);
set.reserve(items);
for(int i = 0; i < items; i++) {
T v;
unpacker(v);
set.insert(v);
}
}
};
}
#endif //PACKETBUFFER_SERIALIZER_STD_SET_H

View File

@@ -0,0 +1,97 @@
/*
* Copyright (c) 2017, Rogiel Sulzbach
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of Rogiel Sulzbach nor the names of contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef PACKETBUFFER_SERIALIZER_STD_STRING_H
#define PACKETBUFFER_SERIALIZER_STD_STRING_H
#include "PacketBuffer/ObjectSerializer.h"
#include <string>
namespace PacketBuffer {
/**
* A ObjectSerializer for std::basic_string with character of type
* <tt>T</tt>, traits of type <tt>Traits</tt> and using an allocator
* of type <tt>Allocator</tt>.
*
* @tparam T the string character type
* @tparam Traits the string traits
* @tparam Allocator the string character allocator
*/
template<typename T, typename Traits, typename Allocator>
class ObjectSerializer<std::basic_string<T, Traits, Allocator>> {
public:
template<typename Packer>
static inline void pack(Packer& packer, const std::basic_string<T, Traits, Allocator>& string) {
auto length = static_cast<uint64_t>(string.size());
packer(length);
for(int i = 0; i < length; i++) {
packer(string[i]);
}
}
template<typename Unpacker>
static inline void unpack(Unpacker& unpacker, std::basic_string<T, Traits, Allocator>& string) {
uint64_t length;
unpacker(length);
string.resize(length);
for(int i = 0; i < length; i++) {
unpacker(string[i]);
}
}
};
/**
* A ObjectSerializer for std::string.
*/
template<>
class ObjectSerializer<std::string> {
public:
template<typename Packer>
static inline void pack(Packer& packer, const std::string& string) {
auto length = static_cast<uint64_t>(string.size());
packer(length);
packer.pack(string.data(), string.size());
}
template<typename Unpacker>
static inline void unpack(Unpacker& unpacker, std::string& string) {
uint64_t length;
unpacker(length);
string.resize(length);
unpacker.unpack(&string[0], string.size());
}
};
}
#endif //PACKETBUFFER_SERIALIZER_STD_STRING_H

View File

@@ -0,0 +1,80 @@
/*
* Copyright (c) 2017, Rogiel Sulzbach
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of Rogiel Sulzbach nor the names of contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef PACKETBUFFER_SERIALIZER_STD_TUPLE_H
#define PACKETBUFFER_SERIALIZER_STD_TUPLE_H
#include "PacketBuffer/ObjectSerializer.h"
#include <tuple>
namespace PacketBuffer {
/**
* A ObjectSerializer for std::tuple with elements of type <tt>Ts</tt>.
*
* @tparam Ts the tuple element types
*/
template<typename... Ts>
class ObjectSerializer<std::tuple<Ts...>> {
public:
template<typename Packer>
static inline void pack(Packer& packer, const std::tuple<Ts...>& tuple) {
packImpl<Packer, 0, Ts...>(packer, tuple);
}
template<typename Unpacker>
static inline void unpack(Unpacker& unpacker, std::tuple<Ts...>& tuple) {
unpackImpl<Unpacker, 0, Ts...>(unpacker, tuple);
}
private:
template<typename Packer, size_t I, typename T, typename... OTs>
static inline void packImpl(Packer& packer, const std::tuple<Ts...>& tuple) {
packer(std::get<I>(tuple));
packImpl<Packer, I + 1, OTs...>(packer, tuple);
}
template<typename Packer, size_t I>
static inline void packImpl(Packer& packer, const std::tuple<Ts...>& tuple) {}
template<typename Unpacker, size_t I, typename T, typename... OTs>
static inline void unpackImpl(Unpacker& unpacker, std::tuple<Ts...>& tuple) {
unpacker(std::get<I>(tuple));
unpackImpl<Unpacker, I + 1, OTs...>(unpacker, tuple);
}
template<typename Unpacker, size_t I>
static inline void unpackImpl(Unpacker& unpacker, std::tuple<Ts...>& tuple) {}
};
}
#endif //PACKETBUFFER_SERIALIZER_STD_TUPLE_H

View File

@@ -0,0 +1,73 @@
/*
* Copyright (c) 2017, Rogiel Sulzbach
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of Rogiel Sulzbach nor the names of contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef PACKETBUFFER_SERIALIZER_STD_VECTOR_H
#define PACKETBUFFER_SERIALIZER_STD_VECTOR_H
#include "PacketBuffer/ObjectSerializer.h"
#include <vector>
namespace PacketBuffer {
/**
* A ObjectSerializer for std::vector with elements of type <tt>R</tt>
* using an allocator of type <tt>Allocator</tt>.
*
* @tparam T the vector element type
* @tparam Allocator the vector allocator type
*/
template<typename T, typename Allocator>
class ObjectSerializer<std::vector<T, Allocator>> {
public:
template<typename Packer>
static inline void pack(Packer& packer, const std::vector<T, Allocator>& vector) {
auto items = static_cast<uint64_t>(vector.size());
packer(items);
for(int i = 0; i < items; i++) {
packer(vector[i]);
}
}
template<typename Unpacker>
static inline void unpack(Unpacker& unpacker, std::vector<T, Allocator>& vector) {
uint64_t items;
unpacker(items);
vector.resize(items);
for(int i = 0; i < items; i++) {
unpacker(vector[i]);
}
}
};
}
#endif //PACKETBUFFER_SERIALIZER_STD_VECTOR_H