Skip to content

Instantly share code, notes, and snippets.

@frsyuki
Created September 6, 2010 01:56
Show Gist options
  • Save frsyuki/566530 to your computer and use it in GitHub Desktop.
Save frsyuki/566530 to your computer and use it in GitHub Desktop.
#include <msgpack.hpp>
#include <iostream>
struct pack_int32 {
public:
pack_int32(int64_t value = 0) : value(value) { }
operator int64_t() const { return value; }
int64_t get() const { return value; }
public:
template <typename Packer>
void msgpack_pack(Packer& pk) const
{
if(value > std::numeric_limits<int32_t>::max()) {
pk.pack_fix_int64(value);
} else {
pk.pack_fix_int32(value);
}
}
void msgpack_unpack(msgpack::object o)
{
o.convert(&value);
}
private:
int64_t value;
};
int main(void)
{
// wrap in time
{
int64_t val = 0;
msgpack::sbuffer out;
msgpack::pack(&out, pack_int32(val));
std::cout << out.size() << " bytes" << std::endl; // 5 bytes
}
// wrap in time
{
int64_t val = 1LL << 32;
msgpack::sbuffer out;
msgpack::pack(&out, pack_int32(val));
std::cout << out.size() << " bytes" << std::endl; // 9 bytes
}
// implicit conversion
{
pack_int32 val = 1LL << 32;
int64_t prim = val;
pack_int32 val2 = prim;
bool eq = prim == val2;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment