Skip to content

Commit

Permalink
Merge pull request #64 from bitshares/1088_unsigned_int
Browse files Browse the repository at this point in the history
64 bits unsigned int
  • Loading branch information
pmconrad authored Aug 22, 2018
2 parents 7ac533b + 02a4516 commit 2405081
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 104 deletions.
6 changes: 3 additions & 3 deletions include/fc/container/flat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace fc {
inline void pack( Stream& s, const flat_set<T>& value, uint32_t _max_depth ) {
FC_ASSERT( _max_depth > 0 );
--_max_depth;
pack( s, unsigned_int((uint32_t)value.size()), _max_depth );
pack( s, unsigned_int(value.size()), _max_depth );
auto itr = value.begin();
auto end = value.end();
while( itr != end ) {
Expand All @@ -38,7 +38,7 @@ namespace fc {
inline void pack( Stream& s, const flat_map<K,V...>& value, uint32_t _max_depth ) {
FC_ASSERT( _max_depth > 0 );
--_max_depth;
pack( s, unsigned_int((uint32_t)value.size()), _max_depth );
pack( s, unsigned_int(value.size()), _max_depth );
auto itr = value.begin();
auto end = value.end();
while( itr != end ) {
Expand Down Expand Up @@ -67,7 +67,7 @@ namespace fc {
void pack( Stream& s, const bip::vector<T,A>& value, uint32_t _max_depth ) {
FC_ASSERT( _max_depth > 0 );
--_max_depth;
pack( s, unsigned_int((uint32_t)value.size()), _max_depth );
pack( s, unsigned_int(value.size()), _max_depth );
if( !std::is_fundamental<T>::value ) {
auto itr = value.begin();
auto end = value.end();
Expand Down
2 changes: 1 addition & 1 deletion include/fc/interprocess/container.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ namespace fc {
inline void pack( Stream& s, const bip::vector<T,A...>& value, uint32_t _max_depth=FC_PACK_MAX_DEPTH ) {
FC_ASSERT( _max_depth > 0 );
--_max_depth;
pack( s, unsigned_int((uint32_t)value.size()), _max_depth );
pack( s, unsigned_int(value.size()), _max_depth );
auto itr = value.begin();
auto end = value.end();
while( itr != end ) {
Expand Down
45 changes: 13 additions & 32 deletions include/fc/io/raw.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,16 +158,6 @@ namespace fc {
fc::raw::unpack( s, *v, _max_depth - 1 );
} FC_RETHROW_EXCEPTIONS( warn, "std::shared_ptr<T>", ("type",fc::get_typename<T>::name()) ) }

template<typename Stream> inline void pack( Stream& s, const signed_int& v, uint32_t _max_depth ) {
uint32_t val = (v.value<<1) ^ (v.value>>31);
do {
uint8_t b = uint8_t(val) & 0x7f;
val >>= 7;
b |= ((val > 0) << 7);
s.write((char*)&b,1);//.put(b);
} while( val );
}

template<typename Stream> inline void pack( Stream& s, const unsigned_int& v, uint32_t _max_depth ) {
uint64_t val = v.value;
do {
Expand All @@ -178,25 +168,16 @@ namespace fc {
}while( val );
}

template<typename Stream> inline void unpack( Stream& s, signed_int& vi, uint32_t _max_depth ) {
uint32_t v = 0; char b = 0; int by = 0;
do {
s.get(b);
v |= uint32_t(uint8_t(b) & 0x7f) << by;
by += 7;
} while( uint8_t(b) & 0x80 );
vi.value = ((v>>1) ^ (v>>31)) + (v&0x01);
vi.value = v&0x01 ? vi.value : -vi.value;
vi.value = -vi.value;
}
template<typename Stream> inline void unpack( Stream& s, unsigned_int& vi, uint32_t _max_depth ) {
uint64_t v = 0; char b = 0; uint8_t by = 0;
do {
s.get(b);
v |= uint32_t(uint8_t(b) & 0x7f) << by;
if( by >= 64 || (by == 63 && uint8_t(b) > 1) )
FC_THROW_EXCEPTION( overflow_exception, "Invalid packed unsigned_int!" );
v |= uint64_t(uint8_t(b) & 0x7f) << by;
by += 7;
} while( uint8_t(b) & 0x80 );
vi.value = static_cast<uint32_t>(v);
vi.value = static_cast<uint64_t>(v);
}

template<typename Stream, typename T> inline void unpack( Stream& s, const T& vi, uint32_t _max_depth )
Expand Down Expand Up @@ -273,9 +254,9 @@ namespace fc {
// std::vector<char>
template<typename Stream> inline void pack( Stream& s, const std::vector<char>& value, uint32_t _max_depth ) {
FC_ASSERT( _max_depth > 0 );
fc::raw::pack( s, unsigned_int((uint32_t)value.size()), _max_depth - 1 );
fc::raw::pack( s, unsigned_int(value.size()), _max_depth - 1 );
if( value.size() )
s.write( &value.front(), (uint32_t)value.size() );
s.write( &value.front(), value.size() );
}
template<typename Stream> inline void unpack( Stream& s, std::vector<char>& value, uint32_t _max_depth ) {
FC_ASSERT( _max_depth > 0 );
Expand All @@ -289,7 +270,7 @@ namespace fc {
// fc::string
template<typename Stream> inline void pack( Stream& s, const fc::string& v, uint32_t _max_depth ) {
FC_ASSERT( _max_depth > 0 );
fc::raw::pack( s, unsigned_int((uint32_t)v.size()), _max_depth - 1 );
fc::raw::pack( s, unsigned_int(v.size()), _max_depth - 1 );
if( v.size() ) s.write( v.c_str(), v.size() );
}

Expand Down Expand Up @@ -433,7 +414,7 @@ namespace fc {
inline void pack( Stream& s, const std::unordered_set<T>& value, uint32_t _max_depth ) {
FC_ASSERT( _max_depth > 0 );
--_max_depth;
fc::raw::pack( s, unsigned_int((uint32_t)value.size()), _max_depth );
fc::raw::pack( s, unsigned_int(value.size()), _max_depth );
auto itr = value.begin();
auto end = value.end();
while( itr != end ) {
Expand Down Expand Up @@ -478,7 +459,7 @@ namespace fc {
inline void pack( Stream& s, const std::unordered_map<K,V>& value, uint32_t _max_depth ) {
FC_ASSERT( _max_depth > 0 );
--_max_depth;
fc::raw::pack( s, unsigned_int((uint32_t)value.size()), _max_depth );
fc::raw::pack( s, unsigned_int(value.size()), _max_depth );
auto itr = value.begin();
auto end = value.end();
while( itr != end ) {
Expand Down Expand Up @@ -506,7 +487,7 @@ namespace fc {
inline void pack( Stream& s, const std::map<K,V>& value, uint32_t _max_depth ) {
FC_ASSERT( _max_depth > 0 );
--_max_depth;
fc::raw::pack( s, unsigned_int((uint32_t)value.size()), _max_depth );
fc::raw::pack( s, unsigned_int(value.size()), _max_depth );
auto itr = value.begin();
auto end = value.end();
while( itr != end ) {
Expand Down Expand Up @@ -534,7 +515,7 @@ namespace fc {
inline void pack( Stream& s, const std::deque<T>& value, uint32_t _max_depth ) {
FC_ASSERT( _max_depth > 0 );
--_max_depth;
fc::raw::pack( s, unsigned_int((uint32_t)value.size()), _max_depth );
fc::raw::pack( s, unsigned_int(value.size()), _max_depth );
auto itr = value.begin();
auto end = value.end();
while( itr != end ) {
Expand Down Expand Up @@ -562,7 +543,7 @@ namespace fc {
inline void pack( Stream& s, const std::vector<T>& value, uint32_t _max_depth ) {
FC_ASSERT( _max_depth > 0 );
--_max_depth;
fc::raw::pack( s, unsigned_int((uint32_t)value.size()), _max_depth );
fc::raw::pack( s, unsigned_int(value.size()), _max_depth );
auto itr = value.begin();
auto end = value.end();
while( itr != end ) {
Expand Down Expand Up @@ -590,7 +571,7 @@ namespace fc {
inline void pack( Stream& s, const std::set<T>& value, uint32_t _max_depth ) {
FC_ASSERT( _max_depth > 0 );
--_max_depth;
fc::raw::pack( s, unsigned_int((uint32_t)value.size()), _max_depth );
fc::raw::pack( s, unsigned_int(value.size()), _max_depth );
auto itr = value.begin();
auto end = value.end();
while( itr != end ) {
Expand Down
3 changes: 0 additions & 3 deletions include/fc/io/raw_fwd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,6 @@ namespace fc {
template<typename Stream, typename T> inline void pack( Stream& s, const std::vector<T>& v, uint32_t _max_depth=FC_PACK_MAX_DEPTH );
template<typename Stream, typename T> inline void unpack( Stream& s, std::vector<T>& v, uint32_t _max_depth=FC_PACK_MAX_DEPTH );

template<typename Stream> inline void pack( Stream& s, const signed_int& v, uint32_t _max_depth=FC_PACK_MAX_DEPTH );
template<typename Stream> inline void unpack( Stream& s, signed_int& vi, uint32_t _max_depth=FC_PACK_MAX_DEPTH );

template<typename Stream> inline void pack( Stream& s, const unsigned_int& v, uint32_t _max_depth=FC_PACK_MAX_DEPTH );
template<typename Stream> inline void unpack( Stream& s, unsigned_int& vi, uint32_t _max_depth=FC_PACK_MAX_DEPTH );

Expand Down
2 changes: 1 addition & 1 deletion include/fc/io/raw_variant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ namespace fc { namespace raw {
{
FC_ASSERT( _max_depth > 0 );
--_max_depth;
unsigned_int vs = (uint32_t)v.size();
unsigned_int vs = v.size();
pack( s, vs, _max_depth );
for( auto itr = v.begin(); itr != v.end(); ++itr )
{
Expand Down
72 changes: 13 additions & 59 deletions include/fc/io/varint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,74 +4,37 @@
namespace fc {

struct unsigned_int {
unsigned_int( uint32_t v = 0 ):value(v){}
unsigned_int( uint64_t v = 0 ):value(v){}

template<typename T>
unsigned_int( T v ):value(v){}

//operator uint32_t()const { return value; }
//operator uint64_t()const { return value; }

template<typename T>
operator T()const { return static_cast<T>(value); }

unsigned_int& operator=( int32_t v ) { value = v; return *this; }
unsigned_int& operator=( uint64_t v ) { value = v; return *this; }

uint32_t value;
uint64_t value;

friend bool operator==( const unsigned_int& i, const uint32_t& v ) { return i.value == v; }
friend bool operator==( const uint32_t& i, const unsigned_int& v ) { return i == v.value; }
friend bool operator==( const unsigned_int& i, const uint64_t& v ) { return i.value == v; }
friend bool operator==( const uint64_t& i, const unsigned_int& v ) { return i == v.value; }
friend bool operator==( const unsigned_int& i, const unsigned_int& v ) { return i.value == v.value; }

friend bool operator!=( const unsigned_int& i, const uint32_t& v ) { return i.value != v; }
friend bool operator!=( const uint32_t& i, const unsigned_int& v ) { return i != v.value; }
friend bool operator!=( const unsigned_int& i, const uint64_t& v ) { return i.value != v; }
friend bool operator!=( const uint64_t& i, const unsigned_int& v ) { return i != v.value; }
friend bool operator!=( const unsigned_int& i, const unsigned_int& v ) { return i.value != v.value; }

friend bool operator<( const unsigned_int& i, const uint32_t& v ) { return i.value < v; }
friend bool operator<( const uint32_t& i, const unsigned_int& v ) { return i < v.value; }
friend bool operator<( const unsigned_int& i, const uint64_t& v ) { return i.value < v; }
friend bool operator<( const uint64_t& i, const unsigned_int& v ) { return i < v.value; }
friend bool operator<( const unsigned_int& i, const unsigned_int& v ) { return i.value < v.value; }

friend bool operator>=( const unsigned_int& i, const uint32_t& v ) { return i.value >= v; }
friend bool operator>=( const uint32_t& i, const unsigned_int& v ) { return i >= v.value; }
friend bool operator>=( const unsigned_int& i, const uint64_t& v ) { return i.value >= v; }
friend bool operator>=( const uint64_t& i, const unsigned_int& v ) { return i >= v.value; }
friend bool operator>=( const unsigned_int& i, const unsigned_int& v ) { return i.value >= v.value; }
};

/**
* @brief serializes a 32 bit signed interger in as few bytes as possible
*
* Uses the google protobuf algorithm for seralizing signed numbers
*/
struct signed_int {
signed_int( int32_t v = 0 ):value(v){}
operator int32_t()const { return value; }
template<typename T>
signed_int& operator=( const T& v ) { value = v; return *this; }
signed_int operator++(int) { return value++; }
signed_int& operator++(){ ++value; return *this; }

int32_t value;

friend bool operator==( const signed_int& i, const int32_t& v ) { return i.value == v; }
friend bool operator==( const int32_t& i, const signed_int& v ) { return i == v.value; }
friend bool operator==( const signed_int& i, const signed_int& v ) { return i.value == v.value; }

friend bool operator!=( const signed_int& i, const int32_t& v ) { return i.value != v; }
friend bool operator!=( const int32_t& i, const signed_int& v ) { return i != v.value; }
friend bool operator!=( const signed_int& i, const signed_int& v ) { return i.value != v.value; }

friend bool operator<( const signed_int& i, const int32_t& v ) { return i.value < v; }
friend bool operator<( const int32_t& i, const signed_int& v ) { return i < v.value; }
friend bool operator<( const signed_int& i, const signed_int& v ) { return i.value < v.value; }

friend bool operator>=( const signed_int& i, const int32_t& v ) { return i.value >= v; }
friend bool operator>=( const int32_t& i, const signed_int& v ) { return i >= v.value; }
friend bool operator>=( const signed_int& i, const signed_int& v ) { return i.value >= v.value; }
};

class variant;

void to_variant( const signed_int& var, variant& vo, uint32_t max_depth = 1 );
void from_variant( const variant& var, signed_int& vo, uint32_t max_depth = 1 );
void to_variant( const unsigned_int& var, variant& vo, uint32_t max_depth = 1 );
void from_variant( const variant& var, unsigned_int& vo, uint32_t max_depth = 1 );

Expand All @@ -80,22 +43,13 @@ void from_variant( const variant& var, unsigned_int& vo, uint32_t max_depth = 1
#include <unordered_map>
namespace std
{
template<>
struct hash<fc::signed_int>
{
public:
size_t operator()(const fc::signed_int &a) const
{
return std::hash<int32_t>()(a.value);
}
};
template<>
struct hash<fc::unsigned_int>
{
public:
size_t operator()(const fc::signed_int &a) const
size_t operator()(const fc::unsigned_int &a) const
{
return std::hash<uint32_t>()(a.value);
return std::hash<uint64_t>()(a.value);
}
};
}
2 changes: 0 additions & 2 deletions include/fc/reflect/typename.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,8 @@ namespace fc {
}
};

struct signed_int;
struct unsigned_int;
struct variant_object;
template<> struct get_typename<signed_int> { static const char* name() { return "signed_int"; } };
template<> struct get_typename<unsigned_int> { static const char* name() { return "unsigned_int"; } };
template<> struct get_typename<variant_object> { static const char* name() { return "fc::variant_object"; } };

Expand Down
4 changes: 1 addition & 3 deletions src/io/varint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

namespace fc
{
void to_variant( const signed_int& var, variant& vo, uint32_t max_depth ) { vo = var.value; }
void from_variant( const variant& var, signed_int& vo, uint32_t max_depth ) { vo.value = static_cast<int32_t>(var.as_int64()); }
void to_variant( const unsigned_int& var, variant& vo, uint32_t max_depth ) { vo = var.value; }
void from_variant( const variant& var, unsigned_int& vo, uint32_t max_depth ) { vo.value = static_cast<uint32_t>(var.as_uint64()); }
void from_variant( const variant& var, unsigned_int& vo, uint32_t max_depth ) { vo.value = var.as_uint64(); }
}
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ add_executable( all_tests all_tests.cpp
io/json_tests.cpp
io/stream_tests.cpp
io/tcp_test.cpp
io/varint_tests.cpp
network/http/websocket_test.cpp
thread/task_cancel.cpp
thread/thread_tests.cpp
Expand Down
Loading

0 comments on commit 2405081

Please sign in to comment.