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 "types.h"
5#include <compare>
6
7
8namespace original {
9
31template <typename DERIVED>
33public:
39 virtual integer compareTo(const DERIVED &other) const = 0;
40
55 template<typename EXTENDED>
56 requires ExtendsOf<comparable<EXTENDED>, EXTENDED>
57 friend std::strong_ordering operator<=>(const EXTENDED& lc, const EXTENDED& rc);
58
64 bool operator==(const DERIVED &other) const;
65
71 bool operator!=(const DERIVED &other) const;
72
78 bool operator<(const DERIVED &other) const;
79
85 bool operator>(const DERIVED &other) const;
86
92 bool operator<=(const DERIVED &other) const;
93
99 bool operator>=(const DERIVED &other) const;
100
104 virtual ~comparable() = default;
105};
106
107// ----------------- Definitions of comparable.h -----------------
108
109template<typename EXTENDED>
110requires ExtendsOf<comparable<EXTENDED>, EXTENDED>
111std::strong_ordering operator<=>(const EXTENDED& lc, const EXTENDED& rc) {
112 return lc.compareTo(rc) <=> 0;
113}
114
115template<typename DERIVED>
116auto comparable<DERIVED>::operator==(const DERIVED &other) const -> bool {
117 return compareTo(other) == 0;
118}
119
120template<typename DERIVED>
121auto comparable<DERIVED>::operator!=(const DERIVED &other) const -> bool {
122 return compareTo(other) != 0;
123}
124
125template<typename DERIVED>
126auto comparable<DERIVED>::operator<(const DERIVED &other) const -> bool {
127 return compareTo(other) < 0;
128}
129
130template<typename DERIVED>
131auto comparable<DERIVED>::operator>(const DERIVED &other) const -> bool {
132 return compareTo(other) > 0;
133}
134
135template<typename DERIVED>
136auto comparable<DERIVED>::operator<=(const DERIVED &other) const -> bool {
137 return compareTo(other) <= 0;
138}
139
140template<typename DERIVED>
141auto comparable<DERIVED>::operator>=(const DERIVED &other) const -> bool {
142 return compareTo(other) >= 0;
143}
144
145} // namespace original
146
147#endif //COMPARABLE_H
Base class for comparable objects.
Definition comparable.h:32
bool operator<=(const DERIVED &other) const
Checks if the current object is less than or equal to another.
Definition comparable.h:136
bool operator!=(const DERIVED &other) const
Checks if the current object is not equal to another.
Definition comparable.h:121
bool operator>=(const DERIVED &other) const
Checks if the current object is greater than or equal to another.
Definition comparable.h:141
virtual integer compareTo(const DERIVED &other) const =0
Compares the current object with another of the same type.
bool operator==(const DERIVED &other) const
Checks if the current object is equal to another.
Definition comparable.h:116
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:131
bool operator<(const DERIVED &other) const
Checks if the current object is less than another.
Definition comparable.h:126
friend std::strong_ordering operator<=>(const EXTENDED &lc, const EXTENDED &rc)
Three-way comparison operator (<=>), returns an ordered comparison result.
Definition comparable.h:111
Checks derivation or type identity using std::derived_from.
Definition types.h:393
Platform-independent type definitions and compiler/platform detection.
std::int64_t integer
64-bit signed integer type for arithmetic operations
Definition config.h:254
Main namespace for the project Original.
Definition algorithms.h:21
std::strong_ordering operator<=>(const EXTENDED &lc, const EXTENDED &rc)
Definition comparable.h:111
Core type system foundations and concept definitions.