variant.cpp 26.2 KB
Newer Older
D
Daniel Larimer 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
#include <fc/variant.hpp>
#include <fc/variant_object.hpp>
#include <fc/exception/exception.hpp>
#include <string.h>
#include <fc/crypto/base64.hpp>
#include <fc/crypto/hex.hpp>
#include <boost/scoped_array.hpp>
#include <fc/reflect/variant.hpp>
#include <fc/io/json.hpp>
#include <algorithm>

namespace fc
{
/**
 *  The TypeID is stored in the 'last byte' of the variant.
 */
void set_variant_type( variant* v, variant::type_id t)
{
   char* data = reinterpret_cast<char*>(v);
   data[ sizeof(variant) -1 ] = t;
}

variant::variant()
{
   set_variant_type( this, null_type );
}

variant::variant( fc::nullptr_t )
{
   set_variant_type( this, null_type );
}

variant::variant( uint8_t val )
{
   *reinterpret_cast<uint64_t*>(this)  = val;
   set_variant_type( this, uint64_type );
}

variant::variant( int8_t val )
{
   *reinterpret_cast<int64_t*>(this)  = val;
   set_variant_type( this, int64_type );
}

variant::variant( uint16_t val )
{
   *reinterpret_cast<uint64_t*>(this)  = val;
   set_variant_type( this, uint64_type );
}

variant::variant( int16_t val )
{
   *reinterpret_cast<int64_t*>(this)  = val;
   set_variant_type( this, int64_type );
}

variant::variant( uint32_t val )
{
   *reinterpret_cast<uint64_t*>(this)  = val;
   set_variant_type( this, uint64_type );
}

variant::variant( int32_t val )
{
   *reinterpret_cast<int64_t*>(this)  = val;
   set_variant_type( this, int64_type );
}

variant::variant( uint64_t val )
{
   *reinterpret_cast<uint64_t*>(this)  = val;
   set_variant_type( this, uint64_type );
}

variant::variant( int64_t val )
{
   *reinterpret_cast<int64_t*>(this)  = val;
   set_variant_type( this, int64_type );
}

variant::variant( float val )
{
   *reinterpret_cast<double*>(this)  = val;
   set_variant_type( this, double_type );
}

variant::variant( double val )
{
   *reinterpret_cast<double*>(this)  = val;
   set_variant_type( this, double_type );
}

variant::variant( bool val )
{
   *reinterpret_cast<bool*>(this)  = val;
   set_variant_type( this, bool_type );
}

variant::variant( char* str )
{
   *reinterpret_cast<string**>(this)  = new string( str );
   set_variant_type( this, string_type );
}

variant::variant( const char* str )
{
   *reinterpret_cast<string**>(this)  = new string( str );
   set_variant_type( this, string_type );
}

// TODO: do a proper conversion to utf8
variant::variant( wchar_t* str )
{
   size_t len = wcslen(str);
   boost::scoped_array<char> buffer(new char[len]);
   for (unsigned i = 0; i < len; ++i)
     buffer[i] = (char)str[i];
   *reinterpret_cast<string**>(this)  = new string(buffer.get(), len);
   set_variant_type( this, string_type );
}

// TODO: do a proper conversion to utf8
variant::variant( const wchar_t* str )
{
   size_t len = wcslen(str);
   boost::scoped_array<char> buffer(new char[len]);
   for (unsigned i = 0; i < len; ++i)
     buffer[i] = (char)str[i];
   *reinterpret_cast<string**>(this)  = new string(buffer.get(), len);
   set_variant_type( this, string_type );
}

variant::variant( fc::string val )
{
   *reinterpret_cast<string**>(this)  = new string( fc::move(val) );
   set_variant_type( this, string_type );
}
variant::variant( blob val )
{
   *reinterpret_cast<blob**>(this)  = new blob( fc::move(val) );
   set_variant_type( this, blob_type );
}

variant::variant( variant_object obj)
{
   *reinterpret_cast<variant_object**>(this)  = new variant_object(fc::move(obj));
   set_variant_type(this,  object_type );
}
variant::variant( mutable_variant_object obj)
{
   *reinterpret_cast<variant_object**>(this)  = new variant_object(fc::move(obj));
   set_variant_type(this,  object_type );
}

variant::variant( variants arr )
{
   *reinterpret_cast<variants**>(this)  = new variants(fc::move(arr));
   set_variant_type(this,  array_type );
}


typedef const variant_object* const_variant_object_ptr; 
typedef const variants* const_variants_ptr; 
typedef const blob*   const_blob_ptr; 
typedef const string* const_string_ptr;

void variant::clear()
{
   switch( get_type() )
   {
     case object_type:
        delete *reinterpret_cast<variant_object**>(this);
        break;
     case array_type:
        delete *reinterpret_cast<variants**>(this);
        break;
     case string_type:
        delete *reinterpret_cast<string**>(this);
        break;
     default:
        break;
   }
   set_variant_type( this, null_type );
}

variant::variant( const variant& v )
{
   switch( v.get_type() )
   {
       case object_type:
          *reinterpret_cast<variant_object**>(this)  = 
             new variant_object(**reinterpret_cast<const const_variant_object_ptr*>(&v));
          set_variant_type( this, object_type );
          return;
       case array_type:
          *reinterpret_cast<variants**>(this)  = 
             new variants(**reinterpret_cast<const const_variants_ptr*>(&v));
          set_variant_type( this,  array_type );
          return;
       case string_type:
          *reinterpret_cast<string**>(this)  = 
             new string(**reinterpret_cast<const const_string_ptr*>(&v) );
          set_variant_type( this, string_type );
          return;
       default:
          memcpy( this, &v, sizeof(v) );
   }
}

variant::variant( variant&& v )
{
   memcpy( this, &v, sizeof(v) );
   set_variant_type( &v, null_type );
}

variant::~variant()
{
   clear();
}

variant& variant::operator=( variant&& v )
{
   if( this == &v ) return *this;
   clear();
   memcpy( (char*)this, (char*)&v, sizeof(v) );
   set_variant_type( &v, null_type ); 
   return *this;
}

variant& variant::operator=( const variant& v )
{
   if( this == &v ) 
      return *this;

   clear();
   switch( v.get_type() )
   {
      case object_type:
         *reinterpret_cast<variant_object**>(this)  = 
            new variant_object((**reinterpret_cast<const const_variant_object_ptr*>(&v)));
         break;
      case array_type:
         *reinterpret_cast<variants**>(this)  = 
            new variants((**reinterpret_cast<const const_variants_ptr*>(&v)));
         break;
      case string_type:
         *reinterpret_cast<string**>(this)  = new string((**reinterpret_cast<const const_string_ptr*>(&v)) );
         break;

      default:
         memcpy( this, &v, sizeof(v) );
   }
   set_variant_type( this, v.get_type() );
   return *this;
}

void  variant::visit( const visitor& v )const
{
   switch( get_type() )
   {
      case null_type:
         v.handle();
         return;
      case int64_type:
         v.handle( *reinterpret_cast<const int64_t*>(this) );
         return;
      case uint64_type:
         v.handle( *reinterpret_cast<const uint64_t*>(this) );
         return;
      case double_type:
         v.handle( *reinterpret_cast<const double*>(this) );
         return;
      case bool_type:
         v.handle( *reinterpret_cast<const bool*>(this) );
         return;
      case string_type:
         v.handle( **reinterpret_cast<const const_string_ptr*>(this) );
         return;
      case array_type:
         v.handle( **reinterpret_cast<const const_variants_ptr*>(this) );
         return;
      case object_type:
         v.handle( **reinterpret_cast<const const_variant_object_ptr*>(this) );
         return;
      default:
         FC_THROW_EXCEPTION( assert_exception, "Invalid Type / Corrupted Memory" );
   }
}

variant::type_id variant::get_type()const
{
   return (type_id)reinterpret_cast<const char*>(this)[sizeof(*this)-1];
}

bool variant::is_null()const
{
   return get_type() == null_type;
}

bool variant::is_string()const
{
   return get_type() == string_type;
}
bool variant::is_bool()const
{
   return get_type() == bool_type;
}
bool variant::is_double()const
{
   return get_type() == double_type;
}
bool variant::is_uint64()const
{
   return get_type() == uint64_type;
}
bool variant::is_int64()const
{
   return get_type() == int64_type;
}

bool variant::is_integer()const
{
   switch( get_type() )
   {
      case int64_type:
      case uint64_type:
      case bool_type:
         return true;
      default:
         return false;
   }
   return false;
}
bool variant::is_numeric()const
{
   switch( get_type() )
   {
      case int64_type:
      case uint64_type:
      case double_type:
      case bool_type:
         return true;
      default:
         return false;
   }
   return false;
}

bool variant::is_object()const
{
   return get_type() == object_type;
}

bool variant::is_array()const
{
   return get_type() == array_type;
}
bool variant::is_blob()const
{
   return get_type() == blob_type;
}

int64_t variant::as_int64()const
{
   switch( get_type() )
   {
      case string_type:
          return to_int64(**reinterpret_cast<const const_string_ptr*>(this)); 
      case double_type:
          return int64_t(*reinterpret_cast<const double*>(this));
      case int64_type:
          return *reinterpret_cast<const int64_t*>(this);
      case uint64_type:
          return int64_t(*reinterpret_cast<const uint64_t*>(this));
      case bool_type:
          return *reinterpret_cast<const bool*>(this);
      case null_type:
          return 0;
      default:
         FC_THROW_EXCEPTION( bad_cast_exception, "Invalid cast from ${type} to int64", ("type", get_type()) );
   }
}

uint64_t variant::as_uint64()const
{ try {
   switch( get_type() )
   {
      case string_type:
          return to_uint64(**reinterpret_cast<const const_string_ptr*>(this)); 
      case double_type:
          return static_cast<uint64_t>(*reinterpret_cast<const double*>(this));
      case int64_type:
          return static_cast<uint64_t>(*reinterpret_cast<const int64_t*>(this));
      case uint64_type:
          return *reinterpret_cast<const uint64_t*>(this);
      case bool_type:
          return static_cast<uint64_t>(*reinterpret_cast<const bool*>(this));
      case null_type:
          return 0;
      default:
         FC_THROW_EXCEPTION( bad_cast_exception,"Invalid cast from ${type} to uint64", ("type",get_type()));
   }
} FC_CAPTURE_AND_RETHROW( (*this) ) }


double  variant::as_double()const
{
   switch( get_type() )
   {
      case string_type:
          return to_double(**reinterpret_cast<const const_string_ptr*>(this)); 
      case double_type:
          return *reinterpret_cast<const double*>(this);
      case int64_type:
          return static_cast<double>(*reinterpret_cast<const int64_t*>(this));
      case uint64_type:
          return static_cast<double>(*reinterpret_cast<const uint64_t*>(this));
      case bool_type:
          return *reinterpret_cast<const bool*>(this);
      case null_type:
          return 0;
      default:
         FC_THROW_EXCEPTION( bad_cast_exception, "Invalid cast from ${type} to double", ("type",get_type()) );
   }
}

bool  variant::as_bool()const
{
   switch( get_type() )
   {
      case string_type:
      {
          const string& s = **reinterpret_cast<const const_string_ptr*>(this);
          if( s == "true" )
             return true;
          if( s == "false" )
             return false;
          FC_THROW_EXCEPTION( bad_cast_exception, "Cannot convert string to bool (only \"true\" or \"false\" can be converted)" );
      }
      case double_type:
          return *reinterpret_cast<const double*>(this) != 0.0;
      case int64_type:
          return *reinterpret_cast<const int64_t*>(this) != 0;
      case uint64_type:
          return *reinterpret_cast<const uint64_t*>(this) != 0;
      case bool_type:
          return *reinterpret_cast<const bool*>(this);
      case null_type:
          return false;
      default:
         FC_THROW_EXCEPTION( bad_cast_exception, "Invalid cast from ${type} to bool" , ("type",get_type()));
   }
}

string    variant::as_string()const
{
   switch( get_type() )
   {
      case string_type:
          return **reinterpret_cast<const const_string_ptr*>(this); 
      case double_type:
          return to_string(*reinterpret_cast<const double*>(this)); 
      case int64_type:
          return to_string(*reinterpret_cast<const int64_t*>(this)); 
      case uint64_type:
          return to_string(*reinterpret_cast<const uint64_t*>(this)); 
      case bool_type:
          return *reinterpret_cast<const bool*>(this) ? "true" : "false";
      case blob_type:
          if( get_blob().data.size() )
             return base64_encode( get_blob().data.data(), get_blob().data.size() ) + "=";
          return string();
      case null_type:
          return string();
      default:
      FC_THROW_EXCEPTION( bad_cast_exception, "Invalid cast from ${type} to string", ("type", get_type() ) );
   }
}

                            
/// @throw if get_type() != array_type | null_type
variants&         variant::get_array()
{
  if( get_type() == array_type )
     return **reinterpret_cast<variants**>(this);
   
  FC_THROW_EXCEPTION( bad_cast_exception, "Invalid cast from ${type} to Array", ("type",get_type()) );
}
blob&         variant::get_blob()
{
  if( get_type() == blob_type )
     return **reinterpret_cast<blob**>(this);
   
  FC_THROW_EXCEPTION( bad_cast_exception, "Invalid cast from ${type} to Blob", ("type",get_type()) );
}
const blob&         variant::get_blob()const
{
  if( get_type() == blob_type )
     return **reinterpret_cast<const const_blob_ptr*>(this);
   
  FC_THROW_EXCEPTION( bad_cast_exception, "Invalid cast from ${type} to Blob", ("type",get_type()) );
}

blob variant::as_blob()const
{
   switch( get_type() )
   {
      case null_type: return blob();
      case blob_type: return get_blob();
      case string_type:
      {
         const string& str = get_string();
         if( str.size() == 0 ) return blob();
         if( str.back() == '=' )
         {
            std::string b64 = base64_decode( get_string() );
            return blob( { std::vector<char>( b64.begin(), b64.end() ) } );
         }
         return blob( { std::vector<char>( str.begin(), str.end() ) } );
      }
      case object_type:
      case array_type:
         FC_THROW_EXCEPTION( bad_cast_exception, "Invalid cast from ${type} to Blob", ("type",get_type()) );
      default:
         return blob( { std::vector<char>( (char*)&_data, (char*)&_data + sizeof(_data) ) } );
   }
}


/// @throw if get_type() != array_type 
const variants&       variant::get_array()const
{
  if( get_type() == array_type )
     return **reinterpret_cast<const const_variants_ptr*>(this);
  FC_THROW_EXCEPTION( bad_cast_exception, "Invalid cast from ${type} to Array", ("type",get_type()) );
}


/// @throw if get_type() != object_type | null_type
variant_object&        variant::get_object()
{
  if( get_type() == object_type )
     return **reinterpret_cast<variant_object**>(this);
  FC_THROW_EXCEPTION( bad_cast_exception, "Invalid cast from ${type} to Object", ("type",get_type()) );
}

const variant& variant::operator[]( const char* key )const
{
    return get_object()[key];
}
const variant&    variant::operator[]( size_t pos )const
{
    return get_array()[pos];
}
        /// @pre is_array()
size_t            variant::size()const
{
    return get_array().size();
}

const string&        variant::get_string()const
{
  if( get_type() == string_type )
     return **reinterpret_cast<const const_string_ptr*>(this);
M
Matias Romeo 已提交
565
  FC_THROW_EXCEPTION( bad_cast_exception, "Invalid cast from type '${type}' to string", ("type",get_type()) );
D
Daniel Larimer 已提交
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
}

/// @throw if get_type() != object_type 
const variant_object&  variant::get_object()const
{
  if( get_type() == object_type )
     return **reinterpret_cast<const const_variant_object_ptr*>(this);
  FC_THROW_EXCEPTION( bad_cast_exception, "Invalid cast from type '${type}' to Object", ("type",get_type()) );
}

void from_variant( const variant& var,  variants& vo )
{
   vo = var.get_array();
}

//void from_variant( const variant& var,  variant_object& vo )
//{
//   vo  = var.get_object();
//}

void from_variant( const variant& var,  variant& vo ) { vo = var; }

void to_variant( const uint8_t& var,  variant& vo )  { vo = uint64_t(var); }
// TODO: warn on overflow?
void from_variant( const variant& var,  uint8_t& vo ){ vo = static_cast<uint8_t>(var.as_uint64()); }

void to_variant( const int8_t& var,  variant& vo )  { vo = int64_t(var); }
// TODO: warn on overflow?
void from_variant( const variant& var,  int8_t& vo ){ vo = static_cast<int8_t>(var.as_int64()); }

void to_variant( const uint16_t& var,  variant& vo )  { vo = uint64_t(var); }
// TODO: warn on overflow?
void from_variant( const variant& var,  uint16_t& vo ){ vo = static_cast<uint16_t>(var.as_uint64()); }

void to_variant( const int16_t& var,  variant& vo )  { vo = int64_t(var); }
// TODO: warn on overflow?
void from_variant( const variant& var,  int16_t& vo ){ vo = static_cast<int16_t>(var.as_int64()); }

void to_variant( const uint32_t& var,  variant& vo )  { vo = uint64_t(var); }
void from_variant( const variant& var,  uint32_t& vo )
{
   vo = static_cast<uint32_t>(var.as_uint64());
}

void to_variant( const int32_t& var,  variant& vo )  { vo = int64_t(var); }
void from_variant( const variant& var,  int32_t& vo )
{
   vo = static_cast<int32_t>(var.as_int64());
}

void from_variant( const variant& var,  int64_t& vo )
{
   vo = var.as_int64();
}

void from_variant( const variant& var,  uint64_t& vo )
{
   vo = var.as_uint64();
}

void from_variant( const variant& var,  bool& vo )
{
   vo = var.as_bool();
}

void from_variant( const variant& var,  double& vo )
{
   vo = var.as_double();
}

void from_variant( const variant& var,  float& vo )
{
   vo = static_cast<float>(var.as_double());
}

void to_variant( const std::string& s, variant& v )
{
    v = variant( fc::string(s) );
}

void from_variant( const variant& var,  string& vo )
{
   vo = var.as_string();
}

void to_variant( const std::vector<char>& var,  variant& vo )
{
  if( var.size() )
      vo = variant(to_hex(var.data(),var.size()));
  else vo = "";
}
void from_variant( const variant& var,  std::vector<char>& vo )
{
     auto str = var.as_string();
     vo.resize( str.size() / 2 );
     if( vo.size() )
     {
        size_t r = from_hex( str, vo.data(), vo.size() );
        FC_ASSERT( r == vo.size() );
     }
//   std::string b64 = base64_decode( var.as_string() );
//   vo = std::vector<char>( b64.c_str(), b64.c_str() + b64.size() );
}

670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
void to_variant( const UInt<8>& n, variant& v ) { v = uint64_t(n); }
// TODO: warn on overflow?
void from_variant( const variant& v, UInt<8>& n ) { n = static_cast<uint8_t>(v.as_uint64()); }

void to_variant( const UInt<16>& n, variant& v ) { v = uint64_t(n); }
// TODO: warn on overflow?
void from_variant( const variant& v, UInt<16>& n ) { n = static_cast<uint16_t>(v.as_uint64()); }

void to_variant( const UInt<32>& n, variant& v ) { v = uint64_t(n); }
// TODO: warn on overflow?
void from_variant( const variant& v, UInt<32>& n ) { n = static_cast<uint32_t>(v.as_uint64()); }

void to_variant( const UInt<64>& n, variant& v ) { v = uint64_t(n); }
void from_variant( const variant& v, UInt<64>& n ) { n = v.as_uint64(); }

D
Daniel Larimer 已提交
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
string      format_string( const string& format, const variant_object& args )
{
   std::stringstream ss;
   size_t prev = 0;
   auto next = format.find( '$' );
   while( prev != size_t(string::npos) && prev < size_t(format.size()) ) 
   {
     ss << format.substr( prev, size_t(next-prev) );
   
     // if we got to the end, return it.
     if( next == size_t(string::npos) ) 
        return ss.str(); 
   
     // if we are not at the end, then update the start
     prev = next + 1;
   
     if( format[prev] == '{' ) 
     { 
        // if the next char is a open, then find close
         next = format.find( '}', prev );
         // if we found close... 
         if( next != size_t(string::npos) ) 
         {
           // the key is between prev and next
           string key = format.substr( prev+1, (next-prev-1) );

           auto val = args.find( key );
           if( val != args.end() )
           {
              if( val->value().is_object() || val->value().is_array() )
              {
                ss << json::to_string( val->value() );
              } 
              else 
              {
                ss << val->value().as_string();
              }
           } 
           else 
           {
              ss << "${"<<key<<"}";
           }
           prev = next + 1;
           // find the next $
           next = format.find( '$', prev );
         } 
         else 
         {
           // we didn't find it.. continue to while...
         }
     } 
     else  
     {
        ss << format[prev];
        ++prev;
        next = format.find( '$', prev );
     }
   }
   return ss.str();
}
   #ifdef __APPLE__
   #elif !defined(_MSC_VER)
   void to_variant( long long int s, variant& v ) { v = variant( int64_t(s) ); }
   void to_variant( unsigned long long int s, variant& v ) { v = variant( uint64_t(s)); }
   #endif

751
   bool operator == ( const variant& a, const variant& b )
D
Daniel Larimer 已提交
752 753 754 755 756
   {
      if( a.is_string()  || b.is_string() ) return a.as_string() == b.as_string();
      if( a.is_double()  || b.is_double() ) return a.as_double() == b.as_double();
      if( a.is_int64()   || b.is_int64() )  return a.as_int64() == b.as_int64();
      if( a.is_uint64()  || b.is_uint64() ) return a.as_uint64() == b.as_uint64();
757
      if( a.is_array()   || b.is_array() )  return a.get_array() == b.get_array();
D
Daniel Larimer 已提交
758 759 760
      return false;
   }

761
   bool operator != ( const variant& a, const variant& b )
D
Daniel Larimer 已提交
762
   {
763
      return !( a == b );
D
Daniel Larimer 已提交
764 765
   }

766
   bool operator ! ( const variant& a )
D
Daniel Larimer 已提交
767 768 769 770
   {
      return !a.as_bool();
   }

771
   bool operator < ( const variant& a, const variant& b )
D
Daniel Larimer 已提交
772 773 774 775 776 777 778 779
   {
      if( a.is_string()  || b.is_string() ) return a.as_string() < b.as_string();
      if( a.is_double()  || b.is_double() ) return a.as_double() < b.as_double();
      if( a.is_int64()   || b.is_int64() )  return a.as_int64() < b.as_int64();
      if( a.is_uint64()  || b.is_uint64() ) return a.as_uint64() < b.as_uint64();
      FC_ASSERT( false, "Invalid operation" );
   }

780
   bool operator > ( const variant& a, const variant& b )
D
Daniel Larimer 已提交
781 782 783 784 785 786 787 788
   {
      if( a.is_string()  || b.is_string() ) return a.as_string() > b.as_string();
      if( a.is_double()  || b.is_double() ) return a.as_double() > b.as_double();
      if( a.is_int64()   || b.is_int64() )  return a.as_int64() > b.as_int64();
      if( a.is_uint64()  || b.is_uint64() ) return a.as_uint64() > b.as_uint64();
      FC_ASSERT( false, "Invalid operation" );
   }

789
   bool operator <= ( const variant& a, const variant& b )
D
Daniel Larimer 已提交
790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
   {
      if( a.is_string()  || b.is_string() ) return a.as_string() <= b.as_string();
      if( a.is_double()  || b.is_double() ) return a.as_double() <= b.as_double();
      if( a.is_int64()   || b.is_int64() )  return a.as_int64() <= b.as_int64();
      if( a.is_uint64()  || b.is_uint64() ) return a.as_uint64() <= b.as_uint64();
      FC_ASSERT( false, "Invalid operation" );
   }


   variant operator + ( const variant& a, const variant& b )
   {
      if( a.is_array()  && b.is_array() )
      {
         const variants& aa = a.get_array();
         const variants& ba = b.get_array();
         variants result;
         result.reserve( std::max(aa.size(),ba.size()) );
         auto num = std::max(aa.size(),ba.size());
         for( unsigned i = 0; i < num; ++i )
         {
            if( aa.size() > i && ba.size() > i )
               result[i]  = aa[i] + ba[i];
            else if( aa.size() > i )
               result[i]  = aa[i];
            else
               result[i]  = ba[i];
         }
         return result;
      }
      if( a.is_string()  || b.is_string() ) return a.as_string() + b.as_string();
      if( a.is_double()  || b.is_double() ) return a.as_double() + b.as_double();
      if( a.is_int64()   || b.is_int64() )  return a.as_int64() + b.as_int64();
      if( a.is_uint64()  || b.is_uint64() ) return a.as_uint64() + b.as_uint64();
      FC_ASSERT( false, "invalid operation ${a} + ${b}", ("a",a)("b",b) );
   }

   variant operator - ( const variant& a, const variant& b )
   {
      if( a.is_array()  && b.is_array() )
      {
         const variants& aa = a.get_array();
         const variants& ba = b.get_array();
         variants result;
         result.reserve( std::max(aa.size(),ba.size()) );
         auto num = std::max(aa.size(),ba.size());
         for( unsigned i = 0; i < num; --i )
         {
            if( aa.size() > i && ba.size() > i )
               result[i]  = aa[i] - ba[i];
            else if( aa.size() > i )
               result[i]  = aa[i];
            else
               result[i]  = ba[i];
         }
         return result;
      }
      if( a.is_string()  || b.is_string() ) return a.as_string() - b.as_string();
      if( a.is_double()  || b.is_double() ) return a.as_double() - b.as_double();
      if( a.is_int64()   || b.is_int64() )  return a.as_int64() - b.as_int64();
      if( a.is_uint64()  || b.is_uint64() ) return a.as_uint64() - b.as_uint64();
      FC_ASSERT( false, "invalid operation ${a} + ${b}", ("a",a)("b",b) );
   }
   variant operator * ( const variant& a, const variant& b )
   {
      if( a.is_double()  || b.is_double() ) return a.as_double() * b.as_double();
      if( a.is_int64()   || b.is_int64() )  return a.as_int64() * b.as_int64();
      if( a.is_uint64()  || b.is_uint64() ) return a.as_uint64() * b.as_uint64();
      if( a.is_array()  && b.is_array() )
      {
         const variants& aa = a.get_array();
         const variants& ba = b.get_array();
         variants result;
         result.reserve( std::max(aa.size(),ba.size()) );
         auto num = std::max(aa.size(),ba.size());
         for( unsigned i = 0; i < num; ++i )
         {
            if( aa.size() > i && ba.size() > i )
               result[i]  = aa[i] * ba[i];
            else if( aa.size() > i )
               result[i]  = aa[i];
            else
               result[i]  = ba[i];
         }
         return result;
      }
      FC_ASSERT( false, "invalid operation ${a} * ${b}", ("a",a)("b",b) );
   }
   variant operator / ( const variant& a, const variant& b )
   {
      if( a.is_double()  || b.is_double() ) return a.as_double() / b.as_double();
      if( a.is_int64()   || b.is_int64() )  return a.as_int64() / b.as_int64();
      if( a.is_uint64()  || b.is_uint64() ) return a.as_uint64() / b.as_uint64();
      if( a.is_array()  && b.is_array() )
      {
         const variants& aa = a.get_array();
         const variants& ba = b.get_array();
         variants result;
         result.reserve( std::max(aa.size(),ba.size()) );
         auto num = std::max(aa.size(),ba.size());
         for( unsigned i = 0; i < num; ++i )
         {
            if( aa.size() > i && ba.size() > i )
               result[i]  = aa[i] / ba[i];
            else if( aa.size() > i )
               result[i]  = aa[i];
            else
               result[i]  = ba[i];
         }
         return result;
      }
      FC_ASSERT( false, "invalid operation ${a} / ${b}", ("a",a)("b",b) );
   }
} // namespace fc