117 lines
3.0 KiB
C++
117 lines
3.0 KiB
C++
#pragma once
|
|
|
|
#include <sstream>
|
|
#include <vector>
|
|
#include <exception>
|
|
|
|
namespace ddkit {
|
|
namespace base {
|
|
|
|
/// @brief exception class for serialization errors
|
|
/// emitted for any errors encouted during parsing
|
|
/// for details refere to what() message
|
|
class SerializationError : public std::exception {
|
|
public:
|
|
SerializationError(const std::string& message) : m_message(message) {
|
|
}
|
|
|
|
virtual const char* what() const noexcept override {
|
|
return m_message.c_str();
|
|
}
|
|
|
|
private:
|
|
const std::string m_message;
|
|
};
|
|
|
|
/// @brief simple serialization like stringstream
|
|
/// - serialized format is a string, readable
|
|
/// - may be deserialized with same operator-calling-pattern of types on another instance
|
|
/// mixed calls to serializing and deserializing on same object -> undefined behaviour
|
|
class Serialization {
|
|
std::ostringstream m_os;
|
|
std::istringstream m_is;
|
|
// bool m_isSerializing; // todo nth check?
|
|
// bool m_isDeserializing;
|
|
public:
|
|
// serializing
|
|
Serialization();
|
|
std::string str();
|
|
|
|
// deserializing
|
|
explicit Serialization(const std::string& text);
|
|
bool isEnd();
|
|
|
|
|
|
// bool
|
|
Serialization& operator<<(bool rhs);
|
|
Serialization& operator>>(bool& rhs);
|
|
|
|
// int char
|
|
Serialization& operator<<(uint8_t rhs);
|
|
Serialization& operator>>(uint8_t& rhs);
|
|
|
|
Serialization& operator<<(uint16_t rhs);
|
|
Serialization& operator>>(uint16_t& rhs);
|
|
|
|
Serialization& operator<<(uint32_t rhs);
|
|
Serialization& operator>>(uint32_t& rhs);
|
|
|
|
Serialization& operator<<(uint64_t rhs);
|
|
Serialization& operator>>(uint64_t& rhs);
|
|
|
|
Serialization& operator<<(int8_t rhs);
|
|
Serialization& operator>>(int8_t& rhs);
|
|
|
|
Serialization& operator<<(int16_t rhs);
|
|
Serialization& operator>>(int16_t& rhs);
|
|
|
|
Serialization& operator<<(int32_t rhs);
|
|
Serialization& operator>>(int32_t& rhs);
|
|
|
|
Serialization& operator<<(int64_t rhs);
|
|
Serialization& operator>>(int64_t& rhs);
|
|
|
|
Serialization& operator<<(char rhs);
|
|
Serialization& operator>>(char& rhs);
|
|
|
|
// float double
|
|
Serialization& operator<<(double rhs);
|
|
Serialization& operator>>(double& rhs);
|
|
|
|
Serialization& operator<<(float rhs);
|
|
Serialization& operator>>(float& rhs);
|
|
|
|
// string
|
|
Serialization& operator<<(const std::string& rhs);
|
|
Serialization& operator>>(std::string& rhs);
|
|
|
|
// vector
|
|
template <typename T>
|
|
Serialization& operator<<(std::vector<T> const& rhs) {
|
|
*this << rhs.size();
|
|
for (T const& t : rhs) {
|
|
*this << t;
|
|
}
|
|
return *this;
|
|
}
|
|
template <typename T>
|
|
Serialization& operator>>(std::vector<T>& rhs) {
|
|
rhs.clear();
|
|
|
|
int size = getSize();
|
|
for (int i = 0; i < size; ++i) {
|
|
T t;
|
|
*this >> t;
|
|
rhs.push_back(t);
|
|
}
|
|
return *this;
|
|
}
|
|
|
|
private:
|
|
std::string readBytes(int size);
|
|
void expectAndSkipComma();
|
|
int getSize();
|
|
};
|
|
|
|
} // namespace base
|
|
} // namespace ddkit
|