Skip to content

Commit

Permalink
Refactor configs
Browse files Browse the repository at this point in the history
  • Loading branch information
fintarin committed Nov 29, 2023
1 parent 386dec5 commit fa05cdd
Show file tree
Hide file tree
Showing 17 changed files with 77 additions and 110 deletions.
5 changes: 4 additions & 1 deletion include/fintamath/core/Converter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ class Converter {
}

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

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

private:
static Parser::Vector<std::unique_ptr<IArithmetic>, const std::string &> &getParser();
static Parser::Vector<std::unique_ptr<IArithmetic>, const std::string &> &getParser() {
static Parser::Vector<std::unique_ptr<IArithmetic>, const std::string &> parser;
return parser;
}
};

template <typename Derived>
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 Parser::Vector<std::unique_ptr<IComparable>, const std::string &> &getParser();
static Parser::Vector<std::unique_ptr<IComparable>, const std::string &> &getParser() {
static Parser::Vector<std::unique_ptr<IComparable>, const std::string &> parser;
return parser;
}
};

template <typename Derived>
Expand Down
5 changes: 4 additions & 1 deletion include/fintamath/core/IMathObject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ class IMathObject {
virtual bool equalsAbstract(const IMathObject &rhs) const = 0;

private:
static Parser::Vector<std::unique_ptr<IMathObject>, const std::string &> &getParser();
static Parser::Vector<std::unique_ptr<IMathObject>, const std::string &> &getParser() {
static Parser::Vector<std::unique_ptr<IMathObject>, const std::string &> parser;
return parser;
}
};

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

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

static Parser::Vector<std::unique_ptr<Term>, const Token &> &getTermMakers();
static Parser::Vector<std::unique_ptr<Term>, const Token &> &getTermMakers() {
static Parser::Vector<std::unique_ptr<Term>, const Token &> maker;
return maker;
}

static Parser::Map<std::unique_ptr<IMathObject>, ArgumentPtrVector &&> &getExpressionMakers();

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 @@ -69,7 +69,10 @@ class IExpression : public IArithmetic {
static ArgumentPtrVector convertToApproximatedNumbers(const ArgumentPtrVector &args);

private:
static Parser::Vector<std::unique_ptr<IExpression>, const std::string &> &getParser();
static Parser::Vector<std::unique_ptr<IExpression>, const std::string &> &getParser() {
static Parser::Vector<std::unique_ptr<IExpression>, const std::string &> parser;
return parser;
}
};

template <typename Derived, bool isMultiFunction = false>
Expand Down
5 changes: 4 additions & 1 deletion include/fintamath/functions/IFunction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@ class IFunction : public IMathObject {
}

private:
static Parser::Map<std::unique_ptr<IFunction>> &getParser();
static Parser::Map<std::unique_ptr<IFunction>> &getParser() {
static Parser::Map<std::unique_ptr<IFunction>> parser;
return parser;
}

static std::unordered_map<std::string, size_t> &getFunctionOrderMutableMap() {
static std::unordered_map<std::string, size_t> orderMap;
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 Parser::Map<std::unique_ptr<IOperator>> &getParser();
static Parser::Map<std::unique_ptr<IOperator>> &getParser() {
static Parser::Map<std::unique_ptr<IOperator>> 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 @@ -28,7 +28,10 @@ class ILiteral : public IMathObject {
}

private:
static Parser::Vector<std::unique_ptr<ILiteral>, const std::string &> &getParser();
static Parser::Vector<std::unique_ptr<ILiteral>, const std::string &> &getParser() {
static Parser::Vector<std::unique_ptr<ILiteral>, const std::string &> parser;
return parser;
}
};

template <typename Derived>
Expand Down
5 changes: 4 additions & 1 deletion include/fintamath/literals/constants/IConstant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ class IConstant : public ILiteral {
virtual std::unique_ptr<IMathObject> call() const = 0;

private:
static Parser::Map<std::unique_ptr<IConstant>> &getParser();
static Parser::Map<std::unique_ptr<IConstant>> &getParser() {
static Parser::Map<std::unique_ptr<IConstant>> parser;
return parser;
}
};

template <typename Return, typename Derived>
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 @@ -87,7 +87,10 @@ class IInteger : public INumber {
virtual IInteger &decreaseAbstract() = 0;

private:
static Parser::Vector<std::unique_ptr<IInteger>, const std::string &> &getParser();
static Parser::Vector<std::unique_ptr<IInteger>, const std::string &> &getParser() {
static Parser::Vector<std::unique_ptr<IInteger>, const std::string &> 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 Parser::Vector<std::unique_ptr<INumber>, const std::string &> &getParser();
static Parser::Vector<std::unique_ptr<INumber>, const std::string &> &getParser() {
static Parser::Vector<std::unique_ptr<INumber>, const std::string &> parser;
return parser;
}
};

inline std::unique_ptr<INumber> operator+(const INumber &lhs, const INumber &rhs) {
Expand Down
5 changes: 4 additions & 1 deletion include/fintamath/parser/Tokenizer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ class Tokenizer {

static bool isSpace(char ch);

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

}
24 changes: 24 additions & 0 deletions src/fintamath/config/Config.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "fintamath/config/ConverterConfig.hpp"
#include "fintamath/config/ExpressionConfig.hpp"
#include "fintamath/config/ParserConfig.hpp"

using namespace fintamath;

namespace fintamath {

Parser::Map<std::unique_ptr<IMathObject>, ArgumentPtrVector &&> &Expression::getExpressionMakers() {
static Parser::Map<std::unique_ptr<IMathObject>, ArgumentPtrVector &&> maker;
return maker;
}

}

using namespace fintamath;

namespace {

const ParserConfig parserConfig;
const ConverterConfig coverterConfig;
const ExpressionConfig expressionConfig;

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,6 @@

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;
}

}

using namespace fintamath;

namespace {

struct ConverterConfig {
ConverterConfig() {
Converter::add<Integer, Integer>([](const Integer & /*type*/, const Integer &value) {
Expand Down Expand Up @@ -56,6 +45,4 @@ struct ConverterConfig {
}
};

const ConverterConfig config;

}
Original file line number Diff line number Diff line change
Expand Up @@ -90,22 +90,6 @@

namespace fintamath {

Parser::Vector<std::unique_ptr<Term>, const Token &> &Expression::getTermMakers() {
static Parser::Vector<std::unique_ptr<Term>, const Token &> maker;
return maker;
}

Parser::Map<std::unique_ptr<IMathObject>, ArgumentPtrVector &&> &Expression::getExpressionMakers() {
static Parser::Map<std::unique_ptr<IMathObject>, ArgumentPtrVector &&> maker;
return maker;
}

}

using namespace fintamath;

namespace {

struct ExpressionConfig {
ExpressionConfig() {
registerTermsMakers();
Expand Down Expand Up @@ -459,6 +443,4 @@ struct ExpressionConfig {
}
};

const ExpressionConfig config;

}
Original file line number Diff line number Diff line change
Expand Up @@ -95,73 +95,10 @@

namespace fintamath {

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

Parser::Vector<std::unique_ptr<IMathObject>, const std::string &> &IMathObject::getParser() {
static Parser::Vector<std::unique_ptr<IMathObject>, const std::string &> parser;
return parser;
}

Parser::Vector<std::unique_ptr<IArithmetic>, const std::string &> &IArithmetic::getParser() {
static Parser::Vector<std::unique_ptr<IArithmetic>, const std::string &> parser;
return parser;
}

Parser::Vector<std::unique_ptr<IComparable>, const std::string &> &IComparable::getParser() {
static Parser::Vector<std::unique_ptr<IComparable>, const std::string &> parser;
return parser;
}

Parser::Vector<std::unique_ptr<INumber>, const std::string &> &INumber::getParser() {
static Parser::Vector<std::unique_ptr<INumber>, const std::string &> parser;
return parser;
}

Parser::Vector<std::unique_ptr<IInteger>, const std::string &> &IInteger::getParser() {
static Parser::Vector<std::unique_ptr<IInteger>, const std::string &> parser;
return parser;
}

Parser::Vector<std::unique_ptr<ILiteral>, const std::string &> &ILiteral::getParser() {
static Parser::Vector<std::unique_ptr<ILiteral>, const std::string &> parser;
return parser;
}

Parser::Map<std::unique_ptr<IConstant>> &IConstant::getParser() {
static Parser::Map<std::unique_ptr<IConstant>> parser;
return parser;
}

Parser::Map<std::unique_ptr<IFunction>> &IFunction::getParser() {
static Parser::Map<std::unique_ptr<IFunction>> parser;
return parser;
}

Parser::Map<std::unique_ptr<IOperator>> &IOperator::getParser() {
static Parser::Map<std::unique_ptr<IOperator>> parser;
return parser;
}

Parser::Vector<std::unique_ptr<IExpression>, const std::string &> &IExpression::getParser() {
static Parser::Vector<std::unique_ptr<IExpression>, const std::string &> parser;
return parser;
}

}

using namespace fintamath;

namespace {

struct ParserConfig {
ParserConfig() {
IMathObject::registerType<ILiteral>(&ILiteral::parse);
IMathObject::registerType<IFunction>([](const std::string &str) {
return IFunction::parse(str);
});
IMathObject::registerType<IFunction>([](const std::string &str) { return IFunction::parse(str); });
IMathObject::registerType<IArithmetic>(&IArithmetic::parse);

IArithmetic::registerType<IComparable>(&IComparable::parse);
Expand Down Expand Up @@ -261,6 +198,4 @@ struct ParserConfig {
}
};

const ParserConfig config;

}

0 comments on commit fa05cdd

Please sign in to comment.