Skip to content

Commit

Permalink
add aliases for ErrorState and NoError
Browse files Browse the repository at this point in the history
  • Loading branch information
Johannes Graeter committed May 7, 2024
1 parent 8a7eb4f commit cb4be13
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 16 deletions.
17 changes: 17 additions & 0 deletions include/aliases.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include <optional>
#if __cplusplus > 202002L
#include "concepts.hpp"
#endif

namespace anywho {
#if __cplusplus > 202002L
template<concepts::Error E>
#else
template<typename E>
#endif
using ErrorState = std::optional<E>;

constexpr auto NoError = std::nullopt;
}// namespace anywho
1 change: 1 addition & 0 deletions include/anywho.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "concepts.hpp"
#include "error_factories.hpp"
#endif
#include "aliases.hpp"
#include "context.hpp"
#include "direct_return.hpp"
#include "errors.hpp"
Expand Down
1 change: 0 additions & 1 deletion include/errors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,4 @@ class ErrorFromCode final : public GenericError
private:
std::error_code code_;
};

}// namespace anywho
30 changes: 15 additions & 15 deletions test/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,36 +39,36 @@ namespace direct_return_optional {

std::expected<int, DummyError> myfuncValid() { return 3; }

std::optional<DummyError> myfuncUnexpectedRaised(int &val)
anywho::ErrorState<DummyError> myfuncUnexpectedRaised(int &val)
{
int val_cur = ANYWHO_OPT(myfuncUnexpected());
val = 3 * val_cur;

return std::nullopt;
return anywho::NoError;
}

std::optional<DummyError> myfuncValidRaised(int &val)
anywho::ErrorState<DummyError> myfuncValidRaised(int &val)
{
int val_cur = ANYWHO_OPT(myfuncValid());
val = 3 * val_cur;

return std::nullopt;
return anywho::NoError;
}

std::optional<DummyError> myfuncUnexpectedRaised2(int &val)
anywho::ErrorState<DummyError> myfuncUnexpectedRaised2(int &val)
{
ANYWHO_LEGACY(myfuncUnexpectedRaised(val));
val = 3 * val;

return std::nullopt;
return anywho::NoError;
}

std::optional<DummyError> myfuncValidRaised2(int &val)
anywho::ErrorState<DummyError> myfuncValidRaised2(int &val)
{
ANYWHO_LEGACY(myfuncValidRaised(val));
val = 3 * val;

return std::nullopt;
return anywho::NoError;
}
}// namespace direct_return_optional
std::expected<int, DummyError> testError() { return std::unexpected(DummyError{}); }
Expand Down Expand Up @@ -104,12 +104,12 @@ int positiveOnlySquareWithException(int num)
}
}

std::optional<DummyError> positiveOnlySquareWithOptional(int num, int &output)
anywho::ErrorState<DummyError> positiveOnlySquareWithOptional(int num, int &output)
{
if (num > 0) {
output = num * num;

return std::nullopt;
return anywho::NoError;
}

return DummyError{};
Expand All @@ -133,27 +133,27 @@ TEST_CASE("value get returned with anywho", "[direct_return]")
TEST_CASE("error get returned with anywho_opt", "[direct_return]")
{
int val = 0;
std::optional<DummyError> result = direct_return_optional::myfuncUnexpectedRaised(val);
anywho::ErrorState<DummyError> result = direct_return_optional::myfuncUnexpectedRaised(val);
REQUIRE(anywho::has_error(result));
}
TEST_CASE("value get returned with anywho_opt", "[direct_return]")
{
int val = 0;
std::optional<DummyError> result = direct_return_optional::myfuncValidRaised(val);
anywho::ErrorState<DummyError> result = direct_return_optional::myfuncValidRaised(val);
REQUIRE(!anywho::has_error(result));
REQUIRE(val == 9);
}

TEST_CASE("error get returned with anywho_legacy", "[direct_return]")
{
int val = 0;
std::optional<DummyError> result = direct_return_optional::myfuncUnexpectedRaised2(val);
anywho::ErrorState<DummyError> result = direct_return_optional::myfuncUnexpectedRaised2(val);
REQUIRE(anywho::has_error(result));
}
TEST_CASE("value get returned with anywho_legacy", "[direct_return]")
{
int val = 0;
std::optional<DummyError> result = direct_return_optional::myfuncValidRaised2(val);
anywho::ErrorState<DummyError> result = direct_return_optional::myfuncValidRaised2(val);
REQUIRE(!anywho::has_error(result));
REQUIRE(val == 27);
}
Expand Down Expand Up @@ -373,4 +373,4 @@ TEST_CASE("test optional<Error> factory with function, error case", "[error_fact
return std::make_tuple(ret, output);
});
REQUIRE(!exp.has_value());
}
}

0 comments on commit cb4be13

Please sign in to comment.