BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet software
flat.hpp
Go to the documentation of this file.
1 #pragma once
2 #include <fc/variant.hpp>
4 #include <boost/container/flat_map.hpp>
5 #include <boost/container/flat_set.hpp>
6 #include <fc/io/raw_fwd.hpp>
7 
8 namespace fc {
9  namespace raw {
10  template<typename Stream, typename T, typename... A>
11  inline void pack( Stream& s, const flat_set<T, A...>& value, uint32_t _max_depth ) {
12  FC_ASSERT( _max_depth > 0 );
13  --_max_depth;
14  pack( s, unsigned_int(value.size()), _max_depth );
15  auto itr = value.begin();
16  auto end = value.end();
17  while( itr != end ) {
18  fc::raw::pack( s, *itr, _max_depth );
19  ++itr;
20  }
21  }
22  template<typename Stream, typename T, typename... A>
23  inline void unpack( Stream& s, flat_set<T, A...>& value, uint32_t _max_depth ) {
24  FC_ASSERT( _max_depth > 0 );
25  --_max_depth;
26  unsigned_int size; unpack( s, size, _max_depth );
27  value.clear();
28  value.reserve( std::min( size.value, static_cast<uint64_t>(FC_MAX_PREALLOC_SIZE) ) );
29  for( uint32_t i = 0; i < size.value; ++i )
30  {
31  T tmp;
32  fc::raw::unpack( s, tmp, _max_depth );
33  value.insert( std::move(tmp) );
34  }
35  }
36  template<typename Stream, typename K, typename... V>
37  inline void pack( Stream& s, const flat_map<K,V...>& value, uint32_t _max_depth ) {
38  FC_ASSERT( _max_depth > 0 );
39  --_max_depth;
40  pack( s, unsigned_int(value.size()), _max_depth );
41  auto itr = value.begin();
42  auto end = value.end();
43  while( itr != end ) {
44  fc::raw::pack( s, *itr, _max_depth );
45  ++itr;
46  }
47  }
48  template<typename Stream, typename K, typename V, typename... A>
49  inline void unpack( Stream& s, flat_map<K,V,A...>& value, uint32_t _max_depth )
50  {
51  FC_ASSERT( _max_depth > 0 );
52  --_max_depth;
53  unsigned_int size; unpack( s, size, _max_depth );
54  value.clear();
55  value.reserve( std::min( size.value, static_cast<uint64_t>(FC_MAX_PREALLOC_SIZE) ) );
56  for( uint32_t i = 0; i < size.value; ++i )
57  {
58  std::pair<K,V> tmp;
59  fc::raw::unpack( s, tmp, _max_depth );
60  value.insert( std::move(tmp) );
61  }
62  }
63 
64  template<typename Stream, typename T, typename A>
65  void pack( Stream& s, const bip::vector<T,A>& value, uint32_t _max_depth ) {
66  FC_ASSERT( _max_depth > 0 );
67  --_max_depth;
68  pack( s, unsigned_int(value.size()), _max_depth );
69  if( !std::is_fundamental<T>::value ) {
70  auto itr = value.begin();
71  auto end = value.end();
72  while( itr != end ) {
73  fc::raw::pack( s, *itr, _max_depth );
74  ++itr;
75  }
76  } else {
77  s.write( (const char*)value.data(), value.size() );
78  }
79  }
80 
81  template<typename Stream, typename T, typename A>
82  void unpack( Stream& s, bip::vector<T,A>& value, uint32_t _max_depth ) {
83  FC_ASSERT( _max_depth > 0 );
84  --_max_depth;
85  unsigned_int size;
86  unpack( s, size, _max_depth );
87  if( !std::is_fundamental<T>::value ) {
88  value.resize( std::min( size.value, static_cast<uint64_t>(FC_MAX_PREALLOC_SIZE) ) );
89  for( uint64_t i = 0; i < size; i++ )
90  {
91  if( i >= value.size() )
92  value.resize( std::min( static_cast<uint64_t>(2*value.size()), size.value ) );
93  unpack( s, value[i], _max_depth );
94  }
95  } else {
96  value.resize( size );
97  s.read( (char*)value.data(), value.size() );
98  }
99  }
100 
101  } // namespace raw
102 
103 
104  template<typename T, typename... A>
105  void to_variant( const flat_set<T, A...>& var, variant& vo, uint32_t _max_depth )
106  {
107  FC_ASSERT( _max_depth > 0 );
108  --_max_depth;
109  std::vector<variant> vars(var.size());
110  size_t i = 0;
111  for( const auto& item : var )
112  vars[i++] = variant( item, _max_depth );
113  vo = vars;
114  }
115  template<typename T, typename... A>
116  void from_variant( const variant& var, flat_set<T, A...>& vo, uint32_t _max_depth )
117  {
118  FC_ASSERT( _max_depth > 0 );
119  --_max_depth;
120  const variants& vars = var.get_array();
121  vo.clear();
122  vo.reserve( vars.size() );
123  for( const auto& item : vars )
124  vo.insert( item.as<T>(_max_depth) );
125  }
126 
127  template<typename K, typename... T>
128  void to_variant( const flat_map<K, T...>& var, variant& vo, uint32_t _max_depth )
129  {
130  FC_ASSERT( _max_depth > 0 );
131  --_max_depth;
132  std::vector< variant > vars(var.size());
133  size_t i = 0;
134  for( const auto& item : var )
135  vars[i++] = variant( item, _max_depth );
136  vo = vars;
137  }
138  template<typename K, typename T, typename... A>
139  void from_variant( const variant& var, flat_map<K, T, A...>& vo, uint32_t _max_depth )
140  {
141  FC_ASSERT( _max_depth > 0 );
142  --_max_depth;
143  const variants& vars = var.get_array();
144  vo.clear();
145  for( const auto& item : vars )
146  vo.insert( item.as<std::pair<K,T>>(_max_depth) );
147  }
148 
149 }
fc::variant::get_array
variants & get_array()
Definition: variant.cpp:496
fc::raw::unpack
void unpack(Stream &s, flat_set< T, A... > &value, uint32_t _max_depth)
Definition: flat.hpp:23
fc
Definition: api.hpp:15
fc::from_variant
void from_variant(const variant &var, flat_set< T, A... > &vo, uint32_t _max_depth)
Definition: flat.hpp:116
flat_fwd.hpp
fc::variants
std::vector< variant > variants
Definition: variant.hpp:170
fc::unsigned_int
Definition: varint.hpp:6
fc::to_variant
void to_variant(const flat_set< T, A... > &var, variant &vo, uint32_t _max_depth)
Definition: flat.hpp:105
FC_ASSERT
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
Definition: exception.hpp:345
fc::variant
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition: variant.hpp:198
fc::unsigned_int::value
uint64_t value
Definition: varint.hpp:17
raw_fwd.hpp
FC_MAX_PREALLOC_SIZE
#define FC_MAX_PREALLOC_SIZE
Definition: config.hpp:13
variant.hpp
fc::raw::pack
void pack(Stream &s, const flat_set< T, A... > &value, uint32_t _max_depth)
Definition: flat.hpp:11