forked from Research/WhisperCom
89 lines
2.4 KiB
C++
89 lines
2.4 KiB
C++
#pragma once
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
*/
|
|
/** @file */
|
|
/** @copyright 2021 MPLv2 */
|
|
#include <vector>
|
|
#include <cstdint>
|
|
#include <string>
|
|
#include <list>
|
|
#include <queue>
|
|
|
|
/**
|
|
* @brief This is the main namespace for all whisper-com related datatypes and classed.
|
|
*/
|
|
namespace Whisper
|
|
{
|
|
|
|
|
|
/**
|
|
* @brief This namespace is for all network related datatypes and classes.
|
|
*/
|
|
namespace Network
|
|
{
|
|
|
|
class Encoding
|
|
{
|
|
|
|
public:
|
|
/**
|
|
* \defgroup conversion_methods Conversion Functions
|
|
* Whisper-com requires conversion functions/ methods to convert from
|
|
* datatypes like std::uint8_t, std::int32_t, double, float, std::string, etc.
|
|
* to a byte vector, which can be transmitted as a payload through a network.
|
|
* This module holds are all of these conversion functions.
|
|
*/
|
|
|
|
|
|
/*
|
|
* @brief template function to byte swap int/uint datatypes - used for endianess conversion
|
|
*
|
|
* @param v - variable of some int/uint datatype
|
|
* @return the swapped bytes in the same variable type
|
|
*/
|
|
template<typename T>
|
|
T swapBytes(T &v)
|
|
{
|
|
T help=v;
|
|
std::uint8_t *byte = (std::uint8_t *)&help;
|
|
|
|
for (unsigned int i = 0; i < sizeof(T)/2; ++i)
|
|
{
|
|
std::swap(byte[i], byte[sizeof(T)-1-i]);
|
|
}
|
|
|
|
return help;
|
|
}
|
|
|
|
/**
|
|
* @brief template for converting of simple datatypes to a byte_array, including endiannes conversion to big endian
|
|
* @ingroup conversion_methods
|
|
* @param variable - the variable to convert to a byte vector, needs to be int/uint datatype
|
|
*
|
|
* @return the std::vector<std::uint8_t>
|
|
*/
|
|
template<typename T>
|
|
std::vector<std::uint8_t> encodeSimple(T &variable)
|
|
{
|
|
}
|
|
|
|
/**
|
|
* @brief function to convert a std::string to an std::vector<std::uint8_t>
|
|
* @ingroup conversion_methods
|
|
*
|
|
* @param variable - the std::string to convert
|
|
*
|
|
* @return the std::vector<std::uint8_t>
|
|
*/
|
|
std::vector<std::uint8_t> fromStringToByteArray(const std::string &variable);
|
|
|
|
|
|
}; // class Encoding
|
|
|
|
|
|
}; // namespace Network
|
|
|
|
}; // namespace Whisper
|