2014-12-20 17:20:18 +00:00
|
|
|
#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; };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-12-20 17:20:18 +00:00
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
class tool_return_base
|
|
|
|
{
|
|
|
|
protected:
|
2016-08-03 02:42:14 +00:00
|
|
|
macos_error _error = noErr;
|
2014-12-20 17:20:18 +00:00
|
|
|
|
2016-08-06 00:37:21 +00:00
|
|
|
constexpr tool_return_base() = default;
|
2014-12-20 17:20:18 +00:00
|
|
|
|
2016-08-06 00:37:21 +00:00
|
|
|
constexpr tool_return_base(macos_error error) : _error(error)
|
2014-12-20 17:20:18 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2016-08-06 00:37:21 +00:00
|
|
|
constexpr macos_error error() const { return _error; }
|
2014-12-20 17:20:18 +00:00
|
|
|
|
2016-08-06 00:37:21 +00:00
|
|
|
constexpr explicit operator bool() const { return !_error; }
|
2014-12-20 17:20:18 +00:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2014-12-20 17:20:18 +00:00
|
|
|
} // namespace
|
|
|
|
|
2016-08-06 00:37:21 +00:00
|
|
|
|
2014-12-20 17:20:18 +00:00
|
|
|
template<class T>
|
|
|
|
class tool_return : public internal::tool_return_base
|
|
|
|
{
|
|
|
|
private:
|
2016-08-06 00:37:21 +00:00
|
|
|
|
2016-08-03 02:42:14 +00:00
|
|
|
T _value = T();
|
2014-12-20 17:20:18 +00:00
|
|
|
|
|
|
|
tool_return() = delete;
|
|
|
|
|
|
|
|
operator T() const
|
|
|
|
{
|
|
|
|
return _value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2016-08-06 00:37:21 +00:00
|
|
|
|
2016-08-05 21:15:52 +00:00
|
|
|
typedef T value_type;
|
2014-12-20 17:20:18 +00:00
|
|
|
|
2016-08-06 00:37:21 +00:00
|
|
|
tool_return(const T &value) : _value(value)
|
2014-12-20 17:20:18 +00:00
|
|
|
{}
|
|
|
|
|
2016-08-06 00:37:21 +00:00
|
|
|
tool_return(T &&value) : _value(std::forward<T>(value))
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
2014-12-20 17:20:18 +00:00
|
|
|
tool_return(macos_error error) : tool_return_base(error)
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
2016-08-06 00:37:21 +00:00
|
|
|
tool_return &operator=(const T &value)
|
2014-12-20 17:20:18 +00:00
|
|
|
{
|
|
|
|
_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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-20 17:20:18 +00:00
|
|
|
tool_return &operator=(macos_error error)
|
|
|
|
{
|
|
|
|
_value = T();
|
|
|
|
_error = error;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-02-18 14:32:01 +00:00
|
|
|
|
2014-12-20 17:20:18 +00:00
|
|
|
constexpr const T* operator->() const
|
|
|
|
{
|
|
|
|
return &_value;
|
|
|
|
}
|
|
|
|
|
2015-02-18 14:32:01 +00:00
|
|
|
|
2014-12-20 17:20:18 +00:00
|
|
|
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);
|
2014-12-20 17:20:18 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-12-20 17:20:18 +00:00
|
|
|
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));
|
|
|
|
}
|
2014-12-20 17:20:18 +00:00
|
|
|
|
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));
|
|
|
|
}
|
2014-12-20 17:20:18 +00:00
|
|
|
|
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));
|
|
|
|
}
|
2014-12-20 17:20:18 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
2014-12-20 17:20:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-08-06 00:37:21 +00:00
|
|
|
|
2015-02-18 14:32:01 +00:00
|
|
|
} // namespace
|
2014-12-20 17:20:18 +00:00
|
|
|
#endif
|