BitShares-Core  4.0.0
BitShares blockchain implementation and command-line interface software
fwd_impl.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <fc/fwd.hpp>
4 #include <cstdint>
5 #include <new>
6 #include <type_traits>
7 #include <utility>
8 
9 namespace fc {
10 
11  namespace detail {
12  template<typename A, typename U>
13  struct add {
14  typedef decltype( *((A*)0) + *((typename std::remove_reference<U>::type*)0) ) type;
15  };
16  template<typename A, typename U>
17  struct add_eq {
18  typedef decltype( *((A*)0) += *((typename std::remove_reference<U>::type*)0) ) type;
19  };
20 
21  template<typename A, typename U>
22  struct sub {
23  typedef decltype( *((A*)0) - *((typename std::remove_reference<U>::type*)0) ) type;
24  };
25 
26  template<typename A, typename U>
27  struct sub_eq {
28  typedef decltype( *((A*)0) -= *((typename std::remove_reference<U>::type*)0) ) type;
29  };
30  template<typename A, typename U>
31  struct insert_op {
32  typedef decltype( *((A*)0) << *((typename std::remove_reference<U>::type*)0) ) type;
33  };
34  template<typename A, typename U>
35  struct extract_op {
36  A* a;
37  U* u;
38  typedef decltype( *a >> *u ) type;
39  };
40  }
41 
42 
43  template<typename T, unsigned int S, typename U, typename A>
44  auto operator + ( const fwd<T,S,A>& x, U&& u ) -> typename detail::add<T,U>::type { return *x+std::forward<U>(u); }
45 
46  template<typename T, unsigned int S, typename U, typename A>
47  auto operator - ( const fwd<T,S,A>& x, U&& u ) -> typename detail::sub<T,U>::type { return *x-std::forward<U>(u); }
48 
49  template<typename T, unsigned int S, typename U, typename A>
50  auto operator << ( U& u, const fwd<T,S,A>& f ) -> typename detail::insert_op<U,T>::type { return u << *f; }
51 
52  template<typename T, unsigned int S, typename U, typename A>
53  auto operator >> ( U& u, fwd<T,S,A>& f ) -> typename detail::extract_op<U,T>::type { return u >> *f; }
54 
55  template<typename T, unsigned int S, typename A>
56  bool fwd<T,S,A>::operator !()const { return !(**this); }
57 
58 
59  template<uint64_t RequiredSize, uint64_t ProvidedSize>
60  void check_size() { static_assert( (ProvidedSize >= RequiredSize), "Failed to reserve enough space in fc::fwd<T,S>" ); }
61 
62  template<typename T,unsigned int S,typename A>
63  template<typename U>
64  fwd<T,S,A>::fwd( U&& u ) {
65  check_size<sizeof(T),sizeof(_store)>();
66  new (this) T( std::forward<U>(u) );
67  }
68 
69  template<typename T,unsigned int S,typename A>
70  template<typename U,typename V>
71  fwd<T,S,A>::fwd( U&& u, V&& v ) {
72  check_size<sizeof(T),sizeof(_store)>();
73  new (this) T( std::forward<U>(u), std::forward<V>(v) );
74  }
75  template<typename T,unsigned int S,typename A>
76  template<typename U,typename V,typename X,typename Y>
77  fwd<T,S,A>::fwd( U&& u, V&& v, X&& x, Y&& y ) {
78  check_size<sizeof(T),sizeof(_store)>();
79  new (this) T( std::forward<U>(u), std::forward<V>(v), std::forward<X>(x), std::forward<Y>(y) );
80  }
81 
82 
83  template<typename T,unsigned int S,typename A>
85  check_size<sizeof(T),sizeof(_store)>();
86  new (this) T;
87  }
88  template<typename T,unsigned int S,typename A>
90  check_size<sizeof(T),sizeof(_store)>();
91  new (this) T( *f );
92  }
93  template<typename T,unsigned int S,typename A>
95  check_size<sizeof(T),sizeof(_store)>();
96  new (this) T( std::move(*f) );
97  }
98 
99 
100 
101  template<typename T,unsigned int S, typename A>
102  fwd<T,S,A>::operator T&() { return *(( T*)this); }
103  template<typename T,unsigned int S, typename A>
104  fwd<T,S,A>::operator const T&()const { return *((const T*)this); }
105 
106  template<typename T,unsigned int S, typename A>
107  T& fwd<T,S,A>::operator*() { return *((T*)this); }
108  template<typename T,unsigned int S, typename A>
109  const T& fwd<T,S,A>::operator*()const { return *((const T*)this); }
110  template<typename T,unsigned int S, typename A>
111  const T* fwd<T,S,A>::operator->()const { return ((const T*)this); }
112 
113  template<typename T,unsigned int S, typename A>
114  T* fwd<T,S,A>::operator->(){ return ((T*)this); }
115 
116 
117  template<typename T,unsigned int S, typename A>
119  ((T*)this)->~T();
120  }
121  template<typename T,unsigned int S, typename A>
122  template<typename U>
124  return **this = std::forward<U>(u);
125  }
126 
127  template<typename T,unsigned int S, typename A>
129  return **this = std::move(*u);
130  }
131  template<typename T,unsigned int S, typename A>
132  T& fwd<T,S,A>::operator = ( const fwd<T,S,A>& u ) {
133  return **this = *u;
134  }
135 
136 } // namespace fc
137 
Used to forward declare value types.
Definition: fwd.hpp:10
decltype(*((A *) 0)+*((typename std::remove_reference< U >::type *) 0)) typede type)
Definition: fwd_impl.hpp:14
const CBigNum operator>>(const CBigNum &a, unsigned int shift)
Definition: base58.cpp:488
T & operator*()
Definition: fwd_impl.hpp:107
const T * operator->() const
Definition: fwd_impl.hpp:111
const CBigNum operator-(const CBigNum &a, const CBigNum &b)
Definition: base58.cpp:438
T & operator=(U &&u)
Definition: fwd_impl.hpp:123
void check_size()
Definition: fwd_impl.hpp:60
bool operator!() const
Definition: fwd_impl.hpp:56
Definition: api.hpp:15