1
0
mirror of https://github.com/Rogiel/PacketBuffer synced 2025-12-06 00:13:04 +00:00

Add support for floating point types and C++ enums

This commit is contained in:
2017-09-13 16:20:01 -03:00
parent dff846799a
commit a9f823b31f
4 changed files with 132 additions and 0 deletions

View File

@@ -168,6 +168,15 @@ namespace PacketBuffer {
return *this; return *this;
} }
/**
* A packer method that does not pack anything.
*
* @return this
*/
inline Packer& pack() {
return *this;
}
public: // Integer types public: // Integer types
/** /**
* Packs a uint8_t integer value. * Packs a uint8_t integer value.
@@ -286,6 +295,30 @@ namespace PacketBuffer {
return pack(reinterpret_cast<const char*>(&b), sizeof(b)); return pack(reinterpret_cast<const char*>(&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<const char*>(&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<const char*>(&d), sizeof(d));
}
public: // write operation public: // write operation
/** /**
* Packs a char-pointer gives by <tt>ptr</tt> with length given by <tt>size</tt>. * Packs a char-pointer gives by <tt>ptr</tt> with length given by <tt>size</tt>.

View File

@@ -31,4 +31,5 @@
#include "Unpacker.h" #include "Unpacker.h"
#include "ObjectSerializer.h" #include "ObjectSerializer.h"
#include "Serializer/Enum.h"
#include "Serializer/Std.h" #include "Serializer/Std.h"

View File

@@ -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 <type_traits>
namespace PacketBuffer {
/**
* A ObjectSerializer for C++ enum and enum class values.
*
* @tparam Enum the enum type
*/
template<typename Enum>
class ObjectSerializer<Enum, typename std::enable_if<std::is_enum<Enum>::value>::type> {
public:
/**
* The underlying storage type for the Enum
*/
using UnderlyingType = typename std::underlying_type<Enum>::type;
template<typename Packer>
static void pack(Packer& packer, Enum object) {
packer((UnderlyingType) object);
}
template<typename Unpacker>
static void unpack(Unpacker& unpacker, Enum& object) {
unpacker((UnderlyingType&) object);
}
};
}
#endif //PACKETBUFFER_SERIALIZER_ENUM_H

View File

@@ -158,6 +158,15 @@ namespace PacketBuffer {
return *this; return *this;
} }
/**
* A unpacker method that does not unpack anything.
*
* @return this
*/
inline Unpacker& unpack() {
return *this;
}
public: // Integer types public: // Integer types
/** /**
* Unpacks a uint8_t integer value. * Unpacks a uint8_t integer value.
@@ -285,6 +294,32 @@ namespace PacketBuffer {
return *this; 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<char*>(&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<char*>(&d), sizeof(d));
return *this;
}
public: // write operation public: // write operation
/** /**
* Unpacks a char-pointer given by <tt>ptr</tt> with length given by <tt>size</tt>. * Unpacks a char-pointer given by <tt>ptr</tt> with length given by <tt>size</tt>.