-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathzpp_throwing.h
1894 lines (1702 loc) · 56.2 KB
/
zpp_throwing.h
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
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef ZPP_THROWING_H
#define ZPP_THROWING_H
#include <cstring>
#include <memory>
#include <new>
#include <stdexcept>
#include <string_view>
#include <system_error>
#include <tuple>
#include <type_traits>
#if __has_include(<coroutine>)
#include <coroutine>
#else
#include <experimental/coroutine>
#endif
namespace zpp
{
/**
* User defined error domain.
*/
template <typename ErrorCode>
std::conditional_t<std::is_void_v<ErrorCode>, ErrorCode, void> err_domain;
/**
* The error domain which responsible for translating error codes to
* error messages.
*/
class error_domain
{
public:
using integral_type = int;
/**
* Returns the error domain name.
*/
virtual std::string_view name() const noexcept = 0;
/**
* Return the error message for a given error code.
* For success codes, it is unspecified what value is returned.
* For convenience, you may return zpp::error::no_error for success.
* All other codes must return non empty string views.
*/
virtual std::string_view
message(integral_type code) const noexcept = 0;
/**
* Returns true if success code, else false.
*/
bool success(integral_type code) const
{
return code == m_success_code;
}
protected:
/**
* Creates an error domain whose success code is 'success_code'.
*/
constexpr error_domain(integral_type success_code) :
m_success_code(success_code)
{
}
/**
* Destroys the error domain.
*/
~error_domain() = default;
private:
/**
* The success code.
*/
integral_type m_success_code{};
};
/**
* Creates an error domain, whose name and success
* code are specified, as well as a message translation
* logic that returns the error message for every error code.
* Note: message translation must not throw.
*/
template <typename ErrorCode, typename Messages>
constexpr auto make_error_domain(std::string_view name,
ErrorCode success_code,
Messages && messages)
{
// Create a domain with the name and messages.
class domain final : public error_domain,
private std::remove_reference_t<Messages>
{
public:
constexpr domain(std::string_view name,
ErrorCode success_code,
Messages && messages) :
error_domain(std::underlying_type_t<ErrorCode>(success_code)),
std::remove_reference_t<Messages>(
std::forward<Messages>(messages)),
m_name(name)
{
}
std::string_view name() const noexcept override
{
return m_name;
}
std::string_view message(int code) const noexcept override
{
return this->operator()(ErrorCode{code});
}
private:
std::string_view m_name;
} domain(name, success_code, std::forward<Messages>(messages));
// Return the domain.
return domain;
}
/**
* Represents an error to be initialized from an error code
* enumeration.
* The error code enumeration must have 'int' as underlying type.
* Defining an error code enum and a domain for it goes as follows.
* Example:
* ```cpp
* enum class my_error
* {
* success = 0,
* operation_not_permitted = 1,
* general_failure = 2,
* };
*
* template <>
* inline constexpr auto zpp::err_domain<my_error> = zpp::make_error_domain(
* "my_error", my_error::success, [](auto code) constexpr->std::string_view {
* switch (code) {
* case my_error::operation_not_permitted:
* return "Operation not permitted.";
* case my_error::general_failure:
* return "General failure.";
* default:
* return "Unspecified error.";
* }
* });
* ```
*/
class error
{
public:
using integral_type = error_domain::integral_type;
/**
* Disables default construction.
*/
error() = delete;
/**
* Constructs an error from an error code enumeration, the
* domain is looked by using argument dependent lookup on a
* function named 'domain' that receives the error code
* enumeration value.
*/
template <typename ErrorCode>
error(ErrorCode error_code) requires std::is_enum_v<ErrorCode>
: m_domain(std::addressof(err_domain<ErrorCode>)),
m_code(std::underlying_type_t<ErrorCode>(error_code))
{
}
/**
* Constructs an error from an error code enumeration/integral type,
* the domain is given explicitly in this overload.
*/
template <typename ErrorCode>
error(ErrorCode error_code, const error_domain & domain) :
m_domain(std::addressof(domain)),
m_code(static_cast<integral_type>(error_code))
{
}
/**
* Returns the error domain.
*/
const error_domain & domain() const
{
return *m_domain;
}
/**
* Returns the error code.
*/
int code() const
{
return m_code;
}
/**
* Returns the error message. Calling this on
* a success error is implementation defined according
* to the error domain.
*/
std::string_view message() const
{
return m_domain->message(m_code);
}
/**
* Returns true if the error indicates success, else false.
*/
explicit operator bool() const noexcept
{
return m_domain->success(m_code);
}
/**
* Returns true if the error indicates success, else false.
*/
bool success() const noexcept
{
return m_domain->success(m_code);
}
/**
* Returns true if the error indicates failure, else false.
*/
bool failure() const noexcept
{
return !m_domain->success(m_code);
}
/**
* No error message value.
*/
static constexpr std::string_view no_error{};
private:
/**
* The error domain.
*/
const error_domain * m_domain{};
/**
* The error code.
*/
integral_type m_code{};
};
#if __has_include(<coroutine>)
template <typename... Arguments>
using coroutine_handle = std::coroutine_handle<Arguments...>;
using suspend_always = std::suspend_always;
using suspend_never = std::suspend_never;
#else
template <typename... Arguments>
using coroutine_handle = std::experimental::coroutine_handle<Arguments...>;
using suspend_always = std::experimental::suspend_always;
using suspend_never = std::experimental::suspend_never;
#endif
/**
* Determine the throwing state via error domain placeholders.
* @{
*/
enum class rethrow_error
{
};
template <>
inline constexpr auto err_domain<rethrow_error> = make_error_domain(
{}, rethrow_error{}, [](auto) constexpr->std::string_view {
return {};
});
enum class throwing_exception
{
};
template <>
inline constexpr auto err_domain<throwing_exception> = make_error_domain(
{}, throwing_exception{}, [](auto) constexpr->std::string_view {
return {};
});
/**
* @}
*/
/**
* Yield `rethrow` for rethrowing exceptions.
*/
struct rethrow_t
{
};
constexpr inline rethrow_t rethrow;
/**
* Return void for void returning coroutines.
*/
struct void_t
{
};
constexpr inline void_t void_v;
struct dynamic_object
{
const void * type_id{};
void * address{};
};
/**
* Exception object type erasure.
*/
class exception_object
{
public:
virtual struct dynamic_object dynamic_object() noexcept = 0;
virtual ~exception_object() = 0;
static constexpr struct dynamic_object null_dynamic_object = {};
};
inline exception_object::~exception_object() = default;
/**
* Define base classes for a particular exception.
*/
template <typename... Bases>
struct define_exception_bases
{
};
/**
* Define an exception to be supported by the framework.
* ```cpp
* template<>
* struct zpp::define_exception<my_exception>
* {
* using type = zpp::define_exception_bases<exception_base_1,
* exception_base_2>;
* };
* ```
*/
template <typename Type>
struct define_exception;
template <typename Type>
using define_exception_t = typename define_exception<Type>::type;
template <typename Allocator>
struct exception_object_delete
{
void operator()(exception_object * pointer)
{
if constexpr (std::is_void_v<Allocator>) {
delete pointer;
} else {
Allocator allocator;
std::allocator_traits<Allocator>::destroy(allocator, pointer);
std::allocator_traits<Allocator>::deallocate(
allocator, reinterpret_cast<std::byte *>(pointer), 0);
}
}
};
template <typename Allocator>
using exception_ptr =
std::unique_ptr<exception_object, exception_object_delete<Allocator>>;
template <typename Type, typename Allocator>
auto make_exception_object(auto &&... arguments)
{
if constexpr (std::is_void_v<Allocator>) {
return static_cast<exception_object *>(
new Type(std::forward<decltype(arguments)>(arguments)...));
} else {
Allocator allocator;
auto allocated = std::allocator_traits<Allocator>::allocate(
allocator, sizeof(Type));
if (!allocated) {
return static_cast<exception_object *>(nullptr);
}
std::allocator_traits<Allocator>::construct(
allocator,
reinterpret_cast<Type *>(allocated),
std::forward<decltype(arguments)>(arguments)...);
return static_cast<exception_object *>(
reinterpret_cast<Type *>(allocated));
}
}
namespace detail
{
union type_info_entry
{
constexpr type_info_entry(std::size_t number) : number(number)
{
}
constexpr type_info_entry(const void * pointer) : pointer(pointer)
{
}
constexpr type_info_entry(void * (*function)(void *) noexcept) :
function(function)
{
}
std::size_t number;
const void * pointer;
void * (*function)(void *);
};
template <typename Source, typename Destination>
void * erased_static_cast(void * source) noexcept
{
return static_cast<Destination *>(static_cast<Source *>(source));
}
template <typename Source, typename Destination>
constexpr auto make_erased_static_cast() noexcept
{
return &erased_static_cast<Source, Destination>;
}
template <typename Type>
constexpr auto type_id() noexcept -> const void *;
template <typename Type, typename... Bases>
struct type_information
{
static_assert((... && std::is_base_of_v<Bases, Type>),
"Bases must be base classes of Type.");
// Construct the type information.
static constexpr type_info_entry info[] = {
sizeof...(Bases), // Number of source classes.
type_id<Bases>()..., // Source classes type information.
make_erased_static_cast<Type,
Bases>()..., // Casts from derived to
// base.
};
};
template <typename Type>
struct type_information<Type>
{
// Construct the type information.
static constexpr type_info_entry info[] = {
std::size_t{}, // Number of source classes.
};
};
template <typename Type, typename... Bases>
constexpr auto type_id(define_exception_bases<Bases...>) noexcept
{
return &type_information<Type, Bases...>::info;
}
template <typename Type>
constexpr auto type_id(define_exception_bases<>) noexcept
{
return &type_information<Type>::info;
}
template <typename Type>
constexpr auto type_id() noexcept -> const void *
{
using type = std::remove_cv_t<std::remove_reference_t<Type>>;
return detail::type_id<type>(define_exception_t<type>{});
}
inline void * dyn_cast(const void * base,
void * most_derived_pointer,
const void * most_derived)
{
// If the most derived and the base are the same.
if (most_derived == base) {
return most_derived_pointer;
}
// Fetch the type info entries.
auto type_info_entries =
reinterpret_cast<const type_info_entry *>(most_derived);
// The number of base types.
auto number_of_base_types = type_info_entries->number;
// The bases type information.
auto bases = type_info_entries + 1;
// The erased static cast function matching base.
auto erased_static_cast = bases + number_of_base_types;
for (std::size_t index = 0; index < number_of_base_types; ++index) {
// Converting the most derived to the type whose id is
// bases[index].number and perform the conversion from this
// type.
auto result = dyn_cast(
base,
erased_static_cast[index].function(most_derived_pointer),
bases[index].pointer);
// If the conversion succeeded, return the result.
if (result) {
return result;
}
}
// No conversion was found.
return nullptr;
}
template <typename Type>
struct catch_type
: catch_type<decltype(&std::remove_cv_t<
std::remove_reference_t<Type>>::operator())>
{
};
template <typename ReturnType, typename Argument>
struct catch_type<ReturnType (*)(Argument)>
{
using type = Argument;
};
template <typename Type, typename ReturnType, typename Argument>
struct catch_type<ReturnType (Type::*)(Argument)>
{
using type = Argument;
};
template <typename Type, typename ReturnType, typename Argument>
struct catch_type<ReturnType (Type::*)(Argument) const>
{
using type = Argument;
};
template <typename ReturnType, typename Argument>
struct catch_type<ReturnType (*)(Argument) noexcept>
{
using type = Argument;
};
template <typename Type, typename ReturnType, typename Argument>
struct catch_type<ReturnType (Type::*)(Argument) noexcept>
{
using type = Argument;
};
template <typename Type, typename ReturnType, typename Argument>
struct catch_type<ReturnType (Type::*)(Argument) const noexcept>
{
using type = Argument;
};
template <typename ReturnType>
struct catch_type<ReturnType (*)()>
{
using type = void;
};
template <typename Type, typename ReturnType>
struct catch_type<ReturnType (Type::*)()>
{
using type = void;
};
template <typename Type, typename ReturnType>
struct catch_type<ReturnType (Type::*)() const>
{
using type = void;
};
template <typename ReturnType>
struct catch_type<ReturnType (*)() noexcept>
{
using type = void;
};
template <typename Type, typename ReturnType>
struct catch_type<ReturnType (Type::*)() noexcept>
{
using type = void;
};
template <typename Type, typename ReturnType>
struct catch_type<ReturnType (Type::*)() const noexcept>
{
using type = void;
};
template <typename Type>
using catch_type_t = typename catch_type<Type>::type;
template <typename Type>
struct catch_value_type
{
using type =
std::remove_cv_t<std::remove_reference_t<catch_type_t<Type>>>;
};
template <typename Type>
using catch_value_type_t = typename catch_value_type<Type>::type;
} // namespace detail
/**
* The exit condition of the coroutine - A value, or error/exception.
*/
template <typename Type, typename Allocator>
struct exit_condition
{
using exception_type = exception_object *;
using error_type = class error;
using value_type = std::conditional_t<
std::is_void_v<Type>,
std::nullptr_t,
std::conditional_t<
std::is_reference_v<Type>,
std::add_pointer_t<std::remove_reference_t<Type>>,
Type>>;
constexpr exit_condition() noexcept :
m_error_domain(std::addressof(err_domain<rethrow_error>))
{
}
template <typename..., typename Dependent = Type>
constexpr explicit exit_condition(void_t) requires std::is_void_v<Dependent>
: m_error_domain(nullptr)
{
}
constexpr explicit exit_condition(auto && value) :
m_error_domain(nullptr),
m_return_value(std::forward<decltype(value)>(value))
{
}
constexpr exit_condition(exit_condition && other) noexcept
{
if (other.is_value()) {
if constexpr (!std::is_void_v<Type>) {
if constexpr (!std::is_reference_v<Type>) {
::new (std::addressof(m_return_value))
Type(std::move(other.m_return_value));
} else {
m_return_value = other.m_return_value;
}
}
} else {
m_error_domain = other.m_error_domain;
std::memcpy(&m_error, &other.m_error, sizeof(m_error));
}
}
constexpr ~exit_condition()
{
if constexpr (!std::is_void_v<Type> &&
!std::is_trivially_destructible_v<Type>) {
if (is_value()) {
m_return_value.~Type();
}
}
}
constexpr bool is_exception() const noexcept
{
return m_error_domain ==
std::addressof(err_domain<throwing_exception>);
}
constexpr bool is_value() const noexcept
{
return !m_error_domain;
}
constexpr bool success() const noexcept
{
return !m_error_domain;
}
constexpr bool failure() const noexcept
{
return m_error_domain;
}
constexpr bool is_error() const noexcept
{
return !is_value() && !is_exception();
}
constexpr auto is_rethrow() const noexcept
{
return m_error_domain == std::addressof(err_domain<rethrow_error>);
}
constexpr explicit operator bool() const noexcept
{
return success();
}
constexpr decltype(auto) value() && noexcept
{
if constexpr (std::is_same_v<Type, decltype(m_return_value)>) {
return std::forward<Type>(m_return_value);
} else {
return std::forward<Type>(*m_return_value);
}
}
constexpr decltype(auto) value() & noexcept
{
if constexpr (std::is_same_v<Type, decltype(m_return_value)>) {
return (m_return_value);
} else {
return (*m_return_value);
}
}
constexpr auto & exception() noexcept
{
return *m_error.exception;
}
constexpr auto error() const noexcept
{
return error_type{m_error.code, *m_error_domain};
}
/**
* Exits with a value.
* Must call exit functions exactly once.
*/
template <typename..., typename Dependent = Type>
constexpr void
exit_with_value(auto && value) requires(!std::is_void_v<Dependent>)
{
if constexpr (!std::is_void_v<Type>) {
if constexpr (!std::is_reference_v<Type>) {
::new (std::addressof(m_return_value))
Type(std::forward<decltype(value)>(value));
} else {
m_return_value = std::addressof(value);
}
}
m_error_domain = nullptr;
}
template <typename..., typename Dependent = Type>
constexpr void exit_with_value() requires std::is_void_v<Dependent>
{
m_error_domain = nullptr;
}
/**
* Exits with exception.
* Must call exit functions exactly once.
*/
template <typename Exception>
auto exit_with_exception(Exception && exception) noexcept
{
using type = std::remove_cv_t<std::remove_reference_t<Exception>>;
// Define the exception object that will be type erased.
struct exception_holder : public exception_object
{
exception_holder(Exception && exception) :
m_exception(std::forward<Exception>(exception))
{
}
struct dynamic_object dynamic_object() noexcept override
{
// Return the type id of the exception object and its
// address.
return {detail::type_id<type>(),
std::addressof(m_exception)};
}
~exception_holder() override = default;
type m_exception;
};
m_error_domain = std::addressof(err_domain<throwing_exception>);
m_error.exception =
make_exception_object<exception_holder, Allocator>(
std::forward<Exception>(exception));
if constexpr (std::is_void_v<Allocator>) {
// Nothing to be done.
} else if constexpr (noexcept(std::declval<Allocator>().allocate(
std::size_t{}))) {
if (!m_error.exception) {
exit_with_error(std::errc::not_enough_memory);
}
}
}
/**
* This must not hold a value or an exception.
* Must call exit functions exactly once.
*/
constexpr void exit_rethrow() noexcept
{
m_error_domain = std::addressof(err_domain<rethrow_error>);
}
/**
* Exits with an error value.
* Must call exit functions exactly once.
*/
constexpr void exit_with_error(const error_type & error) noexcept
{
m_error_domain = std::addressof(error.domain());
m_error.code = error.code();
}
/**
* Propagates an exception/error.
* Must call exit functions exactly once, `other` must have an
* error or an exception.
*/
template <typename OtherType>
constexpr void
exit_propagate(exit_condition<OtherType, Allocator> & other) noexcept
{
m_error_domain = other.m_error_domain;
std::memcpy(&m_error, &other.m_error, sizeof(m_error));
}
union error_storage
{
int code;
exception_type exception;
};
const error_domain * m_error_domain{};
union
{
error_storage m_error;
value_type m_return_value;
};
};
/**
* Use as the return type of the function, throw exceptions
* by using `co_yield` / `co_return`. Using `co_yield` is clearer
* but in some cases may generate less code than `co_return`.
* If you want to throw exceptions using `co_return` in void returning
* functions, use `zpp::throwing<zpp::void_t>` instead, and `co_return
* zpp::void_v` to exit normally). Call throwing functions by `co_await`,
* and return values using `co_return`. Use the `zpp::try_catch` function
* to execute a function object and then to catch exceptions thrown from
* it.
*/
template <typename Type, typename Allocator = void>
class [[nodiscard]] throwing
{
public:
template <typename, typename>
friend class throwing;
struct zpp_throwing_tag
{
};
/**
* The promise type to be extended with return value / return void
* functionality.
*/
class basic_promise_type
{
public:
template <typename, typename>
friend class throwing;
struct suspend_destroy
{
constexpr bool await_ready() noexcept { return false; }
void await_suspend(auto handle) noexcept { handle.destroy(); }
[[noreturn]] void await_resume() noexcept { while (true); }
};
auto get_return_object()
{
return throwing{static_cast<promise_type &>(*this)};
}
auto initial_suspend() noexcept
{
return suspend_never{};
}
auto final_suspend() noexcept
{
return suspend_never{};
}
void unhandled_exception()
{
std::terminate();
}
/**
* Throw and destroy calling coroutine.
*/
template <typename Value>
auto yield_value(Value && value)
{
throw_it(std::forward<Value>(value));
return suspend_destroy{};
}
/**
* Throw an exception.
*/
template <typename Value>
void throw_it(Value && value) requires requires
{
define_exception<
std::remove_cv_t<std::remove_reference_t<Value>>>();
}
{
m_return_object->m_condition.exit_with_exception(
std::forward<Value>(value));
}
/**
* Rethrow from existing.
*/
template <typename ExitCondition>
void throw_it(
std::tuple<const rethrow_t &, ExitCondition &> error_condition)
{
m_return_object->m_condition.exit_propagate(
std::get<1>(error_condition));
}
/**
* Rethrow the current set exception.
*/
void throw_it(rethrow_t)
{
m_return_object->m_condition.exit_rethrow();
}
/**
* Throw an error.
*/
void throw_it(const error & error)
{
m_return_object->m_condition.exit_with_error(error);
}
protected:
~basic_promise_type() = default;
throwing * m_return_object{};
};
template <typename Base>
struct throwing_allocator : public Base
{
void * operator new(std::size_t size)
{
Allocator allocator;
return std::allocator_traits<Allocator>::allocate(allocator,
size);
}
void operator delete(void * pointer, std::size_t size) noexcept
{
Allocator allocator;
std::allocator_traits<Allocator>::deallocate(
allocator, static_cast<std::byte *>(pointer), size);
}
protected:
~throwing_allocator() = default;
};
template <typename Base>
struct noexcept_allocator : public Base
{
void * operator new(std::size_t size) noexcept
{
Allocator allocator;
return std::allocator_traits<Allocator>::allocate(allocator,
size);
}
void operator delete(void * pointer, std::size_t size) noexcept
{