libraries/eosiolib/core/eosio/varint.hpp
Namespaces
Name |
---|
eosio |
Classes
Name | |
---|---|
struct | eosio::unsigned_int |
struct | eosio::signed_int |
Detailed Description
Copyright: defined in eos/LICENSE
Source code
#pragma once
namespace eosio {
struct unsigned_int {
constexpr unsigned_int( uint32_t v = 0 ):value(v){}
template<typename T>
constexpr unsigned_int( T v ):value(v){}
//operator uint32_t()const { return value; }
//operator uint64_t()const { return value; }
template<typename T>
constexpr operator T()const { return static_cast<T>(value); }
constexpr unsigned_int& operator=( uint32_t v ) { value = v; return *this; }
uint32_t value;
constexpr friend bool operator==( const unsigned_int& i, const uint32_t& v ) { return i.value == v; }
constexpr friend bool operator==( const uint32_t& i, const unsigned_int& v ) { return i == v.value; }
constexpr friend bool operator==( const unsigned_int& i, const unsigned_int& v ) { return i.value == v.value; }
constexpr friend bool operator!=( const unsigned_int& i, const uint32_t& v ) { return i.value != v; }
constexpr friend bool operator!=( const uint32_t& i, const unsigned_int& v ) { return i != v.value; }
constexpr friend bool operator!=( const unsigned_int& i, const unsigned_int& v ) { return i.value != v.value; }
constexpr friend bool operator<( const unsigned_int& i, const uint32_t& v ) { return i.value < v; }
constexpr friend bool operator<( const uint32_t& i, const unsigned_int& v ) { return i < v.value; }
constexpr friend bool operator<( const unsigned_int& i, const unsigned_int& v ) { return i.value < v.value; }
constexpr friend bool operator>=( const unsigned_int& i, const uint32_t& v ) { return i.value >= v; }
constexpr friend bool operator>=( const uint32_t& i, const unsigned_int& v ) { return i >= v.value; }
constexpr friend bool operator>=( const unsigned_int& i, const unsigned_int& v ) { return i.value >= v.value; }
template<typename DataStream>
friend DataStream& operator << ( DataStream& ds, const unsigned_int& v ){
uint64_t val = v.value;
do {
uint8_t b = uint8_t(val) & 0x7f;
val >>= 7;
b |= ((val > 0) << 7);
ds.write((char*)&b,1);//.put(b);
} while( val );
return ds;
}
template<typename DataStream>
friend DataStream& operator >> ( DataStream& ds, unsigned_int& vi ){
uint64_t v = 0; char b = 0; uint8_t by = 0;
do {
ds.get(b);
v |= uint32_t(uint8_t(b) & 0x7f) << by;
by += 7;
} while( uint8_t(b) & 0x80 );
vi.value = static_cast<uint32_t>(v);
return ds;
}
};
struct signed_int {
constexpr signed_int( int32_t v = 0 ):value(v){}
constexpr operator int32_t()const { return value; }
template<typename T>
constexpr signed_int& operator=( const T& v ) { value = v; return *this; }
constexpr signed_int operator++(int) { return value++; }
constexpr signed_int& operator++(){ ++value; return *this; }
int32_t value;
constexpr friend bool operator==( const signed_int& i, const int32_t& v ) { return i.value == v; }
constexpr friend bool operator==( const int32_t& i, const signed_int& v ) { return i == v.value; }
constexpr friend bool operator==( const signed_int& i, const signed_int& v ) { return i.value == v.value; }
constexpr friend bool operator!=( const signed_int& i, const int32_t& v ) { return i.value != v; }
constexpr friend bool operator!=( const int32_t& i, const signed_int& v ) { return i != v.value; }
constexpr friend bool operator!=( const signed_int& i, const signed_int& v ) { return i.value != v.value; }
constexpr friend bool operator<( const signed_int& i, const int32_t& v ) { return i.value < v; }
constexpr friend bool operator<( const int32_t& i, const signed_int& v ) { return i < v.value; }
constexpr friend bool operator<( const signed_int& i, const signed_int& v ) { return i.value < v.value; }
constexpr friend bool operator>=( const signed_int& i, const int32_t& v ) { return i.value >= v; }
constexpr friend bool operator>=( const int32_t& i, const signed_int& v ) { return i >= v.value; }
constexpr friend bool operator>=( const signed_int& i, const signed_int& v ) { return i.value >= v.value; }
template<typename DataStream>
friend DataStream& operator << ( DataStream& ds, const signed_int& v ){
uint32_t val = uint32_t((v.value<<1) ^ (v.value>>31));
do {
uint8_t b = uint8_t(val) & 0x7f;
val >>= 7;
b |= ((val > 0) << 7);
ds.write((char*)&b,1);//.put(b);
} while( val );
return ds;
}
template<typename DataStream>
friend DataStream& operator >> ( DataStream& ds, signed_int& vi ){
uint32_t v = 0; char b = 0; int by = 0;
do {
ds.get(b);
v |= uint32_t(uint8_t(b) & 0x7f) << by;
by += 7;
} while( uint8_t(b) & 0x80 );
vi.value = (v>>1) ^ (~(v&1)+1ull);
return ds;
}
};
}
Updated on 2022-12-05 at 15:38:08 +0000