Skip to content

Commit

Permalink
adding fixed_string definition
Browse files Browse the repository at this point in the history
  • Loading branch information
bytemaster committed Sep 15, 2016
1 parent f59a516 commit e7d0d26
Show file tree
Hide file tree
Showing 9 changed files with 261 additions and 28 deletions.
4 changes: 4 additions & 0 deletions include/fc/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ namespace fc {
T& at( size_t pos ) { assert( pos < N); return data[pos]; }
const T& at( size_t pos )const { assert( pos < N); return data[pos]; }
///@}

T& operator[]( size_t pos ) { assert( pos < N); return data[pos]; }
const T& operator[]( size_t pos )const { assert( pos < N); return data[pos]; }


T* begin() { return &data[0]; }
const T* begin()const { return &data[0]; }
Expand Down
16 changes: 8 additions & 8 deletions include/fc/container/flat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ namespace fc {
value.insert( std::move(tmp) );
}
}
template<typename Stream, typename K, typename V>
inline void pack( Stream& s, const flat_map<K,V>& value ) {
template<typename Stream, typename K, typename... V>
inline void pack( Stream& s, const flat_map<K,V...>& value ) {
pack( s, unsigned_int((uint32_t)value.size()) );
auto itr = value.begin();
auto end = value.end();
Expand All @@ -40,8 +40,8 @@ namespace fc {
++itr;
}
}
template<typename Stream, typename K, typename V>
inline void unpack( Stream& s, flat_map<K,V>& value )
template<typename Stream, typename K, typename V, typename... A>
inline void unpack( Stream& s, flat_map<K,V,A...>& value )
{
unsigned_int size; unpack( s, size );
value.clear();
Expand Down Expand Up @@ -76,17 +76,17 @@ namespace fc {
vo.insert( itr->as<T>() );
}

template<typename K, typename T>
void to_variant( const flat_map<K, T>& var, variant& vo )
template<typename K, typename... T>
void to_variant( const flat_map<K, T...>& var, variant& vo )
{
std::vector< variant > vars(var.size());
size_t i = 0;
for( auto itr = var.begin(); itr != var.end(); ++itr, ++i )
vars[i] = fc::variant(*itr);
vo = vars;
}
template<typename K, typename T>
void from_variant( const variant& var, flat_map<K, T>& vo )
template<typename K, typename T, typename... A>
void from_variant( const variant& var, flat_map<K, T, A...>& vo )
{
const variants& vars = var.get_array();
vo.clear();
Expand Down
8 changes: 4 additions & 4 deletions include/fc/container/flat_fwd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ namespace fc {
void pack( Stream& s, const flat_set<T>& value );
template<typename Stream, typename T>
void unpack( Stream& s, flat_set<T>& value );
template<typename Stream, typename K, typename V>
void pack( Stream& s, const flat_map<K,V>& value );
template<typename Stream, typename K, typename V>
void unpack( Stream& s, flat_map<K,V>& value ) ;
template<typename Stream, typename K, typename... V>
void pack( Stream& s, const flat_map<K,V...>& value );
template<typename Stream, typename K, typename... V>
void unpack( Stream& s, flat_map<K,V...>& value ) ;
} // namespace raw

} // fc
159 changes: 159 additions & 0 deletions include/fc/fixed_string.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
#pragma once
#include <fc/io/raw_fwd.hpp>
#include <boost/multiprecision/cpp_int.hpp>


namespace fc {
typedef boost::multiprecision::uint128_t m128;


/**
* This class is designed to offer in-place memory allocation of a string up to Length equal to
* sizeof(Storage).
*
* The string will serialize the same way as std::string for variant and raw formats
* The string will sort according to the comparison operators defined for Storage, this enables effecient
* sorting.
*/
template<typename Storage = std::pair<uint64_t,uint64_t> >
class fixed_string {
public:
fixed_string(){}
fixed_string( const fixed_string& c ):data(c.data){}

fixed_string( const std::string& str ) {
if( str.size() <= sizeof(data) )
memcpy( (char*)&data, str.c_str(), str.size() );
else {
wlog( "truncating string '${str}'", ("str",str) );
memcpy( (char*)&data, str.c_str(), sizeof(data) );
}
}
fixed_string( const char* str ) {
int l = strlen(str);
if( l <= sizeof(data) )
memcpy( (char*)&data, str, l );
else {
wlog( "truncating string '${str}'", ("str",str) );
memcpy( (char*)&data, str, sizeof(data) );
}
}

operator std::string()const {
const char* self = (const char*)&data;
return std::string( self, self + size() );
}

uint32_t size()const {
if( *(((const char*)&data)+sizeof(data) - 1) )
return sizeof(data);
return strnlen( (const char*)&data, sizeof(data) );
}
uint32_t length()const { return size(); }

fixed_string& operator=( const fixed_string& str ) {
data = str.data;
return *this;
}
fixed_string& operator=( const char* str ) {
return *this = fixed_string(str);
}

fixed_string& operator=( const std::string& str ) {
if( str.size() <= sizeof(data) ) {
data = Storage();
memcpy( (char*)&data, str.c_str(), str.size() );
}
else {
wlog( "truncating string '${str}'", ("str",str) );
memcpy( (char*)&data, str.c_str(), sizeof(data) );
}
return *this;
}

friend std::string operator + ( const fixed_string& a, const std::string& b ) {
return std::string(a) + b;
}
friend std::string operator + ( const std::string& a, const fixed_string& b ) {
return a + std::string(b);
}

friend bool operator < ( const fixed_string& a, const fixed_string& b ) {
return a.data < b.data;
}
friend bool operator <= ( const fixed_string& a, const fixed_string& b ) {
return a.data <= b.data;
}
friend bool operator > ( const fixed_string& a, const fixed_string& b ) {
return a.data > b.data;
}
friend bool operator >= ( const fixed_string& a, const fixed_string& b ) {
return a.data >= b.data;
}
friend bool operator == ( const fixed_string& a, const fixed_string& b ) {
return a.data == b.data;
}
friend bool operator != ( const fixed_string& a, const fixed_string& b ) {
return a.data != b.data;
}
//private:
Storage data;
};

namespace raw
{
template<typename Stream, typename Storage>
inline void pack( Stream& s, const fc::fixed_string<Storage>& u ) {
unsigned_int size = u.size();
pack( s, size );
s.write( (const char*)&u.data, size );
}

template<typename Stream, typename Storage>
inline void unpack( Stream& s, fc::fixed_string<Storage>& u ) {
unsigned_int size;
fc::raw::unpack( s, size );
if( size.value > 0 ) {
if( size.value > sizeof(Storage) ) {
s.read( (char*)&u.data, sizeof(Storage) );
s.skip( size.value - sizeof(Storage) );

/*
s.seekp( s.tellp() + (size.value - sizeof(Storage)) );
char tmp;
size.value -= sizeof(storage);
while( size.value ){ s.read( &tmp, 1 ); --size.value; }
*/
// s.skip( size.value - sizeof(Storage) );
} else {
s.read( (char*)&u.data, size.value );
}
}
}

/*
template<typename Stream, typename... Args>
inline void pack( Stream& s, const boost::multiprecision::number<Args...>& d ) {
s.write( (const char*)&d, sizeof(d) );
}
template<typename Stream, typename... Args>
inline void unpack( Stream& s, boost::multiprecision::number<Args...>& u ) {
s.read( (const char*)&u, sizeof(u) );
}
*/
}
}

#include <fc/variant.hpp>
namespace fc {
template<typename Storage>
void to_variant( const fixed_string<Storage>& s, variant& v ) {
v = std::string(s);
}

template<typename Storage>
void from_variant( const variant& v, fixed_string<Storage>& s ) {
s = v.as_string();
}
}
11 changes: 9 additions & 2 deletions include/fc/io/raw_fwd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,19 @@ namespace fc {
namespace ip { class endpoint; }

namespace ecc { class public_key; class private_key; }
template<typename Storage> class fixed_string;

namespace raw {
template<typename Stream, typename Storage> inline void pack( Stream& s, const fc::fixed_string<Storage>& u );
template<typename Stream, typename Storage> inline void unpack( Stream& s, fc::fixed_string<Storage>& u );

template<typename Stream, typename IntType, typename EnumType>
inline void pack( Stream& s, const fc::enum_type<IntType,EnumType>& tp );
template<typename Stream, typename IntType, typename EnumType>
inline void unpack( Stream& s, fc::enum_type<IntType,EnumType>& tp );



template<typename Stream, typename T> inline void pack( Stream& s, const std::set<T>& value );
template<typename Stream, typename T> inline void unpack( Stream& s, std::set<T>& value );
template<typename Stream, typename T> inline void pack( Stream& s, const std::unordered_set<T>& value );
Expand All @@ -51,8 +58,8 @@ namespace fc {
template<typename Stream, typename K, typename V> inline void pack( Stream& s, const std::map<K,V>& value );
template<typename Stream, typename K, typename V> inline void unpack( Stream& s, std::map<K,V>& value );

template<typename Stream, typename K, typename V> inline void pack( Stream& s, const flat_map<K,V>& value );
template<typename Stream, typename K, typename V> inline void unpack( Stream& s, flat_map<K,V>& value );
template<typename Stream, typename K, typename... V> inline void pack( Stream& s, const flat_map<K,V...>& value );
template<typename Stream, typename K, typename V, typename... A> inline void unpack( Stream& s, flat_map<K,V,A...>& value );

template<typename Stream, typename K, typename V> inline void pack( Stream& s, const std::pair<K,V>& value );
template<typename Stream, typename K, typename V> inline void unpack( Stream& s, std::pair<K,V>& value );
Expand Down
8 changes: 4 additions & 4 deletions include/fc/variant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ namespace fc
template<typename K, typename T>
void from_variant( const variant& var, std::unordered_map<K,T>& vo );

template<typename K, typename T>
void to_variant( const fc::flat_map<K,T>& var, variant& vo );
template<typename K, typename T>
void from_variant( const variant& var, fc::flat_map<K,T>& vo );
template<typename K, typename... T>
void to_variant( const fc::flat_map<K,T...>& var, variant& vo );
template<typename K, typename... T>
void from_variant( const variant& var, fc::flat_map<K,T...>& vo );

template<typename T>
void to_variant( const std::map<string,T>& var, variant& vo );
Expand Down
42 changes: 35 additions & 7 deletions src/thread/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <fc/exception/exception.hpp>
#include <vector>

#include <iostream>

#include <boost/version.hpp>

#if BOOST_VERSION >= 105400
Expand Down Expand Up @@ -43,12 +45,17 @@ namespace fc {
struct context {
typedef fc::context* ptr;

#if BOOST_VERSION >= 105400
#if BOOST_VERSION >= 105400 // && BOOST_VERSION <= 106100
bco::stack_context stack_ctx;
#endif

#if BOOST_VERSION >= 106100
typedef bc::detail::transfer_t transfer_t;
#else
typedef intptr_t transfer_t;
#endif

context( void (*sf)(intptr_t), stack_allocator& alloc, fc::thread* t )
context( void (*sf)(transfer_t), stack_allocator& alloc, fc::thread* t )
: caller_context(0),
stack_alloc(&alloc),
next_blocked(0),
Expand All @@ -63,7 +70,13 @@ namespace fc {
cur_task(0),
context_posted_num(0)
{
#if BOOST_VERSION >= 105600
#if BOOST_VERSION >= 106100
// std::cerr<< "HERE: "<< BOOST_VERSION <<"\n";
//my_context = new bc::execution_context<intptr_t>( [=]( bc::execution_context<intptr_t> sink, intptr_t self ){ std::cerr<<"in ex\n"; sf(self); std::cerr<<"exit ex\n"; return sink; } );
size_t stack_size = FC_CONTEXT_STACK_SIZE;
alloc.allocate(stack_ctx, stack_size);
my_context = bc::detail::make_fcontext( stack_ctx.sp, stack_ctx.size, sf );
#elif BOOST_VERSION >= 105600
size_t stack_size = FC_CONTEXT_STACK_SIZE;
alloc.allocate(stack_ctx, stack_size);
my_context = bc::make_fcontext( stack_ctx.sp, stack_ctx.size, sf);
Expand All @@ -84,7 +97,7 @@ namespace fc {
}

context( fc::thread* t) :
#if BOOST_VERSION >= 105600
#if BOOST_VERSION >= 105600 && BOOST_VERSION <= 106100
my_context(nullptr),
#elif BOOST_VERSION >= 105300
my_context(new bc::fcontext_t),
Expand All @@ -102,10 +115,21 @@ namespace fc {
complete(false),
cur_task(0),
context_posted_num(0)
{}
{

#if BOOST_VERSION >= 106100
/*
bc::execution_context<intptr_t> tmp( [=]( bc::execution_context<intptr_t> sink, intptr_t ) { std::cerr<<"get current\n"; return sink; } );
auto result = tmp(0);
my_context = new bc::execution_context<intptr_t>( std::move( std::get<0>(result) ) );
*/
#endif
}

~context() {
#if BOOST_VERSION >= 105600
#if BOOST_VERSION >= 106100
// delete my_context;
#elif BOOST_VERSION >= 105600
if(stack_alloc)
stack_alloc->deallocate( stack_ctx );
#elif BOOST_VERSION >= 105400
Expand Down Expand Up @@ -209,7 +233,11 @@ namespace fc {



#if BOOST_VERSION >= 105300 && BOOST_VERSION < 105600

#if BOOST_VERSION >= 106100
//bc::execution_context<intptr_t>* my_context;
bc::detail::fcontext_t my_context;
#elif BOOST_VERSION >= 105300 && BOOST_VERSION < 105600
bc::fcontext_t* my_context;
#else
bc::fcontext_t my_context;
Expand Down
Loading

0 comments on commit e7d0d26

Please sign in to comment.