Skip to content

Commit

Permalink
Rework all exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
fintarin committed Apr 14, 2024
1 parent 7671a33 commit d501166
Show file tree
Hide file tree
Showing 136 changed files with 4,345 additions and 1,461 deletions.
3 changes: 3 additions & 0 deletions include/fintamath/core/IArithmetic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
#include <string>
#include <utility>

#include <fmt/core.h>

#include "fintamath/core/IMathObject.hpp"
#include "fintamath/core/MathObjectClass.hpp"
#include "fintamath/core/MathObjectUtils.hpp"
#include "fintamath/core/Parser.hpp"
#include "fintamath/exceptions/InvalidInputException.hpp"

namespace fintamath {

Expand Down
8 changes: 7 additions & 1 deletion include/fintamath/core/IArithmeticCRTP.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,13 @@ class IArithmeticCRTP_ : public IArithmetic {
return cast<IArithmetic>(res->toMinimalObject());
}

throw InvalidInputBinaryOperatorException(operStr, toString(), rhs.toString());
throw InvalidInputException(fmt::format(
R"(Invalid arguments of the {} operator ({} "{}" and {} "{}" are unconvertible to each other))",
operStr,
this->getClass()->getName(),
this->toString(),
rhs.getClass()->getName(),
rhs.toString()));
}

private:
Expand Down
2 changes: 2 additions & 0 deletions include/fintamath/core/IComparable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <string>
#include <utility>

#include <fmt/core.h>

#include "fintamath/core/IArithmetic.hpp"
#include "fintamath/core/MathObjectClass.hpp"
#include "fintamath/core/MathObjectUtils.hpp"
Expand Down
7 changes: 6 additions & 1 deletion include/fintamath/core/IComparableCRTP.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ class IComparableCRTP_ : public IComparable {
return 0 <=> (rhs <=> *this);
}

throw InvalidInputBinaryOperatorException("<=>", toString(), rhs.toString());
throw InvalidInputException(fmt::format(
R"(Invalid arguments of the comparison operator ({} "{}" and {} "{}" are unconvertible to each other))",
this->getClass()->getName(),
this->toString(),
rhs.getClass()->getName(),
rhs.toString()));
}

private:
Expand Down
9 changes: 8 additions & 1 deletion include/fintamath/exceptions/Exception.hpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
#pragma once

#include <exception>
#include <string>
#include <utility>

namespace fintamath {

class Exception : public std::exception {
public:
explicit Exception(std::string inMessage) noexcept : message(std::move(inMessage)) {}

const char *what() const noexcept override {
return "Something went wrong...";
return message.data();
}

private:
std::string message;
};

}
50 changes: 11 additions & 39 deletions include/fintamath/exceptions/InvalidInputException.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <cstdint>
#include <string>
#include <utility>
#include <vector>

#include "fintamath/exceptions/Exception.hpp"
Expand All @@ -10,48 +11,26 @@ namespace fintamath {

class InvalidInputException : public Exception {
public:
InvalidInputException() noexcept = default;

explicit InvalidInputException(const std::string &input) noexcept {
content += ": " + input;
}

const char *what() const noexcept override {
return content.c_str();
}

protected:
std::string content = "Invalid input";
explicit InvalidInputException(std::string inMessage) noexcept : Exception(std::move(inMessage)) {}
};

// TODO! remove
class InvalidInputFunctionException final : public InvalidInputException {
public:
explicit InvalidInputFunctionException(const std::string &func, const std::vector<std::string> &argVect) noexcept {
content += ": " + func + "(";

if (!argVect.empty()) {
for (const auto &arg : argVect) {
content += arg + ',';
}

content.pop_back();
}

content += ")";
}

const char *what() const noexcept override {
return content.c_str();
explicit InvalidInputFunctionException(const std::string &func, const std::vector<std::string> &) noexcept
: InvalidInputException(func) {
}
};

// TODO! remove
class InvalidInputBinaryOperatorException final : public InvalidInputException {
public:
explicit InvalidInputBinaryOperatorException(const std::string &oper, const std::string &lhs, const std::string &rhs) noexcept {
content += ": (" + lhs + ")" + oper + "(" + rhs + ")";
explicit InvalidInputBinaryOperatorException(const std::string &oper, const std::string &, const std::string &) noexcept
: InvalidInputException(oper) {
}
};

// TODO! remove
class InvalidInputUnaryOperatorException final : public InvalidInputException {
public:
enum class Type : uint8_t {
Expand All @@ -60,15 +39,8 @@ class InvalidInputUnaryOperatorException final : public InvalidInputException {
};

public:
explicit InvalidInputUnaryOperatorException(const std::string &oper, const std::string &rhs, const Type type) noexcept {
switch (type) {
case Type::Prefix:
content += ": " + oper + "(" + rhs + ")";
break;
case Type::Postfix:
content += ": (" + rhs + ")" + oper;
break;
}
explicit InvalidInputUnaryOperatorException(const std::string &oper, const std::string &, const Type) noexcept
: InvalidInputException(oper) {
}
};

Expand Down
51 changes: 11 additions & 40 deletions include/fintamath/exceptions/UndefinedException.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,34 @@

#include <cstdint>
#include <string>
#include <vector>
#include <utility>

#include "fintamath/exceptions/Exception.hpp"

namespace fintamath {

class UndefinedException : public Exception {
public:
UndefinedException() noexcept = default;

explicit UndefinedException(const std::string &input) noexcept {
content += ": " + input;
}

const char *what() const noexcept override {
return content.c_str();
}

protected:
std::string content = "Undefined";
explicit UndefinedException(std::string inMessage) noexcept : Exception(std::move(inMessage)) {}
};

// TODO! remove
class UndefinedFunctionException final : public UndefinedException {
public:
explicit UndefinedFunctionException(const std::string &func, const std::vector<std::string> &argVect) noexcept {
content += ": " + func + "(";

if (!argVect.empty()) {
for (const auto &arg : argVect) {
content += arg + ',';
}

content.pop_back();
}

content += ")";
}

const char *what() const noexcept override {
return content.c_str();
explicit UndefinedFunctionException(const std::string &func, const std::vector<std::string> &) noexcept
: UndefinedException(func) {
}
};

// TODO! remove
class UndefinedBinaryOperatorException final : public UndefinedException {
public:
explicit UndefinedBinaryOperatorException(const std::string &oper, const std::string &lhs, const std::string &rhs) noexcept {
content += ": (" + lhs + ")" + oper + "(" + rhs + ")";
explicit UndefinedBinaryOperatorException(const std::string &oper, const std::string &, const std::string &) noexcept
: UndefinedException(oper) {
}
};

// TODO! remove
class UndefinedUnaryOperatorException final : public UndefinedException {
public:
enum class Type : uint8_t {
Expand All @@ -60,15 +38,8 @@ class UndefinedUnaryOperatorException final : public UndefinedException {
};

public:
explicit UndefinedUnaryOperatorException(const std::string &oper, const std::string &rhs, const Type type) noexcept {
switch (type) {
case Type::Prefix:
content += ": " + oper + "(" + rhs + ")";
break;
case Type::Postfix:
content += ": (" + rhs + ")" + oper;
break;
}
explicit UndefinedUnaryOperatorException(const std::string &oper, const std::string &, const Type) noexcept
: UndefinedException(oper) {
}
};

Expand Down
9 changes: 4 additions & 5 deletions include/fintamath/literals/Boolean.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,17 @@ class Boolean : public ILiteralCRTP<Boolean> {
public:
Boolean();

explicit Boolean(const std::string &str);

template <std::same_as<bool> Bool>
Boolean(const Bool val) : name(val ? trueStr : falseStr) {
}
Boolean(const Bool rhs) : val(rhs) {}

explicit Boolean(std::string_view str);

std::string toString() const override;

explicit operator bool() const;

private:
std::string name;
bool val;

static constexpr std::string_view trueStr = "True";

Expand Down
4 changes: 2 additions & 2 deletions include/fintamath/literals/Variable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ class Variable : public ILiteralCRTP<Variable> {
FINTAMATH_CLASS_BODY(Variable, ILiteral)

public:
explicit Variable(std::string inName);
explicit Variable(std::string_view inName);

explicit Variable(std::string inName, Integer inIndex);
explicit Variable(std::string_view inName, Integer inIndex);

std::string toString() const override;

Expand Down
3 changes: 2 additions & 1 deletion include/fintamath/numbers/Integer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <cstdint>
#include <memory>
#include <string>
#include <string_view>

#include <boost/multiprecision/fwd.hpp>
#include <boost/multiprecision/gmp.hpp>
Expand All @@ -29,7 +30,7 @@ class Integer : public INumberCRTP<Integer> {

Integer(Backend inBackend);

explicit Integer(std::string str);
explicit Integer(std::string_view str);

std::string toString() const override;

Expand Down
4 changes: 2 additions & 2 deletions include/fintamath/numbers/IntegerFunctions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ using FactorToCountMap = std::unordered_map<Integer, Integer>;
template <std::derived_from<INumber> Lhs>
Lhs pow(const Lhs &lhs, Integer rhs) {
if (lhs == 0 && rhs == 0) {
throw UndefinedBinaryOperatorException("^", lhs.toString(), rhs.toString());
throw UndefinedException("Undefined 0^0");
}

if (rhs < 0) {
Expand Down Expand Up @@ -60,6 +60,6 @@ FactorToCountMap factors(Integer rhs, Integer limit = -1);

Integer combinations(const Integer &totalNumber, const Integer &choosedNumber);

Integer multinomialCoefficient(const Integer &totalNumber, const std::vector<Integer> &groupNumbers);
Integer multinomialCoefficient(const std::vector<Integer> &groupNumbers);

}
2 changes: 1 addition & 1 deletion include/fintamath/numbers/Rational.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Rational : public INumberCRTP<Rational> {

explicit Rational(Integer inNumer, Integer inDenom);

explicit Rational(const std::string &str);
explicit Rational(std::string_view str);

std::string toString() const override;

Expand Down
3 changes: 2 additions & 1 deletion include/fintamath/numbers/Real.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <cstdint>
#include <memory>
#include <string>
#include <string_view>

#include <boost/multiprecision/fwd.hpp>
#include <boost/multiprecision/mpfr.hpp>
Expand Down Expand Up @@ -51,7 +52,7 @@ class Real : public INumberCRTP<Real> {

Real(const Integer &rhs);

explicit Real(std::string str);
explicit Real(std::string_view str);

std::string toString() const override;

Expand Down
Loading

0 comments on commit d501166

Please sign in to comment.