-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from ilovenoah/57-feature-create-result-class
Result class handles error value and true value
- Loading branch information
Showing
5 changed files
with
92 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,28 @@ | ||
#ifndef ERROR_HPP | ||
#define ERROR_HPP | ||
|
||
#include "env.hpp" | ||
#include "Result.hpp" | ||
|
||
class GenericException : public std::exception { | ||
template <typename E> | ||
class Error { | ||
private: | ||
std::string errorMessage; | ||
Error() {} | ||
|
||
public: | ||
GenericException(const std::string &message); | ||
~GenericException() throw(); | ||
E e; | ||
|
||
const char *what() const throw(); | ||
public: | ||
Error(E const &_e) : e(_e) {} | ||
Error(Error const &sourceError) { *this = sourceError; } | ||
Error &operator=(Error const &sourceError) { | ||
if (this != &sourceError) { | ||
e = sourceError.e; | ||
} | ||
return *this; | ||
} | ||
~Error() {} | ||
template <typename T, typename V> | ||
operator Result<T, V>() const { | ||
return Result<T, V>(e); | ||
} | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#ifndef OK_HPP | ||
#define OK_HPP | ||
|
||
#include "Result.hpp" | ||
|
||
template <typename T> | ||
class Ok { | ||
private: | ||
Ok() {} | ||
|
||
T t; | ||
|
||
public: | ||
Ok(T const &_t) : t(_t) {} | ||
Ok(Ok const &sourceOk) { *this = sourceOk; } | ||
Ok &operator=(Ok const &sourceOk) { | ||
if (this != &sourceOk) { | ||
t = sourceOk.t; | ||
} | ||
return *this; | ||
} | ||
~Ok() {} | ||
template <typename V, typename E> | ||
operator Result<V, E>() const { | ||
return Result<V, E>(t); | ||
} | ||
}; | ||
|
||
#endif | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#ifndef RESULT_HPP | ||
#define RESULT_HPP | ||
|
||
template <typename T, typename E> | ||
class Result { | ||
private: | ||
Result(); | ||
|
||
enum tag { | ||
OK, | ||
ERROR, | ||
}; | ||
tag t; | ||
T ok; | ||
E error; | ||
|
||
public: | ||
Result(T const &_ok) : t(OK), ok(_ok) {} | ||
Result(E const &_error) : t(ERROR), error(_error) {} | ||
Result(Result const &sourceResult) { *this = sourceResult; } | ||
Result &operator=(Result const &sourceResult) { | ||
if (this != &sourceResult) { | ||
t = sourceResult.t; | ||
switch (t) { | ||
case OK: | ||
ok = sourceResult.ok; | ||
break; | ||
case ERROR: | ||
error = sourceResult.error; | ||
break; | ||
} | ||
} | ||
return *this; | ||
} | ||
|
||
bool isOK() const { return t == OK; } | ||
bool isError() const { return t == ERROR; } | ||
T const &getOk() const { return ok; } | ||
E const &getError() const { return error; } | ||
}; | ||
|
||
#endif |
This file was deleted.
Oops, something went wrong.