From 8b998caa1b14b537c341d47fdf3f7ecbfe5c512a Mon Sep 17 00:00:00 2001 From: Daniel Larimer Date: Mon, 17 Sep 2012 23:04:42 -0400 Subject: [PATCH] adding program opts, shared mem, raw packing, varint, and more --- CMakeLists.txt | 2 + include/fc/exception.hpp | 8 +- include/fc/fwd.hpp | 2 + include/fc/fwd_impl.hpp | 15 ++ include/fc/interprocess/file_mapping.hpp | 38 +++ include/fc/pke.hpp | 9 +- include/fc/program_options.hpp | 64 +++++ include/fc/raw.hpp | 288 +++++++++++++++++++++++ include/fc/reflect.hpp | 6 +- include/fc/sha1.hpp | 2 + include/fc/stream.hpp | 80 ++++++- include/fc/utility.hpp | 3 + include/fc/varint.hpp | 30 +++ include/fc/vector.hpp | 10 +- src/exception.cpp | 9 +- src/file_mapping.cpp | 20 ++ src/log.cpp | 4 +- src/program_options.cpp | 72 ++++++ src/sha1.cpp | 10 + src/stream.cpp | 125 ++++++++++ 20 files changed, 775 insertions(+), 22 deletions(-) create mode 100644 include/fc/interprocess/file_mapping.hpp create mode 100644 include/fc/program_options.hpp create mode 100644 include/fc/raw.hpp create mode 100644 include/fc/varint.hpp create mode 100644 src/file_mapping.cpp create mode 100644 src/program_options.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 73ed568..08c7538 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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} ) diff --git a/include/fc/exception.hpp b/include/fc/exception.hpp index 6540e29..6f76d90 100644 --- a/include/fc/exception.hpp +++ b/include/fc/exception.hpp @@ -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 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(a1) ) ); + throw_exception_( func, file, line, msg, to_string(fc::forward(a1) ) ); } template 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(a1) ), to_string( fc::forward(a2) ) ); + throw_exception_( func, file, line, msg, to_string(fc::forward(a1) ), to_string( fc::forward(a2) ) ); } diff --git a/include/fc/fwd.hpp b/include/fc/fwd.hpp index c6d2911..43e15ec 100644 --- a/include/fc/fwd.hpp +++ b/include/fc/fwd.hpp @@ -12,6 +12,8 @@ template class fwd { public: template fwd( U&& u ); + template fwd( U&& u, V&& v ); + template fwd( U&& u, V&& v, X&&, Y&& ); fwd(); fwd( const fwd& f ); diff --git a/include/fc/fwd_impl.hpp b/include/fc/fwd_impl.hpp index 7979cfb..da52e63 100644 --- a/include/fc/fwd_impl.hpp +++ b/include/fc/fwd_impl.hpp @@ -64,6 +64,21 @@ namespace fc { check_size(); new (this) T( fc::forward(u) ); } + + template + template + fwd::fwd( U&& u, V&& v ) { + check_size(); + new (this) T( fc::forward(u), fc::forward(v) ); + } + template + template + fwd::fwd( U&& u, V&& v, X&& x, Y&& y ) { + check_size(); + new (this) T( fc::forward(u), fc::forward(v), fc::forward(x), fc::forward(y) ); + } + + template fwd::fwd() { check_size(); diff --git a/include/fc/interprocess/file_mapping.hpp b/include/fc/interprocess/file_mapping.hpp new file mode 100644 index 0000000..412bf09 --- /dev/null +++ b/include/fc/interprocess/file_mapping.hpp @@ -0,0 +1,38 @@ +#ifndef _FC_INTERPROCESS_FILEMAPPING_HPP_ +#define _FC_INTERPROCESS_FILEMAPPING_HPP_ +#include +#include + +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 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 my; + }; +} +#endif diff --git a/include/fc/pke.hpp b/include/fc/pke.hpp index 66147ce..912f7fc 100644 --- a/include/fc/pke.hpp +++ b/include/fc/pke.hpp @@ -107,16 +107,17 @@ namespace fc { template friend T& operator<<( T& ds, const fc::private_key& 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 friend T& operator>>( T& ds, fc::private_key& 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: diff --git a/include/fc/program_options.hpp b/include/fc/program_options.hpp new file mode 100644 index 0000000..f839d72 --- /dev/null +++ b/include/fc/program_options.hpp @@ -0,0 +1,64 @@ +#ifndef _FC_PROGRAM_OPTIONS_HPP_ +#define _FC_PROGRAM_OPTIONS_HPP_ +#include +#include +#include +#include +#include + +namespace boost { + namespace program_options { + class variables_map; + } +} + +namespace fc { + class ostream; + + namespace program_options { + template + class value { + public: + value( T* v ):_v(v){} + value& default_value( const T& d ) { _default = d; } + + T* get()const { return _v; } + private: + fc::optional _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&, const char* desc ); + options_description& operator()( const char* o, const value&, const char* desc ); + options_description& operator()( const char* o, const value >&, const char* desc ); + + private: + class impl; + fwd 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 my; + }; + + } +} +#endif // _FC_PROGRAM_OPTIONS_HPP_ diff --git a/include/fc/raw.hpp b/include/fc/raw.hpp new file mode 100644 index 0000000..0505a40 --- /dev/null +++ b/include/fc/raw.hpp @@ -0,0 +1,288 @@ +#ifndef _TORNET_RPC_RAW_HPP_ +#define _TORNET_RPC_RAW_HPP_ +#include +#include +#include +#include +//#include + +namespace fc { namespace raw { + + template + void unpack( Stream& s, fc::optional& v ); + template + void pack( Stream& s, const fc::optional& v ); + + template + void unpack( Stream& s, fc::value& ); + template + void pack( Stream& s, const fc::value& ); + + template + void unpack( Stream& s, fc::string& ); + + template + void pack( Stream& s, const fc::string& ); + + template + inline void pack( Stream& s, const T& v ); + + template + inline void unpack( Stream& s, T& v ); + + template + inline void pack( Stream& s, const fc::vector& v ); + template + inline void unpack( Stream& s, fc::vector& v ); + + template 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 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 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 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 inline void pack( Stream& s, const char* v ) { pack( s, fc::string(v) ); } + + // optional + template + inline void pack( Stream& s, const fc::optional& v ) { + pack( s, bool(!!v) ); + if( !!v ) pack( s, *v ); + } + + template + void unpack( Stream& s, fc::optional& v ) { + bool b; unpack( s, b ); + if( b ) { v = T(); unpack( s, *v ); } + } + + // fc::vector + template inline void pack( Stream& s, const fc::vector& value ) { + pack( s, unsigned_int(value.size()) ); + if( value.size() ) + s.write( &value.front(), value.size() ); + } + template inline void unpack( Stream& s, fc::vector& 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 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 inline void unpack( Stream& s, fc::string& v ) { + fc::vector tmp; unpack(s,tmp); + v = fc::string(tmp.begin(),tmp.end()); + } + + // bool + template inline void pack( Stream& s, const bool& v ) { pack( s, uint8_t(v) ); } + template inline void unpack( Stream& s, bool& v ) { uint8_t b; unpack( s, b ); v=b; } + + namespace detail { + + template + struct pack_object_visitor { + pack_object_visitor(const Class& _c, Stream& _s) + :c(_c),s(_s){} + + template + void operator()( const char* name )const { + slog( "packing %s", name ); + raw::pack( s, c.*p ); + } + private: + const Class& c; + Stream& s; + }; + + template + struct unpack_object_visitor { + unpack_object_visitor(Class& _c, Stream& _s) + :c(_c),s(_s){} + + template + inline void operator()( const char* name )const { + raw::unpack( s, c.*p ); + } + private: + Class& c; + Stream& s; + }; + + template + struct pack_sequence { + pack_sequence( Stream& _s ):s(_s){} + template + void operator() ( const T& v )const { raw::pack(s,v); } + Stream& s; + }; + + template + struct unpack_sequence { + unpack_sequence( Stream& _s ):s(_s){} + template + void operator() ( T& v )const { raw::unpack(s,v); } + Stream& s; + }; + + template + struct if_class{ + template + static inline void pack( Stream& s, const T& v ) { s << v; } + template + static inline void unpack( Stream& s, T& v ) { s >> v; } + }; + + template<> + struct if_class { + template + static inline void pack( Stream& s, const T& v ) { + s.write( (char*)&v, sizeof(v) ); + } + template + static inline void unpack( Stream& s, T& v ) { + s.read( (char*)&v, sizeof(v) ); + } + }; + + template + struct if_reflected { + template + static inline void pack( Stream& s, const T& v ) { + if_class::type>::pack(s,v); + } + template + static inline void unpack( Stream& s, T& v ) { + if_class::type>::unpack(s,v); + } + }; + template<> + struct if_reflected { + template + static inline void pack( Stream& s, const T& v ) { + fc::static_reflector::visit( pack_object_visitor( v, s ) ); + } + template + static inline void unpack( Stream& s, T& v ) { + fc::static_reflector::visit( unpack_object_visitor( v, s ) ); + } + }; + + } // namesapce detail + + + template + inline void pack( Stream& s, const fc::vector& 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 + inline void unpack( Stream& s, fc::vector& 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 + inline void pack( Stream& s, const T& v ) { + fc::raw::detail::if_reflected< typename fc::static_reflector::is_defined >::pack(s,v); + } + template + inline void unpack( Stream& s, T& v ) { + fc::raw::detail::if_reflected< typename fc::static_reflector::is_defined >::unpack(s,v); + } + + + template + inline fc::vector pack( const T& v ) { + datastream ps; + raw::pack(ps,v ); + fc::vector vec(ps.tellp()); + slog( "vec.size %d", vec.size() ); + if( vec.size() ) { + datastream ds( vec.data(), vec.size() ); + raw::pack(ds,v); + } + return vec; + } + + template + inline T unpack( const fc::vector& s ) { + T tmp; + if( s.size() ) { + datastream ds( s.data(), s.size() ); + raw::unpack(ds,tmp); + } + return tmp; + } + + template + inline void pack( char* d, uint32_t s, const T& v ) { + datastream ds(d,s); + raw::pack(ds,v ); + } + + template + inline T unpack( const char* d, uint32_t s ) { + T v; + datastream ds( d, s ); + raw::unpack(ds,v); + return v; + } + +} } // namespace fc::raw + +#endif // BOOST_RPC_RAW_HPP diff --git a/include/fc/reflect.hpp b/include/fc/reflect.hpp index 8b2ce10..b4a60e3 100644 --- a/include/fc/reflect.hpp +++ b/include/fc/reflect.hpp @@ -151,8 +151,7 @@ namespace fc { } - template - struct get_typename {}; + template class get_typename{}; template<> struct get_typename { static const char* name() { return "int32_t"; } }; template<> struct get_typename { static const char* name() { return "int64_t"; } }; template<> struct get_typename { static const char* name() { return "int16_t"; } }; @@ -164,6 +163,7 @@ namespace fc { template<> struct get_typename { static const char* name() { return "double"; } }; template<> struct get_typename { static const char* name() { return "float"; } }; template<> struct get_typename { static const char* name() { return "bool"; } }; + template<> struct get_typename { static const char* name() { return "char"; } }; template<> struct get_typename { static const char* name() { return "string"; } }; template @@ -178,7 +178,7 @@ namespace fc { v.visit( *((const T*)s) ); } - static reflector& instance() { static reflector inst; return inst; } + static reflector& instance() { static reflector inst; return inst; } }; template reflector& reflect( const T& ) { return reflector::instance(); } diff --git a/include/fc/sha1.hpp b/include/fc/sha1.hpp index e95ff15..30204b8 100644 --- a/include/fc/sha1.hpp +++ b/include/fc/sha1.hpp @@ -2,6 +2,7 @@ #define _FC_SHA1_HPP_ #include #include +#include namespace fc { @@ -59,6 +60,7 @@ namespace fc { }; } +FC_REFLECTABLE( fc::sha1 ) #endif // _FC_SHA1_HPP_ diff --git a/include/fc/stream.hpp b/include/fc/stream.hpp index 0b9955b..c4584fb 100644 --- a/include/fc/stream.hpp +++ b/include/fc/stream.hpp @@ -1,7 +1,7 @@ #ifndef _FC_STREAM_HPP_ #define _FC_STREAM_HPP_ #include -#include +#include 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 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 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 my; }; extern ostream cout; diff --git a/include/fc/utility.hpp b/include/fc/utility.hpp index 02463a5..10e7a01 100644 --- a/include/fc/utility.hpp +++ b/include/fc/utility.hpp @@ -33,6 +33,9 @@ namespace fc { template struct is_class { typedef decltype(detail::is_class_helper(0)) type; }; + template + const T& min( const T& a, const T& b ) { return a < b ? a: b; } + template void swap( T& a, T& b ) { T tmp = fc::move(a); diff --git a/include/fc/varint.hpp b/include/fc/varint.hpp new file mode 100644 index 0000000..2e98be5 --- /dev/null +++ b/include/fc/varint.hpp @@ -0,0 +1,30 @@ +#ifndef _FC_VARINT_HPP_ +#define _FC_VARINT_HPP_ +#include + +namespace fc { + +struct unsigned_int { + unsigned_int( uint32_t v = 0 ):value(v){} + + operator uint32_t()const { return value; } + + template + 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 + signed_int& operator=( const T& v ) { value = v; return *this; } + + int32_t value; +}; + +} // namespace fc + + +#endif diff --git a/include/fc/vector.hpp b/include/fc/vector.hpp index 99fe3d5..f49a31e 100644 --- a/include/fc/vector.hpp +++ b/include/fc/vector.hpp @@ -106,9 +106,13 @@ namespace fc { } void resize( uint64_t i ) { - if( capacity() < i ) - _data = detail::data::reallocate( _data, i ); - _data->size = i; + if( capacity() < i ) { + if( _data ) + _data = detail::data::reallocate( _data, i ); + else + _data = detail::data::allocate( i ); + } + if( _data ) _data->size = i; } template diff --git a/src/exception.cpp b/src/exception.cpp index 70537f1..3e931ed 100644 --- a/src/exception.cpp +++ b/src/exception.cpp @@ -2,6 +2,7 @@ #include #include #include +#include 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 ) { diff --git a/src/file_mapping.cpp b/src/file_mapping.cpp new file mode 100644 index 0000000..c55710d --- /dev/null +++ b/src/file_mapping.cpp @@ -0,0 +1,20 @@ +#include +#include +#include +#include + +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(); } +} diff --git a/src/log.cpp b/src/log.cpp index 90efa4f..a33576f 100644 --- a/src/log.cpp +++ b/src/log.cpp @@ -35,8 +35,8 @@ namespace fc { if(isatty(fileno(stderr))) std::cerr<<"\r"< +#include +#include +#include +#include + +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& v, const char* desc ){ + my->opts.add_options()( o, boost::program_options::value(reinterpret_cast(v.get())), desc ); + return *this; + } + + options_description& options_description::operator()( const char* o, const value& v, const char* desc ){ + my->opts.add_options()( o, boost::program_options::value(v.get()), desc ); + return *this; + } + + options_description& options_description::operator()( const char* o, const value >& v, const char* desc ){ + my->opts.add_options()( o, boost::program_options::value >(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; + } + +} } diff --git a/src/sha1.cpp b/src/sha1.cpp index 114b069..6bbad2a 100644 --- a/src/sha1.cpp +++ b/src/sha1.cpp @@ -84,3 +84,13 @@ namespace fc { } } // namespace fc + +namespace fc { + const char* reflector::name()const { return "sha1"; } + void reflector::visit( void* s, const abstract_visitor& v )const { + } + void reflector::visit( const void* s, const abstract_const_visitor& v )const { + v.visit( fc::string( *((const sha1*)s)) ); + } + reflector& reflector::instance() { static reflector inst; return inst; } +} // namespace fc diff --git a/src/stream.cpp b/src/stream.cpp index 7da9d0d..73710bc 100644 --- a/src/stream.cpp +++ b/src/stream.cpp @@ -2,6 +2,9 @@ #include #include #include +#include +#include +#include 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(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(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 +