Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
utility.hpp
Go to the documentation of this file.
1#pragma once
2#include <stdint.h>
3#include <algorithm>
4#include <functional>
5#include <new>
6#include <vector>
7#include <type_traits>
8#include <tuple>
9#include <memory>
10
11#ifdef _MSC_VER
12#pragma warning(disable: 4482) // nonstandard extension used enum Name::Val, standard in C++11
13#define NO_RETURN __declspec(noreturn)
14#else
15#define NO_RETURN __attribute__((noreturn))
16#endif
17
18#define MAX_NUM_ARRAY_ELEMENTS (1024*1024)
19#define MAX_SIZE_OF_BYTE_ARRAYS (20*1024*1024)
20
21//namespace std {
22// typedef decltype(sizeof(int)) size_t;
23// typedef decltype(nullptr) nullptr_t;
24//}
25
26namespace fc {
27 using std::size_t;
28 typedef decltype(nullptr) nullptr_t;
29
30 template<typename T> struct remove_reference { typedef T type; };
31 template<typename T> struct remove_reference<T&> { typedef T type; };
32 template<typename T> struct remove_reference<T&&> { typedef T type; };
33
34 template<typename T> struct deduce { typedef T type; };
35 template<typename T> struct deduce<T&> { typedef T type; };
36 template<typename T> struct deduce<const T&> { typedef T type; };
37 template<typename T> struct deduce<T&&> { typedef T type; };
38 template<typename T> struct deduce<const T&&>{ typedef T type; };
39
40
41 using std::move;
42 using std::forward;
43
44 struct true_type { enum _value { value = 1 }; };
45 struct false_type { enum _value { value = 0 }; };
46
47 namespace detail {
48 template<typename T> fc::true_type is_class_helper(void(T::*)());
49 template<typename T> fc::false_type is_class_helper(...);
50
51 template<typename T, typename A, typename... Args>
53 public:
54 static constexpr bool value = std::uses_allocator<T, A>::value;
55 static constexpr bool leading_allocator = std::is_constructible< T, std::allocator_arg_t, A, Args... >::value;
56 static constexpr bool trailing_allocator = std::is_constructible< T, Args..., A>::value;
57 };
58
59 template<typename T, typename A, typename... Args>
60 auto construct_maybe_with_allocator( A&& allocator, Args&&... args )
61 -> std::enable_if_t<supports_allocator<T, A>::value && supports_allocator<T, A>::leading_allocator, T>
62 {
63 return T( std::allocator_arg, std::forward<A>(allocator), std::forward<Args>(args)... );
64 }
65
66 template<typename T, typename A, typename... Args>
67 auto construct_maybe_with_allocator( A&& allocator, Args&&... args )
68 -> std::enable_if_t<supports_allocator<T, A>::value && !supports_allocator<T, A>::leading_allocator, T>
69 {
70 static_assert( supports_allocator<T, A>::trailing_allocator, "type supposedly supports allocators but cannot be constructed by either the leading- or trailing-allocator convention" );
71 return T( std::forward<Args>(args)..., std::forward<A>(allocator) );
72 }
73
74 template<typename T, typename A, typename... Args>
75 auto construct_maybe_with_allocator( A&&, Args&&... args )
76 -> std::enable_if_t<!supports_allocator<T, A>::value , T>
77 {
78 return T( std::forward<Args>(args)... );
79 }
80
81 template<typename T, typename A, typename... Args>
83 A&& allocator, std::tuple<Args&&...> args,
85 ) {
86 return std::tuple_cat( std::forward_as_tuple<std::allocator_arg_t, A>( std::allocator_arg, allocator ), args );
87 }
88
89 template<typename T, typename A, typename... Args>
91 A&& allocator, std::tuple<>,
93 ) {
94 return std::forward_as_tuple<std::allocator_arg_t, A>( std::allocator_arg, allocator );
95 }
96
97 template<typename T, typename A, typename... Args>
99 A&& allocator, std::tuple<Args&&...> args,
101 ) {
102 static_assert( supports_allocator<T, A>::trailing_allocator, "type supposedly supports allocators but cannot be constructed by either the leading- or trailing-allocator convention" );
103 return std::tuple_cat( args, std::forward_as_tuple<A>( allocator ) );
104 }
105
106 template<typename T, typename A, typename... Args>
108 A&& allocator, std::tuple<>,
110 ) {
111 static_assert( supports_allocator<T, A>::trailing_allocator, "type supposedly supports allocators but cannot be constructed by either the leading- or trailing-allocator convention" );
112 return std::forward_as_tuple<A>( allocator );
113 }
114
115 template<typename T, typename A, typename... Args>
117 A&&, std::tuple<Args&&...> args,
118 std::enable_if_t<!supports_allocator<T, A>::value, int> = 0
119 ) {
120 return args;
121 }
122
123 template<typename T1, typename T2, typename A>
124 std::pair<T1,T2> default_construct_pair_maybe_with_allocator( A&& allocator )
125 {
126 return std::pair<T1,T2>(
127 std::piecewise_construct,
128 maybe_augment_constructor_arguments_with_allocator<T1>( allocator, std::make_tuple() ),
129 maybe_augment_constructor_arguments_with_allocator<T2>( allocator, std::make_tuple() )
130 );
131 }
132 }
133
134 template<typename T>
135 struct is_class { typedef decltype(detail::is_class_helper<T>(0)) type; enum value_enum { value = type::value }; };
136#ifdef min
137#undef min
138#endif
139 template<typename T>
140 const T& min( const T& a, const T& b ) { return a < b ? a: b; }
141
142 constexpr size_t const_strlen(const char* str) {
143 int i = 0;
144 while(*(str+i) != '\0')
145 i++;
146 return i;
147 }
148
149
150 template<typename Container>
151 void move_append(Container& dest, Container&& src ) {
152 if (src.empty()) {
153 return;
154 } else if (dest.empty()) {
155 dest = std::move(src);
156 } else {
157 dest.insert(std::end(dest), std::make_move_iterator(std::begin(src)), std::make_move_iterator(std::end(src)));
158 }
159 }
160
161 template<typename Container>
162 void copy_append(Container& dest, const Container& src ) {
163 if (src.empty()) {
164 return;
165 } else {
166 dest.insert(std::end(dest), std::begin(src), std::end(src));
167 }
168 }
169
170 template<typename Container>
171 void deduplicate( Container& entries ) {
172 if (entries.size() > 1) {
173 std::sort( entries.begin(), entries.end() );
174 auto itr = std::unique( entries.begin(), entries.end() );
175 entries.erase( itr, entries.end() );
176 }
177 }
178
182 template<typename Signature>
184
185 template<typename R, typename ...Args>
186 class optional_delegate<R(Args...)> : private std::function<R(Args...)> {
187 public:
188 using std::function<R(Args...)>::function;
189
190 auto operator()( Args... args ) const -> R {
191 if (static_cast<bool>(*this)) {
192 if constexpr( std::is_move_constructible_v<R> ) {
193 return std::function<R(Args...)>::operator()(std::move(args)...);
194 } else {
195 return std::function<R(Args...)>::operator()(args...);
196 }
197 } else {
198 if constexpr( !std::is_void_v<R> ) {
199 return {};
200 }
201 }
202 }
203 };
204
206
207}
208
209 // outside of namespace fc becuase of VC++ conflict with std::swap
210 template<typename T>
211 void fc_swap( T& a, T& b ) {
212 T tmp = fc::move(a);
213 a = fc::move(b);
214 b = fc::move(tmp);
215 }
216
217#define LLCONST(constant) static_cast<int64_t>(constant##ll)
218#define ULLCONST(constant) static_cast<uint64_t>(constant##ull)
auto operator()(Args... args) const -> R
Definition utility.hpp:190
auto construct_maybe_with_allocator(A &&allocator, Args &&... args) -> std::enable_if_t< supports_allocator< T, A >::value &&supports_allocator< T, A >::leading_allocator, T >
Definition utility.hpp:60
auto maybe_augment_constructor_arguments_with_allocator(A &&allocator, std::tuple< Args &&... > args, std::enable_if_t< supports_allocator< T, A >::value &&supports_allocator< T, A >::leading_allocator, int >=0)
Definition utility.hpp:82
std::pair< T1, T2 > default_construct_pair_maybe_with_allocator(A &&allocator)
Definition utility.hpp:124
fc::false_type is_class_helper(...)
namespace sysio::chain
Definition authority.cpp:3
void move_append(Container &dest, Container &&src)
Definition utility.hpp:151
void deduplicate(Container &entries)
Definition utility.hpp:171
constexpr size_t const_strlen(const char *str)
Definition utility.hpp:142
const T & min(const T &a, const T &b)
Definition utility.hpp:140
void copy_append(Container &dest, const Container &src)
Definition utility.hpp:162
decltype(nullptr) nullptr_t
Definition utility.hpp:28
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1181
#define T(meth, val, expected)
static constexpr bool trailing_allocator
Definition utility.hpp:56
static constexpr bool leading_allocator
Definition utility.hpp:55
static constexpr bool value
Definition utility.hpp:54
decltype(detail::is_class_helper< T >(0)) type
Definition utility.hpp:135
#define R
#define A
void fc_swap(T &a, T &b)
Definition utility.hpp:211