mirror of
https://github.com/arcan1s/result.git
synced 2025-07-23 09:59:55 +00:00
initial import
This commit is contained in:
68
README.md
Normal file
68
README.md
Normal file
@ -0,0 +1,68 @@
|
||||
# result
|
||||
|
||||
Simple header-only variant-driven C++17 `Result<T,E>` implementation.
|
||||
|
||||
## Requirements
|
||||
|
||||
* c++17 or c++1z with support of variant
|
||||
|
||||
## Usage example
|
||||
|
||||
```cpp
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
|
||||
#include "result.h"
|
||||
|
||||
using namespace std::string_literals;
|
||||
|
||||
enum class ErrorCode { Error };
|
||||
using IError = Result::Error<ErrorCode>;
|
||||
template <class T> using IResult = Result::Result<T, ErrorCode>;
|
||||
|
||||
|
||||
class Test
|
||||
{
|
||||
public:
|
||||
Test() = default;
|
||||
~Test() = default;
|
||||
friend std::ostream &operator<<(std::ostream &os, const Test &)
|
||||
{
|
||||
os << "I'm test class";
|
||||
return os;
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
template <class T> Result::Content print_example(IResult<T> r)
|
||||
{
|
||||
switch (r.type()) {
|
||||
case Result::Content::Value:
|
||||
std::cout << "Result has value " << r.get() << std::endl;
|
||||
return Result::Content::Value;
|
||||
case Result::Content::Error:
|
||||
std::cout << "Result has error " << r.error().message() << std::endl;
|
||||
return Result::Content::Error;
|
||||
case Result::Content::Empty:
|
||||
std::cout << "Result does not contain anything" << std::endl;
|
||||
return Result::Content::Empty;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
assert(print_example<int>(42) == Result::Content::Value);
|
||||
assert(print_example<int>(IError("int error"s)) == Result::Content::Error);
|
||||
|
||||
assert(print_example<std::string>("a string"s) == Result::Content::Value);
|
||||
assert(print_example<std::string>(IError("std::string error"s))
|
||||
== Result::Content::Error);
|
||||
|
||||
assert(print_example<Test>(Test()) == Result::Content::Value);
|
||||
assert(print_example<Test>(IError("Test error"s))
|
||||
== Result::Content::Error);
|
||||
|
||||
return 0;
|
||||
}
|
||||
```
|
Reference in New Issue
Block a user