Skip to content

Commit

Permalink
Use noexcept if function doesn't throw any exception
Browse files Browse the repository at this point in the history
  • Loading branch information
fintarin committed Apr 5, 2024
1 parent f76a8e8 commit 71e1442
Show file tree
Hide file tree
Showing 130 changed files with 260 additions and 277 deletions.
6 changes: 3 additions & 3 deletions include/fintamath/core/Converter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ class Converter final {
using ConverterMultiMethod = MultiMethod<std::unique_ptr<IMathObject>(const IMathObject &, const IMathObject &)>;

public:
static std::unique_ptr<IMathObject> convert(const IMathObject &to, const IMathObject &from) {
static std::unique_ptr<IMathObject> convert(const IMathObject &to, const IMathObject &from) noexcept {
return getConverter()(to, from);
}

static bool isConvertible(const IMathObject &to, const IMathObject &from) {
static bool isConvertible(const IMathObject &to, const IMathObject &from) noexcept {
return getConverter().contains(to, from);
}

template <std::derived_from<IMathObject> To, std::derived_from<IMathObject> From>
static void add(const ConverterFunction<To, From> &convertFunc) {
static void add(const ConverterFunction<To, From> &convertFunc) noexcept {
getConverter().add<To, From>(convertFunc);
}

Expand Down
20 changes: 10 additions & 10 deletions include/fintamath/core/IMathObject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,28 @@ class IMathObject {
FINTAMATH_PARENT_CLASS_BODY(IMathObject)

public:
virtual ~IMathObject() = default;
virtual ~IMathObject() noexcept = default;

virtual std::unique_ptr<IMathObject> clone() const & = 0;
virtual std::unique_ptr<IMathObject> clone() const & noexcept = 0;

virtual std::unique_ptr<IMathObject> clone() && = 0;
virtual std::unique_ptr<IMathObject> clone() && noexcept = 0;

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

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

virtual MathObjectClass getClass() const = 0;
virtual MathObjectClass getClass() const noexcept = 0;

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

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

template <typename Derived>
Expand All @@ -50,11 +50,11 @@ class IMathObjectCRTP : public IMathObject {
};

template <std::derived_from<IMathObject> Lhs, ConvertibleToAndNotSameAs<Lhs> Rhs>
bool operator==(const Lhs &lhs, const Rhs &rhs) {
bool operator==(const Lhs &lhs, const Rhs &rhs) noexcept {
return lhs == Lhs(rhs);
}

inline std::ostream &operator<<(std::ostream &out, const IMathObject &rhs) {
inline std::ostream &operator<<(std::ostream &out, const IMathObject &rhs) noexcept {
return out << rhs.toString();
}

Expand Down
16 changes: 8 additions & 8 deletions include/fintamath/core/IMathObjectCRTP.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,28 @@ class IMathObjectCRTP_ : public IMathObject {
#endif // I_MATH_OBJECT_CRTP

public:
std::unique_ptr<IMathObject> clone() const & final {
std::unique_ptr<IMathObject> clone() const & noexcept final {
return std::make_unique<Derived>(cast<Derived>(*this));
}

std::unique_ptr<IMathObject> clone() && final {
std::unique_ptr<IMathObject> clone() && noexcept final {
return std::make_unique<Derived>(std::move(cast<Derived>(*this)));
}

bool operator==(const I_MATH_OBJECT_CRTP &rhs) const {
return equals(cast<Derived>(rhs));
MathObjectClass getClass() const noexcept final {
return Derived::getClassStatic();
}

MathObjectClass getClass() const override {
return Derived::getClassStatic();
bool operator==(const I_MATH_OBJECT_CRTP &rhs) const noexcept {
return equals(cast<Derived>(rhs));
}

protected:
virtual bool equals(const Derived &rhs) const {
virtual bool equals(const Derived &rhs) const noexcept {
return toString() == rhs.toString();
}

bool equalsAbstract(const IMathObject &rhs) const override {
bool equalsAbstract(const IMathObject &rhs) const noexcept override {
if (const auto *rhsPtr = cast<Derived>(&rhs)) {
return equals(*rhsPtr);
}
Expand Down
26 changes: 13 additions & 13 deletions include/fintamath/core/MathObjectBody.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
#include "fintamath/core/MathObjectClass.hpp"
#include "fintamath/core/Parser.hpp"

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

#define FINTAMATH_PARENT_CLASS_BODY(Class) \
Expand All @@ -19,7 +19,7 @@ public: \
private: \
using Class##Parser = detail::Parser<std::unique_ptr<Class>>; \
\
static Class##Parser &getParser(); \
static Class##Parser &getParser() noexcept; \
\
public: \
static auto parse(std::string str) { \
Expand All @@ -31,15 +31,15 @@ public: \
} \
\
template <std::derived_from<Class> T> \
static void registerType() { \
static void registerType() noexcept { \
MathObjectClass::bindTypes<Class, T>(); \
getParser().registerType<T>(); \
getParser().template registerType<T>(); \
} \
\
private:

#define FINTAMATH_PARENT_CLASS_IMPLEMENTATION(Class) \
Class::Class##Parser &Class::getParser() { \
static Class##Parser parser; \
return parser; \
#define FINTAMATH_PARENT_CLASS_IMPLEMENTATION(Class) \
Class::Class##Parser &Class::getParser() noexcept { \
static Class##Parser parser; \
return parser; \
}
26 changes: 13 additions & 13 deletions include/fintamath/core/MathObjectClass.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,36 +38,36 @@ class MathObjectClass final {
using ParentToChildrenMap = std::unordered_map<MathObjectClass, Children>;

public:
constexpr MathObjectClass(const Name inName) : name(inName) {
constexpr MathObjectClass(const Name inName) noexcept : name(inName) {
}

constexpr Name getName() const {
constexpr Name getName() const noexcept {
return name;
}

constexpr bool operator==(const MathObjectClass rhs) const {
constexpr bool operator==(const MathObjectClass rhs) const noexcept {
return name == rhs.name;
}

std::strong_ordering operator<=>(MathObjectClass rhs) const;
std::strong_ordering operator<=>(MathObjectClass rhs) const noexcept;

std::optional<MathObjectClass> getParent() const;
std::optional<MathObjectClass> getParent() const noexcept;

const Children &getChildren(bool recursive = false) const;
const Children &getChildren(bool recursive = false) const noexcept;

template <typename Parent, std::derived_from<Parent> Child>
static void bindTypes();
static void bindTypes() noexcept;

private:
Id getId() const;
Id getId() const noexcept;

static ClassToIdMap &getClassToIdMap();
static ClassToIdMap &getClassToIdMap() noexcept;

static ChildToParentMap &getChildToParentMap();
static ChildToParentMap &getChildToParentMap() noexcept;

static ParentToChildrenMap &getParentToChildrenMap();
static ParentToChildrenMap &getParentToChildrenMap() noexcept;

static ParentToChildrenMap &getParentToRecursiveChildrenMap();
static ParentToChildrenMap &getParentToRecursiveChildrenMap() noexcept;

private:
Name name;
Expand All @@ -78,7 +78,7 @@ class MathObjectClass final {
};

template <typename Parent, std::derived_from<Parent> Child>
void MathObjectClass::bindTypes() {
void MathObjectClass::bindTypes() noexcept {
MathObjectClass parent = Parent::getClassStatic();
MathObjectClass child = Child::getClassStatic();

Expand Down
24 changes: 12 additions & 12 deletions include/fintamath/core/MathObjectUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ namespace fintamath {

class IMathObject;

inline bool is(const MathObjectClass to, const MathObjectClass from) {
inline bool is(const MathObjectClass to, const MathObjectClass from) noexcept {
return to == from || to.getChildren(true).contains(from);
}

template <std::derived_from<IMathObject> To, std::derived_from<IMathObject> From>
bool is(const From &from) {
bool is(const From &from) noexcept {
if constexpr (std::is_base_of_v<To, From>) {
return true;
}
Expand All @@ -29,7 +29,7 @@ bool is(const From &from) {
}

template <std::derived_from<IMathObject> To, std::derived_from<IMathObject> From>
bool is(const From *from) {
bool is(const From *from) noexcept {
if (!from) {
return false;
}
Expand All @@ -38,27 +38,27 @@ bool is(const From *from) {
}

template <std::derived_from<IMathObject> To, std::derived_from<IMathObject> From>
bool is(const std::unique_ptr<From> &from) {
bool is(const std::unique_ptr<From> &from) noexcept {
return is<To>(from.get());
}

template <std::derived_from<IMathObject> To, std::derived_from<IMathObject> From>
bool is(const std::shared_ptr<From> &from) {
bool is(const std::shared_ptr<From> &from) noexcept {
return is<To>(from.get());
}

template <std::derived_from<IMathObject> To, std::derived_from<IMathObject> From>
bool is(const std::shared_ptr<const From> &from) {
bool is(const std::shared_ptr<const From> &from) noexcept {
return is<To>(from.get());
}

template <std::derived_from<IMathObject> To, std::derived_from<IMathObject> From>
bool is(const std::reference_wrapper<From> &from) {
bool is(const std::reference_wrapper<From> &from) noexcept {
return is<To>(from.get());
}

template <std::derived_from<IMathObject> To, std::derived_from<IMathObject> From>
bool is(const std::reference_wrapper<const From> &from) {
bool is(const std::reference_wrapper<const From> &from) noexcept {
return is<To>(from.get());
}

Expand All @@ -85,7 +85,7 @@ To &cast(From &from) {
}

template <std::derived_from<IMathObject> To, std::derived_from<IMathObject> From>
const To *cast(const From *from) {
const To *cast(const From *from) noexcept {
if constexpr (!std::is_base_of_v<To, From>) {
if (!is<To>(from)) {
return {};
Expand All @@ -96,7 +96,7 @@ const To *cast(const From *from) {
}

template <std::derived_from<IMathObject> To, std::derived_from<IMathObject> From>
To *cast(From *from) {
To *cast(From *from) noexcept {
if constexpr (!std::is_base_of_v<To, From>) {
if (!is<To>(from)) {
return {};
Expand All @@ -107,7 +107,7 @@ To *cast(From *from) {
}

template <std::derived_from<IMathObject> To, std::derived_from<IMathObject> From>
std::unique_ptr<To> cast(std::unique_ptr<From> &&from) {
std::unique_ptr<To> cast(std::unique_ptr<From> &&from) noexcept {
if constexpr (!std::is_base_of_v<To, From>) {
if (!is<To>(from)) {
from.reset();
Expand All @@ -121,7 +121,7 @@ std::unique_ptr<To> cast(std::unique_ptr<From> &&from) {
}

template <std::derived_from<IMathObject> To, std::derived_from<IMathObject> From>
std::shared_ptr<const To> cast(const std::shared_ptr<const From> &from) {
std::shared_ptr<const To> cast(const std::shared_ptr<const From> &from) noexcept {
if constexpr (!std::is_base_of_v<To, From>) {
if (!is<To>(from)) {
return {};
Expand Down
14 changes: 7 additions & 7 deletions include/fintamath/core/MultiMethod.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,18 @@ class MultiMethod<Res(ArgsBase...)> final {
public:
template <typename... Args>
requires(sizeof...(Args) == sizeof...(ArgsBase))
void add(const auto &func) {
void add(const auto &func) noexcept {
idToCallbackMap[CallbackId(Args::getClassStatic()...)] = [func](const ArgsBase &...args) {
return func(cast<Args>(args)...);
};
}

template <typename... Args>
requires(sizeof...(Args) == sizeof...(ArgsBase))
bool contains(const Args &...args) const noexcept {
return idToCallbackMap.contains(CallbackId(args.getClass()...));
}

template <typename... Args>
requires(sizeof...(Args) == sizeof...(ArgsBase))
Res operator()(Args &&...args) const {
Expand All @@ -43,12 +49,6 @@ class MultiMethod<Res(ArgsBase...)> final {
return {};
}

template <typename... Args>
requires(sizeof...(Args) == sizeof...(ArgsBase))
bool contains(const Args &...args) const {
return idToCallbackMap.contains(CallbackId(args.getClass()...));
}

private:
IdToCallbackMap idToCallbackMap;
};
Expand Down
8 changes: 4 additions & 4 deletions include/fintamath/core/Parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class Parser final {

template <typename Type>
requires(!std::is_abstract_v<Type> && !StringConstructable<Type> && EmptyConstructable<Type>)
void registerType() {
void registerType() noexcept {
static const std::string name = Type{}.toString();

stringToConstructorsMap[name].emplace_back([]() -> Return {
Expand All @@ -81,7 +81,7 @@ class Parser final {

template <typename Type>
requires(!std::is_abstract_v<Type> && StringConstructable<Type>)
void registerType() {
void registerType() noexcept {
generatorConstructors.emplace_back([](std::string str) -> Generator {
try {
co_yield std::make_unique<Type>(std::move(str));
Expand All @@ -94,7 +94,7 @@ class Parser final {

template <typename Type>
requires(std::is_abstract_v<Type>)
void registerType() {
void registerType() noexcept {
generatorConstructors.emplace_back([](std::string str) -> Generator {
for (auto &value : Type::parse(std::move(str))) {
co_yield std::move(value);
Expand All @@ -103,7 +103,7 @@ class Parser final {
}

template <typename Type>
void registerType() const {
void registerType() const noexcept {
// No object of this type can be constructed
}

Expand Down
4 changes: 2 additions & 2 deletions include/fintamath/core/Tokenizer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Tokenizer final {
public:
static TokenVector tokenize(std::string str);

static void registerToken(const Token &token);
static void registerToken(const Token &token) noexcept;

private:
static bool appendToken(TokenVector &tokens, Token &token, bool shouldSplit = false);
Expand All @@ -23,7 +23,7 @@ class Tokenizer final {

static bool isSpace(char ch);

static TokenVector &getRegisteredTokens();
static TokenVector &getRegisteredTokens() noexcept;
};

}
Loading

0 comments on commit 71e1442

Please sign in to comment.