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 d147bed
Show file tree
Hide file tree
Showing 275 changed files with 1,856 additions and 2,382 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
16 changes: 13 additions & 3 deletions include/fintamath/core/CoreUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <ranges>
#include <type_traits>

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

namespace fintamath {

Expand All @@ -21,6 +21,16 @@ concept ConvertibleToAndNotSameAs = std::convertible_to<From, To> && !std::same_
template <typename FromArg, typename ToArg>
concept SameAsUnqual = (std::same_as<std::remove_cvref_t<FromArg>, std::remove_cvref_t<ToArg>>);

inline bool is(const MathObjectClass to, const MathObjectClass from) {
for (std::optional parent = from; parent; parent = parent->getParent()) {
if (parent == to) {
return true;
}
}

return false;
}

template <std::derived_from<IMathObject> To, std::derived_from<IMathObject> From>
bool is(const From &from) {
if constexpr (std::is_base_of_v<To, From>) {
Expand All @@ -30,7 +40,7 @@ bool is(const From &from) {
return false;
}
else {
return isBaseOf(To::getTypeStatic(), from.getType());
return is(To::getClassStatic(), from.getClass());
}
}

Expand Down Expand Up @@ -142,4 +152,4 @@ std::shared_ptr<To> cast(const std::shared_ptr<From> &from) {
return std::const_pointer_cast<To>(cast<To>(std::const_pointer_cast<const From>(from)));
}

}
}
24 changes: 2 additions & 22 deletions include/fintamath/core/IArithmetic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

#include "fintamath/core/CoreUtils.hpp"
#include "fintamath/core/IMathObject.hpp"
#include "fintamath/core/MathObjectType.hpp"
#include "fintamath/core/MathObjectClass.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
19 changes: 2 additions & 17 deletions include/fintamath/core/IComparable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,21 @@

#include "fintamath/core/CoreUtils.hpp"
#include "fintamath/core/IArithmetic.hpp"
#include "fintamath/core/MathObjectType.hpp"
#include "fintamath/core/MathObjectClass.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
25 changes: 6 additions & 19 deletions include/fintamath/core/IMathObject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@

#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/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,42 +25,28 @@ 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>
class IMathObjectCRTP : public IMathObject {
#define I_MATH_OBJECT_CRTP IMathObjectCRTP
#include "fintamath/core/IMathObjectCRTP.hpp"

#undef I_MATH_OBJECT_CRTP
};

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
33 changes: 33 additions & 0 deletions include/fintamath/core/MathObjectBody.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once

#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 Class##Parser &getParser() { \
static Class##Parser parser; \
return parser; \
} \
\
public: \
static Class##Parser::Generator parse(const std::string &str) { \
return getParser().parse(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 d147bed

Please sign in to comment.