Skip to content

Commit

Permalink
MathObjectClass, new Parser, class body macro
Browse files Browse the repository at this point in the history
  • Loading branch information
fintarin committed Mar 25, 2024
1 parent df8c9d6 commit b152101
Show file tree
Hide file tree
Showing 354 changed files with 2,063 additions and 2,827 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@
[submodule "thirdparty/fmt"]
path = thirdparty/fmt
url = https://github.com/fmtlib/fmt
[submodule "thirdparty/cppcoro"]
path = thirdparty/cppcoro
url = https://github.com/andreasbuhr/cppcoro
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ target_link_libraries(
Boost::multiprecision
Boost::math
Boost::container_hash
fmt::fmt)
fmt::fmt
cppcoro)

include(cmake/CompilerWarnings.cmake)
include(cmake/Coverage.cmake)
Expand Down
10 changes: 5 additions & 5 deletions cmake/Coverage.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
if(CMAKE_CXX_COMPILER_ID MATCHES ".*Clang")
option(${PROJECT_NAME}_enable_coverage "Enable coverage reporting for gcc/clang" OFF)
if(${PROJECT_NAME}_enable_coverage)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-instr-generate -fcoverage-mapping -O0 -g")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-instr-generate -fcoverage-mapping -O0")

add_custom_target(
${PROJECT_NAME}_coverage
COMMAND ./bin/fintamath_tests
COMMAND llvm-profdata merge -o merged.profdata *.profraw
COMMAND llvm-cov show --show-branches=count --ignore-filename-regex='tests|build|thirdparty|MathObjectType.hpp'
--show-instantiations --instr-profile merged.profdata bin/fintamath_tests > coverage.txt
COMMAND llvm-cov export --ignore-filename-regex='tests|build|thirdparty|MathObjectType.hpp' --instr-profile
merged.profdata bin/fintamath_tests -format lcov > lcov.info
COMMAND llvm-cov show --show-branches=count --ignore-filename-regex='tests|build|thirdparty' --instr-profile
merged.profdata bin/fintamath_tests > coverage.txt
COMMAND llvm-cov export --ignore-filename-regex='tests|build|thirdparty' --instr-profile merged.profdata
bin/fintamath_tests -format lcov > lcov.info
COMMAND genhtml --branch-coverage lcov.info -o coverage
DEPENDS ${PROJECT_NAME}_tests
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
Expand Down
26 changes: 3 additions & 23 deletions include/fintamath/core/IArithmetic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
#include <string>
#include <utility>

#include "fintamath/core/CoreUtils.hpp"
#include "fintamath/core/IMathObject.hpp"
#include "fintamath/core/MathObjectType.hpp"
#include "fintamath/core/MathObjectClass.hpp"
#include "fintamath/core/MathObjectUtils.hpp"
#include "fintamath/core/Parser.hpp"

namespace fintamath {

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

public:
friend std::unique_ptr<IArithmetic> operator+(const IArithmetic &lhs, const IArithmetic &rhs) {
Expand All @@ -40,23 +40,6 @@ class IArithmetic : public IMathObject {
return rhs.negateAbstract();
}

static std::unique_ptr<IArithmetic> parse(const std::string &str) {
return getParser().parse(str);
}

template <std::derived_from<IArithmetic> T>
static void registerType() {
getParser().registerType<T>();
}

static void registerType(ArithmeticParser::StringConstructor constructor) {
getParser().registerType(std::move(constructor));
}

static constexpr MathObjectType getTypeStatic() {
return {MathObjectType::IArithmetic, "IArithmetic"};
}

protected:
virtual std::unique_ptr<IArithmetic> addAbstract(const IArithmetic &rhs) const = 0;

Expand All @@ -67,9 +50,6 @@ class IArithmetic : public IMathObject {
virtual std::unique_ptr<IArithmetic> divideAbstract(const IArithmetic &rhs) const = 0;

virtual std::unique_ptr<IArithmetic> negateAbstract() const = 0;

private:
static ArithmeticParser &getParser();
};

template <typename Derived>
Expand Down
21 changes: 3 additions & 18 deletions include/fintamath/core/IComparable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,23 @@
#include <string>
#include <utility>

#include "fintamath/core/CoreUtils.hpp"
#include "fintamath/core/IArithmetic.hpp"
#include "fintamath/core/MathObjectType.hpp"
#include "fintamath/core/MathObjectClass.hpp"
#include "fintamath/core/MathObjectUtils.hpp"
#include "fintamath/core/Parser.hpp"

namespace fintamath {

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

public:
friend std::strong_ordering operator<=>(const IComparable &lhs, const IComparable &rhs) {
return lhs.compareAbstract(rhs);
}

static std::unique_ptr<IComparable> parse(const std::string &str) {
return getParser().parse(str);
}

static void registerType(ComparableParser::StringConstructor constructor) {
getParser().registerType(std::move(constructor));
}

static constexpr MathObjectType getTypeStatic() {
return {MathObjectType::IComparable, "IComparable"};
}

protected:
virtual std::strong_ordering compareAbstract(const IComparable &rhs) const = 0;

private:
static ComparableParser &getParser();
};

template <typename Derived>
Expand Down
26 changes: 6 additions & 20 deletions include/fintamath/core/IMathObject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
#include <utility>

#include "fintamath/core/Converter.hpp"
#include "fintamath/core/CoreUtils.hpp"
#include "fintamath/core/MathObjectType.hpp"
#include "fintamath/core/MathObjectBody.hpp"
#include "fintamath/core/MathObjectClass.hpp"
#include "fintamath/core/MathObjectUtils.hpp"
#include "fintamath/core/Parser.hpp"

namespace fintamath {

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

public:
virtual ~IMathObject() = default;
Expand All @@ -24,36 +25,21 @@ class IMathObject {
virtual std::unique_ptr<IMathObject> clone() && = 0;

virtual std::string toString() const {
return std::string(getType().getName());
return std::string(getClass().getName());
}

virtual std::unique_ptr<IMathObject> toMinimalObject() const {
return clone();
}

virtual MathObjectType getType() const = 0;
virtual MathObjectClass getClass() const = 0;

friend bool operator==(const IMathObject &lhs, const IMathObject &rhs) {
return lhs.equalsAbstract(rhs);
}

static std::unique_ptr<IMathObject> parse(const std::string &str) {
return getParser().parse(str);
}

static void registerType(MathObjectParser::StringConstructor constructor) {
getParser().registerType(std::move(constructor));
}

static constexpr MathObjectType getTypeStatic() {
return {MathObjectType::IMathObject, "IMathObject"};
}

protected:
virtual bool equalsAbstract(const IMathObject &rhs) const = 0;

private:
static MathObjectParser &getParser();
};

template <typename Derived>
Expand Down
4 changes: 2 additions & 2 deletions include/fintamath/core/IMathObjectCRTP.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class IMathObjectCRTP_ : public IMathObject {
return equals(cast<Derived>(rhs));
}

MathObjectType getType() const override {
return Derived::getTypeStatic();
MathObjectClass getClass() const override {
return Derived::getClassStatic();
}

protected:
Expand Down
42 changes: 42 additions & 0 deletions include/fintamath/core/MathObjectBody.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#pragma once

#include <memory>

#include "fintamath/core/MathObjectClass.hpp"
#include "fintamath/core/Parser.hpp"

#define FINTAMATH_CLASS_BODY(Class) \
public: \
static constexpr MathObjectClass getClassStatic() { \
return {#Class}; \
} \
\
private:

#define FINTAMATH_PARENT_CLASS_BODY(Class) \
FINTAMATH_CLASS_BODY(Class) \
\
private: \
using Class##Parser = detail::Parser<std::unique_ptr<Class>()>; \
\
static auto &getParser() { \
static Class##Parser parser; \
return parser; \
} \
\
public: \
static auto parse(const std::string &str) { \
return getParser().parse(str); \
} \
\
static auto parseFirst(const std::string &str) { \
return getParser().parseFirst(str); \
} \
\
template <std::derived_from<Class> T> \
static void registerType() { \
MathObjectClass::bindTypes<Class, T>(); \
getParser().registerType<T>(); \
} \
\
private:
63 changes: 0 additions & 63 deletions include/fintamath/core/MathObjectBoundTypes.hpp

This file was deleted.

Loading

0 comments on commit b152101

Please sign in to comment.