public
Created

Consturct++: Pickler combinators in C++11

  • Download Gist
construct++.cpp
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
#include <iostream>
#include <sstream>
#include <cstdint>
#include <cstddef>
#include <string>
#include <memory>
#include <vector>
#include <tuple>
#include <utility>
#include <type_traits>
 
 
template<typename T> typename std::enable_if<std::is_integral<T>::value || std::is_floating_point<T>::value>::type
pack(std::ostream& stream, const T& value) {
stream.write((char*)&value, sizeof(T));
}
template<typename T> typename std::enable_if<std::is_integral<T>::value || std::is_floating_point<T>::value>::type
unpack(std::istream& stream, T &value) {
stream.read((char*)&value, sizeof(T));
}
 
template<typename T> typename std::enable_if<std::is_enum<T>::value>::type
pack(std::ostream& stream, const T& val) {
typename std::underlying_type<T>::type tmp = (typename std::underlying_type<T>::type)val;
pack(stream, tmp);
}
template<typename T> typename std::enable_if<std::is_enum<T>::value>::type
unpack(std::istream& stream, T &val) {
typename std::underlying_type<T>::type tmp;
unpack(stream, tmp);
val = (T)tmp;
}
 
void pack(std::ostream& stream, const bool& value) {
pack(stream, (uint8_t)value);
}
void unpack(std::istream& stream, bool& value) {
uint8_t tmp;
unpack(stream, tmp);
value = (tmp != 0);
}
 
typedef uint8_t length_type;
 
template<typename L=length_type> void pack(std::ostream& stream, const std::string& str) {
L length = (L)str.size();
pack(stream, length);
stream.write(str.data(), length);
}
template<typename L=length_type> void unpack(std::istream& stream, std::string& str) {
L length;
unpack(stream, length);
str.resize(length);
stream.read(const_cast<char*>(str.data()), length);
}
 
template<typename T, typename L=length_type> void pack(std::ostream& stream, const std::vector<T>& vec) {
L length = (L)vec.size();
pack(stream, length);
for (int i = 0; i < length; i++) {
pack(stream, vec[i]);
}
}
template<typename T, typename L=length_type> void unpack(std::istream& stream, std::vector<T>& vec) {
L length;
unpack(stream, length);
vec.resize(length);
for (int i = 0; i < length; i++) {
unpack(stream, vec[i]);
}
}
 
template<typename T, std::size_t N> void pack(std::ostream& stream, const T (&arr)[N]) {
for (int i = 0; i < N; i++) {
pack(stream, arr[i]);
}
}
template<typename T, std::size_t N> void unpack(std::istream& stream, T (&arr)[N]) {
for (int i = 0; i < N; i++) {
unpack(stream, arr[i]);
}
}
 
template<typename TUPLE, unsigned N, unsigned Last>
struct _tuple_packer {
static void tup_pack(std::ostream& stream, const TUPLE& tup) {
pack(stream, std::get<N>(tup));
_tuple_packer<TUPLE, N + 1, Last>::tup_pack(stream, tup);
}
static void tup_unpack(std::istream& stream, TUPLE& tup) {
unpack(stream, std::get<N>(tup));
_tuple_packer<TUPLE, N + 1, Last>::tup_unpack(stream, tup);
}
};
template<typename TUPLE, unsigned N>
struct _tuple_packer<TUPLE, N, N> {
static void tup_pack(std::ostream& stream, const TUPLE& tup) {
pack(stream, std::get<N>(tup));
}
static void tup_unpack(std::istream& stream, TUPLE& tup) {
unpack(stream, std::get<N>(tup));
}
};
 
template<typename... Types> void pack(std::ostream& stream, const std::tuple<Types...>& tup) {
_tuple_packer<std::tuple<Types...>, 0, sizeof...(Types) - 1>::tup_pack(stream, tup);
}
template<typename... Types> void pack(std::istream& stream, const std::tuple<Types&...>& tup) {
_tuple_packer<std::tuple<Types&...>, 0, sizeof...(Types) - 1>::tup_pack(stream, tup);
}
 
template<typename... Types> void unpack(std::ostream& stream, std::tuple<Types...>& tup) {
_tuple_packer<std::tuple<Types...>, 0, sizeof...(Types) - 1>::tup_unpack(stream, tup);
}
template<typename... Types> void unpack(std::istream& stream, std::tuple<Types&...>& tup) {
_tuple_packer<std::tuple<Types&...>, 0, sizeof...(Types) - 1>::tup_unpack(stream, tup);
}
 
struct packable {
virtual void _pack_self(std::ostream& stream) const = 0;
virtual void _unpack_self(std::istream& stream) = 0;
};
 
template<typename T> typename std::enable_if<std::is_base_of<packable, T>::value>::type
pack(std::ostream& stream, const T& pkd) {
pkd._pack_self(stream);
}
template<typename T> typename std::enable_if<std::is_base_of<packable, T>::value>::type
unpack(std::istream& stream, T &pkd) {
pkd._unpack_self(stream);
}
 
#define PACKED(...) \
void _pack_self(std::ostream& stream) const override { \
auto tmp = std::tie(__VA_ARGS__); \
pack(stream, tmp); \
} \
void _unpack_self(std::istream& stream) override { \
auto tmp = std::tie(__VA_ARGS__); \
unpack(stream, tmp); \
}
 
 
struct first_struct : packable {
uint8_t x;
uint16_t y;
 
PACKED(x, y);
};
 
struct second_struct : packable {
uint8_t a;
std::string b;
int16_t c[3][2];
first_struct d;
enum : uint16_t {
A = 1,
B = 2,
C = 3
} e;
 
PACKED(a, b, c, d, e)
};
 
 
int main(int argc, const char ** argv) {
std::stringstream ss;
ss << "A\x05helloa0b0c0d0e0f0Bg0\x02\x00XXXXXXXXXX";
second_struct x = {};
unpack(ss, x);
std::cout
<< "a=" << (int)x.a << ","
<< "b=" << x.b << ","
<< "c=" << x.c[0][0] << ".." << x.c[2][1] << ","
<< "d=(" << x.d.x << "," << x.d.y << "),"
<< "e=" << x.e
<< std::endl;
 
return 0;
}
output
1
a=65,b=hello,c=12385..12390,d=(B,12391),e=2

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.