Skip to content

Commit

Permalink
Merge pull request #58 from ilovenoah/57-feature-create-result-class
Browse files Browse the repository at this point in the history
Result class handles error value and true value
  • Loading branch information
ilovenoah authored Feb 10, 2024
2 parents a3c1b83 + 5e88b88 commit 931d913
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ RESP_DIR = response/
MAIN_NAME = main.cpp
CONF_NAME = Config.cpp Server.cpp AConfigurable.cpp Location.cpp
SOCK_NAME = ServerSocket.cpp ClientSocket.cpp
UTILS_NAME = Error.cpp utils.cpp CaseInsensitiveCompare.cpp
UTILS_NAME = utils.cpp CaseInsensitiveCompare.cpp
LOOP_NAME = loop.cpp
REQU_NAME = Request.cpp
RESP_NAME = Response.cpp
Expand Down
27 changes: 19 additions & 8 deletions inc/Error.hpp
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
30 changes: 30 additions & 0 deletions inc/Ok.hpp
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

42 changes: 42 additions & 0 deletions inc/Result.hpp
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
10 changes: 0 additions & 10 deletions src/utils/Error.cpp

This file was deleted.

0 comments on commit 931d913

Please sign in to comment.