ORIGINAL
Loading...
Searching...
No Matches
couple.h
Go to the documentation of this file.
1#ifndef COUPLE_H
2#define COUPLE_H
3
10
11#include "printable.h"
12#include "comparable.h"
13#include "types.h"
14#include "error.h"
15
16
17namespace original
18{
32 template<typename F_TYPE, typename S_TYPE>
33 class couple final : public printable, public comparable<couple<F_TYPE, S_TYPE>>
34 {
35 F_TYPE first_;
36 S_TYPE second_;
37
38 public:
42 explicit couple();
43
49 couple(F_TYPE* first, S_TYPE* second);
50
56 couple(const F_TYPE& first, const S_TYPE& second);
57
62 couple(const couple& other);
63
69 couple& operator=(const couple& other);
70
76 template<u_integer IDX>
77 auto get() const;
78
85 template<u_integer IDX, typename T>
86 void set(const T& e);
87
94 integer compareTo(const couple &other) const override;
95
100 F_TYPE& first();
101
106 S_TYPE& second();
107
111 ~couple() override;
112
117 [[nodiscard]] std::string className() const override;
118
124 [[nodiscard]] std::string toString(bool enter) const override;
125 };
126}
127
128 template <typename F_TYPE, typename S_TYPE>
130
131 template <typename F_TYPE, typename S_TYPE>
133 : first_(*first), second_(*second) {}
134
135 template <typename F_TYPE, typename S_TYPE>
137 : first_(first), second_(second) {}
138
139 template <typename F_TYPE, typename S_TYPE>
141 : first_(other.first_), second_(other.second_) {}
142
143 template <typename F_TYPE, typename S_TYPE>
145 {
146 if (this == &other) return *this;
147 first_ = other.first_;
148 second_ = other.second_;
149 return *this;
150 }
151
152 template<typename F_TYPE, typename S_TYPE>
153 template<original::u_integer IDX>
155 staticError<outOfBoundError, (IDX > 1)>{};
156 if constexpr (IDX == 0){
157 return this->first_;
158 }else {
159 return this->second_;
160 }
161 }
162
163 template<typename F_TYPE, typename S_TYPE>
164 template<original::u_integer IDX, typename T>
166 staticError<outOfBoundError, (IDX > 1)>{};
167 if constexpr (IDX == 0){
169 this->first_ = static_cast<F_TYPE>(e);
170 } else{
172 this->second_ = static_cast<S_TYPE>(e);
173 }
174 }
175
176 template<typename F_TYPE, typename S_TYPE>
178 {
179 if constexpr (Comparable<F_TYPE>){
180 if (this->first_ < other.first_)
181 return -1;
182 if (this->first_ > other.first_)
183 return 1;
184 }
185 if constexpr (Comparable<S_TYPE>){
186 if (this->second_ < other.second_)
187 return -1;
188 if (this->second_ > other.second_)
189 return 1;
190 }
191 return 0;
192 }
193
194 template <typename F_TYPE, typename S_TYPE>
196 {
197 return this->first_;
198 }
199
200 template <typename F_TYPE, typename S_TYPE>
202 {
203 return this->second_;
204 }
205
206 template <typename F_TYPE, typename S_TYPE>
208
209 template <typename F_TYPE, typename S_TYPE>
211 {
212 return "couple";
213 }
214
215 template <typename F_TYPE, typename S_TYPE>
216 auto original::couple<F_TYPE, S_TYPE>::toString(const bool enter) const -> std::string
217 {
218 std::stringstream ss;
219 ss << this->className() << "(" << formatString(this->first_)
220 << ", " << formatString(this->second_) << ")";
221 if (enter) ss << "\n";
222 return ss.str();
223 }
224
225#endif //COUPLE_H
Base class for comparable objects.
Definition comparable.h:31
std::string className() const override
Gets class name identifier.
Definition couple.h:210
std::string toString(bool enter) const override
Formats pair elements as string.
Definition couple.h:216
~couple() override
Default destructor.
F_TYPE & first()
Access first element.
Definition couple.h:195
S_TYPE & second()
Access second element.
Definition couple.h:201
auto get() const
element access template method
Definition couple.h:154
void set(const T &e)
element modifies the template method
Definition couple.h:165
couple & operator=(const couple &other)
Copy assignment operator.
Definition couple.h:144
couple()
Default constructs both elements.
Definition couple.h:129
integer compareTo(const couple &other) const override
Lexicographical comparison operation.
Definition couple.h:177
Exception for container index out-of-range errors.
Definition error.h:84
Base class providing polymorphic string conversion capabilities.
Definition printable.h:25
static std::string formatString(const TYPE &t)
Universal value-to-string conversion.
Compile-time error assertion utility.
Definition error.h:73
Interface for objects that can be compared.
Requires type to support all comparison operators.
Definition types.h:29
Custom exception classes and callback validation utilities.
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
Interface for polymorphic string formatting and output.
Type system foundations and concept definitions.