BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet 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 
fc::detail::add
Definition: fwd_impl.hpp:13
fc::fwd::fwd
fwd()
Definition: fwd_impl.hpp:84
fc::check_size
void check_size()
Definition: fwd_impl.hpp:60
fc::detail::extract_op::a
A * a
Definition: fwd_impl.hpp:36
fc::fwd::operator!
bool operator!() const
Definition: fwd_impl.hpp:56
fc::operator>>
auto operator>>(U &u, fwd< T, S, A > &f) -> typename detail::extract_op< U, T >::type
Definition: fwd_impl.hpp:53
fc::detail::sub_eq
Definition: fwd_impl.hpp:27
fwd.hpp
fc
Definition: api.hpp:15
fc::detail::insert_op
Definition: fwd_impl.hpp:31
fc::detail::sub
Definition: fwd_impl.hpp:22
fc::detail::insert_op::type
decltype(*((A *) 0)<< *((typename std::remove_reference< U >::type *) 0)) typede type)
Definition: fwd_impl.hpp:32
fc::fwd::~fwd
~fwd()
Definition: fwd_impl.hpp:118
fc::operator<<
auto operator<<(U &u, const fwd< T, S, A > &f) -> typename detail::insert_op< U, T >::type
Definition: fwd_impl.hpp:50
fc::detail::add_eq::type
decltype(*((A *) 0)+=*((typename std::remove_reference< U >::type *) 0)) typede type)
Definition: fwd_impl.hpp:18
fc::detail::extract_op::type
decltype(*a >> *u) typedef type
Definition: fwd_impl.hpp:38
fc::detail::sub::type
decltype(*((A *) 0) - *((typename std::remove_reference< U >::type *) 0)) typede type)
Definition: fwd_impl.hpp:23
fc::detail::extract_op::u
U * u
Definition: fwd_impl.hpp:37
fc::operator-
auto operator-(const fwd< T, S, A > &x, U &&u) -> typename detail::sub< T, U >::type
Definition: fwd_impl.hpp:47
fc::detail::add::type
decltype(*((A *) 0)+*((typename std::remove_reference< U >::type *) 0)) typede type)
Definition: fwd_impl.hpp:14
fc::detail::extract_op
Definition: fwd_impl.hpp:35
fc::operator+
auto operator+(const fwd< T, S, A > &x, U &&u) -> typename detail::add< T, U >::type
Definition: fwd_impl.hpp:44
fc::fwd::operator*
T & operator*()
Definition: fwd_impl.hpp:107
fc::detail::add_eq
Definition: fwd_impl.hpp:17
fc::fwd::operator->
const T * operator->() const
Definition: fwd_impl.hpp:111
fc::detail::sub_eq::type
decltype(*((A *) 0) -=*((typename std::remove_reference< U >::type *) 0)) typede type)
Definition: fwd_impl.hpp:28
fc::fwd
Used to forward declare value types.
Definition: fwd.hpp:10
fc::fwd::operator=
T & operator=(U &&u)
Definition: fwd_impl.hpp:123