Skip to content

Commit

Permalink
Move some classes and functions to detail namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
fintarin committed Feb 13, 2024
1 parent 176acd3 commit 529a20b
Show file tree
Hide file tree
Showing 102 changed files with 221 additions and 77 deletions.
2 changes: 1 addition & 1 deletion include/fintamath/config/Config.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

namespace fintamath {
namespace fintamath::detail {

struct Config final {
Config();
Expand Down
2 changes: 1 addition & 1 deletion include/fintamath/core/Cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <functional>
#include <unordered_map>

namespace fintamath {
namespace fintamath::detail {

template <typename Key, typename Value>
class Cache final {
Expand Down
10 changes: 7 additions & 3 deletions include/fintamath/core/Converter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace fintamath {

class IMathObject;

namespace detail {

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)>;
Expand All @@ -34,9 +36,11 @@ class Converter final {
static ConverterMultiMethod &getConverter();
};

}

template <std::derived_from<IMathObject> To, std::derived_from<IMathObject> From>
std::unique_ptr<To> convert(const To &to, const From &from) {
return cast<To>(Converter::convert(to, from));
return cast<To>(detail::Converter::convert(to, from));
}

template <std::derived_from<IMathObject> To, std::derived_from<IMathObject> From>
Expand All @@ -47,13 +51,13 @@ std::unique_ptr<To> convert(const From &from) {

template <std::derived_from<IMathObject> To, std::derived_from<IMathObject> From>
bool isConvertible(const To &to, const From &from) {
return Converter::isConvertible(to, from);
return detail::Converter::isConvertible(to, from);
}

template <std::derived_from<IMathObject> To, std::derived_from<IMathObject> From>
bool isConvertible(const From &from) {
static const To to;
return Converter::isConvertible(to, from);
return detail::Converter::isConvertible(to, from);
}

}
2 changes: 1 addition & 1 deletion include/fintamath/core/IArithmetic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
namespace fintamath {

class IArithmetic : public IMathObject {
using ArithmeticParser = Parser<std::unique_ptr<IArithmetic>()>;
using ArithmeticParser = detail::Parser<std::unique_ptr<IArithmetic>()>;

public:
friend std::unique_ptr<IArithmetic> operator+(const IArithmetic &lhs, const IArithmetic &rhs) {
Expand Down
2 changes: 1 addition & 1 deletion include/fintamath/core/IComparable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace fintamath {

class IComparable : public IArithmetic {
using ComparableParser = Parser<std::unique_ptr<IComparable>()>;
using ComparableParser = detail::Parser<std::unique_ptr<IComparable>()>;

public:
friend std::strong_ordering operator<=>(const IComparable &lhs, const IComparable &rhs) {
Expand Down
2 changes: 1 addition & 1 deletion include/fintamath/core/IMathObject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace fintamath {

class IMathObject {
using MathObjectParser = Parser<std::unique_ptr<IMathObject>()>;
using MathObjectParser = detail::Parser<std::unique_ptr<IMathObject>()>;

public:
virtual ~IMathObject() = default;
Expand Down
8 changes: 6 additions & 2 deletions include/fintamath/core/MathObjectTypes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,15 @@ class MathObjectType final {
size_t id;

private:
[[maybe_unused]] inline static const Config config;
[[maybe_unused]] inline static const detail::Config config;
};

inline size_t hash_value(const MathObjectType &rhs) noexcept {
return boost::hash<size_t>{}(rhs);
}

namespace detail {

class MathObjectBoundTypes final {
using enum MathObjectType::Id;

Expand Down Expand Up @@ -236,8 +238,10 @@ class MathObjectBoundTypes final {
}
};

}

inline bool isBaseOf(const MathObjectType &toType, const MathObjectType &fromType) {
const auto &typeToTypeMap = MathObjectBoundTypes::get();
const auto &typeToTypeMap = detail::MathObjectBoundTypes::get();

if (const auto toTypeBoundaries = typeToTypeMap.find(toType); toTypeBoundaries != typeToTypeMap.end()) {
return fromType >= toTypeBoundaries->first && fromType < toTypeBoundaries->second;
Expand Down
2 changes: 1 addition & 1 deletion include/fintamath/core/MultiMethod.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "fintamath/core/CoreUtils.hpp"
#include "fintamath/core/MathObjectTypes.hpp"

namespace fintamath {
namespace fintamath::detail {

template <typename Signature>
class MultiMethod;
Expand Down
2 changes: 1 addition & 1 deletion include/fintamath/core/Parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include "fintamath/core/Tokenizer.hpp"
#include "fintamath/exceptions/InvalidInputException.hpp"

namespace fintamath {
namespace fintamath::detail {

template <typename T, typename... Args>
concept StringConstructable = requires(const std::string &str, Args &&...args) {
Expand Down
2 changes: 1 addition & 1 deletion include/fintamath/core/Tokenizer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <string>
#include <vector>

namespace fintamath {
namespace fintamath::detail {

using Token = std::string;
using TokenVector = std::vector<Token>;
Expand Down
28 changes: 16 additions & 12 deletions include/fintamath/expressions/Expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

namespace fintamath {

namespace detail {

struct Term final {
Token name;

Expand All @@ -34,12 +36,14 @@ struct Term final {
}
};

using TermVector = std::vector<std::unique_ptr<Term>>;
using TermVector = std::vector<std::unique_ptr<detail::Term>>;
using OperandStack = std::stack<std::unique_ptr<IMathObject>>;

}

class Expression final : public IExpressionCRTP<Expression> {
using TermParser = Parser<std::unique_ptr<Term>()>;
using ExpressionParser = Parser<std::unique_ptr<IMathObject>(ArgumentPtrVector &&)>;
using TermParser = detail::Parser<std::unique_ptr<detail::Term>()>;
using ExpressionParser = detail::Parser<std::unique_ptr<IMathObject>(ArgumentPtrVector &&)>;
using ExpressionMaker = std::function<std::unique_ptr<IMathObject>(ArgumentPtrVector &&)>;

public:
Expand Down Expand Up @@ -94,23 +98,23 @@ class Expression final : public IExpressionCRTP<Expression> {

void updateStringMutable() const;

static TermVector tokensToTerms(const TokenVector &tokens);
static detail::TermVector tokensToTerms(const detail::TokenVector &tokens);

static OperandStack termsToOperands(TermVector &terms);
static detail::OperandStack termsToOperands(detail::TermVector &terms);

static std::unique_ptr<IMathObject> operandsToObject(OperandStack &operands);
static std::unique_ptr<IMathObject> operandsToObject(detail::OperandStack &operands);

static ArgumentPtrVector unwrapComma(const ArgumentPtr &child);

static void insertMultiplications(TermVector &terms);
static void insertMultiplications(detail::TermVector &terms);

static void fixOperatorTypes(const TermVector &terms);
static void fixOperatorTypes(const detail::TermVector &terms);

static void collapseFactorials(TermVector &terms);
static void collapseFactorials(detail::TermVector &terms);

static bool canNextTermBeBinaryOperator(const Term &term);
static bool canNextTermBeBinaryOperator(const detail::Term &term);

static bool canPrevTermBeBinaryOperator(const Term &term);
static bool canPrevTermBeBinaryOperator(const detail::Term &term);

static bool isBinaryOperator(const IMathObject *val);

Expand All @@ -126,7 +130,7 @@ class Expression final : public IExpressionCRTP<Expression> {

static ArgumentPtr compress(const ArgumentPtr &child);

friend std::unique_ptr<IMathObject> makeExpr(const IFunction &func, ArgumentPtrVector args);
friend std::unique_ptr<IMathObject> detail::makeExpr(const IFunction &func, ArgumentPtrVector args);

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

Expand Down
2 changes: 1 addition & 1 deletion include/fintamath/expressions/ExpressionComparator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include "fintamath/functions/FunctionArguments.hpp"

namespace fintamath {
namespace fintamath::detail {

struct ComparatorOptions final {
bool constantGreaterThanVariable = false;
Expand Down
4 changes: 2 additions & 2 deletions include/fintamath/expressions/ExpressionUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "fintamath/literals/Variable.hpp"
#include "fintamath/literals/constants/Undefined.hpp"

namespace fintamath {
namespace fintamath::detail {

template <std::same_as<ArgumentPtr>... Args, std::invocable<IFunction, Args...> SimplifyFunction>
ArgumentPtr useSimplifyFunctions(const std::vector<SimplifyFunction> &simplFuncs,
Expand Down Expand Up @@ -98,4 +98,4 @@ std::string prefixUnaryOperatorToString(const IOperator &oper, const ArgumentPtr

std::string postfixUnaryOperatorToString(const IOperator &oper, const ArgumentPtr &rhs);

}
}
2 changes: 1 addition & 1 deletion include/fintamath/expressions/IExpression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
namespace fintamath {

class IExpression : public IArithmetic {
using ExpressionParser = Parser<std::unique_ptr<IExpression>()>;
using ExpressionParser = detail::Parser<std::unique_ptr<IExpression>()>;

public:
virtual const std::shared_ptr<IFunction> &getFunction() const = 0;
Expand Down
13 changes: 8 additions & 5 deletions include/fintamath/functions/FunctionUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@
#include "fintamath/core/IMathObject.hpp"
#include "fintamath/functions/FunctionArguments.hpp"

#define FINTAMATH_FUNCTION_EXPRESSION(Function, name) \
std::unique_ptr<IMathObject> name(auto &&...args) { \
static const Function f; \
return makeExpr(f, std::forward<decltype(args)>(args)...); \
#define FINTAMATH_FUNCTION_EXPRESSION(Function, name) \
std::unique_ptr<IMathObject> name(auto &&...args) { \
static const Function f; \
return detail::makeExpr(f, std::forward<decltype(args)>(args)...); \
}

namespace fintamath {

class IFunction;

namespace detail {

extern bool isExpression(const IMathObject &arg);

extern std::unique_ptr<IMathObject> makeExpr(const IFunction &func, ArgumentPtrVector args);
Expand All @@ -31,3 +32,5 @@ std::unique_ptr<IMathObject> makeExpr(const IFunction &func, std::convertible_to
}

}

}
2 changes: 1 addition & 1 deletion include/fintamath/functions/IFunction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace fintamath {
class IFunction : public IMathObject {
using FunctionOrderMap = std::unordered_map<std::string, size_t>;

using FunctionParser = Parser<std::unique_ptr<IFunction>()>;
using FunctionParser = detail::Parser<std::unique_ptr<IFunction>()>;

public:
virtual const ArgumentTypeVector &getArgumentTypes() const = 0;
Expand Down
8 changes: 4 additions & 4 deletions include/fintamath/functions/IFunctionCRTP.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,20 @@ class IFunctionCRTP_ : public IFunction {
return res;
}

return makeExpr(*this, argVect);
return detail::makeExpr(*this, argVect);
}
catch (const UndefinedException &) {
return makeExpr(*this, argVect);
return detail::makeExpr(*this, argVect);
}
}

return makeExpr(*this, argVect)->toMinimalObject();
return detail::makeExpr(*this, argVect)->toMinimalObject();
}

private:
template <size_t i, typename Head, typename... Tail>
bool doArgsMatch(const ArgumentRefVector &argVect) const {
if (!is<Head>(argVect[i]) || isExpression(argVect[i])) {
if (!is<Head>(argVect[i]) || detail::isExpression(argVect[i])) {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion include/fintamath/functions/IOperator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
namespace fintamath {

class IOperator : public IFunction {
using OperatorParser = Parser<std::unique_ptr<IOperator>()>;
using OperatorParser = detail::Parser<std::unique_ptr<IOperator>()>;

public:
enum class Priority : uint8_t {
Expand Down
2 changes: 1 addition & 1 deletion include/fintamath/literals/ILiteral.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace fintamath {

class ILiteral : public IMathObject {
using LiteralParser = Parser<std::unique_ptr<ILiteral>()>;
using LiteralParser = detail::Parser<std::unique_ptr<ILiteral>()>;

public:
static std::unique_ptr<ILiteral> parse(const std::string &str) {
Expand Down
2 changes: 1 addition & 1 deletion include/fintamath/literals/constants/IConstant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace fintamath {

class IConstant : public ILiteral {
using ConstantParser = Parser<std::unique_ptr<IConstant>()>;
using ConstantParser = detail::Parser<std::unique_ptr<IConstant>()>;

public:
virtual MathObjectType getReturnType() const = 0;
Expand Down
2 changes: 1 addition & 1 deletion include/fintamath/numbers/IInteger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace fintamath {

class IInteger : public INumber {
using IntegerParser = Parser<std::unique_ptr<IInteger>()>;
using IntegerParser = detail::Parser<std::unique_ptr<IInteger>()>;

public:
friend std::unique_ptr<IInteger> operator%(const IInteger &lhs, const IInteger &rhs) {
Expand Down
2 changes: 1 addition & 1 deletion include/fintamath/numbers/INumber.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace fintamath {

class INumber : public IComparable {
using NumberParser = Parser<std::unique_ptr<INumber>()>;
using NumberParser = detail::Parser<std::unique_ptr<INumber>()>;

public:
virtual bool isPrecise() const {
Expand Down
2 changes: 1 addition & 1 deletion include/fintamath/numbers/NumberUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <string>

namespace fintamath {
namespace fintamath::detail {

std::string removeLeadingZeroes(std::string str);

Expand Down
2 changes: 1 addition & 1 deletion src/fintamath/config/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "fintamath/config/ParserConfig.hpp"
#include "fintamath/config/PrecisionConfig.hpp"

namespace fintamath {
namespace fintamath::detail {

Config::Config() {
[[maybe_unused]] static const PrecisionConfig precisionConfig;
Expand Down
2 changes: 1 addition & 1 deletion src/fintamath/config/ConverterConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "fintamath/numbers/Rational.hpp"
#include "fintamath/numbers/Real.hpp"

namespace fintamath {
namespace fintamath::detail {

ConverterConfig::ConverterConfig() {
Converter::add<Integer, Integer>([](const Integer & /*type*/, const Integer &value) {
Expand Down
2 changes: 1 addition & 1 deletion src/fintamath/config/ConverterConfig.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

namespace fintamath {
namespace fintamath::detail {

struct ConverterConfig final {
ConverterConfig();
Expand Down
2 changes: 1 addition & 1 deletion src/fintamath/config/ExpressionConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
#include "fintamath/literals/ILiteral.hpp"
#include "fintamath/literals/constants/E.hpp"

namespace fintamath {
namespace fintamath::detail {

namespace {

Expand Down
2 changes: 1 addition & 1 deletion src/fintamath/config/ExpressionConfig.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

namespace fintamath {
namespace fintamath::detail {

struct ExpressionConfig final {
ExpressionConfig();
Expand Down
Loading

0 comments on commit 529a20b

Please sign in to comment.