mpw/macos/tool_return.h

235 lines
4.6 KiB
C
Raw Normal View History

#ifndef __tool_return__
#define __tool_return__
#include "errors.h"
#include <utility>
namespace MacOS {
2016-08-06 00:37:21 +00:00
template<class T>
class tool_return;
template<class T>
struct tool_return_type { typedef tool_return<T> type; };
template<class T>
struct tool_return_type<tool_return<T>> { typedef tool_return<T> type; };
namespace internal {
class tool_return_base
{
protected:
macos_error _error = noErr;
2016-08-06 00:37:21 +00:00
constexpr tool_return_base() = default;
2016-08-06 00:37:21 +00:00
constexpr tool_return_base(macos_error error) : _error(error)
{}
public:
2016-08-06 00:37:21 +00:00
constexpr macos_error error() const { return _error; }
2016-08-06 00:37:21 +00:00
constexpr explicit operator bool() const { return !_error; }
template<class... Args>
void throw_macos_error(Args&&... args) const
{
if (_error) MacOS::throw_macos_error(_error, std::forward<Args>(args)...);
}
};
2016-08-06 00:56:24 +00:00
class void_tool_return : public tool_return_base {
public:
typedef void value_type;
void_tool_return() = default;
void_tool_return(macos_error error) : tool_return_base(error)
{}
void_tool_return &operator=(macos_error error)
{
_error = error;
return *this;
}
template<class F, typename RT = typename std::result_of<F(void_tool_return)>::type>
void then(F &&f, typename std::enable_if<std::is_void<RT>::value>::type* = 0) {
f(std::move(*this));
}
template<class F, typename RT = typename std::result_of<F(void_tool_return)>::type>
RT then(F &&f, typename std::enable_if<!std::is_void<RT>::value>::type* = 0) {
return f(std::move(*this));
}
template<class F, typename RT = typename std::result_of<F()>::type>
typename tool_return_type<RT>::type
map(F &&f, typename std::enable_if<std::is_void<RT>::value>::type* = 0) {
if (_error) return _error;
f();
//return tool_return<void>();
return noErr;
}
template<class F, typename RT = typename std::result_of<F()>::type>
typename tool_return_type<RT>::type
map(F &&f, typename std::enable_if<!std::is_void<RT>::value>::type* = 0) {
if (_error) return _error;
return f();
}
};
} // namespace
2016-08-06 00:37:21 +00:00
template<class T>
class tool_return : public internal::tool_return_base
{
private:
2016-08-06 00:37:21 +00:00
T _value = T();
tool_return() = delete;
operator T() const
{
return _value;
}
public:
2016-08-06 00:37:21 +00:00
typedef T value_type;
2016-08-06 00:37:21 +00:00
tool_return(const T &value) : _value(value)
{}
2016-08-06 00:37:21 +00:00
tool_return(T &&value) : _value(std::forward<T>(value))
{}
tool_return(macos_error error) : tool_return_base(error)
{}
2016-08-06 00:37:21 +00:00
tool_return &operator=(const T &value)
{
_value = value;
_error = 0;
return *this;
}
2016-08-06 00:37:21 +00:00
tool_return &operator=(T &&value)
{
_value = std::forward<T>(value);
_error = 0;
return *this;
}
tool_return &operator=(macos_error error)
{
_value = T();
_error = error;
return *this;
}
constexpr const T* operator->() const
{
return &_value;
}
constexpr const T& operator *() const
{
return _value;
}
T value() const
{
return _value;
}
template<class U>
T value_or(U&& u) const
{
2016-08-06 00:37:21 +00:00
if (_error) return std::forward<U>(u);
return _value;
}
2016-08-06 00:37:21 +00:00
template<class U>
T &&value_or(U&& u) &&
{
if (_error) return std::forward<U>(u);
return std::move(_value);
}
template<class... Args>
T value_or_throw(Args&&... args) const
{
if (_error) throw_macos_error(std::forward<Args>(args)...);
return _value;
}
2016-08-06 00:56:24 +00:00
template<class F, typename RT = typename std::result_of<F(tool_return)>::type>
2016-08-06 00:37:21 +00:00
void then(F &&f, typename std::enable_if<std::is_void<RT>::value>::type* = 0) {
f(std::move(*this));
}
2016-08-06 00:56:24 +00:00
template<class F, typename RT = typename std::result_of<F(tool_return)>::type>
2016-08-06 00:37:21 +00:00
RT then(F &&f, typename std::enable_if<!std::is_void<RT>::value>::type* = 0) {
return f(std::move(*this));
}
2016-08-06 00:37:21 +00:00
template<class F, typename RT = typename std::result_of<F(value_type)>::type>
typename tool_return_type<RT>::type
map(F &&f, typename std::enable_if<std::is_void<RT>::value>::type* = 0) {
if (_error) return _error;
f(std::move(_value));
//return tool_return<void>();
return noErr;
}
template<class F, typename RT = typename std::result_of<F(value_type)>::type>
typename tool_return_type<RT>::type
map(F &&f, typename std::enable_if<!std::is_void<RT>::value>::type* = 0) {
if (_error) return _error;
return f(std::move(_value));
}
};
template<>
2016-08-06 00:56:24 +00:00
class tool_return<void> : public internal::void_tool_return {
public: using void_tool_return::void_tool_return;
2016-08-06 00:37:21 +00:00
};
template<>
2016-08-06 00:56:24 +00:00
class tool_return<macos_error> : public internal::void_tool_return {
public: using void_tool_return::void_tool_return;
};
2016-08-06 00:37:21 +00:00
} // namespace
#endif