Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #pragma once
00025
00026 #include <stdexcept>
00027
00028 #include "ut_types.h"
00029
00030 template<class T>
00031 class ABI_EXPORT UT_Option
00032 {
00033 public:
00034 typedef T data_type;
00035
00036 UT_Option()
00037 : m_none(true)
00038 {
00039 }
00040 UT_Option(T&& data)
00041 : m_none(false)
00042 , m_data(data)
00043 {
00044 }
00045 explicit UT_Option(const T& data)
00046 : m_none(false)
00047 , m_data(data)
00048 {
00049 }
00050 template<class... Args>
00051 UT_Option(Args&&... args)
00052 : m_none(false)
00053 , m_data(args...)
00054 {
00055 }
00056
00057 T&& unwrap()
00058 {
00059 if (m_none) {
00060 throw std::runtime_error("none option value");
00061 }
00062 m_none = true;
00063 return std::move(m_data);
00064 }
00065 T unwrap_or(const T& value)
00066 {
00067 if (m_none) {
00068 return value;
00069 }
00070 return unwrap();
00071 }
00072 bool empty() const
00073 { return m_none; }
00074 explicit operator bool() const
00075 { return !m_none; }
00076 private:
00077 bool m_none;
00078 T m_data;
00079 };