ORIGINAL
Loading...
Searching...
No Matches
comparable.h
Go to the documentation of this file.
1#ifndef COMPARABLE_H
2#define COMPARABLE_H
3#include "config.h"
4#include <compare>
5
6
7namespace original {
8
16
30template <typename DERIVED>
32public:
38 virtual integer compareTo(const DERIVED &other) const = 0;
39
54 template<typename EXTENDED>
55 friend std::strong_ordering operator<=>(const EXTENDED& lc, const EXTENDED& rc);
56
62 bool operator==(const DERIVED &other) const;
63
69 bool operator!=(const DERIVED &other) const;
70
76 bool operator<(const DERIVED &other) const;
77
83 bool operator>(const DERIVED &other) const;
84
90 bool operator<=(const DERIVED &other) const;
91
97 bool operator>=(const DERIVED &other) const;
98
102 virtual ~comparable() = default;
103};
104
105// ----------------- Definitions of comparable.h -----------------
106
107template<typename EXTENDED>
108std::strong_ordering operator<=>(const EXTENDED& lc, const EXTENDED& rc) {
109 return lc.compareTo(rc) <=> 0;
110}
111
112template<typename DERIVED>
113auto comparable<DERIVED>::operator==(const DERIVED &other) const -> bool {
114 return compareTo(other) == 0;
115}
116
117template<typename DERIVED>
118auto comparable<DERIVED>::operator!=(const DERIVED &other) const -> bool {
119 return compareTo(other) != 0;
120}
121
122template<typename DERIVED>
123auto comparable<DERIVED>::operator<(const DERIVED &other) const -> bool {
124 return compareTo(other) < 0;
125}
126
127template<typename DERIVED>
128auto comparable<DERIVED>::operator>(const DERIVED &other) const -> bool {
129 return compareTo(other) > 0;
130}
131
132template<typename DERIVED>
133auto comparable<DERIVED>::operator<=(const DERIVED &other) const -> bool {
134 return compareTo(other) <= 0;
135}
136
137template<typename DERIVED>
138auto comparable<DERIVED>::operator>=(const DERIVED &other) const -> bool {
139 return compareTo(other) >= 0;
140}
141
142} // namespace original
143
144#endif //COMPARABLE_H
Base class for comparable objects.
Definition comparable.h:31
bool operator<=(const DERIVED &other) const
Checks if the current object is less than or equal to another.
Definition comparable.h:133
bool operator!=(const DERIVED &other) const
Checks if the current object is not equal to another.
Definition comparable.h:118
bool operator>=(const DERIVED &other) const
Checks if the current object is greater than or equal to another.
Definition comparable.h:138
virtual integer compareTo(const DERIVED &other) const =0
Compares the current object with another of the same type.
friend std::strong_ordering operator<=>(const EXTENDED &lc, const EXTENDED &rc)
Three-way comparison operator (<=>), returns an ordered comparison result.
Definition comparable.h:108
bool operator==(const DERIVED &other) const
Checks if the current object is equal to another.
Definition comparable.h:113
virtual ~comparable()=default
Virtual destructor for proper cleanup of derived objects.
bool operator>(const DERIVED &other) const
Checks if the current object is greater than another.
Definition comparable.h:128
bool operator<(const DERIVED &other) const
Checks if the current object is less than another.
Definition comparable.h:123
Platform-independent integer type definitions.
Main namespace for the project Original.
Definition algorithms.h:21
std::int64_t integer
64-bit signed integer type for arithmetic operations
Definition config.h:15
std::strong_ordering operator<=>(const EXTENDED &lc, const EXTENDED &rc)
Definition comparable.h:108