adding program opts, shared mem, raw packing, varint, and more
This commit is contained in:
parent
249dcd05ee
commit
8b998caa1b
20 changed files with 775 additions and 22 deletions
|
|
@ -73,6 +73,8 @@ set( sources
|
|||
src/udp_socket.cpp
|
||||
src/asio.cpp
|
||||
src/super_fast_hash.cpp
|
||||
src/file_mapping.cpp
|
||||
src/program_options.cpp
|
||||
)
|
||||
setup_library( fc SOURCES ${sources} )
|
||||
|
||||
|
|
|
|||
|
|
@ -51,9 +51,9 @@ namespace fc {
|
|||
void rethrow_exception( const exception_ptr& e );
|
||||
|
||||
void throw_exception( const char* func, const char* file, int line, const char* msg );
|
||||
void throw_exception( const char* func, const char* file, int line, const char* msg,
|
||||
void throw_exception_( const char* func, const char* file, int line, const char* msg,
|
||||
const fc::string& a1 );
|
||||
void throw_exception( const char* func, const char* file, int line, const char* msg,
|
||||
void throw_exception_( const char* func, const char* file, int line, const char* msg,
|
||||
const fc::string& a1, const fc::string& a2 );
|
||||
void throw_exception( const char* func, const char* file, int line, const char* msg,
|
||||
const fc::string& a1, const fc::string& a2, const fc::string& a3 );
|
||||
|
|
@ -76,12 +76,12 @@ namespace fc {
|
|||
|
||||
template<typename T>
|
||||
void throw_exception( const char* func, const char* file, int line, const char* msg, T&& a1 ) {
|
||||
throw_exception( func, file, line, msg, to_string(fc::forward<T>(a1) ) );
|
||||
throw_exception_( func, file, line, msg, to_string(fc::forward<T>(a1) ) );
|
||||
}
|
||||
|
||||
template<typename T1, typename T2>
|
||||
void throw_exception( const char* func, const char* file, int line, const char* msg, T1&& a1, T2&& a2 ) {
|
||||
throw_exception( func, file, line, msg, to_string(fc::forward<T1>(a1) ), to_string( fc::forward<T2>(a2) ) );
|
||||
throw_exception_( func, file, line, msg, to_string(fc::forward<T1>(a1) ), to_string( fc::forward<T2>(a2) ) );
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -12,6 +12,8 @@ template<typename T,unsigned int S, typename Align=double>
|
|||
class fwd {
|
||||
public:
|
||||
template<typename U> fwd( U&& u );
|
||||
template<typename U, typename V> fwd( U&& u, V&& v );
|
||||
template<typename U, typename V, typename X, typename Y> fwd( U&& u, V&& v, X&&, Y&& );
|
||||
fwd();
|
||||
|
||||
fwd( const fwd& f );
|
||||
|
|
|
|||
|
|
@ -64,6 +64,21 @@ namespace fc {
|
|||
check_size<sizeof(T),sizeof(_store)>();
|
||||
new (this) T( fc::forward<U>(u) );
|
||||
}
|
||||
|
||||
template<typename T,unsigned int S,typename A>
|
||||
template<typename U,typename V>
|
||||
fwd<T,S,A>::fwd( U&& u, V&& v ) {
|
||||
check_size<sizeof(T),sizeof(_store)>();
|
||||
new (this) T( fc::forward<U>(u), fc::forward<V>(v) );
|
||||
}
|
||||
template<typename T,unsigned int S,typename A>
|
||||
template<typename U,typename V,typename X,typename Y>
|
||||
fwd<T,S,A>::fwd( U&& u, V&& v, X&& x, Y&& y ) {
|
||||
check_size<sizeof(T),sizeof(_store)>();
|
||||
new (this) T( fc::forward<U>(u), fc::forward<V>(v), fc::forward<X>(x), fc::forward<Y>(y) );
|
||||
}
|
||||
|
||||
|
||||
template<typename T,unsigned int S,typename A>
|
||||
fwd<T,S,A>::fwd() {
|
||||
check_size<sizeof(T),sizeof(_store)>();
|
||||
|
|
|
|||
38
include/fc/interprocess/file_mapping.hpp
Normal file
38
include/fc/interprocess/file_mapping.hpp
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
#ifndef _FC_INTERPROCESS_FILEMAPPING_HPP_
|
||||
#define _FC_INTERPROCESS_FILEMAPPING_HPP_
|
||||
#include <fc/fwd.hpp>
|
||||
#include <fc/utility.hpp>
|
||||
|
||||
namespace boost {
|
||||
namespace interprocess {
|
||||
class file_mapping;
|
||||
class mapped_region;
|
||||
}
|
||||
}
|
||||
namespace fc {
|
||||
enum mode_t {
|
||||
read_only,
|
||||
write_only,
|
||||
read_write
|
||||
};
|
||||
|
||||
class file_mapping {
|
||||
public:
|
||||
file_mapping( const char* file, mode_t );
|
||||
~file_mapping();
|
||||
private:
|
||||
friend class mapped_region;
|
||||
fc::fwd<boost::interprocess::file_mapping,16> my;
|
||||
};
|
||||
class mapped_region {
|
||||
public:
|
||||
mapped_region( const file_mapping& fm, mode_t m, size_t start, size_t size );
|
||||
mapped_region( const file_mapping& fm, mode_t m );
|
||||
~mapped_region();
|
||||
void* get_address()const;
|
||||
size_t get_size()const;
|
||||
private:
|
||||
fc::fwd<boost::interprocess::mapped_region,40> my;
|
||||
};
|
||||
}
|
||||
#endif
|
||||
|
|
@ -107,16 +107,17 @@ namespace fc {
|
|||
|
||||
template<typename T,uint32_t KS, uint32_t PE>
|
||||
friend T& operator<<( T& ds, const fc::private_key<KS,PE>& pk ) {
|
||||
ds << uint16_t(pk.key.size());
|
||||
ds.write( &pk.key.front(), pk.key.size() );
|
||||
uint16_t s = pk.key.size();
|
||||
ds.write( (const char*)&s, sizeof(s) );
|
||||
ds.write( pk.key.data(), pk.key.size() );
|
||||
return ds;
|
||||
}
|
||||
template<typename T,uint32_t KS, uint32_t PE>
|
||||
friend T& operator>>( T& ds, fc::private_key<KS,PE>& pk ) {
|
||||
uint16_t s;
|
||||
ds >> s;
|
||||
ds.read((char*)&s,sizeof(s) );
|
||||
pk.key.resize(s);
|
||||
ds.read( &pk.key.front(), pk.key.size() );
|
||||
ds.read( pk.key.data(), pk.key.size() );
|
||||
return ds;
|
||||
}
|
||||
private:
|
||||
|
|
|
|||
64
include/fc/program_options.hpp
Normal file
64
include/fc/program_options.hpp
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
#ifndef _FC_PROGRAM_OPTIONS_HPP_
|
||||
#define _FC_PROGRAM_OPTIONS_HPP_
|
||||
#include <fc/optional.hpp>
|
||||
#include <fc/string.hpp>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <fc/fwd.hpp>
|
||||
|
||||
namespace boost {
|
||||
namespace program_options {
|
||||
class variables_map;
|
||||
}
|
||||
}
|
||||
|
||||
namespace fc {
|
||||
class ostream;
|
||||
|
||||
namespace program_options {
|
||||
template<typename T>
|
||||
class value {
|
||||
public:
|
||||
value( T* v ):_v(v){}
|
||||
value& default_value( const T& d ) { _default = d; }
|
||||
|
||||
T* get()const { return _v; }
|
||||
private:
|
||||
fc::optional<T> _default;
|
||||
T* _v;
|
||||
};
|
||||
|
||||
class options_description {
|
||||
public:
|
||||
options_description( const char* c );
|
||||
~options_description();
|
||||
|
||||
options_description& add_options();
|
||||
options_description& operator()( const char* o, const char* desc );
|
||||
options_description& operator()( const char* o, const value<fc::string>&, const char* desc );
|
||||
options_description& operator()( const char* o, const value<uint16_t>&, const char* desc );
|
||||
options_description& operator()( const char* o, const value<std::vector<std::string> >&, const char* desc );
|
||||
|
||||
private:
|
||||
class impl;
|
||||
fwd<impl,104> my;
|
||||
|
||||
friend class variables_map;
|
||||
friend fc::ostream& operator<<( fc::ostream& o, const options_description& );
|
||||
};
|
||||
|
||||
class variables_map {
|
||||
public:
|
||||
variables_map();
|
||||
~variables_map();
|
||||
|
||||
void parse_command_line( int argc, char** argv, const options_description& d );
|
||||
int count( const char* opt );
|
||||
private:
|
||||
class impl;
|
||||
fwd<impl,160> my;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
#endif // _FC_PROGRAM_OPTIONS_HPP_
|
||||
288
include/fc/raw.hpp
Normal file
288
include/fc/raw.hpp
Normal file
|
|
@ -0,0 +1,288 @@
|
|||
#ifndef _TORNET_RPC_RAW_HPP_
|
||||
#define _TORNET_RPC_RAW_HPP_
|
||||
#include <fc/static_reflect.hpp>
|
||||
#include <fc/datastream.hpp>
|
||||
#include <fc/varint.hpp>
|
||||
#include <fc/optional.hpp>
|
||||
//#include <fc/value.hpp>
|
||||
|
||||
namespace fc { namespace raw {
|
||||
|
||||
template<typename Stream, typename T>
|
||||
void unpack( Stream& s, fc::optional<T>& v );
|
||||
template<typename Stream, typename T>
|
||||
void pack( Stream& s, const fc::optional<T>& v );
|
||||
|
||||
template<typename Stream>
|
||||
void unpack( Stream& s, fc::value& );
|
||||
template<typename Stream>
|
||||
void pack( Stream& s, const fc::value& );
|
||||
|
||||
template<typename Stream>
|
||||
void unpack( Stream& s, fc::string& );
|
||||
|
||||
template<typename Stream>
|
||||
void pack( Stream& s, const fc::string& );
|
||||
|
||||
template<typename Stream, typename T>
|
||||
inline void pack( Stream& s, const T& v );
|
||||
|
||||
template<typename Stream, typename T>
|
||||
inline void unpack( Stream& s, T& v );
|
||||
|
||||
template<typename Stream, typename T>
|
||||
inline void pack( Stream& s, const fc::vector<T>& v );
|
||||
template<typename Stream, typename T>
|
||||
inline void unpack( Stream& s, fc::vector<T>& v );
|
||||
|
||||
template<typename Stream> inline void pack( Stream& s, const signed_int& v ) {
|
||||
uint32_t val = (v.value<<1) ^ (v.value>>31);
|
||||
do {
|
||||
uint8_t b = uint8_t(val) & 0x7f;
|
||||
val >>= 7;
|
||||
b |= ((val > 0) << 7);
|
||||
s.put(b);
|
||||
} while( val );
|
||||
}
|
||||
|
||||
template<typename Stream> inline void pack( Stream& s, const unsigned_int& v ) {
|
||||
uint64_t val = v.value;
|
||||
do {
|
||||
uint8_t b = uint8_t(val) & 0x7f;
|
||||
val >>= 7;
|
||||
b |= ((val > 0) << 7);
|
||||
s.put(b);
|
||||
}while( val );
|
||||
}
|
||||
|
||||
template<typename Stream> inline void unpack( Stream& s, signed_int& vi ) {
|
||||
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 ) {
|
||||
uint64_t v = 0; char b = 0; uint8_t by = 0;
|
||||
do {
|
||||
s.get(b);
|
||||
v |= uint32_t(uint8_t(b) & 0x7f) << by;
|
||||
by += 7;
|
||||
} while( uint8_t(b) & 0x80 );
|
||||
vi.value = v;
|
||||
}
|
||||
|
||||
template<typename Stream> inline void pack( Stream& s, const char* v ) { pack( s, fc::string(v) ); }
|
||||
|
||||
// optional
|
||||
template<typename Stream, typename T>
|
||||
inline void pack( Stream& s, const fc::optional<T>& v ) {
|
||||
pack( s, bool(!!v) );
|
||||
if( !!v ) pack( s, *v );
|
||||
}
|
||||
|
||||
template<typename Stream, typename T>
|
||||
void unpack( Stream& s, fc::optional<T>& v ) {
|
||||
bool b; unpack( s, b );
|
||||
if( b ) { v = T(); unpack( s, *v ); }
|
||||
}
|
||||
|
||||
// fc::vector
|
||||
template<typename Stream> inline void pack( Stream& s, const fc::vector<char>& value ) {
|
||||
pack( s, unsigned_int(value.size()) );
|
||||
if( value.size() )
|
||||
s.write( &value.front(), value.size() );
|
||||
}
|
||||
template<typename Stream> inline void unpack( Stream& s, fc::vector<char>& value ) {
|
||||
unsigned_int size; unpack( s, size );
|
||||
slog( "size %d", size.value );
|
||||
value.resize(size.value);
|
||||
if( value.size() )
|
||||
s.read( value.data(), value.size() );
|
||||
}
|
||||
|
||||
// fc::string
|
||||
template<typename Stream> inline void pack( Stream& s, const fc::string& v ) {
|
||||
pack( s, unsigned_int(v.size()) );
|
||||
if( v.size() ) s.write( v.c_str(), v.size() );
|
||||
}
|
||||
|
||||
template<typename Stream> inline void unpack( Stream& s, fc::string& v ) {
|
||||
fc::vector<char> tmp; unpack(s,tmp);
|
||||
v = fc::string(tmp.begin(),tmp.end());
|
||||
}
|
||||
|
||||
// bool
|
||||
template<typename Stream> inline void pack( Stream& s, const bool& v ) { pack( s, uint8_t(v) ); }
|
||||
template<typename Stream> inline void unpack( Stream& s, bool& v ) { uint8_t b; unpack( s, b ); v=b; }
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<typename Stream, typename Class>
|
||||
struct pack_object_visitor {
|
||||
pack_object_visitor(const Class& _c, Stream& _s)
|
||||
:c(_c),s(_s){}
|
||||
|
||||
template<typename T, typename C, T(C::*p)>
|
||||
void operator()( const char* name )const {
|
||||
slog( "packing %s", name );
|
||||
raw::pack( s, c.*p );
|
||||
}
|
||||
private:
|
||||
const Class& c;
|
||||
Stream& s;
|
||||
};
|
||||
|
||||
template<typename Stream, typename Class>
|
||||
struct unpack_object_visitor {
|
||||
unpack_object_visitor(Class& _c, Stream& _s)
|
||||
:c(_c),s(_s){}
|
||||
|
||||
template<typename T, typename C, T(C::*p)>
|
||||
inline void operator()( const char* name )const {
|
||||
raw::unpack( s, c.*p );
|
||||
}
|
||||
private:
|
||||
Class& c;
|
||||
Stream& s;
|
||||
};
|
||||
|
||||
template<typename Stream>
|
||||
struct pack_sequence {
|
||||
pack_sequence( Stream& _s ):s(_s){}
|
||||
template<typename T>
|
||||
void operator() ( const T& v )const { raw::pack(s,v); }
|
||||
Stream& s;
|
||||
};
|
||||
|
||||
template<typename Stream>
|
||||
struct unpack_sequence {
|
||||
unpack_sequence( Stream& _s ):s(_s){}
|
||||
template<typename T>
|
||||
void operator() ( T& v )const { raw::unpack(s,v); }
|
||||
Stream& s;
|
||||
};
|
||||
|
||||
template<typename IsClass=fc::true_type>
|
||||
struct if_class{
|
||||
template<typename Stream, typename T>
|
||||
static inline void pack( Stream& s, const T& v ) { s << v; }
|
||||
template<typename Stream, typename T>
|
||||
static inline void unpack( Stream& s, T& v ) { s >> v; }
|
||||
};
|
||||
|
||||
template<>
|
||||
struct if_class<fc::false_type> {
|
||||
template<typename Stream, typename T>
|
||||
static inline void pack( Stream& s, const T& v ) {
|
||||
s.write( (char*)&v, sizeof(v) );
|
||||
}
|
||||
template<typename Stream, typename T>
|
||||
static inline void unpack( Stream& s, T& v ) {
|
||||
s.read( (char*)&v, sizeof(v) );
|
||||
}
|
||||
};
|
||||
|
||||
template<typename IsReflected=fc::false_type>
|
||||
struct if_reflected {
|
||||
template<typename Stream, typename T>
|
||||
static inline void pack( Stream& s, const T& v ) {
|
||||
if_class<typename fc::is_class<T>::type>::pack(s,v);
|
||||
}
|
||||
template<typename Stream, typename T>
|
||||
static inline void unpack( Stream& s, T& v ) {
|
||||
if_class<typename fc::is_class<T>::type>::unpack(s,v);
|
||||
}
|
||||
};
|
||||
template<>
|
||||
struct if_reflected<fc::true_type> {
|
||||
template<typename Stream, typename T>
|
||||
static inline void pack( Stream& s, const T& v ) {
|
||||
fc::static_reflector<T>::visit( pack_object_visitor<Stream,T>( v, s ) );
|
||||
}
|
||||
template<typename Stream, typename T>
|
||||
static inline void unpack( Stream& s, T& v ) {
|
||||
fc::static_reflector<T>::visit( unpack_object_visitor<Stream,T>( v, s ) );
|
||||
}
|
||||
};
|
||||
|
||||
} // namesapce detail
|
||||
|
||||
|
||||
template<typename Stream, typename T>
|
||||
inline void pack( Stream& s, const fc::vector<T>& value ) {
|
||||
pack( s, unsigned_int(value.size()) );
|
||||
auto itr = value.begin();
|
||||
auto end = value.end();
|
||||
while( itr != end ) {
|
||||
fc::raw::pack( s, *itr );
|
||||
++itr;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Stream, typename T>
|
||||
inline void unpack( Stream& s, fc::vector<T>& value ) {
|
||||
unsigned_int size; unpack( s, size );
|
||||
value.resize(size.value);
|
||||
auto itr = value.begin();
|
||||
auto end = value.end();
|
||||
while( itr != end ) {
|
||||
fc::raw::unpack( s, *itr );
|
||||
++itr;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Stream, typename T>
|
||||
inline void pack( Stream& s, const T& v ) {
|
||||
fc::raw::detail::if_reflected< typename fc::static_reflector<T>::is_defined >::pack(s,v);
|
||||
}
|
||||
template<typename Stream, typename T>
|
||||
inline void unpack( Stream& s, T& v ) {
|
||||
fc::raw::detail::if_reflected< typename fc::static_reflector<T>::is_defined >::unpack(s,v);
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
inline fc::vector<char> pack( const T& v ) {
|
||||
datastream<size_t> ps;
|
||||
raw::pack(ps,v );
|
||||
fc::vector<char> vec(ps.tellp());
|
||||
slog( "vec.size %d", vec.size() );
|
||||
if( vec.size() ) {
|
||||
datastream<char*> ds( vec.data(), vec.size() );
|
||||
raw::pack(ds,v);
|
||||
}
|
||||
return vec;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline T unpack( const fc::vector<char>& s ) {
|
||||
T tmp;
|
||||
if( s.size() ) {
|
||||
datastream<const char*> ds( s.data(), s.size() );
|
||||
raw::unpack(ds,tmp);
|
||||
}
|
||||
return tmp;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline void pack( char* d, uint32_t s, const T& v ) {
|
||||
datastream<char*> ds(d,s);
|
||||
raw::pack(ds,v );
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline T unpack( const char* d, uint32_t s ) {
|
||||
T v;
|
||||
datastream<const char*> ds( d, s );
|
||||
raw::unpack(ds,v);
|
||||
return v;
|
||||
}
|
||||
|
||||
} } // namespace fc::raw
|
||||
|
||||
#endif // BOOST_RPC_RAW_HPP
|
||||
|
|
@ -151,8 +151,7 @@ namespace fc {
|
|||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
struct get_typename {};
|
||||
template<typename T> class get_typename{};
|
||||
template<> struct get_typename<int32_t> { static const char* name() { return "int32_t"; } };
|
||||
template<> struct get_typename<int64_t> { static const char* name() { return "int64_t"; } };
|
||||
template<> struct get_typename<int16_t> { static const char* name() { return "int16_t"; } };
|
||||
|
|
@ -164,6 +163,7 @@ namespace fc {
|
|||
template<> struct get_typename<double> { static const char* name() { return "double"; } };
|
||||
template<> struct get_typename<float> { static const char* name() { return "float"; } };
|
||||
template<> struct get_typename<bool> { static const char* name() { return "bool"; } };
|
||||
template<> struct get_typename<char> { static const char* name() { return "char"; } };
|
||||
template<> struct get_typename<string> { static const char* name() { return "string"; } };
|
||||
|
||||
template<typename T>
|
||||
|
|
@ -178,7 +178,7 @@ namespace fc {
|
|||
v.visit( *((const T*)s) );
|
||||
}
|
||||
|
||||
static reflector& instance() { static reflector inst; return inst; }
|
||||
static reflector& instance() { static reflector<T> inst; return inst; }
|
||||
};
|
||||
|
||||
template<typename T> reflector<T>& reflect( const T& ) { return reflector<T>::instance(); }
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
#define _FC_SHA1_HPP_
|
||||
#include <fc/fwd.hpp>
|
||||
#include <fc/string.hpp>
|
||||
#include <fc/reflect.hpp>
|
||||
|
||||
namespace fc {
|
||||
|
||||
|
|
@ -59,6 +60,7 @@ namespace fc {
|
|||
};
|
||||
|
||||
}
|
||||
FC_REFLECTABLE( fc::sha1 )
|
||||
|
||||
#endif // _FC_SHA1_HPP_
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
#ifndef _FC_STREAM_HPP_
|
||||
#define _FC_STREAM_HPP_
|
||||
#include <fc/utility.hpp>
|
||||
#include <fc/log.hpp>
|
||||
#include <fc/fwd.hpp>
|
||||
|
||||
namespace fc {
|
||||
class string;
|
||||
|
|
@ -156,7 +156,83 @@ namespace fc {
|
|||
private:
|
||||
ostream( const ostream& o );
|
||||
ostream& operator=(const ostream& o);
|
||||
char _store[54*sizeof(void*)];
|
||||
void* _store[54];
|
||||
};
|
||||
|
||||
bool getline( fc::istream&, fc::string& );
|
||||
|
||||
class stringstream {
|
||||
public:
|
||||
stringstream();
|
||||
stringstream( fc::string& s);
|
||||
~stringstream();
|
||||
|
||||
fc::string str();
|
||||
|
||||
friend stringstream& operator>>( stringstream&, int64_t& );
|
||||
friend stringstream& operator>>( stringstream&, uint64_t& );
|
||||
friend stringstream& operator>>( stringstream&, int32_t& );
|
||||
friend stringstream& operator>>( stringstream&, uint32_t& );
|
||||
friend stringstream& operator>>( stringstream&, int16_t& );
|
||||
friend stringstream& operator>>( stringstream&, uint16_t& );
|
||||
friend stringstream& operator>>( stringstream&, int8_t& );
|
||||
friend stringstream& operator>>( stringstream&, uint8_t& );
|
||||
friend stringstream& operator>>( stringstream&, float& );
|
||||
friend stringstream& operator>>( stringstream&, double& );
|
||||
friend stringstream& operator>>( stringstream&, bool& );
|
||||
friend stringstream& operator>>( stringstream&, char& );
|
||||
friend stringstream& operator>>( stringstream&, fc::string& );
|
||||
|
||||
friend stringstream& operator<<( stringstream&, const int64_t& );
|
||||
friend stringstream& operator<<( stringstream&, const uint64_t& );
|
||||
friend stringstream& operator<<( stringstream&, const int32_t& );
|
||||
friend stringstream& operator<<( stringstream&, const uint32_t& );
|
||||
friend stringstream& operator<<( stringstream&, const int16_t& );
|
||||
friend stringstream& operator<<( stringstream&, const uint16_t& );
|
||||
friend stringstream& operator<<( stringstream&, const int8_t& );
|
||||
friend stringstream& operator<<( stringstream&, const uint8_t& );
|
||||
friend stringstream& operator<<( stringstream&, const float& );
|
||||
friend stringstream& operator<<( stringstream&, const double& );
|
||||
friend stringstream& operator<<( stringstream&, const bool& );
|
||||
friend stringstream& operator<<( stringstream&, const char& );
|
||||
friend stringstream& operator<<( stringstream&, const fc::string& );
|
||||
private:
|
||||
class impl;
|
||||
fwd<impl,368> my;
|
||||
};
|
||||
|
||||
class ofstream {
|
||||
public:
|
||||
enum mode { out, binary };
|
||||
ofstream();
|
||||
ofstream( const fc::string& file, int m );
|
||||
~ofstream();
|
||||
|
||||
void open( const fc::string& file, int m );
|
||||
ofstream& write( const char* buf, size_t len );
|
||||
void put( char c );
|
||||
void close();
|
||||
void flush();
|
||||
|
||||
private:
|
||||
class impl;
|
||||
fwd<impl,896> my;
|
||||
};
|
||||
class ifstream {
|
||||
public:
|
||||
enum mode { in, binary };
|
||||
ifstream();
|
||||
ifstream( const fc::string& file, int m );
|
||||
~ifstream();
|
||||
|
||||
void open( const fc::string& file, int m );
|
||||
ifstream& read( char* buf, size_t len );
|
||||
void close();
|
||||
void flush();
|
||||
|
||||
private:
|
||||
class impl;
|
||||
fwd<impl,904> my;
|
||||
};
|
||||
|
||||
extern ostream cout;
|
||||
|
|
|
|||
|
|
@ -33,6 +33,9 @@ namespace fc {
|
|||
template<typename T>
|
||||
struct is_class { typedef decltype(detail::is_class_helper<T>(0)) type; };
|
||||
|
||||
template<typename T>
|
||||
const T& min( const T& a, const T& b ) { return a < b ? a: b; }
|
||||
|
||||
template<typename T>
|
||||
void swap( T& a, T& b ) {
|
||||
T tmp = fc::move(a);
|
||||
|
|
|
|||
30
include/fc/varint.hpp
Normal file
30
include/fc/varint.hpp
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
#ifndef _FC_VARINT_HPP_
|
||||
#define _FC_VARINT_HPP_
|
||||
#include <stdint.h>
|
||||
|
||||
namespace fc {
|
||||
|
||||
struct unsigned_int {
|
||||
unsigned_int( uint32_t v = 0 ):value(v){}
|
||||
|
||||
operator uint32_t()const { return value; }
|
||||
|
||||
template<typename T>
|
||||
unsigned_int& operator=( const T& v ) { value = v; return *this; }
|
||||
|
||||
uint32_t value;
|
||||
};
|
||||
|
||||
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; }
|
||||
|
||||
int32_t value;
|
||||
};
|
||||
|
||||
} // namespace fc
|
||||
|
||||
|
||||
#endif
|
||||
|
|
@ -106,9 +106,13 @@ namespace fc {
|
|||
}
|
||||
|
||||
void resize( uint64_t i ) {
|
||||
if( capacity() < i )
|
||||
_data = detail::data<T>::reallocate( _data, i );
|
||||
_data->size = i;
|
||||
if( capacity() < i ) {
|
||||
if( _data )
|
||||
_data = detail::data<T>::reallocate( _data, i );
|
||||
else
|
||||
_data = detail::data<T>::allocate( i );
|
||||
}
|
||||
if( _data ) _data->size = i;
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
#include <fc/error.hpp>
|
||||
#include <boost/exception/all.hpp>
|
||||
#include <boost/lexical_cast.hpp>
|
||||
#include <boost/format.hpp>
|
||||
|
||||
namespace fc {
|
||||
#define bexcept void* e = &my[0]; (*((boost::exception_ptr*)e))
|
||||
|
|
@ -85,13 +86,13 @@ namespace fc {
|
|||
void throw_exception( const char* func, const char* file, int line, const char* msg ) {
|
||||
::boost::exception_detail::throw_exception_(fc::generic_exception(msg),func, file, line );
|
||||
}
|
||||
void throw_exception( const char* func, const char* file, int line, const char* msg,
|
||||
void throw_exception_( const char* func, const char* file, int line, const char* msg,
|
||||
const fc::string& a1 ) {
|
||||
::boost::exception_detail::throw_exception_(fc::generic_exception(msg),func, file, line );
|
||||
::boost::exception_detail::throw_exception_(fc::generic_exception( (boost::format(msg) % a1.c_str() ).str().c_str()) ,func, file, line );
|
||||
}
|
||||
void throw_exception( const char* func, const char* file, int line, const char* msg,
|
||||
void throw_exception_( const char* func, const char* file, int line, const char* msg,
|
||||
const fc::string& a1, const fc::string& a2 ) {
|
||||
::boost::exception_detail::throw_exception_(fc::generic_exception(msg),func, file, line );
|
||||
::boost::exception_detail::throw_exception_(fc::generic_exception( (boost::format(msg) % a1.c_str() %a2.c_str()).str().c_str()),func, file, line );
|
||||
}
|
||||
void throw_exception( const char* func, const char* file, int line, const char* msg,
|
||||
const fc::string& a1, const fc::string& a2, const fc::string& a3 ) {
|
||||
|
|
|
|||
20
src/file_mapping.cpp
Normal file
20
src/file_mapping.cpp
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
#include <fc/interprocess/file_mapping.hpp>
|
||||
#include <boost/interprocess/file_mapping.hpp>
|
||||
#include <boost/interprocess/mapped_region.hpp>
|
||||
#include <fc/fwd_impl.hpp>
|
||||
|
||||
namespace fc {
|
||||
file_mapping::file_mapping( const char* file, mode_t m )
|
||||
:my(file, m == read_only ? boost::interprocess::read_only : boost::interprocess::read_write ){}
|
||||
file_mapping::~file_mapping(){}
|
||||
|
||||
|
||||
|
||||
mapped_region::mapped_region( const file_mapping& fm, mode_t m, size_t start, size_t size )
|
||||
:my( *fm.my, m == read_only ? boost::interprocess::read_only : boost::interprocess::read_write ,start, size) { }
|
||||
mapped_region::mapped_region( const file_mapping& fm, mode_t m )
|
||||
:my( *fm.my, m == read_only ? boost::interprocess::read_only : boost::interprocess::read_write) { }
|
||||
mapped_region::~mapped_region(){}
|
||||
void* mapped_region::get_address()const { return my->get_address(); }
|
||||
size_t mapped_region::get_size()const { return my->get_size(); }
|
||||
}
|
||||
|
|
@ -35,8 +35,8 @@ namespace fc {
|
|||
if(isatty(fileno(stderr)))
|
||||
std::cerr<<"\r"<<color;
|
||||
|
||||
//fprintf( stderr, "%p %-15s %-15s %-5zd %-15s ",
|
||||
std::cerr<< thread_ptr()<< thread_name()<< short_name(file_name)<< line_num<< method_name ;
|
||||
fprintf( stderr, "%p %-15s %-15s %-5zd %-15s ", thread_ptr(), thread_name(), short_name(file_name), line_num, method_name );
|
||||
//std::cerr<<thread_ptr()<< thread_name()<< short_name(file_name)<< line_num<< method_name ;
|
||||
va_list args;
|
||||
va_start(args,format);
|
||||
vfprintf( stderr, format, args );
|
||||
|
|
|
|||
72
src/program_options.cpp
Normal file
72
src/program_options.cpp
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
#include <fc/program_options.hpp>
|
||||
#include <fc/fwd_impl.hpp>
|
||||
#include <sstream>
|
||||
#include <boost/program_options.hpp>
|
||||
#include <fc/stream.hpp>
|
||||
|
||||
namespace fc { namespace program_options {
|
||||
|
||||
class options_description::impl {
|
||||
public:
|
||||
impl( const char* c )
|
||||
:opts(c){}
|
||||
|
||||
boost::program_options::options_description opts;
|
||||
};
|
||||
|
||||
options_description::options_description( const char* c )
|
||||
:my(c)
|
||||
{ }
|
||||
|
||||
options_description::~options_description(){
|
||||
|
||||
}
|
||||
|
||||
|
||||
options_description& options_description::add_options(){
|
||||
return *this;
|
||||
}
|
||||
|
||||
options_description& options_description::operator()( const char* o, const char* desc ){
|
||||
my->opts.add_options()( o, desc );
|
||||
return *this;
|
||||
}
|
||||
|
||||
options_description& options_description::operator()( const char* o, const value<fc::string>& v, const char* desc ){
|
||||
my->opts.add_options()( o, boost::program_options::value<std::string>(reinterpret_cast<std::string*>(v.get())), desc );
|
||||
return *this;
|
||||
}
|
||||
|
||||
options_description& options_description::operator()( const char* o, const value<uint16_t>& v, const char* desc ){
|
||||
my->opts.add_options()( o, boost::program_options::value<uint16_t>(v.get()), desc );
|
||||
return *this;
|
||||
}
|
||||
|
||||
options_description& options_description::operator()( const char* o, const value<std::vector<std::string> >& v, const char* desc ){
|
||||
my->opts.add_options()( o, boost::program_options::value<std::vector<std::string> >(v.get()), desc );
|
||||
//my->opts.add_options()( o, desc );
|
||||
return *this;
|
||||
}
|
||||
|
||||
class variables_map::impl {
|
||||
public:
|
||||
boost::program_options::variables_map vm;
|
||||
};
|
||||
|
||||
variables_map::variables_map(){}
|
||||
variables_map::~variables_map(){}
|
||||
|
||||
void variables_map::parse_command_line( int argc, char** argv, const options_description& d ) {
|
||||
boost::program_options::store( boost::program_options::parse_command_line( argc, argv, d.my->opts ), my->vm );
|
||||
}
|
||||
int variables_map::count( const char* opt ) {
|
||||
return my->vm.count(opt);
|
||||
}
|
||||
|
||||
fc::ostream& operator<<( fc::ostream& o, const options_description& od ) {
|
||||
std::stringstream ss; ss << od.my->opts;
|
||||
fc::cout << ss.str().c_str();
|
||||
return o;
|
||||
}
|
||||
|
||||
} }
|
||||
10
src/sha1.cpp
10
src/sha1.cpp
|
|
@ -84,3 +84,13 @@ namespace fc {
|
|||
}
|
||||
|
||||
} // namespace fc
|
||||
|
||||
namespace fc {
|
||||
const char* reflector<sha1>::name()const { return "sha1"; }
|
||||
void reflector<sha1>::visit( void* s, const abstract_visitor& v )const {
|
||||
}
|
||||
void reflector<sha1>::visit( const void* s, const abstract_const_visitor& v )const {
|
||||
v.visit( fc::string( *((const sha1*)s)) );
|
||||
}
|
||||
reflector<sha1>& reflector<sha1>::instance() { static reflector<sha1> inst; return inst; }
|
||||
} // namespace fc
|
||||
|
|
|
|||
125
src/stream.cpp
125
src/stream.cpp
|
|
@ -2,6 +2,9 @@
|
|||
#include <iostream>
|
||||
#include <boost/iostreams/stream.hpp>
|
||||
#include <fc/thread.hpp>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <fc/fwd_impl.hpp>
|
||||
|
||||
namespace fc {
|
||||
namespace detail {
|
||||
|
|
@ -176,7 +179,129 @@ namespace fc {
|
|||
|
||||
#undef print_help
|
||||
|
||||
class ofstream::impl {
|
||||
public:
|
||||
impl(){}
|
||||
impl( const fc::string& s, int m )
|
||||
:ofs(s.c_str(), std::ios_base::binary ){}
|
||||
|
||||
|
||||
std::ofstream ofs;
|
||||
};
|
||||
|
||||
ofstream::ofstream(){}
|
||||
ofstream::ofstream( const fc::string& s, int m )
|
||||
:my(s,m){}
|
||||
ofstream::~ofstream(){}
|
||||
void ofstream::open( const fc::string& s, int m ) {
|
||||
my->ofs.open(s.c_str(), std::ios_base::binary );
|
||||
}
|
||||
ofstream& ofstream::write( const char* b, size_t s ) {
|
||||
my->ofs.write(b,s);
|
||||
return *this;
|
||||
}
|
||||
void ofstream::put( char c ) { my->ofs.put(c); }
|
||||
|
||||
|
||||
class ifstream::impl {
|
||||
public:
|
||||
impl(){}
|
||||
impl( const fc::string& s, int m )
|
||||
:ifs(s.c_str(), std::ios_base::binary ){}
|
||||
|
||||
|
||||
std::ifstream ifs;
|
||||
};
|
||||
|
||||
|
||||
|
||||
ifstream::ifstream() {
|
||||
}
|
||||
ifstream::~ifstream() {}
|
||||
ifstream::ifstream( const fc::string& s, int m )
|
||||
:my(s.c_str(), std::ios_base::binary){}
|
||||
|
||||
ifstream& ifstream::read( char* b, size_t s ) {
|
||||
my->ifs.read(b,s);
|
||||
return *this;
|
||||
}
|
||||
void ifstream::open( const fc::string& s, int m ) {
|
||||
my->ifs.open(s.c_str(), std::ios_base::binary );
|
||||
}
|
||||
|
||||
class stringstream::impl {
|
||||
public:
|
||||
impl( fc::string&s )
|
||||
:ss( reinterpret_cast<std::string&>(s) )
|
||||
{
|
||||
}
|
||||
std::stringstream ss;
|
||||
};
|
||||
|
||||
stringstream::stringstream( fc::string& s )
|
||||
:my(s) {
|
||||
}
|
||||
stringstream::~stringstream(){}
|
||||
|
||||
stringstream& operator>>( stringstream& s, int64_t& v ){
|
||||
s.my->ss >> v;
|
||||
return s;
|
||||
}
|
||||
stringstream& operator>>( stringstream& s, uint64_t& v ){
|
||||
s.my->ss >> v;
|
||||
return s;
|
||||
}
|
||||
stringstream& operator>>( stringstream& s, int32_t& v ){
|
||||
s.my->ss >> v;
|
||||
return s;
|
||||
}
|
||||
stringstream& operator>>( stringstream& s, uint32_t& v ){
|
||||
s.my->ss >> v;
|
||||
return s;
|
||||
}
|
||||
stringstream& operator>>( stringstream& s, int16_t& v ){
|
||||
s.my->ss >> v;
|
||||
return s;
|
||||
}
|
||||
stringstream& operator>>( stringstream& s, uint16_t& v ){
|
||||
s.my->ss >> v;
|
||||
return s;
|
||||
}
|
||||
stringstream& operator>>( stringstream& s, int8_t& v ){
|
||||
s.my->ss >> v;
|
||||
return s;
|
||||
}
|
||||
stringstream& operator>>( stringstream& s, uint8_t& v ){
|
||||
s.my->ss >> v;
|
||||
return s;
|
||||
}
|
||||
stringstream& operator>>( stringstream& s, float& v ){
|
||||
s.my->ss >> v;
|
||||
return s;
|
||||
}
|
||||
stringstream& operator>>( stringstream& s, double& v ){
|
||||
s.my->ss >> v;
|
||||
return s;
|
||||
}
|
||||
stringstream& operator>>( stringstream& s, bool& v ){
|
||||
s.my->ss >> v;
|
||||
return s;
|
||||
}
|
||||
stringstream& operator>>( stringstream& s, char& v ){
|
||||
s.my->ss >> v;
|
||||
return s;
|
||||
}
|
||||
stringstream& operator>>( stringstream& s, fc::string& v ){
|
||||
s.my->ss >> reinterpret_cast<std::string&>(v);
|
||||
return s;
|
||||
}
|
||||
|
||||
bool getline( istream& in, fc::string& f ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ostream cout( std::cout );
|
||||
ostream cerr( std::cerr );
|
||||
istream cin( detail::get_cin_stream() );
|
||||
} //namespace fc
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue