-
Notifications
You must be signed in to change notification settings - Fork 10
/
Result.h
53 lines (43 loc) · 1.31 KB
/
Result.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
* File: Result.h
* Author: kjell
*
* https://github.com/weberr13/FileIO
* Created on April 21
*/
#pragma once
#include <string>
/**
* Example Usage:
* return Result<bool> success{true};
* or in case of a failure
* return Result<bool> failure{false, error};
*/
template<typename T> struct Result {
const T result;
const std::string error;
/**
* Result of an operation.
* @param output whatever the expected output would be
* @param err error message to the client, default is empty which means successful operation
*/
Result(T output, const std::string& err)
: result{output}, error{err} {
}
Result(T output) : result{output}, error{""}{
}
Result() = delete;
Result(const Result&) = default; // Copy constructor
Result(Result&&) = default; // Move constructor
Result& operator=(const Result&) & = default; // Copy assignment operator
Result& operator=(Result&&) & = default; // Move assignment operator
~Result() = default;
/** @return status whether or not the Result contains a failure*/
bool HasFailed() const {
return (!error.empty());
}
/// convenience function @return status whether or not the Result was a success
bool HasSuccess() const {
return (error.empty());
}
};