#include <cstring>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <iostream>
#include <limits>
#include <bits/stdc++.h>
template <typename T>
bool is_signed(const T &t)
{
return std::numeric_limits<T>::is_signed;
}
class Data
{
public:
int32_t Int32;
float Float;
int64_t Int64;
double Double;
Data(int32_t x = 0, float y = 0, int64_t z = 0, double d = 0)
{
Int32 = x;
Float = y;
Int64 = z;
Double = d;
}
};
template <class T>
std::vector<uint8_t> encode(T d)
{
std::vector<uint8_t> encoded;
T v = d;
if (is_signed(v))
{
// signed LEB128 encoding
bool more = true;
while (more)
{
auto byte = v & 0b01111111;
v >>= 7;
if (v == 0 && (byte & 0b01000000) == 0)
more = false;
else if (v == -1 && (byte & 0b01000000) > 0)
more = false;
else
{
byte |= 0b10000000;
}
encoded.push_back(byte);
}
}
else
{
// unsigned LEB128 encoding
do
{
auto x = v & 0b01111111;
v >>= 7;
if (v)
x |= 0b10000000;
encoded.push_back(x);
} while (v);
}
return encoded;
}
uint64_t decode(std::vector<uint8_t> &Bytes, Data &D)
{
uint16_t v = D.Int64;
if (!is_signed(v))
{
uint64_t result(0), shift(0), i(0);
int n(64);
while (true)
{
int64_t byte = Bytes[i];
result |= ((byte & 0b01111111) << shift);
shift += 7;
if ((byte & 0b10000000) == 0)
{
break;
}
else
{
i += 1;
}
}
if ((shift < n) && (Bytes[i] & 0b01000000) != 0)
{
result |= (~0 << shift);
}
return result;
}
else
{
// unsigned LEB128 decoding
auto res(0), shift(0), i(0);
auto len(Bytes.size());
while (true)
{
if (i < len)
{
uint8_t b = Bytes[i++];
res |= (b & 0b01111111) << shift;
if (!(b & 0b10000000))
{
break;
}
shift += 7;
}
}
return res;
}
}
std::vector<uint8_t> serialize(Data &D)
{
std::vector<uint8_t> s;
std::vector<uint8_t> vecofInt32 = encode(D.Int32);
std::vector<uint8_t> vecofInt64 = encode(D.Int64);
// float
float ft = D.Float;
float *a;
char result[sizeof(float)];
memcpy(result, &ft, sizeof(ft));
// double
double dt = D.Double;
double *c;
char resultdouble[sizeof(double)];
memcpy(resultdouble, &dt, sizeof(dt));
//// Need help here
////
////
////
//////
return vecofInt64; ///change to vecofInt32 to return Int32 encodinf
}
Data deserialize(std::vector<uint8_t> &Bytes)
{
Data D2;
//// Need help here
////
////
////
/////
D2.Int64 = decode(Bytes, D2);
// D2.Int32 = decode(Bytes, D2);
// D2.Int64 = decode(Bytes, D2);
// D2.Int64 = decode(Bytes, D2);
/// Return original data ( All class members)
return D2;
}
int main()
{
Data D1{214742, 223.232f, 9223372036854775803, 2.12888888888};
std::vector<uint8_t> Sero = serialize(D1);
Data Zero = deserialize(Sero); // For now only retuning Int64,
std::cout << Zero.Int64;
return 0;
}