-
Notifications
You must be signed in to change notification settings - Fork 0
/
stdx
904 lines (777 loc) · 34 KB
/
stdx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
#pragma once
#include <stdexcept>
#include <cassert>
#include <utility>
#include <type_traits>
#include <memory>
#include <cstring>
#if defined(_MSC_VER) && !defined(__clang__)
#pragma warning(push)
// Decorated name length exceeded all the time by various STL containers
#pragma warning(disable : 4503)
// Can't do anything about methods not being inlined
#pragma warning(disable : 4714)
// Formal parameters named for documentation purposes
#pragma warning(disable : 4100)
// Constant conditional expressions occur quite often in template code
#pragma warning(disable : 4127)
// Sometimes, using 'this' in initializier lists is unavoidable
#pragma warning(disable : 4355)
// We actually want arrays & PODs to be default-initialized
#pragma warning(disable : 4351)
#pragma warning(disable : 4345)
// Assignment operators suppressed intentionally
#pragma warning(disable : 4512)
// Extern template now standard
#pragma warning(disable : 4231)
// 'override' specifier is now standard
#pragma warning(disable : 4481)
// nameless struct/union
#pragma warning(disable : 4201)
// requiring user-defined ctors: wrong for pointers, generally useless
#pragma warning(disable : 4510)
#pragma warning(disable : 4610)
#if _MSC_VER < 1800
#define CPP11_NO_VARIADIC_TEMPLATES
#endif
#if _MSC_VER < 1900
#define CPP11_NO_MOVE_DEFAULTS
#endif
#endif
#ifndef CPP11_NO_VARIADIC_TEMPLATES
#define CPP11_IF_VARIADIC_TEMPLATES(...) __VA_ARGS__
#else
#define CPP11_IF_VARIADIC_TEMPLATES(...)
#endif
// Forward & move w/o function call
#define FORWARD(T, x) static_cast<T&&>(x)
#define MOVE(x) static_cast<std::remove_reference<decltype(x)>::type&&>(x)
#define MOVE_T(x) static_cast<typename std::remove_reference<decltype(x)>::type&&>(x)
#ifdef _MSC_VER
namespace std { struct random_access_iterator_tag; }
#else
#include <iterator>
#endif
namespace stdx
{
using std::size_t;
using std::ptrdiff_t;
template <class T> inline T min_value(T a, T b) { return (a < b || !(b == b)) ? a : b; } // min by-value w/ preference for non-NaN
template <class T> inline T max_value(T a, T b) { return (a > b || !(b == b)) ? a : b; } // max by-value w/ preference for non-NaN
namespace detail
{
template <class T, size_t Size>
char (&arraylen_helper(T (&)[Size]))[Size];
}
template <class T, class X>
inline T const& as_(X const& x)
{
static_assert(sizeof(T) == sizeof(X), "size mismatch");
union U { X x; T t; char c[sizeof(T)]; };
return reinterpret_cast<U const&>(x).t;
}
template <class T, size_t Size>
inline T* arrayend(T (&a)[Size]) { return &a[0] + Size; }
template <class T, size_t Size>
inline T* strend(T (&a)[Size]) { assert(!a[Size - 1]); return &a[Size - 1]; }
template <class T> struct identity { typedef T type; };
// MSVC' extra invitation in decltype
template <class T>
inline typename std::make_unsigned<T>::type to_unsigned(T sgn) { return sgn; }
template <class T>
inline typename std::make_signed<T>::type to_signed(T usgn) { return usgn; }
namespace detail
{
namespace iterator_delta_types
{
template <class It>
ptrdiff_t operator -(It, It);
template <class Iterator>
struct iterator_delta_types
{
typedef decltype(std::declval<Iterator>() - std::declval<Iterator>()) difference_type;
typedef decltype(to_unsigned(std::declval<Iterator>() - std::declval<Iterator>())) size_type;
};
template <class T>
struct iterator_delta_types<T*>
{
typedef ptrdiff_t difference_type;
typedef size_t size_type;
};
}
}
using detail::iterator_delta_types::iterator_delta_types;
// SFINAE
template <class I>
inline auto maybe_deref_iterator(I it, typename I::iterator_category* = nullptr) -> decltype(*it) { return *it; }
template <class I>
inline auto maybe_deref_iterator(I* it) -> decltype(*it) { return *it; }
CPP11_IF_VARIADIC_TEMPLATES(template <class... Args>)
inline void maybe_deref_iterator(CPP11_IF_VARIADIC_TEMPLATES(Args&&)...) { }
/// Range type.
template <class Iterator>
struct range
{
typedef Iterator iterator;
typedef Iterator const_iterator;
typedef Iterator pointer;
typedef Iterator const_pointer;
iterator first, last;
static void assert_stride_compatible(iterator, iterator) { }
template <class It2>
static void assert_stride_compatible(iterator a, It2 b)
{ static_assert(sizeof(*a) == sizeof(*b), "Range iterator type or stride mismatch"); (void) a; (void) b; }
range() : first(), last() { }
range(iterator f, iterator l) : first((iterator&&) f), last((iterator&&) l) { }
template <class Range>
explicit range(Range& r) : first(r.begin()), last(r.end()) { assert_stride_compatible(begin(), r.begin()); }
template <class Range>
explicit range(Range const& r) : first(r.begin()), last(r.end()) { assert_stride_compatible(begin(), r.begin()); }
template <class T>
range(range<T> const& r) : first(r.first), last(r.last) { assert_stride_compatible(first, r.first); }
void assign(iterator begin, iterator end) { first = begin; last = end; }
template <class Range>
void assign(Range &range) { first = range.begin(); last = range.end(); assert_stride_compatible(begin(), range.begin()); }
// const& to catch pre-POD usage
iterator const& begin() const { return first; }
iterator const& end() const { return last; }
bool empty() const { return (first == last); }
typename iterator_delta_types<iterator>::difference_type delta() const { return last - first; }
typename iterator_delta_types<iterator>::size_type size() const { return last - first; }
template <class Index>
auto operator [](Index i) const -> decltype(*(first + i)) { return *(first + i); }
/// Gets a reference to the first element.
auto operator *() const -> decltype(maybe_deref_iterator(first)) { return *first; }
/// Gets an iterator to the first element.
iterator operator ->() const { return first; }
/// Gets whether this range is non-empty.
operator bool() const { return (first != last); }
/// Same as begin.
pointer data() const { return first; }
/// Same as begin.
const_pointer cdata() const { return first; }
template <class Iterator2>
range<Iterator2> iterator_cast() const { return range<Iterator2>((Iterator2) first, (Iterator2) last); }
};
template <class SrcIt, class DestIt>
struct enable_if_iterator_compatible : std::enable_if<std::is_convertible<SrcIt, DestIt>::value> { };
/// Range type w/ implicit conversion constructors, FOR USE IN PARAMETER LISTS.
template <class Value, class Iterator = Value*>
struct data_range_param : range<Iterator>
{
using range<Iterator>::assert_stride_compatible;
data_range_param() { }
data_range_param(Iterator f, Iterator l) : range<Iterator>(f, l) { }
template <class Range>
data_range_param(Range& r
, typename enable_if_iterator_compatible<typename Range::pointer, Iterator>::type *enable_if_typematch = nullptr
) : range<Iterator>(r.data(), r.data() + r.size()) { assert_stride_compatible(this->data(), r.data()); }
template <class Range>
data_range_param(Range const& r
, typename enable_if_iterator_compatible<typename Range::const_pointer, Iterator>::type *enable_if_typematch = nullptr)
: range<Iterator>(r.data(), r.data() + r.size()) { assert_stride_compatible(this->data(), r.data()); }
};
template <class Type> struct strided_ptr;
/// Strided range type w/ implicit conversion constructors, FOR USE IN PARAMETER LISTS.
template <class Value, class Iterator = strided_ptr<Value>>
struct relaxed_data_range_param : range<Iterator>
{
relaxed_data_range_param() { }
relaxed_data_range_param(Iterator f, Iterator l) : range<Iterator>(f, l) { }
template <class Range>
relaxed_data_range_param(Range& r
, typename enable_if_iterator_compatible<typename Range::pointer, Iterator>::type *enable_if_typematch = nullptr)
: range<Iterator>(Iterator(r.data()), Iterator(r.data() + r.size())) { }
template <class Range>
relaxed_data_range_param(Range const& r
, typename enable_if_iterator_compatible<typename Range::const_pointer, Iterator>::type *enable_if_typematch = nullptr)
: range<Iterator>(Iterator(r.data()), Iterator(r.data() + r.size())) { }
template <class Value2>
range<typename Iterator::template reinterpret_t<Value2>::t> reinterpret() const
{ return range<typename Iterator::template reinterpret_t<Value2>::t>(this->first.template reinterpret<Value2>(), this->last.template reinterpret<Value2>()); }
};
template <class Iterator>
inline range<Iterator> make_range(Iterator begin, Iterator end)
{ return range<Iterator>(begin, end); }
template <class Iterator, class Size>
inline range<Iterator> make_range_n(Iterator begin, Size len)
{ return range<Iterator>(begin, begin + len); }
template <class Element>
inline range<Element*> one_range(Element& begin)
{ return range<Element*>(&begin, &begin + 1); }
template <class Range>
inline range<typename Range::iterator> make_range(Range &r)
{ return range<typename Range::iterator>(r.begin(), r.end()); }
template <class Range>
inline range<typename Range::const_iterator> make_range(Range const &r)
{ return range<typename Range::const_iterator>(r.begin(), r.end()); }
template <class E, size_t N>
inline range<E*> data_range(E(&r)[N])
{ return range<E*>(&r[0], &r[0] + N); }
template <class Iterator>
inline range<Iterator> str_range(Iterator cstr)
{ Iterator end = cstr; [&end](){ while (*end) ++end; }();
return range<Iterator>(cstr, end); }
template <class Char, size_t Size>
inline range<Char*> strlit_range(Char (&cstr)[Size])
{ return range<Char*>(cstr, strend(cstr)); }
template <class Range>
inline range<typename Range::pointer> data_range(Range &r
, typename enable_if_iterator_compatible<decltype(static_cast<Range*>(nullptr)->data()), typename Range::pointer>::type *enable_if_typematch = nullptr)
{ return range<typename Range::pointer>(r.data(), r.data() + r.size()); }
template <class Range>
inline range<typename Range::const_pointer> data_range(Range const &r)
{ return range<typename Range::const_pointer>(r.data(), r.data() + r.size()); }
template <class Range>
inline range<typename Range::pointer> data_range(Range &r, size_t len, size_t offset = 0
, typename enable_if_iterator_compatible<decltype(static_cast<Range*>(nullptr)->data()), typename Range::pointer>::type *enable_if_typematch = nullptr)
{ return range<typename Range::pointer>(r.data() + offset, r.data() + offset + len); }
template <class Range>
inline range<typename Range::const_pointer> data_range(Range const &r, size_t len, size_t offset = 0)
{ return range<typename Range::const_pointer>(r.data() + offset, r.data() + offset + len); }
template <class Type>
struct strided_ptr
{
typedef Type value_type;
typedef value_type* pointer;
typedef value_type& reference;
typedef ptrdiff_t difference_type;
typedef std::random_access_iterator_tag iterator_category;
pointer ptr;
difference_type stride;
strided_ptr()
: ptr( nullptr )
, stride( sizeof(value_type) ) { }
strided_ptr(pointer object, difference_type stride)
: ptr( object )
, stride( stride ) { }
template <class Type2>
strided_ptr(Type2 *object, difference_type stride = sizeof(Type2)
, typename std::enable_if<std::is_convertible<Type2*, pointer>::value>::type *enable_if_typematch = nullptr)
: ptr( object )
, stride( stride ) { }
template <class Type2>
strided_ptr(strided_ptr<Type2> const& right
, typename std::enable_if<std::is_convertible<Type2*, pointer>::value>::type *enable_if_typematch = nullptr)
: ptr( right.ptr )
, stride( right.stride ) { }
template <class Type2>
strided_ptr& operator =(strided_ptr<Type2> const& right)
{ ptr = right.ptr;
stride = right.stride;
return *this; }
template <class Type2>
struct reinterpret_t { typedef strided_ptr<Type2> t; };
template <class Type2>
strided_ptr<Type2> reinterpret() const
{ return strided_ptr<Type2>( reinterpret_cast<Type2*>(ptr), stride ); }
/// Increments the pointer.
strided_ptr& operator ++()
{ ptr = (pointer) &(char&)((*this)[1]);
return *this; }
/// Decrements the pointer.
strided_ptr& operator --()
{ ptr = (pointer) &(char&)((*this)[-1]);
return *this; }
/// Increments the pointer.
strided_ptr operator ++(int)
{ strided_ptr old(*this);
++(*this);
return old; }
/// Decrements the pointer.
strided_ptr operator --(int)
{ strided_ptr old(*this);
--(*this);
return old; }
/// Gets the pointer stored by this strided pointer. Don't call unless you know what you are doing!
pointer get() const { return ptr; }
/// Gets the stride stored by this strided pointer. Don't call unless you know what you are doing!
difference_type get_stride() const { return stride; }
/// Gets the first object referenced by this strided pointer.
reference operator *() const { return *ptr; }
/// Gets the first object referenced by this strided pointer.
pointer operator ->() const { return ptr; }
/// Gets the n-th object referenced by this strided pointer.
reference operator [](difference_type n) const
{ return *pointer((char*) ptr + stride * n); }
friend inline strided_ptr operator +(const strided_ptr &p, ptrdiff_t diff)
{ return strided_ptr( (pointer) &(char&)(p[diff]), p.get_stride() ); }
friend inline strided_ptr operator -(const strided_ptr &p, ptrdiff_t diff)
{ return strided_ptr( (pointer) &(char&)(p[-diff]), p.get_stride() ); }
friend inline bool operator ==(const strided_ptr &l, const strided_ptr &r) { return l.get() == r.get(); }
friend inline bool operator !=(const strided_ptr &l, const strided_ptr &r) { return l.get() != r.get(); }
friend inline bool operator <=(const strided_ptr &l, const strided_ptr &r) { return l.get() <= r.get(); }
friend inline bool operator >=(const strided_ptr &l, const strided_ptr &r) { return l.get() >= r.get(); }
friend inline bool operator <(const strided_ptr &l, const strided_ptr &r) { return l.get() < r.get(); }
friend inline bool operator >(const strided_ptr &l, const strided_ptr &r) { return l.get() > r.get(); }
friend inline ptrdiff_t operator -(const strided_ptr &p, const strided_ptr &q)
{ assert(p.get_stride() == q.get_stride());
return ( (char*) p.get() - (char*) q.get() ) / p.get_stride(); }
};
extern bool const is_debugger_present;
struct noop
{
CPP11_IF_VARIADIC_TEMPLATES(template <class... Args>)
void operator ()(CPP11_IF_VARIADIC_TEMPLATES(Args&&)...) const { }
};
template <class Exception>
class error : public std::runtime_error
{
public:
error(char const* msg)
: runtime_error(msg) { }
error(std::string const& msg)
: runtime_error(msg) { }
static bool break_on_error;
};
template <class Exception>
bool error<Exception>::break_on_error = true;
template <class Exception>
inline bool break_on_error(error<Exception> const&) { return error<Exception>::break_on_error; }
inline bool break_on_error(...) { return false; }
template <class Pointer>
struct no_delete
{
typedef Pointer pointer;
void operator ()(pointer ptr) const { }
};
template < class Handle, class Deleter = no_delete<Handle> >
class unique_handle : public std::unique_ptr<Handle, Deleter>
{
public:
// Need fully qualified template, otherwise compiler crashes
typedef typename std::unique_ptr<Handle, Deleter>::pointer pointer;
// Inherit constructors
unique_handle(std::nullptr_t = nullptr) { }
explicit unique_handle(pointer value)
: unique_handle::unique_ptr(MOVE_T(value)) { }
unique_handle(unique_handle &&right)
: unique_handle::unique_ptr(MOVE_T(right)) { }
unique_handle& operator =(unique_handle &&right)
{
typedef typename unique_handle::unique_ptr unique_ptr_t;
this->unique_ptr_t::swap(right);
return *this;
}
pointer* rebind()
{
this->reset();
static_assert(sizeof(*this) == sizeof(pointer), "unique_handle not binary compatible!");
return &reinterpret_cast<pointer&>(*this);
}
pointer const& get() const
{
static_assert(sizeof(*this) == sizeof(pointer), "unique_handle not binary compatible!");
return reinterpret_cast<pointer const&>(*this);
}
operator pointer() const { return get(); }
bool valid() const { return get() != pointer(); }
private:
// strictly noncopyable
template <class T>
unique_handle(unique_handle<T> const &right);
};
template <class Pointer, void (*Deleter)(Pointer)>
struct fun_delete
{
typedef Pointer pointer;
void operator ()(pointer ptr) const { (*Deleter)(ptr); }
typedef std::unique_ptr< Pointer, fun_delete<Pointer, Deleter> > pointer_type;
typedef stdx::unique_handle< Pointer, fun_delete<Pointer, Deleter> > handle_type;
};
template <class Handle, Handle Invalid>
struct nullable_handle
{
Handle p;
nullable_handle(std::nullptr_t = nullptr) : p(Invalid) { }
nullable_handle(Handle p) : p(p) { }
operator Handle() const { return p; }
};
class noncopyable
{
noncopyable(noncopyable const&);
noncopyable& operator =(noncopyable const&);
public:
noncopyable() { }
};
template <class RefObj>
class own : public noncopyable, public RefObj
{
public:
typedef RefObj pointer;
// "inherit" constructors
own() : RefObj() { }
own(std::nullptr_t) : RefObj(nullptr) { }
explicit own(pointer&& value)
: RefObj(MOVE_T(value)) { value.forget(); }
explicit own(pointer const& value)
: RefObj(value) { }
// movement
own(own&& right)
: RefObj(MOVE_T(right)) { right.forget(); }
own& operator =(own right)
{
using std::swap;
swap(static_cast<RefObj&>(*this), static_cast<RefObj&>(right));
return *this;
}
pointer detach_ptr()
{
pointer r = *this;
// note: forget method is a requirement for RefObj type!
this->forget();
return r;
}
// deleter
~own()
{
// note: destroy method is a requirement for RefObj type!
this->destroy();
}
};
template <class RefObj>
struct ref_base
{
RefObj ref;
RefObj operator ->() const { return ref; }
RefObj const& get() const { return ref; }
operator RefObj() const { return ref; }
};
// compare bytes of unequally-sized memory blocks
using std::memcmp;
inline int memcmp(void const* a, size_t sa, void const* b, size_t sb)
{
int prefixCmp = std::memcmp(a, b, min_value(sa, sb));
return (prefixCmp != 0 || sa == sb) ? prefixCmp : (sa < sb) ? -1 : 1;
}
template <class Range1, class Range2>
inline int memcmp(Range1 const& a, Range2 const& b)
{ return memcmp(a.data(), sizeof(*a.data()) * a.size(), b.data(), sizeof(*b.data()) * b.size()); }
template <class Range1, class Range2>
inline bool memeq(Range1 const& a, Range2 const& b)
{ return memcmp(a, b) == 0; }
template <class Range1, class Range2>
inline auto mem_less(Range1 const&, Range2 const&) -> decltype(&memcmp<Range1, Range2>)
{ return &memcmp<Range1, Range2>; }
template <class Range1, class Range2>
inline auto mem_equal(Range1 const& a, Range2 const& b) -> decltype(&memeq<Range1, Range2>)
{ return &memeq<Range1, Range2>; }
inline bool streq(char const* a, char const* b) { return strcmp(a, b) == 0; }
inline bool strieq(char const* a, char const* b)
#ifdef WIN32
{ return _stricmp(a, b) == 0; }
#else
{ return strcasecmp(a, b) == 0; }
#endif
inline bool check_flag(char const* arg, char const* flag)
{ return (arg[0] == '/' || arg[0] == '-') && strieq(arg + 1, flag); }
template <class S>
struct signature { typedef S type; };
template <class S>
struct signature<S*> { typedef S type; };
template <class S>
struct signature<S&> { typedef S type; };
template <class S, class C>
struct signature<S C::*> { typedef S type; typedef C clazz; };
template <class S>
struct return_type { typedef S type; };
template <class R>
struct return_type<R()> { typedef R type; };
template <class R, class A1>
struct return_type<R(A1)> { typedef R type; };
template <class R, class A1, class A2>
struct return_type<R(A1, A2)> { typedef R type; };
template <class R, class A1, class A2, class A3>
struct return_type<R(A1, A2, A3)> { typedef R type; };
template <class R, class A1, class A2, class A3, class A4>
struct return_type<R(A1, A2, A3, A4)> { typedef R type; };
template <class R, class A1, class A2, class A3, class A4, class A5>
struct return_type<R(A1, A2, A3, A4, A5)> { typedef R type; };
struct fun_ref_data
{
void* self;
};
template <class Signature>
struct fun_ref : fun_ref_data
{
typedef Signature signature_type;
template <class Callable>
struct callable_dispatcher
{
template <class S>
struct dispatcher;
template <class R>
struct dispatcher<R()> {
static R call(fun_ref_data ref)
{ return static_cast<R>( (*static_cast<Callable*>(ref.self))() ); }
};
template <class R, class A1>
struct dispatcher<R(A1)> {
static R call(fun_ref_data ref, A1 a1)
{ return static_cast<R>( (*static_cast<Callable*>(ref.self))(FORWARD(A1, a1)) ); }
};
template <class R, class A1, class A2>
struct dispatcher<R(A1, A2)> {
static R call(fun_ref_data ref, A1 a1, A2 a2)
{ return static_cast<R>( (*static_cast<Callable*>(ref.self))(FORWARD(A1, a1), FORWARD(A2, a2)) ); }
};
template <class R, class A1, class A2, class A3>
struct dispatcher<R(A1, A2, A3)> {
static R call(fun_ref_data ref, A1 a1, A2 a2, A3 a3)
{ return static_cast<R>( (*static_cast<Callable*>(ref.self))(FORWARD(A1, a1), FORWARD(A2, a2), FORWARD(A3, a3)) ); }
};
template <class R, class A1, class A2, class A3, class A4>
struct dispatcher<R(A1, A2, A3, A4)> {
static R call(fun_ref_data ref, A1 a1, A2 a2, A3 a3, A4 a4)
{ return static_cast<R>( (*static_cast<Callable*>(ref.self))(FORWARD(A1, a1), FORWARD(A2, a2), FORWARD(A3, a3), FORWARD(A4, a4)) ); }
};
template <class R, class A1, class A2, class A3, class A4, class A5>
struct dispatcher<R(A1, A2, A3, A4, A5)> {
static R call(fun_ref_data ref, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5)
{ return static_cast<R>( (*static_cast<Callable*>(ref.self))(FORWARD(A1, a1), FORWARD(A2, a2), FORWARD(A3, a3), FORWARD(A4, a4), FORWARD(A5, a5)) ); }
};
};
template <class Class, signature_type (Class::*Callable)>
struct memfun_dispatcher
{
template <class S>
struct dispatcher;
template <class R>
struct dispatcher<R()> {
static R call(fun_ref_data ref)
{ return static_cast<R>( (static_cast<Class*>(ref.self)->*Callable)() ); }
};
template <class R, class A1>
struct dispatcher<R(A1)> {
static R call(fun_ref_data ref, A1 a1)
{ return static_cast<R>( (static_cast<Class*>(ref.self)->*Callable)(FORWARD(A1, a1)) ); }
};
template <class R, class A1, class A2>
struct dispatcher<R(A1, A2)> {
static R call(fun_ref_data ref, A1 a1, A2 a2)
{ return static_cast<R>( (static_cast<Class*>(ref.self)->*Callable)(FORWARD(A1, a1), FORWARD(A2, a2)) ); }
};
template <class R, class A1, class A2, class A3>
struct dispatcher<R(A1, A2, A3)> {
static R call(fun_ref_data ref, A1 a1, A2 a2, A3 a3)
{ return static_cast<R>( (static_cast<Class*>(ref.self)->*Callable)(FORWARD(A1, a1), FORWARD(A2, a2), FORWARD(A3, a3)) ); }
};
template <class R, class A1, class A2, class A3, class A4>
struct dispatcher<R(A1, A2, A3, A4)> {
static R call(fun_ref_data ref, A1 a1, A2 a2, A3 a3, A4 a4)
{ return static_cast<R>( (static_cast<Class*>(ref.self)->*Callable)(FORWARD(A1, a1), FORWARD(A2, a2), FORWARD(A3, a3), FORWARD(A4, a4)) ); }
};
template <class R, class A1, class A2, class A3, class A4, class A5>
struct dispatcher<R(A1, A2, A3, A4, A5)> {
static R call(fun_ref_data ref, A1 a1, A2 a2, A3 a3, A4 a4, A5 a5)
{ return static_cast<R>( (static_cast<Class*>(ref.self)->*Callable)(FORWARD(A1, a1), FORWARD(A2, a2), FORWARD(A3, a3), FORWARD(A4, a4), FORWARD(A5, a5)) ); }
};
};
typedef decltype(&callable_dispatcher<void>::template dispatcher<signature_type>::call) dispatch_ptr;
dispatch_ptr dispatch;
fun_ref(std::nullptr_t) : dispatch(nullptr) { this->self = nullptr; }
template <class Callable>
fun_ref(Callable&& callable
, typename std::enable_if<!std::is_same<typename std::decay<Callable>::type, fun_ref>::value>::type *enable_if_not_copy = nullptr)
: dispatch(&callable_dispatcher<typename std::remove_reference<Callable>::type>::template dispatcher<signature_type>::call)
{ this->self = &callable; } // not enforced as commonly used as param type: static_assert(std::is_pointer<Callable>::value || std::is_reference<Callable>::value, "Error: Temporaries not stored by fun_ref");
template <class Class>
fun_ref(Class&& obj, dispatch_ptr dispatch)
: dispatch(dispatch)
{ this->self = &obj; }
};
template <class MemFunT, MemFunT MemFun, class Class>
fun_ref<typename signature<MemFunT>::type> mem_fun_ref(Class&& obj)
{
typedef fun_ref<typename signature<MemFunT>::type> fun_ref;
return fun_ref(
obj
, &fun_ref::template memfun_dispatcher<typename std::remove_reference<Class>::type, MemFun>::template dispatcher<typename fun_ref::signature_type>::call);
}
struct freely_stored
{
virtual void destroy() const = 0;
struct deleter {
void operator ()(freely_stored const* ptr) {
if (ptr)
ptr->destroy();
}
};
protected:
~freely_stored() { }
};
struct onetype_store
{
std::unique_ptr<freely_stored, freely_stored::deleter> store;
template <class T>
struct value_store : freely_stored
{
T value;
value_store() { }
template <class V>
value_store(V&& v) : value(FORWARD(V, v)) { }
void destroy() const override { delete this; }
};
// ugly: lambdas not assignable
template <class T, bool Assignable = std::is_copy_assignable<T>::value || std::is_move_assignable<T>::value>
struct assign_helper {
template <class V>
static void assign(T& x, V&& v) { x = FORWARD(V, v); }
};
template <class T>
struct assign_helper<T, false> {
template <class V>
static void assign(T& x, V&& v) { x.~T(); new(&x) T(FORWARD(V, v)); }
};
// store & return reference TO STORED
template <class V>
V& operator =(V&& v)
{
typedef typename std::decay<V>::type T;
T* stored;
if (auto p = store.get())
{
assert (typeid(*p) == typeid(value_store<V>));
stored = &static_cast<value_store<T>*>(p)->value;
assign_helper<T>::assign(*stored, FORWARD(V, v));
}
else
{
auto newStore = new value_store<T>(FORWARD(V, v));
store.reset(newStore);
stored = &newStore->value;
}
return *stored;
}
// return reference to store (UNDEFINED BEHAVIOR if nothing stored!)
template <class T>
T& get()
{
auto p = store.get();
assert (p);
assert (typeid(*p) == typeid(value_store<T>));
return static_cast<value_store<T>*>(p)->value;
}
};
// Allows for values of ONE unknown type to be stored alongside a reference
template <class Ref>
struct ref_with_onetype_store : private onetype_store
{
Ref ref;
ref_with_onetype_store() : ref() { }
ref_with_onetype_store(std::nullptr_t) : ref(nullptr) { }
template <class V>
ref_with_onetype_store& operator =(V&& v)
{
ref = onetype_store::operator =(FORWARD(V, v));
return *this;
}
};
template <int CounterStart>
struct enumerable
{
// C++ awkwardly forbids explicit specialization of nested templates + complex expressions in partial specializations,
// CounterCutoff is a workaround for both
template <int Counter, int CounterCutoff = CounterStart - 1>
struct enum_iterator : enum_iterator<Counter - 1, CounterCutoff> { static int const value = Counter; static int const next_value = Counter - 1; };
template <int CounterCutoff>
struct enum_iterator<CounterCutoff, CounterCutoff> { static int const value = 0; };
template <class This, class Op>
friend void enumerate_from(This&, enum_iterator<CounterStart - 1>, Op&&) { }
template <class This, class Op>
friend void reflect_from(This*, enum_iterator<CounterStart - 1>, Op&&) { }
};
#define ENUMERABLE ::stdx::enumerable<__COUNTER__>
#define MAKE_ENUM_ITERATOR() enum_iterator<__COUNTER__>()
#define NEXT_ENUM_ITERATOR(i) enum_iterator<i.next_value>()
#define ENUMERABLE_MEMBER(basetype, memname, ...) \
struct _##memname##_type : basetype \
{ \
MOVE_BASE(_##memname##_type, basetype) \
static int const enum_counter = __COUNTER__; \
typedef enum_iterator<enum_counter> enum_iterator; \
typedef basetype enum_type; \
static char const* enum_name() { return #memname; } \
_##memname##_type() : basetype(__VA_ARGS__) { } \
_##memname##_type(basetype&& right) : basetype(std::move(right)) { } \
_##memname##_type(basetype const& right) : basetype(right) { } \
using basetype::operator =; \
} memname; \
template <class This, class Op> \
friend void enumerate_from(This& self, _##memname##_type::enum_iterator i, Op&& op) \
{ \
enumerate_from(self, NEXT_ENUM_ITERATOR(i), op); \
op(self.memname); \
} \
template <class This, class Op> \
friend void reflect_from(This *self, _##memname##_type::enum_iterator i, Op&& op) \
{ \
reflect_from(self, NEXT_ENUM_ITERATOR(i), op); \
op(&This::memname); \
}
#define ENUMERABLE_FINISH(type) \
typedef enum_iterator<__COUNTER__> enum_iterator_begin; \
template <class Op> \
void enumerate(Op&& op) { enumerate_from(*this, enum_iterator_begin(), op); } \
template <class Op> \
void enumerate(Op&& op) const { enumerate_from(*this, enum_iterator_begin(), op); } \
template <class Op> \
static void reflect(Op&& op) { reflect_from(static_cast<type*>(nullptr), enum_iterator_begin(), op); }
} // namespace
#define arraylen(x) sizeof(::stdx::detail::arraylen_helper(x))
#ifdef _MSC_VER
#define debugbreak() __debugbreak()
#else
#define debugbreak() __builtin_trap()
#endif
#if defined(NDEBUG) && !defined(DEBUG_EXCEPTIONS)
#define throwx(x) throw x
#else
#define throwx(x) do { using ::stdx::break_on_error; auto&& exc = x; if (break_on_error(exc) && ::stdx::is_debugger_present) debugbreak(); throw exc; } while (false)
#endif
#define TOKEN_TO_STRING(x) #x
#define VALUE_TO_STRING(x) TOKEN_TO_STRING(x)
#define TOKEN_CONCAT(x, y) x##y
#define VALUE_CONCAT(x, y) TOKEN_CONCAT(x, y)
#define FILE_LINE __FILE__ "(" VALUE_TO_STRING(__LINE__) ")"
#define FILE_LINE_PREFIX FILE_LINE ": "
#define AUTO_VAR auto VALUE_CONCAT(autoVar, __COUNTER__)
// MSVC <= 2013 shame
#ifdef CPP11_NO_MOVE_DEFAULTS
#define MSVC_EXPAND(x) x
#define MOVE_GENERATE(clazz, generator, ...) \
clazz(clazz &&right) : MSVC_EXPAND(generator##_CONSTRUCT(right, __VA_ARGS__)) { } \
clazz& operator =(clazz &&right) { MSVC_EXPAND(generator##_ASSIGN(right, __VA_ARGS__)) return *this; }
#define MOVE_BASE_CONSTRUCT(right, base) base(std::move(right))
#define MOVE_BASE_ASSIGN(right, base) this->base::operator =(std::move(right))
#define MOVE_MEMBER_CONSTRUCT(right, member) member(std::move(right.member))
#define MOVE_MEMBER_ASSIGN(right, member) std::swap(member, right.member)
#define MOVE_1_CONSTRUCT(right, what, that) MOVE_##what##_CONSTRUCT(right, that)
#define MOVE_1_ASSIGN(right, what, that) MOVE_##what##_ASSIGN(right, that);
#define MOVE_2_CONSTRUCT(right, what, that, ...) MOVE_##what##_CONSTRUCT(right, that), MSVC_EXPAND(MOVE_1_CONSTRUCT(right, __VA_ARGS__))
#define MOVE_2_ASSIGN(right, what, that, ...) MOVE_##what##_ASSIGN(right, that); MSVC_EXPAND(MOVE_1_ASSIGN(right, __VA_ARGS__))
#define MOVE_3_CONSTRUCT(right, what, that, ...) MOVE_##what##_CONSTRUCT(right, that), MSVC_EXPAND(MOVE_2_CONSTRUCT(right, __VA_ARGS__))
#define MOVE_3_ASSIGN(right, what, that, ...) MOVE_##what##_ASSIGN(right, that); MSVC_EXPAND(MOVE_2_ASSIGN(right, __VA_ARGS__))
#define MOVE_4_CONSTRUCT(right, what, that, ...) MOVE_##what##_CONSTRUCT(right, that), MSVC_EXPAND(MOVE_3_CONSTRUCT(right, __VA_ARGS__))
#define MOVE_4_ASSIGN(right, what, that, ...) MOVE_##what##_ASSIGN(right, that); MSVC_EXPAND(MOVE_3_ASSIGN(right, __VA_ARGS__))
#define MOVE_5_CONSTRUCT(right, what, that, ...) MOVE_##what##_CONSTRUCT(right, that), MSVC_EXPAND(MOVE_4_CONSTRUCT(right, __VA_ARGS__))
#define MOVE_5_ASSIGN(right, what, that, ...) MOVE_##what##_ASSIGN(right, that); MSVC_EXPAND(MOVE_4_ASSIGN(right, __VA_ARGS__))
#define MOVE_6_CONSTRUCT(right, what, that, ...) MOVE_##what##_CONSTRUCT(right, that), MSVC_EXPAND(MOVE_5_CONSTRUCT(right, __VA_ARGS__))
#define MOVE_6_ASSIGN(right, what, that, ...) MOVE_##what##_ASSIGN(right, that); MSVC_EXPAND(MOVE_5_ASSIGN(right, __VA_ARGS__))
#define MOVE_7_CONSTRUCT(right, what, that, ...) MOVE_##what##_CONSTRUCT(right, that), MSVC_EXPAND(MOVE_6_CONSTRUCT(right, __VA_ARGS__))
#define MOVE_7_ASSIGN(right, what, that, ...) MOVE_##what##_ASSIGN(right, that); MSVC_EXPAND(MOVE_6_ASSIGN(right, __VA_ARGS__))
#define MOVE_8_CONSTRUCT(right, what, that, ...) MOVE_##what##_CONSTRUCT(right, that), MSVC_EXPAND(MOVE_7_CONSTRUCT(right, __VA_ARGS__))
#define MOVE_8_ASSIGN(right, what, that, ...) MOVE_##what##_ASSIGN(right, that); MSVC_EXPAND(MOVE_7_ASSIGN(right, __VA_ARGS__))
#else
#define MOVE_GENERATE(clazz, generator, ...) // move constructors & operators automatically defined, welcome to the future!
#endif
#define MOVE_BASE(clazz, base) MOVE_GENERATE(clazz, MOVE_1, BASE, base)
#define MOVE_MEMBER(clazz, member) MOVE_GENERATE(clazz, MOVE_1, MEMBER, member)
#define MOVE_BASE_MEMBER(clazz, base, member) MOVE_GENERATE(clazz, MOVE_2, BASE, base, MEMBER, member)
#define INPLACE_OSTREAM(t, x) static_cast<t&&>(t() << x)
#define INPLACE_SSTREAM(x) INPLACE_OSTREAM(std::stringstream, x)
#define INPLACE_STR(x) INPLACE_SSTREAM(x).str()
#define ENUM_ENTRY_DEFINE(name, ...) name __VA_ARGS__,
#define ENUM_ENTRY_C_STR_SWITCH_CASE(name, ...) case c_str_enum_type::name: return #name;
#define ENUM_ENTRIES(x) x(ENUM_ENTRY_DEFINE)
#define ENUM_C_STR(type, x) inline char const* c_str(type e) { typedef type c_str_enum_type; switch (e) { x(ENUM_ENTRY_C_STR_SWITCH_CASE) } return "unknown_enum_value"; }