From 1201d1c1f6044a473c9beb3d6d86a0f0e7aa7b9c Mon Sep 17 00:00:00 2001 From: Daniel Larimer Date: Mon, 22 Jul 2013 14:03:34 -0400 Subject: [PATCH] adding time_point_sec --- include/fc/io/raw.hpp | 175 +++------------------------------- include/fc/io/raw_fwd.hpp | 24 ++++- include/fc/io/raw_variant.hpp | 147 ++++++++++++++++++++++++++++ include/fc/time.hpp | 26 ++++- include/fc/variant.hpp | 4 + src/time.cpp | 6 ++ 6 files changed, 217 insertions(+), 165 deletions(-) create mode 100644 include/fc/io/raw_variant.hpp diff --git a/include/fc/io/raw.hpp b/include/fc/io/raw.hpp index 2f6375c..e187ff8 100644 --- a/include/fc/io/raw.hpp +++ b/include/fc/io/raw.hpp @@ -3,41 +3,31 @@ #include #include #include -#include #include #include #include #include -#include -#include #include -#include -#include -#include #define MAX_ARRAY_ALLOC_SIZE (1024*1024*10) namespace fc { namespace raw { - template - inline void pack( Stream& s, const std::set& value ); - template - inline void unpack( Stream& s, std::set& value ); - template - inline void pack( Stream& s, const std::unordered_set& value ); - template - inline void unpack( Stream& s, std::unordered_set& value ); + template + inline void pack( Stream& s, const fc::time_point_sec& tp ) + { + uint32_t usec = tp.sec_since_epoch(); + s.write( (const char*)&usec, sizeof(usec) ); + } - template - inline void pack( Stream& s, const variant_object& v ); - template - inline void unpack( Stream& s, variant_object& v ); - - template - inline void pack( Stream& s, const variant& v ); - template - inline void unpack( Stream& s, variant& v ); + template + inline void unpack( Stream& s, fc::time_point_sec& tp ) + { + uint32_t sec; + s.read( (char*)&usec, sizeof(sec) ); + tp = fc::time_point() + fc::seconds(sec); + } template inline void pack( Stream& s, const fc::time_point& tp ) @@ -307,145 +297,6 @@ namespace fc { fc::raw::detail::if_reflected< typename fc::reflector::is_defined >::unpack(s,v); } - template - class variant_packer : public variant::visitor - { - public: - variant_packer( Stream& _s ):s(_s){} - virtual void handle()const { } - virtual void handle( const int64_t& v )const - { - fc::raw::pack( s, v ); - } - virtual void handle( const uint64_t& v )const - { - fc::raw::pack( s, v ); - } - virtual void handle( const double& v )const - { - fc::raw::pack( s, v ); - } - virtual void handle( const bool& v )const - { - fc::raw::pack( s, v ); - } - virtual void handle( const string& v )const - { - fc::raw::pack( s, v ); - } - virtual void handle( const variant_object& v)const - { - fc::raw::pack( s, v ); - } - virtual void handle( const variants& v)const - { - fc::raw::pack( s, v ); - } - - Stream& s; - - }; - - - template - inline void pack( Stream& s, const variant& v ) - { - pack( s, uint8_t(v.get_type()) ); - v.visit( variant_packer(s) ); - } - template - inline void unpack( Stream& s, variant& v ) - { - uint8_t t; - unpack( s, t ); - switch( t ) - { - case variant::null_type: - return; - case variant::int64_type: - { - int64_t val; - raw::unpack(s,val); - v = val; - return; - } - case variant::uint64_type: - { - uint64_t val; - raw::unpack(s,val); - v = val; - return; - } - case variant::double_type: - { - double val; - raw::unpack(s,val); - v = val; - return; - } - case variant::bool_type: - { - bool val; - raw::unpack(s,val); - v = val; - return; - } - case variant::string_type: - { - fc::string val; - raw::unpack(s,val); - v = fc::move(val); - return; - } - case variant::array_type: - { - variants val; - raw::unpack(s,val); - v = fc::move(val); - return; - } - case variant::object_type: - { - variant_object val; - raw::unpack(s,val); - v = fc::move(val); - return; - } - default: - FC_THROW_EXCEPTION( parse_error_exception, "Unknown Variant Type ${t}", ("t", t) ); - } - } - - template - inline void pack( Stream& s, const variant_object& v ) - { - unsigned_int vs = v.size(); - pack( s, vs ); - for( auto itr = v.begin(); itr != v.end(); ++itr ) - { - pack( s, itr->key() ); - pack( s, itr->value() ); - } - wlog( "------------ done pack -------------" ); - } - template - inline void unpack( Stream& s, variant_object& v ) - { - unsigned_int vs; - unpack( s, vs ); - - mutable_variant_object mvo; - mvo.reserve(vs.value); - for( auto i = 0; i < vs.value; ++i ) - { - fc::string key; - fc::variant value; - fc::raw::unpack(s,key); - fc::raw::unpack(s,value); - mvo.set( fc::move(key), fc::move(value) ); - } - v = fc::move(mvo); - } template inline std::vector pack( const T& v ) { diff --git a/include/fc/io/raw_fwd.hpp b/include/fc/io/raw_fwd.hpp index ce3159a..e11a98a 100644 --- a/include/fc/io/raw_fwd.hpp +++ b/include/fc/io/raw_fwd.hpp @@ -3,15 +3,35 @@ #include #include #include +#include +#include namespace fc { + class time_point; + class time_point_sec; + class variant; + class variant_object; + namespace ecc { class public_key; class private_key; } namespace raw { + + template inline void pack( Stream& s, const std::set& value ); + template inline void unpack( Stream& s, std::set& value ); + template inline void pack( Stream& s, const std::unordered_set& value ); + template inline void unpack( Stream& s, std::unordered_set& value ); + template inline void pack( Stream& s, const variant_object& v ); + template inline void unpack( Stream& s, variant_object& v ); + template inline void pack( Stream& s, const variant& v ); + template inline void unpack( Stream& s, variant& v ); + + template void unpack( Stream& s, fc::optional& v ); template void pack( Stream& s, const fc::optional& v ); - template void unpack( Stream& s, fc::string& ); - template void pack( Stream& s, const fc::string& ); + template void unpack( Stream& s, time_point& ); + template void pack( Stream& s, const time_point& ); + template void unpack( Stream& s, time_point_sec& ); + template void pack( Stream& s, const time_point_sec& ); template void unpack( Stream& s, std::string& ); template void pack( Stream& s, const std::string& ); template void unpack( Stream& s, fc::ecc::public_key& ); diff --git a/include/fc/io/raw_variant.hpp b/include/fc/io/raw_variant.hpp new file mode 100644 index 0000000..8e3630b --- /dev/null +++ b/include/fc/io/raw_variant.hpp @@ -0,0 +1,147 @@ +#pragma once +#include +#include +#include + +namespace fc { namespace raw { + + template + class variant_packer : public variant::visitor + { + public: + variant_packer( Stream& _s ):s(_s){} + virtual void handle()const { } + virtual void handle( const int64_t& v )const + { + fc::raw::pack( s, v ); + } + virtual void handle( const uint64_t& v )const + { + fc::raw::pack( s, v ); + } + virtual void handle( const double& v )const + { + fc::raw::pack( s, v ); + } + virtual void handle( const bool& v )const + { + fc::raw::pack( s, v ); + } + virtual void handle( const string& v )const + { + fc::raw::pack( s, v ); + } + virtual void handle( const variant_object& v)const + { + fc::raw::pack( s, v ); + } + virtual void handle( const variants& v)const + { + fc::raw::pack( s, v ); + } + + Stream& s; + + }; + + + template + inline void pack( Stream& s, const variant& v ) + { + pack( s, uint8_t(v.get_type()) ); + v.visit( variant_packer(s) ); + } + template + inline void unpack( Stream& s, variant& v ) + { + uint8_t t; + unpack( s, t ); + switch( t ) + { + case variant::null_type: + return; + case variant::int64_type: + { + int64_t val; + raw::unpack(s,val); + v = val; + return; + } + case variant::uint64_type: + { + uint64_t val; + raw::unpack(s,val); + v = val; + return; + } + case variant::double_type: + { + double val; + raw::unpack(s,val); + v = val; + return; + } + case variant::bool_type: + { + bool val; + raw::unpack(s,val); + v = val; + return; + } + case variant::string_type: + { + fc::string val; + raw::unpack(s,val); + v = fc::move(val); + return; + } + case variant::array_type: + { + variants val; + raw::unpack(s,val); + v = fc::move(val); + return; + } + case variant::object_type: + { + variant_object val; + raw::unpack(s,val); + v = fc::move(val); + return; + } + default: + FC_THROW_EXCEPTION( parse_error_exception, "Unknown Variant Type ${t}", ("t", t) ); + } + } + + template + inline void pack( Stream& s, const variant_object& v ) + { + unsigned_int vs = v.size(); + pack( s, vs ); + for( auto itr = v.begin(); itr != v.end(); ++itr ) + { + pack( s, itr->key() ); + pack( s, itr->value() ); + } + } + template + inline void unpack( Stream& s, variant_object& v ) + { + unsigned_int vs; + unpack( s, vs ); + + mutable_variant_object mvo; + mvo.reserve(vs.value); + for( auto i = 0; i < vs.value; ++i ) + { + fc::string key; + fc::variant value; + fc::raw::unpack(s,key); + fc::raw::unpack(s,value); + mvo.set( fc::move(key), fc::move(value) ); + } + v = fc::move(mvo); + } + +} } // fc::raw diff --git a/include/fc/time.hpp b/include/fc/time.hpp index 653e1c0..06c5970 100644 --- a/include/fc/time.hpp +++ b/include/fc/time.hpp @@ -46,10 +46,34 @@ namespace fc { time_point& operator += ( const microseconds& m ) { elapsed+=m; return *this; } friend time_point operator + ( const time_point& t, const microseconds& m ) { return time_point(t.elapsed+m); } friend time_point operator - ( const time_point& t, const microseconds& m ) { return time_point(t.elapsed-m); } - friend microseconds operator - ( const time_point& t, const time_point& m ) { return microseconds(t.elapsed.count() - m.elapsed.count()); } + friend microseconds operator - ( const time_point& t, const time_point& m ) { return microseconds(t.elapsed.count() - m.elapsed.count()); } private: microseconds elapsed; }; + /** + * A lower resolution time_point accurate only to seconds from 1970 + */ + class time_point_sec + { + public: + time_point_sec() + :utc_seconds(0){} + + time_point_sec( const time_point& t = time_point() ) + :utc_seconds( t.time_since_epoch().count() / 1000000ll ){} + + operator time_point()const { return time_point( fc::seconds( utc_seconds) ); } + + time_point_sec operator = ( const fc::time_point& t ) + { + utc_seconds = t.time_since_epoch().count() / 1000000ll; + return *this; + } + + private: + uint32_t utc_seconds; + }; + typedef fc::optional otime_point; } diff --git a/include/fc/variant.hpp b/include/fc/variant.hpp index 766f14b..bf4ba0c 100644 --- a/include/fc/variant.hpp +++ b/include/fc/variant.hpp @@ -26,6 +26,7 @@ namespace fc class variant_object; class mutable_variant_object; class time_point; + class time_point_sec; void to_variant( const uint16_t& var, variant& vo ); void from_variant( const variant& var, uint16_t& vo ); @@ -52,6 +53,9 @@ namespace fc void to_variant( const time_point& var, variant& vo ); void from_variant( const variant& var, time_point& vo ); + + void to_variant( const time_point_sec& var, variant& vo ); + void from_variant( const variant& var, time_point_sec& vo ); #ifdef __APPLE__ void to_variant( size_t s, variant& v ); #endif diff --git a/src/time.cpp b/src/time.cpp index abf93bc..88c7b46 100644 --- a/src/time.cpp +++ b/src/time.cpp @@ -27,4 +27,10 @@ namespace fc { void from_variant( const fc::variant& v, fc::time_point& t ) { t = fc::time_point::from_iso_string(v.as_string()); } + void to_variant( const fc::time_point_sec& t, variant& v ) { + v = fc::string(fc::time_point(t)); + } + void from_variant( const fc::variant& v, fc::time_point_sec& t ) { + t = fc::time_point::from_iso_string(v.as_string()); + } }