9 #include <unordered_map>
10 #include <unordered_set>
19 #include <boost/endian/buffers.hpp>
20 #include <boost/multi_index_container_fwd.hpp>
23 #define _FC_ASSERT(...) FC_ASSERT( __VA_ARGS__ )
30 #define _FC_ASSERT( cond, msg ) { if( !(cond) ) { char t[] = #cond ": " msg; fc::throw_assertion_failure( t ); } }
50 class mutable_variant_object;
54 template<
typename T>
struct safe;
55 template<
typename... Types>
64 template<
typename T,
typename... Args>
void to_variant(
const boost::multi_index_container<T,Args...>& s,
variant& v, uint32_t max_depth );
65 template<
typename T,
typename... Args>
void from_variant(
const variant& v, boost::multi_index_container<T,Args...>& s, uint32_t max_depth );
69 template<
typename T>
void to_variant(
const std::unique_ptr<T>& s,
variant& v, uint32_t max_depth );
70 template<
typename T>
void from_variant(
const variant& v, std::unique_ptr<T>& s, uint32_t max_depth );
101 void to_variant(
const std::vector<char>& var,
variant& vo, uint32_t max_depth = 1 );
104 template<
typename K,
typename T>
105 void to_variant(
const std::unordered_map<K,T>& var,
variant& vo, uint32_t max_depth );
106 template<
typename K,
typename T>
109 template<
typename K,
typename... T>
110 void to_variant(
const fc::flat_map<K,T...>& var,
variant& vo, uint32_t max_depth );
111 template<
typename K,
typename T,
typename... A>
114 template<
typename K,
typename... T>
115 void to_variant(
const std::map<K, T...>& var,
variant& vo, uint32_t max_depth );
116 template<
typename K,
typename T,
typename... A>
118 template<
typename K,
typename T>
119 void to_variant(
const std::multimap<K,T>& var,
variant& vo, uint32_t max_depth );
120 template<
typename K,
typename T>
125 void to_variant(
const std::unordered_set<T>& var,
variant& vo, uint32_t max_depth );
134 template<
typename T,
typename... A>
135 void to_variant(
const fc::flat_set<T, A...>& var,
variant& vo, uint32_t max_depth );
136 template<
typename T,
typename... A>
156 #if defined(__APPLE__) or defined(__OpenBSD__)
158 #elif !defined(_WIN32)
165 void to_variant(
const std::shared_ptr<T>& var,
variant& vo, uint32_t max_depth );
171 template<
typename A,
typename B>
173 template<
typename A,
typename B>
176 template<boost::endian::order O,
class T, std::
size_t N, boost::endian::align A>
177 void to_variant(
const boost::endian::endian_buffer<O,T,N,A>& var,
variant& vo, uint32_t max_depth )
181 template<boost::endian::order O,
class T, std::
size_t N, boost::endian::align A>
217 variant( std::nullptr_t, uint32_t max_depth = 1 );
221 variant(
const char* str, uint32_t max_depth = 1 );
222 variant(
char* str, uint32_t max_depth = 1 );
223 variant(
wchar_t* str, uint32_t max_depth = 1 );
224 variant(
const wchar_t* str, uint32_t max_depth = 1 );
225 variant(
float val, uint32_t max_depth = 1 );
226 variant( uint8_t val, uint32_t max_depth = 1 );
227 variant( int8_t val, uint32_t max_depth = 1 );
228 variant( uint16_t val, uint32_t max_depth = 1 );
229 variant( int16_t val, uint32_t max_depth = 1 );
230 variant( uint32_t val, uint32_t max_depth = 1 );
231 variant( int32_t val, uint32_t max_depth = 1 );
232 variant( uint64_t val, uint32_t max_depth = 1 );
233 #if defined(__APPLE__) or defined(__OpenBSD__)
234 variant(
size_t val, uint32_t max_depth = 1 );
236 variant( int64_t val, uint32_t max_depth = 1 );
237 variant(
double val, uint32_t max_depth = 1 );
238 variant(
bool val, uint32_t max_depth = 1 );
240 variant( std::string val, uint32_t max_depth = 1 );
256 virtual void handle()
const = 0;
257 virtual void handle(
const int64_t& v )
const = 0;
258 virtual void handle(
const uint64_t& v )
const = 0;
259 virtual void handle(
const double& v )
const = 0;
260 virtual void handle(
const bool& v )
const = 0;
261 virtual void handle(
const std::string& v )
const = 0;
266 void visit(
const visitor& v )
const;
337 T
as( uint32_t max_depth )
const
345 void as( T& v, uint32_t max_depth )
const
356 return *
this =
variant( std::forward<T>(v) );
362 _FC_ASSERT( max_depth > 0,
"Recursion depth exceeded!" );
363 memset(
this, 0,
sizeof(*
this) );
368 variant(
const T& val, uint32_t max_depth );
375 char _type[
sizeof(
void*)];
401 _FC_ASSERT( max_depth > 0,
"Recursion depth exceeded!" );
409 _FC_ASSERT( max_depth > 0,
"Recursion depth exceeded!" );
410 std::vector<variant> vars(var.size());
412 for(
const auto& item : var )
413 vars[i++] =
variant( item, max_depth - 1 );
419 _FC_ASSERT( max_depth > 0,
"Recursion depth exceeded!" );
422 vo.reserve( vars.size() );
423 for(
const auto& item : vars )
424 vo.insert( item.as<T>( max_depth - 1 ) );
428 template<
typename K,
typename T>
431 _FC_ASSERT( max_depth > 0,
"Recursion depth exceeded!" );
432 std::vector< variant > vars(var.size());
434 for(
const auto& key_value : var )
435 vars[i++] =
fc::variant( key_value, max_depth - 1 );
438 template<
typename K,
typename T>
441 _FC_ASSERT( max_depth > 0,
"Recursion depth exceeded!" );
444 vo.reserve( vars.size() );
445 for(
const auto& item : vars )
446 vo.insert( item.as< std::pair<K,T> >( max_depth - 1 ) );
448 template<
typename K,
typename... T>
451 _FC_ASSERT( max_depth > 0,
"Recursion depth exceeded!" );
452 std::vector< variant > vars(var.size());
454 for(
const auto& key_value : var )
455 vars[i++] =
fc::variant( key_value, max_depth - 1 );
458 template<
typename K,
typename T,
typename... A>
461 _FC_ASSERT( max_depth > 0,
"Recursion depth exceeded!" );
464 for(
const auto& item : vars )
465 vo.insert( item.as< std::pair<K,T> >( max_depth - 1 ) );
468 template<
typename K,
typename T>
471 _FC_ASSERT( max_depth > 0,
"Recursion depth exceeded!" );
472 std::vector< variant > vars(var.size());
474 for(
const auto& key_value : var )
475 vars[i++] =
fc::variant( key_value, max_depth - 1 );
478 template<
typename K,
typename T>
481 _FC_ASSERT( max_depth > 0,
"Recursion depth exceeded!" );
484 for(
auto item : vars )
485 vo.insert( item.as< std::pair<K,T> >( max_depth - 1 ) );
492 _FC_ASSERT( max_depth > 0,
"Recursion depth exceeded!" );
493 std::vector<variant> vars(var.size());
495 for(
const auto& item : var )
502 _FC_ASSERT( max_depth > 0,
"Recursion depth exceeded!" );
505 for(
const auto& item : vars )
506 vo.insert( item.as<T>( max_depth - 1 ) );
513 _FC_ASSERT( max_depth > 0,
"Recursion depth exceeded!" );
516 dest.resize( vars.size() );
517 for(
const auto& item : vars )
518 dest.push_back( item.as<T>( max_depth - 1 ) );
525 _FC_ASSERT( max_depth > 0,
"Recursion depth exceeded!" );
526 std::vector<variant> vars(src.size());
527 for(
size_t i = 0; i < src.size(); ++i )
528 vars[i] =
variant( src[i], max_depth - 1 );
537 _FC_ASSERT( max_depth > 0,
"Recursion depth exceeded!" );
540 dest.reserve( vars.size() );
541 for(
const auto& item : vars )
542 dest.push_back( item.as<T>( max_depth - 1 ) );
549 _FC_ASSERT( max_depth > 0,
"Recursion depth exceeded!" );
550 std::vector<variant> vars(t.size());
551 for(
size_t i = 0; i < t.size(); ++i )
552 vars[i] =
variant( t[i], max_depth - 1 );
558 template<
typename A,
typename B>
561 _FC_ASSERT( max_depth > 0,
"Recursion depth exceeded!" );
562 std::vector<variant> vars(2);
563 vars[0] =
variant( t.first, max_depth - 1 );
564 vars[1] =
variant( t.second, max_depth - 1 );
567 template<
typename A,
typename B>
570 _FC_ASSERT( max_depth > 0,
"Recursion depth exceeded!" );
572 if( vars.size() > 0 )
573 p.first = vars[0].as<A>( max_depth - 1 );
574 if( vars.size() > 1 )
575 p.second = vars[1].as<B>( max_depth - 1 );
582 memset(
this, 0,
sizeof(*
this) );
591 _FC_ASSERT( max_depth > 0,
"Recursion depth exceeded!" );
600 if( var.
is_null() ) vo =
nullptr;
603 _FC_ASSERT( max_depth > 0,
"Recursion depth exceeded!" );
604 if( !vo ) vo = std::make_shared<T>();
613 _FC_ASSERT( max_depth > 0,
"Recursion depth exceeded!" );
622 if( var.
is_null() ) vo.reset();
625 _FC_ASSERT( max_depth > 0,
"Recursion depth exceeded!" );
626 if( !vo ) vo.reset(
new T() );
639 s.
value = v.
as<T>( max_depth );
642 template<
typename T,
typename... Args>
643 void to_variant(
const boost::multi_index_container<T,Args...>& c,
variant& v, uint32_t max_depth )
645 _FC_ASSERT( max_depth > 0,
"Recursion depth exceeded!" );
646 std::vector<variant> vars;
647 vars.reserve( c.size() );
648 for(
const auto& item : c )
649 vars.emplace_back(
variant( item, max_depth - 1 ) );
653 template<
typename T,
typename... Args>
656 _FC_ASSERT( max_depth > 0,
"Recursion depth exceeded!" );
659 for(
const auto& item : vars )
660 c.insert( item.as<T>( max_depth - 1 ) );
673 from_hex( ve, bi.data(), std::min<size_t>( ve.size() / 2, bi.size() ) );
675 memset( bi.data(), 0, bi.size() );
688 from_hex( ve, (
char*)bi.data(), std::min<size_t>( ve.size() / 2, bi.size() ) );
690 memset( bi.data(), 0, bi.size() );
693 variant
operator + (
const variant& a,
const variant& b );
694 variant
operator - (
const variant& a,
const variant& b );
695 variant
operator * (
const variant& a,
const variant& b );
696 variant
operator / (
const variant& a,
const variant& b );
697 bool operator == (
const variant& a,
const variant& b );
698 bool operator != (
const variant& a,
const variant& b );
699 bool operator < (
const variant& a,
const variant& b );
700 bool operator > (
const variant& a,
const variant& b );