Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Tree: 44f23d0658
Fetching contributors…

Cannot retrieve contributors at this time

118 lines (93 sloc) 2.056 kB
#ifndef _PACKER_H_
#define _PACKER_H_
#include <stdint.h>
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>
class Packer {
std::iostream &ios;
public:
Packer(std::iostream& ios);
inline void pack(const bool& v) { pack((const int8_t&)v); }
inline void unpack(bool& v) { unpack((int8_t&)v); }
void pack(const int8_t& v);
void unpack(int8_t& v);
void pack(const int16_t& v);
void unpack(int16_t& v);
void pack(const int32_t& v);
void unpack(int32_t& v);
void pack(const float& v);
void unpack(float& v);
void pack(const std::string& v);
void unpack(std::string& v);
template<typename T>
void pack(const std::vector<T>& v)
{
int32_t size = v.size();
pack(size);
for (typename std::vector<T>::const_iterator i = v.begin(); i != v.end(); ++i)
pack(*i);
}
template<typename T>
void unpack(std::vector<T>& v)
{
int32_t size = 0;
unpack(size);
v.resize(size);
for (typename std::vector<T>::iterator i = v.begin(); i != v.end(); ++i)
unpack(*i);
}
template<typename T>
void pack(const std::set<T>& v)
{
int32_t size = v.size();
pack(size);
for (typename std::set<T>::const_iterator i = v.begin(); i != v.end(); ++i)
pack(*i);
}
template<typename T>
void unpack(std::set<T>& v)
{
int32_t size = 0;
unpack(size);
while (size--) {
T value;
unpack(value);
v.insert(value);
}
}
template<typename T1, typename T2>
void pack(const std::pair<T1, T2>& v)
{
pack(v.first);
pack(v.second);
}
template<typename T1, typename T2>
void unpack(std::pair<T1, T2>& v)
{
unpack(v.first);
unpack(v.second);
}
template<typename T1, typename T2>
void pack(const std::map<T1, T2>& v)
{
int32_t size = v.size();
pack(size);
for (typename std::map<T1, T2>::const_iterator i = v.begin(); i != v.end(); ++i)
pack(*i);
}
template<typename T1, typename T2>
void unpack(std::map<T1, T2>& v)
{
int32_t size = 0;
unpack(size);
while (size--) {
std::pair<T1, T2> pair;
unpack(pair);
v.insert(pair);
}
}
};
#endif
Jump to Line
Something went wrong with that request. Please try again.