From a9f823b31ffedec0e5f12641b2fb309c16f40404 Mon Sep 17 00:00:00 2001 From: Rogiel Sulzbach Date: Wed, 13 Sep 2017 16:20:01 -0300 Subject: [PATCH] Add support for floating point types and C++ enums --- include/PacketBuffer/Packer.h | 33 ++++++++++++++ include/PacketBuffer/PacketBuffer.h | 1 + include/PacketBuffer/Serializer/Enum.h | 63 ++++++++++++++++++++++++++ include/PacketBuffer/Unpacker.h | 35 ++++++++++++++ 4 files changed, 132 insertions(+) create mode 100644 include/PacketBuffer/Serializer/Enum.h diff --git a/include/PacketBuffer/Packer.h b/include/PacketBuffer/Packer.h index d44bd06..97cd841 100644 --- a/include/PacketBuffer/Packer.h +++ b/include/PacketBuffer/Packer.h @@ -168,6 +168,15 @@ namespace PacketBuffer { return *this; } + /** + * A packer method that does not pack anything. + * + * @return this + */ + inline Packer& pack() { + return *this; + } + public: // Integer types /** * Packs a uint8_t integer value. @@ -286,6 +295,30 @@ namespace PacketBuffer { return pack(reinterpret_cast(&b), sizeof(b)); } + /** + * Packs a float value. + * + * @param f the float value to pack + * + * @return this + */ + inline Packer& pack(float f) { + static_assert(sizeof(f) == 4, "float size must be 4 bytes"); + return pack(reinterpret_cast(&f), sizeof(f)); + } + + /** + * Packs a double value. + * + * @param d the double value to pack + * + * @return this + */ + inline Packer& pack(double d) { + static_assert(sizeof(d) == 8, "double size must be 8 bytes"); + return pack(reinterpret_cast(&d), sizeof(d)); + } + public: // write operation /** * Packs a char-pointer gives by ptr with length given by size. diff --git a/include/PacketBuffer/PacketBuffer.h b/include/PacketBuffer/PacketBuffer.h index 72608cf..4b824c6 100644 --- a/include/PacketBuffer/PacketBuffer.h +++ b/include/PacketBuffer/PacketBuffer.h @@ -31,4 +31,5 @@ #include "Unpacker.h" #include "ObjectSerializer.h" +#include "Serializer/Enum.h" #include "Serializer/Std.h" diff --git a/include/PacketBuffer/Serializer/Enum.h b/include/PacketBuffer/Serializer/Enum.h new file mode 100644 index 0000000..bd856b3 --- /dev/null +++ b/include/PacketBuffer/Serializer/Enum.h @@ -0,0 +1,63 @@ +/* + * 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_ENUM_H +#define PACKETBUFFER_SERIALIZER_ENUM_H + +#include + +namespace PacketBuffer { + + /** + * A ObjectSerializer for C++ enum and enum class values. + * + * @tparam Enum the enum type + */ + template + class ObjectSerializer::value>::type> { + public: + /** + * The underlying storage type for the Enum + */ + using UnderlyingType = typename std::underlying_type::type; + + template + static void pack(Packer& packer, Enum object) { + packer((UnderlyingType) object); + } + + template + static void unpack(Unpacker& unpacker, Enum& object) { + unpacker((UnderlyingType&) object); + } + }; + +} + +#endif //PACKETBUFFER_SERIALIZER_ENUM_H diff --git a/include/PacketBuffer/Unpacker.h b/include/PacketBuffer/Unpacker.h index 97146c4..618f3fc 100644 --- a/include/PacketBuffer/Unpacker.h +++ b/include/PacketBuffer/Unpacker.h @@ -158,6 +158,15 @@ namespace PacketBuffer { return *this; } + /** + * A unpacker method that does not unpack anything. + * + * @return this + */ + inline Unpacker& unpack() { + return *this; + } + public: // Integer types /** * Unpacks a uint8_t integer value. @@ -285,6 +294,32 @@ namespace PacketBuffer { return *this; } + /** + * Unpacks a float value. + * + * @param f the float value to unpack + * + * @return this + */ + Unpacker& unpack(float& f) { + static_assert(sizeof(f) == 4, "float size must be 4 bytes"); + unpack(reinterpret_cast(&f), sizeof(f)); + return *this; + } + + /** + * Unpacks a double value. + * + * @param d the double value to unpack + * + * @return this + */ + Unpacker& unpack(double& d) { + static_assert(sizeof(d) == 8, "double size must be 8 bytes"); + unpack(reinterpret_cast(&d), sizeof(d)); + return *this; + } + public: // write operation /** * Unpacks a char-pointer given by ptr with length given by size.