From cde67a202c573b2025459d5b27bf564fe1c82cf0 Mon Sep 17 00:00:00 2001 From: Vikram Rajkumar Date: Fri, 16 May 2014 22:29:46 -0400 Subject: [PATCH 1/2] Implement assignment by subtraction for time classes --- include/fc/time.hpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/include/fc/time.hpp b/include/fc/time.hpp index 6a6146d..204d586 100644 --- a/include/fc/time.hpp +++ b/include/fc/time.hpp @@ -9,7 +9,7 @@ #endif //// _MSC_VER namespace fc { - class microseconds { + class microseconds { public: explicit microseconds( int64_t c = 0) :_count(c){} static microseconds maximum() { return microseconds(0x7fffffffffffffffll); } @@ -23,10 +23,11 @@ namespace fc { friend bool operator<(const microseconds& a, const microseconds& b){ return a._count < b._count; } friend bool operator<=(const microseconds& a, const microseconds& b){ return a._count <= b._count; } microseconds& operator+=(const microseconds& c) { _count += c._count; return *this; } + microseconds& operator-=(const microseconds& c) { _count -= c._count; return *this; } int64_t count()const { return _count; } private: friend class time_point; - int64_t _count; + int64_t _count; }; inline microseconds seconds( int64_t s ) { return microseconds( s * 1000000 ); } inline microseconds milliseconds( int64_t s ) { return microseconds( s * 1000 ); } @@ -34,14 +35,14 @@ namespace fc { inline microseconds hours(int64_t h) { return minutes(60*h); } inline microseconds days(int64_t d) { return hours(24*d); } - class time_point { + class time_point { public: explicit time_point( microseconds e = microseconds() ) :elapsed(e){} static time_point now(); static time_point maximum() { return time_point( microseconds::maximum() ); } static time_point min() { return time_point(); } operator fc::string()const; - + static time_point from_iso_string( const fc::string& s ); const microseconds& time_since_epoch()const { return elapsed; } @@ -53,17 +54,18 @@ namespace fc { bool operator ==( const time_point& t )const { return elapsed._count ==t.elapsed._count; } bool operator !=( const time_point& t )const { return elapsed._count !=t.elapsed._count; } time_point& operator += ( const microseconds& m) { elapsed+=m; return *this; } + time_point& operator -= ( const microseconds& m) { elapsed-=m; return *this; } time_point operator + (const microseconds& m) const { return time_point(elapsed+m); } time_point operator - (const microseconds& m) const { return time_point(elapsed-m); } microseconds operator - (const time_point& m) const { return microseconds(elapsed.count() - m.elapsed.count()); } private: - microseconds elapsed; + microseconds elapsed; }; /** * A lower resolution time_point accurate only to seconds from 1970 */ - class time_point_sec + class time_point_sec { public: time_point_sec() @@ -90,6 +92,7 @@ namespace fc { friend bool operator == ( const time_point_sec& a, const time_point_sec& b ) { return a.utc_seconds == b.utc_seconds; } friend bool operator != ( const time_point_sec& a, const time_point_sec& b ) { return a.utc_seconds != b.utc_seconds; } time_point_sec& operator += ( uint32_t m ) { utc_seconds+=m; return *this; } + time_point_sec& operator -= ( uint32_t m ) { utc_seconds-=m; return *this; } friend time_point operator - ( const time_point_sec& t, const microseconds& m ) { return time_point(t) - m; } friend microseconds operator - ( const time_point_sec& t, const time_point_sec& m ) { return time_point(t) - time_point(m); } From e8326ca66c096714b8bd1361e3305dc3b0b2090d Mon Sep 17 00:00:00 2001 From: Vikram Rajkumar Date: Sat, 17 May 2014 03:00:07 -0400 Subject: [PATCH 2/2] Make objectFromStream always throw parse_error_exception when throwing --- src/io/json.cpp | 61 +++++++++++++++++++++++++------------------------ 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/src/io/json.cpp b/src/io/json.cpp index e17729e..641164b 100644 --- a/src/io/json.cpp +++ b/src/io/json.cpp @@ -43,9 +43,8 @@ namespace fc FC_THROW_EXCEPTION( parse_error_exception, "Expected '\\'" ); } - template - void skip_white_space( T& in ) + void skip_white_space( T& in ) { while( true ) { @@ -67,18 +66,18 @@ namespace fc fc::string stringFromStream( T& in ) { fc::stringstream token; - try + try { char c = in.peek(); if( c != '"' ) - FC_THROW_EXCEPTION( parse_error_exception, - "Expected '\"' but read '${char}'", + FC_THROW_EXCEPTION( parse_error_exception, + "Expected '\"' but read '${char}'", ("char", string(&c, (&c) + 1) ) ); in.get(); while( true ) { - + switch( c = in.peek() ) { case '\\': @@ -94,14 +93,14 @@ namespace fc } FC_THROW_EXCEPTION( parse_error_exception, "EOF before closing '\"' in string '${token}'", ("token", token.str() ) ); - } FC_RETHROW_EXCEPTIONS( warn, "while parsing token '${token}'", + } FC_RETHROW_EXCEPTIONS( warn, "while parsing token '${token}'", ("token", token.str() ) ); } template fc::string stringFromToken( T& in ) { fc::stringstream token; - try + try { char c = in.peek(); @@ -128,12 +127,12 @@ namespace fc } } return token.str(); - } + } catch( const fc::eof_exception& eof ) { return token.str(); } - FC_RETHROW_EXCEPTIONS( warn, "while parsing token '${token}'", + FC_RETHROW_EXCEPTIONS( warn, "while parsing token '${token}'", ("token", token.str() ) ); } @@ -143,10 +142,10 @@ namespace fc mutable_variant_object obj; try { - char c = in.peek(); + char c = in.peek(); if( c != '{' ) - FC_THROW_EXCEPTION( parse_error_exception, - "Expected '{', but read '${char}'", + FC_THROW_EXCEPTION( parse_error_exception, + "Expected '{', but read '${char}'", ("char",string(&c, &c + 1)) ); in.get(); skip_white_space(in); @@ -176,8 +175,10 @@ namespace fc return obj; } FC_THROW_EXCEPTION( parse_error_exception, "Expected '}' after ${variant}", ("variant", obj ) ); - - + } + catch( const fc::eof_exception& e ) + { + FC_THROW_EXCEPTION( parse_error_exception, "Unexpected EOF: ${e}", ("e", e.to_detail_string() ) ); } FC_RETHROW_EXCEPTIONS( warn, "Error parsing object" ); } @@ -194,7 +195,7 @@ namespace fc while( in.peek() != ']' ) { - while( in.peek() == ',' ) + while( in.peek() == ',' ) in.get(); ar.push_back( variant_from_stream(in) ); skip_white_space(in); @@ -204,7 +205,7 @@ namespace fc ("variant", ar) ); in.get(); - } FC_RETHROW_EXCEPTIONS( warn, "Attempting to parse array ${array}", + } FC_RETHROW_EXCEPTIONS( warn, "Attempting to parse array ${array}", ("array", ar ) ); return ar; } @@ -228,11 +229,11 @@ namespace fc char c; while((c = in.peek()) && !done) { - + switch( c ) { case '.': - if (dot) + if (dot) FC_THROW_EXCEPTION(parse_error_exception, "Can't parse a number with two decimal places"); dot = true; case '0': @@ -265,7 +266,7 @@ namespace fc FC_THROW_EXCEPTION(parse_error_exception, "Can't parse token \"${token}\" as a JSON numeric constant", ("token", str)); if( dot ) return to_double(str); - if( neg ) + if( neg ) return to_int64(str); return to_uint64(str); } @@ -309,7 +310,7 @@ namespace fc if( str == "null" ) return variant(); if( str == "true" ) return true; if( str == "false" ) return false; - else + else { if (received_eof) FC_THROW_EXCEPTION( parse_error_exception, "Unexpected EOF" ); @@ -372,7 +373,7 @@ namespace fc default: // ilog( "unhandled char '${c}' int ${int}", ("c", fc::string( &c, 1 ) )("int", int(c)) ); return stringFromToken(in); - in.get(); // + in.get(); // ilog( "unhandled char '${c}' int ${int}", ("c", fc::string( &c, 1 ) )("int", int(c)) ); return variant(); } @@ -395,7 +396,7 @@ namespace fc void toUTF8( const wchar_t c, ostream& os ) { - utf8::utf16to8( &c, (&c)+1, ostream_iterator(os) ); + utf8::utf16to8( &c, (&c)+1, ostream_iterator(os) ); } */ @@ -462,7 +463,7 @@ namespace fc { os << '{'; auto itr = o.begin(); - + while( itr != o.end() ) { escape_string( itr->key(), os ); @@ -480,13 +481,13 @@ namespace fc { switch( v.get_type() ) { - case variant::null_type: + case variant::null_type: os << "null"; return; - case variant::int64_type: + case variant::int64_type: os << v.as_int64(); return; - case variant::uint64_type: + case variant::uint64_type: os << v.as_uint64(); return; case variant::double_type: @@ -531,7 +532,7 @@ namespace fc switch( v[i] ) { case '\\': if( !escape ) { - if( quote ) + if( quote ) escape = true; } else { escape = false; } ss<( p, ifstream::binary ); //auto tmp = std::make_shared( p.generic_string().c_str(), std::ios::binary ); - //buffered_istream bi( tmp ); + //buffered_istream bi( tmp ); std::ifstream bi( p.generic_string().c_str(), std::ios::binary ); return variant_from_stream( bi ); }