Skip to content

Commit

Permalink
Refactor configs
Browse files Browse the repository at this point in the history
  • Loading branch information
fintarin committed Feb 1, 2024
1 parent 84afe8a commit bf09998
Show file tree
Hide file tree
Showing 23 changed files with 572 additions and 546 deletions.
9 changes: 9 additions & 0 deletions include/fintamath/config/Config.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

namespace fintamath {

struct Config final {
Config();
};

}
7 changes: 6 additions & 1 deletion include/fintamath/core/Converter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class Converter final {
template <std::derived_from<IMathObject> To, std::derived_from<IMathObject> From>
using ConverterFunction = std::function<std::unique_ptr<IMathObject>(const To &to, const From &from)>;

using ConverterMultiMethod = MultiMethod<std::unique_ptr<IMathObject>(const IMathObject &, const IMathObject &)>;

public:
template <std::derived_from<IMathObject> To, std::derived_from<IMathObject> From>
static void add(const ConverterFunction<To, From> &convertFunc) {
Expand All @@ -27,7 +29,10 @@ class Converter final {
}

private:
static MultiMethod<std::unique_ptr<IMathObject>(const IMathObject &, const IMathObject &)> &getConverter();
static ConverterMultiMethod &getConverter() {
static ConverterMultiMethod converter;
return converter;
}
};

template <std::derived_from<IMathObject> To, std::derived_from<IMathObject> From>
Expand Down
6 changes: 4 additions & 2 deletions include/fintamath/core/IArithmetic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,16 @@ class IArithmetic : public IMathObject {
virtual std::unique_ptr<IArithmetic> negateAbstract() const = 0;

private:
static ArithmeticParser &getParser();
static ArithmeticParser &getParser() {
static ArithmeticParser parser;
return parser;
}
};

template <typename Derived>
class IArithmeticCRTP : public IArithmetic {
#define I_ARITHMETIC_CRTP IArithmeticCRTP<Derived>
#include "fintamath/core/IArithmeticCRTP.hpp"

#undef I_ARITHMETIC_CRTP
};

Expand Down
5 changes: 4 additions & 1 deletion include/fintamath/core/IComparable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ class IComparable : public IArithmetic {
virtual std::strong_ordering compareAbstract(const IComparable &rhs) const = 0;

private:
static ComparableParser &getParser();
static ComparableParser &getParser() {
static ComparableParser parser;
return parser;
}
};

template <typename Derived>
Expand Down
9 changes: 7 additions & 2 deletions include/fintamath/core/IMathObject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <sstream>
#include <string>

#include "fintamath/config//Config.hpp"
#include "fintamath/core/Converter.hpp"
#include "fintamath/core/CoreUtils.hpp"
#include "fintamath/core/Parser.hpp"
Expand Down Expand Up @@ -50,14 +51,16 @@ class IMathObject {
virtual bool equalsAbstract(const IMathObject &rhs) const = 0;

private:
static MathObjectParser &getParser();
static MathObjectParser &getParser() {
static MathObjectParser parser;
return parser;
}
};

template <typename Derived>
class IMathObjectCRTP : public IMathObject {
#define I_MATH_OBJECT_CRTP IMathObjectCRTP
#include "fintamath/core/IMathObjectCRTP.hpp"

#undef I_MATH_OBJECT_CRTP
};

Expand All @@ -70,4 +73,6 @@ inline std::ostream &operator<<(std::ostream &out, const IMathObject &rhs) {
return out << rhs.toString();
}

inline static const Config config;

}
5 changes: 4 additions & 1 deletion include/fintamath/core/Tokenizer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ class Tokenizer final {

static bool isSpace(char ch);

static TokenVector &getRegisteredTokens();
static TokenVector &getRegisteredTokens() {
static TokenVector registeredTokens;
return registeredTokens;
}
};

}
10 changes: 8 additions & 2 deletions include/fintamath/expressions/Expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,15 @@ class Expression final : public IExpressionCRTP<Expression> {

friend std::unique_ptr<IMathObject> parseFintamath(const std::string &str);

static TermParser &getTermParser();
static TermParser &getTermParser() {
static TermParser parser;
return parser;
}

static ExpressionParser &getExpressionParser();
static ExpressionParser &getExpressionParser() {
static Parser<std::unique_ptr<IMathObject>, ArgumentPtrVector &&> maker;
return maker;
}

private:
mutable ArgumentPtr child;
Expand Down
5 changes: 4 additions & 1 deletion include/fintamath/expressions/IExpression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ class IExpression : public IArithmetic {
static ArgumentPtrVector convertToApproximatedNumbers(const ArgumentPtrVector &args);

private:
static ExpressionParser &getParser();
static ExpressionParser &getParser() {
static ExpressionParser parser;
return parser;
}
};

template <typename Derived, bool isMultiFunction = false>
Expand Down
12 changes: 9 additions & 3 deletions include/fintamath/functions/IFunction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,17 @@ class IFunction : public IMathObject {
}

private:
static FunctionOrderMap &getFunctionOrderMutableMap();
static FunctionOrderMap &getFunctionOrderMutableMap() {
static FunctionOrderMap orderMap;
return orderMap;
}

static FunctionParser &getParser();
static FunctionParser &getParser() {
static FunctionParser parser;
return parser;
}

static inline size_t maxFunctionOrder = 0;
inline static size_t maxFunctionOrder = 0;
};

template <typename Return, typename Derived, typename... Args>
Expand Down
5 changes: 4 additions & 1 deletion include/fintamath/functions/IOperator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ class IOperator : public IFunction {
}

private:
static OperatorParser &getParser();
static OperatorParser &getParser() {
static OperatorParser parser;
return parser;
}
};

template <typename Return, typename Derived, typename... Args>
Expand Down
5 changes: 4 additions & 1 deletion include/fintamath/literals/ILiteral.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ class ILiteral : public IMathObject {
}

private:
static LiteralParser &getParser();
static LiteralParser &getParser() {
static LiteralParser parser;
return parser;
}
};

template <typename Derived>
Expand Down
6 changes: 4 additions & 2 deletions include/fintamath/literals/constants/IConstant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@ class IConstant : public ILiteral {
virtual std::unique_ptr<IMathObject> call() const = 0;

private:
static ConstantParser &getParser();
static ConstantParser &getParser() {
static ConstantParser parser;
return parser;
}
};

template <typename Return, typename Derived>
class IConstantCRTP : public IConstant {
#define I_CONSTANT_CRTP IConstantCRTP<Return, Derived>
#include "fintamath/literals/constants/IConstantCRTP.hpp"

#undef I_CONSTANT_CRTP
};

Expand Down
5 changes: 4 additions & 1 deletion include/fintamath/numbers/IInteger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ class IInteger : public INumber {
virtual IInteger &decreaseAbstract() = 0;

private:
static IntegerParser &getParser();
static IntegerParser &getParser() {
static IntegerParser parser;
return parser;
}
};

template <typename Derived>
Expand Down
5 changes: 4 additions & 1 deletion include/fintamath/numbers/INumber.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ class INumber : public IComparable {
}

private:
static NumberParser &getParser();
static NumberParser &getParser() {
static NumberParser parser;
return parser;
}
};

inline std::unique_ptr<INumber> operator+(const INumber &lhs, const INumber &rhs) {
Expand Down
17 changes: 17 additions & 0 deletions src/fintamath/config/Config.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "fintamath/config/Config.hpp"

#include "ParserConfig.hpp"
#include "PrecisionConfig.hpp"
#include "fintamath/config/ConverterConfig.hpp"
#include "fintamath/config/ExpressionConfig.hpp"

namespace fintamath {

Config::Config() {
static const PrecisionConfig precisionConfig;
static const ParserConfig parserConfig;
static const ConverterConfig converterConfig;
static const ExpressionConfig expressionConfig;
}

}
86 changes: 36 additions & 50 deletions src/fintamath/config/ConverterConfig.cpp
Original file line number Diff line number Diff line change
@@ -1,61 +1,47 @@
#include "fintamath/core/Converter.hpp"
#include "fintamath/config/ConverterConfig.hpp"

#include "fintamath/core/Converter.hpp"
#include "fintamath/numbers/Complex.hpp"
#include "fintamath/numbers/Integer.hpp"
#include "fintamath/numbers/Rational.hpp"
#include "fintamath/numbers/Real.hpp"

namespace fintamath {

MultiMethod<std::unique_ptr<IMathObject>(const IMathObject &, const IMathObject &)> &Converter::getConverter() {
static MultiMethod<std::unique_ptr<IMathObject>(const IMathObject &, const IMathObject &)> converter;
return converter;
}

ConverterConfig::ConverterConfig() {
Converter::add<Integer, Integer>([](const Integer & /*type*/, const Integer &value) {
return Integer(value).clone();
});

Converter::add<Rational, Rational>([](const Rational & /*type*/, const Rational &value) {
return Rational(value).clone();
});
Converter::add<Rational, Integer>([](const Rational & /*type*/, const Integer &value) {
return Rational(value).clone();
});

Converter::add<Real, Real>([](const Real & /*type*/, const Real &value) {
return Real(value).clone();
});
Converter::add<Real, Integer>([](const Real & /*type*/, const Integer &value) {
return Real(value).clone();
});
Converter::add<Real, Rational>([](const Real & /*type*/, const Rational &value) {
return Real(value).clone();
});

Converter::add<Complex, Complex>([](const Complex & /*type*/, const Complex &value) {
return std::make_unique<Complex>(value);
});
Converter::add<Complex, Integer>([](const Complex & /*type*/, const Integer &value) {
return std::make_unique<Complex>(value);
});
Converter::add<Complex, Rational>([](const Complex & /*type*/, const Rational &value) {
return std::make_unique<Complex>(value);
});
Converter::add<Complex, Real>([](const Complex & /*type*/, const Real &value) {
return std::make_unique<Complex>(value);
});
}

using namespace fintamath;

namespace {

struct ConverterConfig final {
ConverterConfig() {
Converter::add<Integer, Integer>([](const Integer & /*type*/, const Integer &value) {
return Integer(value).clone();
});

Converter::add<Rational, Rational>([](const Rational & /*type*/, const Rational &value) {
return Rational(value).clone();
});
Converter::add<Rational, Integer>([](const Rational & /*type*/, const Integer &value) {
return Rational(value).clone();
});

Converter::add<Real, Real>([](const Real & /*type*/, const Real &value) {
return Real(value).clone();
});
Converter::add<Real, Integer>([](const Real & /*type*/, const Integer &value) {
return Real(value).clone();
});
Converter::add<Real, Rational>([](const Real & /*type*/, const Rational &value) {
return Real(value).clone();
});

Converter::add<Complex, Complex>([](const Complex & /*type*/, const Complex &value) {
return std::make_unique<Complex>(value);
});
Converter::add<Complex, Integer>([](const Complex & /*type*/, const Integer &value) {
return std::make_unique<Complex>(value);
});
Converter::add<Complex, Rational>([](const Complex & /*type*/, const Rational &value) {
return std::make_unique<Complex>(value);
});
Converter::add<Complex, Real>([](const Complex & /*type*/, const Real &value) {
return std::make_unique<Complex>(value);
});
}
};

const ConverterConfig config;

}
9 changes: 9 additions & 0 deletions src/fintamath/config/ConverterConfig.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

namespace fintamath {

struct ConverterConfig final {
ConverterConfig();
};

}
Loading

0 comments on commit bf09998

Please sign in to comment.