Files
VoidNet/include/VoidNet_HL/ByteConverter.hpp
TheDoctor 5ab59a6fb8 Update
Integrated std::byte added to C++17 recently
2019-10-15 21:03:24 +01:00

32 lines
643 B
C++

#pragma once
// i want to use bitshifting but i red in stackoverflow that casting doesnt generate overhead
// now ive hit a wall
// casting vs bitshifting
#include "VoidNet_LL/Net.hpp"
namespace std
{
class ByteConverter
{
public:
template<typename T>
inline static byte *ToBytes(T value)
{
byte *data = new byte[sizeof(T)]();
memcpy(data, &value, sizeof(T));
return data;
}
template<typename T>
inline static T FromBytes(byte *data)
{
if (!data)
throw std::invalid_argument("cant have null parameter -> ByteConverter::FromBytes");
T value;
memcpy(&value, data, sizeof(T));
return value;
}
};
}