2021-04-12 23:18:32 +01:00
|
|
|
/* -*- C++ -*-
|
|
|
|
* Serene programming language.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2019-2021 Sameer Rahmani <lxsameer@gnu.org>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SERENE_UTILS_H
|
|
|
|
#define SERENE_UTILS_H
|
|
|
|
|
|
|
|
#include "llvm/Support/Error.h"
|
2021-06-13 22:58:46 +01:00
|
|
|
|
2021-04-12 23:18:32 +01:00
|
|
|
#include <variant>
|
|
|
|
|
|
|
|
// C++17 required. We can't go back to 14 any more :))
|
|
|
|
|
|
|
|
namespace serene {
|
|
|
|
|
|
|
|
/// A similar type to Rust's Result data structure. It either holds a value of
|
|
|
|
/// type `T` successfully or holds a value of type `E` errorfully. It is
|
|
|
|
/// designed to be used in situations which the return value of a function might
|
|
|
|
/// contains some errors. The official way to use this type is to use the
|
|
|
|
/// factory functions `Success` and `Error`. For example:
|
|
|
|
///
|
|
|
|
/// \code
|
2021-04-20 19:33:25 +01:00
|
|
|
/// auto successfulResult = Result<int>::success(3);
|
|
|
|
/// auto notOkResult = Result<int>::error(SomeLLVMError());
|
2021-04-12 23:18:32 +01:00
|
|
|
// \endcode
|
|
|
|
///
|
|
|
|
/// In order check for a value being errorful or successful checkout the `ok`
|
|
|
|
/// method or simply use the value as a conditiona.
|
2021-06-13 22:58:46 +01:00
|
|
|
template <typename T, typename E = llvm::Error>
|
|
|
|
class Result {
|
2021-04-12 23:18:32 +01:00
|
|
|
|
|
|
|
// The actual data container
|
|
|
|
std::variant<T, E> contents;
|
|
|
|
|
|
|
|
/// The main constructor which we made private to avoid ambiguousness in
|
|
|
|
/// input type. `Success` and `Error` call this ctor.
|
|
|
|
template <typename InPlace, typename Content>
|
|
|
|
Result(InPlace i, Content &&c) : contents(i, std::forward<Content>(c)){};
|
|
|
|
|
|
|
|
public:
|
|
|
|
/// Create a succesfull result with the given value of type `T`.
|
2021-04-20 19:33:25 +01:00
|
|
|
static Result success(T v) {
|
2021-04-12 23:18:32 +01:00
|
|
|
return Result(std::in_place_index_t<0>(), std::move(v));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create an errorful result with the given value of type `E` (default
|
|
|
|
/// `llvm::Error`).
|
2021-04-20 19:33:25 +01:00
|
|
|
static Result error(E e) {
|
2021-04-12 23:18:32 +01:00
|
|
|
return Result(std::in_place_index_t<1>(), std::move(e));
|
|
|
|
}
|
|
|
|
|
2021-04-17 00:18:13 +01:00
|
|
|
/// Return the value if it's successful otherwise throw an error
|
|
|
|
T &&getValue() && { return std::move(std::get<0>(contents)); };
|
|
|
|
|
|
|
|
/// Return the error value if it's errorful otherwise throw an error
|
|
|
|
E &&getError() && { return std::move(std::get<1>(contents)); };
|
|
|
|
|
2021-04-12 23:18:32 +01:00
|
|
|
// using std::get, it'll throw if contents doesn't contain what you ask for
|
|
|
|
|
|
|
|
/// Return the value if it's successful otherwise throw an error
|
2021-04-17 00:18:13 +01:00
|
|
|
T &getValue() & { return std::get<0>(contents); };
|
2021-04-12 23:18:32 +01:00
|
|
|
|
|
|
|
/// Return the error value if it's errorful otherwise throw an error
|
2021-04-17 00:18:13 +01:00
|
|
|
E &getError() & { return std::get<1>(contents); };
|
2021-04-12 23:18:32 +01:00
|
|
|
|
2021-04-17 00:18:13 +01:00
|
|
|
const T &getValue() const & { return std::get<0>(contents); }
|
|
|
|
const E &getError() const & { return std::get<1>(contents); }
|
2021-04-12 23:18:32 +01:00
|
|
|
|
|
|
|
/// Return the a boolean value indicating whether the value is succesful
|
|
|
|
/// or errorful.
|
2021-04-14 18:14:39 +01:00
|
|
|
bool ok() const { return std::holds_alternative<T>(contents); };
|
2021-04-12 23:18:32 +01:00
|
|
|
|
|
|
|
operator bool() const { return ok(); }
|
|
|
|
};
|
2021-04-17 00:18:13 +01:00
|
|
|
|
2021-04-12 23:18:32 +01:00
|
|
|
} // namespace serene
|
|
|
|
#endif
|