adding time_point_sec
This commit is contained in:
parent
72a1c45905
commit
1201d1c1f6
6 changed files with 217 additions and 165 deletions
|
|
@ -3,41 +3,31 @@
|
|||
#include <fc/io/datastream.hpp>
|
||||
#include <fc/io/varint.hpp>
|
||||
#include <fc/optional.hpp>
|
||||
#include <fc/vector.hpp>
|
||||
#include <fc/fwd.hpp>
|
||||
#include <fc/array.hpp>
|
||||
#include <fc/time.hpp>
|
||||
#include <fc/io/raw_fwd.hpp>
|
||||
#include <fc/variant_object.hpp>
|
||||
#include <fc/variant.hpp>
|
||||
#include <fc/exception/exception.hpp>
|
||||
#include <fc/log/logger.hpp>
|
||||
#include <set>
|
||||
#include <unordered_set>
|
||||
|
||||
#define MAX_ARRAY_ALLOC_SIZE (1024*1024*10)
|
||||
|
||||
namespace fc {
|
||||
namespace raw {
|
||||
|
||||
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 );
|
||||
template<typename Stream, typename T>
|
||||
inline void unpack( Stream& s, std::unordered_set<T>& value );
|
||||
template<typename Stream>
|
||||
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<typename Stream>
|
||||
inline void pack( Stream& s, const variant_object& v );
|
||||
template<typename Stream>
|
||||
inline void unpack( Stream& s, variant_object& v );
|
||||
|
||||
template<typename Stream>
|
||||
inline void pack( Stream& s, const variant& v );
|
||||
template<typename Stream>
|
||||
inline void unpack( Stream& s, variant& v );
|
||||
template<typename Stream>
|
||||
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<typename Stream>
|
||||
inline void pack( Stream& s, const fc::time_point& tp )
|
||||
|
|
@ -307,145 +297,6 @@ namespace fc {
|
|||
fc::raw::detail::if_reflected< typename fc::reflector<T>::is_defined >::unpack(s,v);
|
||||
}
|
||||
|
||||
template<typename Stream>
|
||||
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<typename Stream>
|
||||
inline void pack( Stream& s, const variant& v )
|
||||
{
|
||||
pack( s, uint8_t(v.get_type()) );
|
||||
v.visit( variant_packer<Stream>(s) );
|
||||
}
|
||||
template<typename Stream>
|
||||
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<typename Stream>
|
||||
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<typename Stream>
|
||||
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<typename T>
|
||||
inline std::vector<char> pack( const T& v ) {
|
||||
|
|
|
|||
|
|
@ -3,15 +3,35 @@
|
|||
#include <fc/array.hpp>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <unordered_set>
|
||||
#include <set>
|
||||
|
||||
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<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 );
|
||||
template<typename Stream, typename T> inline void unpack( Stream& s, std::unordered_set<T>& value );
|
||||
template<typename Stream> inline void pack( Stream& s, const variant_object& v );
|
||||
template<typename Stream> inline void unpack( Stream& s, variant_object& v );
|
||||
template<typename Stream> inline void pack( Stream& s, const variant& v );
|
||||
template<typename Stream> inline void unpack( Stream& s, variant& v );
|
||||
|
||||
|
||||
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::string& );
|
||||
template<typename Stream> void pack( Stream& s, const fc::string& );
|
||||
template<typename Stream> void unpack( Stream& s, time_point& );
|
||||
template<typename Stream> void pack( Stream& s, const time_point& );
|
||||
template<typename Stream> void unpack( Stream& s, time_point_sec& );
|
||||
template<typename Stream> void pack( Stream& s, const time_point_sec& );
|
||||
template<typename Stream> void unpack( Stream& s, std::string& );
|
||||
template<typename Stream> void pack( Stream& s, const std::string& );
|
||||
template<typename Stream> void unpack( Stream& s, fc::ecc::public_key& );
|
||||
|
|
|
|||
147
include/fc/io/raw_variant.hpp
Normal file
147
include/fc/io/raw_variant.hpp
Normal file
|
|
@ -0,0 +1,147 @@
|
|||
#pragma once
|
||||
#include <fc/io/raw_fwd.hpp>
|
||||
#include <fc/variant_object.hpp>
|
||||
#include <fc/variant.hpp>
|
||||
|
||||
namespace fc { namespace raw {
|
||||
|
||||
template<typename Stream>
|
||||
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<typename Stream>
|
||||
inline void pack( Stream& s, const variant& v )
|
||||
{
|
||||
pack( s, uint8_t(v.get_type()) );
|
||||
v.visit( variant_packer<Stream>(s) );
|
||||
}
|
||||
template<typename Stream>
|
||||
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<typename Stream>
|
||||
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<typename Stream>
|
||||
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
|
||||
|
|
@ -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<time_point> otime_point;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue