2010-02-14 23:39:15 +01:00
|
|
|
/*
|
|
|
|
|
RESULT.hpp - intermediary token representing the result of an operation
|
2010-12-17 23:28:49 +01:00
|
|
|
|
2010-02-14 23:39:15 +01:00
|
|
|
Copyright (C) Lumiera.org
|
|
|
|
|
2010, Hermann Vosseler <Ichthyostega@web.de>
|
2010-12-17 23:28:49 +01:00
|
|
|
|
2010-02-14 23:39:15 +01:00
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the GNU General Public License as
|
2010-12-17 23:28:49 +01:00
|
|
|
published by the Free Software Foundation; either version 2 of
|
|
|
|
|
the License, or (at your option) any later version.
|
|
|
|
|
|
2010-02-14 23:39:15 +01:00
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
2010-12-17 23:28:49 +01:00
|
|
|
|
2010-02-14 23:39:15 +01:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
2010-12-17 23:28:49 +01:00
|
|
|
|
2010-02-14 23:39:15 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @file result.hpp
|
2023-09-27 02:51:00 +02:00
|
|
|
** Intermediary value object to represent »either« an operation result or a failure.
|
|
|
|
|
** Some operation might have produced a value result or failed with an exception.
|
|
|
|
|
** Typically, the Result token is used _inline_ — immediately either invoking
|
2011-09-25 04:07:30 +02:00
|
|
|
** one of the member function or employing the built-in result type conversion.
|
|
|
|
|
** It will be copyable iff the result value is copyable. There is an implicit
|
2010-02-14 23:39:15 +01:00
|
|
|
** valid or failure state, which can be tested. Any attempt to get the value
|
2018-04-07 02:28:29 +02:00
|
|
|
** of an invalid result token will cause an exception to be thrown.
|
2010-02-14 23:39:15 +01:00
|
|
|
**
|
2018-11-15 23:59:23 +01:00
|
|
|
** @see vault::ThreadJoinable usage example
|
2010-02-14 23:39:15 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef LIB_RESULT_H
|
|
|
|
|
#define LIB_RESULT_H
|
|
|
|
|
|
|
|
|
|
#include "lib/error.hpp"
|
|
|
|
|
#include "lib/wrapper.hpp"
|
|
|
|
|
#include "lib/util.hpp"
|
|
|
|
|
|
2023-09-27 02:51:00 +02:00
|
|
|
#include <exception>
|
2018-04-09 00:51:24 +02:00
|
|
|
#include <utility>
|
2010-02-14 23:39:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace lib {
|
|
|
|
|
|
|
|
|
|
using util::isnil;
|
|
|
|
|
namespace error = lumiera::error;
|
2023-09-27 01:27:53 +02:00
|
|
|
|
2010-02-14 23:39:15 +01:00
|
|
|
|
2023-09-27 01:27:53 +02:00
|
|
|
/**
|
|
|
|
|
* Representation of the result of some operation, _EITHER_ a value or a failure.
|
|
|
|
|
* It can be created for passing a result produced by the operation, or the failure
|
|
|
|
|
* to do so. The value can be retrieved by implicit or explicit conversion.
|
|
|
|
|
* @throw error::State on any attempt to access the value in case of failure
|
|
|
|
|
* @warning this class has a lot of implicit conversions;
|
|
|
|
|
* care should be taken when defining functions
|
|
|
|
|
* to take Result instances as parameter....
|
|
|
|
|
*/
|
|
|
|
|
template<typename RES>
|
|
|
|
|
class Result;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The base case is just to capture success or failure,
|
|
|
|
|
* without returning any value result.
|
|
|
|
|
*/
|
|
|
|
|
template<>
|
|
|
|
|
class Result<void>
|
|
|
|
|
{
|
2023-09-27 02:51:00 +02:00
|
|
|
std::exception_ptr failure_;
|
2023-09-27 01:27:53 +02:00
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
/** mark either failure (default) or success */
|
|
|
|
|
Result (bool success =false)
|
2023-09-27 02:51:00 +02:00
|
|
|
: failure_{success? nullptr: std::make_exception_ptr (error::State{"operation failed"})}
|
2023-09-27 01:27:53 +02:00
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
/** failed result, with reason given.*/
|
|
|
|
|
Result (lumiera::Error const& reason)
|
2023-09-27 02:51:00 +02:00
|
|
|
: failure_{std::make_exception_ptr (reason)}
|
2023-09-27 01:27:53 +02:00
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
operator bool() const { return isValid(); }
|
2023-09-27 02:51:00 +02:00
|
|
|
bool isValid() const { return not failure_; }
|
2023-09-27 01:27:53 +02:00
|
|
|
|
|
|
|
|
void
|
|
|
|
|
maybeThrow() const
|
|
|
|
|
{
|
2023-09-27 02:51:00 +02:00
|
|
|
if (failure_)
|
|
|
|
|
std::rethrow_exception(failure_);
|
2023-09-27 01:27:53 +02:00
|
|
|
}
|
|
|
|
|
};
|
2010-02-14 23:39:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2011-06-14 05:41:02 +02:00
|
|
|
* Optional Result value or status of some operation.
|
2018-04-07 02:28:29 +02:00
|
|
|
* It can be created for passing a result produced by the operation, or the
|
2011-06-14 05:41:02 +02:00
|
|
|
* failure to do so. The value can be retrieved by implicit or explicit conversion.
|
|
|
|
|
* @throw error::State on any attempt to access the value in case of failure
|
2010-02-14 23:39:15 +01:00
|
|
|
* @warning this class has a lot of implicit conversions;
|
|
|
|
|
* care should be taken when defining functions
|
|
|
|
|
* to take Result instances as parameter....
|
|
|
|
|
*/
|
|
|
|
|
template<typename RES>
|
|
|
|
|
class Result
|
2023-09-27 01:27:53 +02:00
|
|
|
: public Result<void>
|
2010-02-14 23:39:15 +01:00
|
|
|
{
|
|
|
|
|
wrapper::ItemWrapper<RES> value_;
|
|
|
|
|
|
|
|
|
|
public:
|
2023-09-27 02:51:00 +02:00
|
|
|
/** mark failed result, with reason given.*/
|
2010-02-14 23:39:15 +01:00
|
|
|
Result (lumiera::Error const& reason)
|
2023-09-27 01:27:53 +02:00
|
|
|
: Result<void>{reason}
|
2018-04-07 02:28:29 +02:00
|
|
|
{ }
|
2010-02-14 23:39:15 +01:00
|
|
|
|
|
|
|
|
/** standard case: valid result */
|
2018-04-09 00:51:24 +02:00
|
|
|
Result (RES&& value)
|
2023-09-27 01:27:53 +02:00
|
|
|
: Result<void>{true}
|
2018-04-09 00:51:24 +02:00
|
|
|
, value_{std::forward<RES> (value)}
|
2010-02-14 23:39:15 +01:00
|
|
|
{ }
|
|
|
|
|
|
2023-09-27 02:51:00 +02:00
|
|
|
// is or is not copyable depending on RES
|
2010-02-14 23:39:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
operator RES() const
|
|
|
|
|
{
|
|
|
|
|
maybeThrow();
|
|
|
|
|
return *value_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename TY>
|
|
|
|
|
TY
|
|
|
|
|
get() const
|
|
|
|
|
{
|
|
|
|
|
maybeThrow();
|
|
|
|
|
return static_cast<TY> (*value_);
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-27 01:27:53 +02:00
|
|
|
template<typename MAKE, typename...ARGS>
|
|
|
|
|
RES
|
|
|
|
|
getOrElse (MAKE&& producer, ARGS ...args)
|
2010-02-14 23:39:15 +01:00
|
|
|
{
|
2023-09-27 01:27:53 +02:00
|
|
|
if (isValid())
|
|
|
|
|
return *value_;
|
|
|
|
|
else
|
|
|
|
|
return std::invoke(std::forward<MAKE> (producer), std::forward<ARGS> (args)...);
|
2010-02-14 23:39:15 +01:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-09-27 02:51:00 +02:00
|
|
|
/** deduction guard: allow _perfect forwarding_ of a any result into the ctor call. */
|
2023-09-27 01:27:53 +02:00
|
|
|
template<typename VAL>
|
2023-09-27 02:51:00 +02:00
|
|
|
Result (VAL&&) -> Result<VAL>;
|
2010-02-14 23:39:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace lib
|
|
|
|
|
#endif
|