12#if !defined(_INCLUDE_MM_RESULT_HPP_)
13#define _INCLUDE_MM_RESULT_HPP_ 1
19template <
typename T,
typename E>
class result;
33 static constexpr std::size_t
size =
40 static constexpr std::size_t
align =
47 using storager_t =
typename std::aligned_storage<size, align>::type;
56 using dtor_t =
typename std::add_pointer<void(
void*)>::type;
78 return (*
reinterpret_cast<T*
>(ptr)).~T();
86 return (*
reinterpret_cast<E*
>(ptr)).~E();
121 const T& value = *
reinterpret_cast<const T*
>(&
storager);
128 const E& value = *
reinterpret_cast<const E*
>(&
storager);
141 const T& value = *
reinterpret_cast<const T*
>(&rhs.
storager);
148 const E& value = *
reinterpret_cast<const E*
>(&rhs.
storager);
181 std::is_copy_constructible<T>::value,
186 return *
reinterpret_cast<const T*
>(&
storager);
190 std::is_copy_constructible<E>::value,
195 return *
reinterpret_cast<const E*
>(&
storager);
199 std::is_copy_constructible<T>::value,
204 return *
reinterpret_cast<T*
>(&
storager);
208 std::is_copy_constructible<E>::value,
213 return *
reinterpret_cast<E*
>(&
storager);
221 void* ptr =
reinterpret_cast<void*
>(&
storager);
230 void* ptr =
reinterpret_cast<void*
>(&
storager);
347 static constexpr bool value = std::is_trivial<T>::value;
354template <
typename... T>
376template <
typename T,
typename E>
413template <
typename T,
typename E>
423 typename base_t::const_reference_value_type;
425 typename base_t::const_reference_error_type;
426 using base_t::base_t;
434 return base_t::type_tag == base_t::TypeTag::SuccTag;
453 return base_t::unsafe_get_succ_value();
463 return base_t::unsafe_get_err_value();
473 return base_t::unsafe_get_succ_value_mut();
483 return base_t::unsafe_get_err_value_mut();
506 template <
typename T1 = T>
512 static_assert(std::is_same<typename T1::error_type, E>::value);
515 if (ref_succ.is_succ()) {
516 return ref_succ.unsafe_get_succ_value();
519 return ref_succ.unsafe_get_err_value();
531 template <
typename T1 = T>
553 return map_to(base_t::unsafe_get_succ_value());
556 return base_t::unsafe_get_err_value();
574 return base_t::unsafe_get_succ_value();
577 return map_to(base_t::unsafe_get_err_value());
591 typename R1::value_type>>
598 return then_do(base_t::unsafe_get_succ_value());
601 return base_t::unsafe_get_err_value();
615 typename R1::error_type>>
622 return base_t::unsafe_get_succ_value();
625 return else_do(base_t::unsafe_get_err_value());
638 return base_t::unsafe_get_succ_value();
654 typename R = details::
655 enable_if_t<std::is_base_of<std::exception, R1>::value, R1>>
662 return base_t::unsafe_get_succ_value();
665#if _MSTR_USE_CPP_EXCEPTION
666 throw cont(base_t::unsafe_get_err_value());
结果类型的基类
Definition mm_result.hpp:55
result_non_trivial_base(const result_non_trivial_base &rhs)
Definition mm_result.hpp:117
typename std::add_pointer< void(void *)>::type dtor_t
Definition mm_result.hpp:56
enable_if_t< std::is_copy_constructible< T >::value, void > unsafe_set_succ_value(const T &value) noexcept
Definition mm_result.hpp:218
const T & const_reference_value_type
Definition mm_result.hpp:94
T value_type
Definition mm_result.hpp:90
E & reference_error_type
Definition mm_result.hpp:93
static void dtor_succ_type(void *ptr) noexcept
析构函数 (T type)
Definition mm_result.hpp:76
result_non_trivial_base(const_reference_value_type succ_val)
从succ value构造
Definition mm_result.hpp:101
~result_non_trivial_base()
Definition mm_result.hpp:156
typename result_storager_type< T, E >::storager_t storager_t
Definition mm_result.hpp:57
enable_if_t< std::is_copy_constructible< T >::value, reference_value_type > unsafe_get_succ_value_mut() noexcept
Definition mm_result.hpp:201
storager_t storager
数据存放在这里
Definition mm_result.hpp:65
result_non_trivial_base(const_reference_error_type err_val)
从err value构造
Definition mm_result.hpp:111
TypeTag
类型标签
Definition mm_result.hpp:169
dtor_t dtor
这里存着析构函数
Definition mm_result.hpp:71
E error_type
Definition mm_result.hpp:91
enable_if_t< std::is_copy_constructible< E >::value, void > unsafe_set_err_value(const E &value) noexcept
Definition mm_result.hpp:227
enable_if_t< std::is_copy_constructible< E >::value, reference_error_type > unsafe_get_err_value_mut() noexcept
Definition mm_result.hpp:210
const E & const_reference_error_type
Definition mm_result.hpp:95
TypeTag type_tag
类型标签
Definition mm_result.hpp:178
result_non_trivial_base & operator=(const result_non_trivial_base &rhs)
Definition mm_result.hpp:135
static void dtor_err_type(void *ptr) noexcept
析构函数 (E type)
Definition mm_result.hpp:84
enable_if_t< std::is_copy_constructible< T >::value, const_reference_value_type > unsafe_get_succ_value() const noexcept
Definition mm_result.hpp:183
T & reference_value_type
Definition mm_result.hpp:92
enable_if_t< std::is_copy_constructible< E >::value, const_reference_error_type > unsafe_get_err_value() const noexcept
Definition mm_result.hpp:192
结果类型的基类(trivial type)
Definition mm_result.hpp:242
reference_error_type unsafe_get_err_value_mut() noexcept
Definition mm_result.hpp:324
const_reference_value_type unsafe_get_succ_value() const noexcept
Definition mm_result.hpp:306
result_trivial_base & operator=(const result_trivial_base &)=default
E error_type
Definition mm_result.hpp:250
result_trivial_base(result_trivial_base &&)=default
T t_val
Definition mm_result.hpp:244
const E & const_reference_error_type
Definition mm_result.hpp:254
constexpr result_trivial_base(T succ_val)
从succ value构造
Definition mm_result.hpp:260
E e_val
Definition mm_result.hpp:245
T value_type
Definition mm_result.hpp:249
const_reference_error_type unsafe_get_err_value() const noexcept
Definition mm_result.hpp:312
T & reference_value_type
Definition mm_result.hpp:251
reference_value_type unsafe_get_succ_value_mut() noexcept
Definition mm_result.hpp:318
TypeTag
类型标签
Definition mm_result.hpp:295
const T & const_reference_value_type
Definition mm_result.hpp:253
result_trivial_base(const result_trivial_base &)=default
constexpr result_trivial_base(E err_val)
从err value构造
Definition mm_result.hpp:269
E & reference_error_type
Definition mm_result.hpp:252
~result_trivial_base()=default
result_trivial_base & operator=(result_trivial_base &&)=default
TypeTag type_tag
类型标签
Definition mm_result.hpp:304
表示存在错误
Definition mm_result.hpp:400
mtfmt_error(error_code_t)
Definition mm_result.hpp:402
结果类
Definition mm_result.hpp:415
bool is_err() const noexcept
是否为err?
Definition mm_result.hpp:441
details::enable_if_t< details::holds_prototype< F, R, E >::value, result< T, R > > map_err(F map_to) const
在result是err的时候执行map, 转换result
Definition mm_result.hpp:571
const_reference_value_type or_value(const_reference_value_type or_val) const noexcept
如果为T则返回T, 不然返回or_val
Definition mm_result.hpp:633
typename base_t::value_type value_type
Definition mm_result.hpp:418
details::enable_if_t< details::is_result< T1 >::value, result< typename T1::value_type, E > > flatten() const noexcept
扁平化
Definition mm_result.hpp:510
bool is_succ() const noexcept
是否为succ?
Definition mm_result.hpp:432
details::enable_if_t< details::holds_prototype< F, R, T >::value, result< R, E > > map(F map_to) const
在result是succ的时候执行map, 转换result
Definition mm_result.hpp:550
details::enable_if_t< details::holds_prototype< F, result< T, R >, E >::value, result< T, R > > or_else(F else_do) const
在self是err的时候执行else_do, 否则返回succ
Definition mm_result.hpp:619
const_reference_error_type unsafe_get_err_value() const noexcept
取得err的值
Definition mm_result.hpp:461
const_reference_value_type unsafe_get_succ_value() const noexcept
取得succ的值
Definition mm_result.hpp:451
details::enable_if_t< details::holds_prototype< F, result< R, E >, T >::value, result< R, E > > and_then(F then_do) const
在self是succ的时候执行then do, 否则返回err
Definition mm_result.hpp:595
typename base_t::reference_value_type reference_value_type
Definition mm_result.hpp:420
typename base_t::error_type error_type
Definition mm_result.hpp:419
typename base_t::const_reference_value_type const_reference_value_type
Definition mm_result.hpp:423
details::enable_if_t<!details::is_result< T1 >::value, result< T1, E > > flatten() const noexcept
扁平化 (T不是result<T, E>的情况)
Definition mm_result.hpp:533
details::enable_if_t< details::holds_prototype< F, R, E >::value, const_reference_value_type > or_exception(F cont) const
如果为T则返回T, 不然抛出异常
Definition mm_result.hpp:659
reference_error_type unsafe_get_err_value_mut() noexcept
取得err的值(可变引用)
Definition mm_result.hpp:481
typename base_t::reference_error_type reference_error_type
Definition mm_result.hpp:421
reference_value_type unsafe_get_succ_value_mut() noexcept
取得succ的值(可变引用)
Definition mm_result.hpp:471
details::result_base_t< T, E > base_t
Definition mm_result.hpp:417
typename base_t::const_reference_error_type const_reference_error_type
Definition mm_result.hpp:425
result< E, T > conjugate() const noexcept
交换succ和err的情况以及值
Definition mm_result.hpp:490
#define mstr_assert(e)
Definition mm_cfg.h:396
#define mstr_cause_exception(code)
Definition mm_cfg.h:395
enum tagMStrResult mstr_result_t
结果类型
type trait, type alias和type def
typename std::conditional< is_trivial_result< T, E >::value, result_trivial_base< T, E >, result_non_trivial_base< T, E > >::type result_base_t
result的基类
Definition mm_result.hpp:380
typename function_trait< F >::return_type_t function_return_type_t
取得函数的返回值类型
Definition mm_type.hpp:126
typename std::enable_if< cond, T >::type enable_if_t
enable_if_t, 和cpp14一样(但是这里是cpp11呜呜呜)
Definition mm_type.hpp:205
Definition mm_parser.hpp:17
mstr_result_t error_code_t
返回值的错误结果
Definition mm_result.hpp:394
取得and
Definition mm_type.hpp:232
检查函数F的类型为 ( T1, T2 ...) -> R
Definition mm_type.hpp:189
判断Ti是否为模板Tt的实例化
Definition mm_type.hpp:150
判断T是不是一个result类型
Definition mm_result.hpp:336
static constexpr bool value
Definition mm_result.hpp:337
帮助判断T是trivial或者trivial result (case1)
Definition mm_result.hpp:346
static constexpr bool value
Definition mm_result.hpp:347
帮助判断result<T, E>是不是trivial
Definition mm_result.hpp:366
static constexpr bool value
Definition mm_result.hpp:367
取得最大值
Definition mm_type.hpp:211
结果类型的stroager的types
Definition mm_result.hpp:28
static constexpr std::size_t size
存储的数据大小
Definition mm_result.hpp:33
typename std::aligned_storage< size, align >::type storager_t
借助std storager实现数据存放
Definition mm_result.hpp:47
static constexpr std::size_t align
存储的数据的内存对齐
Definition mm_result.hpp:40
单位类型
Definition mm_type.hpp:29