SleakEngine
0.1.0
C++23 multi-backend game engine
Toggle main menu visibility
Loading...
Searching...
No Matches
Pair.hpp
Go to the documentation of this file.
1
#ifndef _PAIR_H_
2
#define _PAIR_H_
3
4
#include <utility>
// For std::forward, std::swap (optional)
5
6
namespace
Sleak
{
7
8
/**
9
* @class Pair
10
* @brief A simple container for storing two values as a pair.
11
*
12
* This class provides a way to store two values together as a single unit.
13
* It is similar to `std::pair` and can be used in similar situations,
14
* such as storing key-value pairs, coordinates, or any other data
15
* that needs to be grouped together.
16
*
17
* Example Use Cases:
18
* - Storing key-value pairs in a map or hash table.
19
* - Representing coordinates (x, y) or (latitude, longitude).
20
* - Returning multiple values from a function.
21
*
22
* Implementation Details:
23
* - Stores two values of potentially different types.
24
* - Provides access to the values through `first` and `second` members.
25
* - Implements comparison operators for lexicographical ordering.
26
* - Offers a `makePair` function for convenient creation of `Pair` objects.
27
* @ingroup utility
28
*/
29
template
<
typename
T1,
typename
T2>
30
class
Pair
{
31
public
:
32
// Member variables to store the first and second values
33
T1
first
;
34
T2
second
;
35
36
/**
37
* @brief Default constructor.
38
*/
39
Pair
() =
default
;
40
41
/**
42
* @brief Constructor with initial values.
43
*
44
* @param first The initial value for the first member.
45
* @param second The initial value for the second member.
46
*/
47
Pair
(
const
T1&
first
,
const
T2&
second
) :
first
(
first
),
second
(
second
) {}
48
49
/**
50
* @brief Copy constructor.
51
*
52
* @param other The `Pair` object to copy from.
53
*/
54
Pair
(
const
Pair
& other) =
default
;
55
56
/**
57
* @brief Move constructor.
58
*
59
* @param other The `Pair` object to move from.
60
*/
61
Pair
(
Pair
&& other) noexcept
62
:
first
(std::forward<T1>(other.first)),
63
second
(std::forward<T2>(other.second)) {}
64
65
/**
66
* @brief Copy assignment operator.
67
*
68
* @param other The `Pair` object to copy from.
69
* @return A reference to the assigned `Pair` object.
70
*/
71
Pair
&
operator=
(
const
Pair
& other) =
default
;
72
73
/**
74
* @brief Move assignment operator.
75
*
76
* @param other The `Pair` object to move from.
77
* @return A reference to the assigned `Pair` object.
78
*/
79
Pair
&
operator=
(
Pair
&& other)
noexcept
{
80
if
(
this
!= &other) {
81
first
= std::forward<T1>(other.first);
82
second
= std::forward<T2>(other.second);
83
}
84
return
*
this
;
85
}
86
87
/**
88
* @brief Swaps the contents of this `Pair` with another `Pair`.
89
*
90
* @param other The `Pair` object to swap with.
91
*/
92
void
swap
(
Pair
& other)
noexcept
{
93
using
std::swap;
// Bring std::swap into scope (optional)
94
swap
(
first
, other.first);
95
swap
(
second
, other.second);
96
}
97
};
98
99
/**
100
* @brief Creates a `Pair` object with the given values.
101
*
102
* This is a helper function for convenient creation of `Pair` objects.
103
*
104
* @param first The first value.
105
* @param second The second value.
106
* @return A `Pair` object containing the given values.
107
*/
108
template
<
typename
T1,
typename
T2>
109
Pair<T1, T2>
makePair
(T1&& first, T2&& second) {
110
return
Pair<T1, T2>
(std::forward<T1>(first), std::forward<T2>(second));
111
}
112
113
// Comparison operators (implementing lexicographical comparison)
114
template
<
typename
T1,
typename
T2>
115
bool
operator==
(
const
Pair<T1, T2>
& lhs,
const
Pair<T1, T2>
& rhs) {
116
return
lhs.
first
== rhs.
first
&& lhs.
second
== rhs.
second
;
117
}
118
119
template
<
typename
T1,
typename
T2>
120
bool
operator!=
(
const
Pair<T1, T2>
& lhs,
const
Pair<T1, T2>
& rhs) {
121
return
!(lhs == rhs);
122
}
123
124
template
<
typename
T1,
typename
T2>
125
bool
operator<
(
const
Pair<T1, T2>
& lhs,
const
Pair<T1, T2>
& rhs) {
126
if
(lhs.
first
< rhs.
first
)
return
true
;
127
if
(rhs.
first
< lhs.
first
)
return
false
;
128
return
lhs.
second
< rhs.
second
;
129
}
130
131
template
<
typename
T1,
typename
T2>
132
bool
operator<=
(
const
Pair<T1, T2>
& lhs,
const
Pair<T1, T2>
& rhs) {
133
return
!(rhs < lhs);
134
}
135
136
template
<
typename
T1,
typename
T2>
137
bool
operator>
(
const
Pair<T1, T2>
& lhs,
const
Pair<T1, T2>
& rhs) {
138
return
rhs < lhs;
139
}
140
141
template
<
typename
T1,
typename
T2>
142
bool
operator>=
(
const
Pair<T1, T2>
& lhs,
const
Pair<T1, T2>
& rhs) {
143
return
!(lhs < rhs);
144
}
145
}
146
147
#endif
// _PAIR_H_
Sleak::Pair
A simple container for storing two values as a pair.
Definition
Pair.hpp:30
Sleak::Pair::first
T1 first
Definition
Pair.hpp:33
Sleak::Pair::operator=
Pair & operator=(Pair &&other) noexcept
Move assignment operator.
Definition
Pair.hpp:79
Sleak::Pair::Pair
Pair(Pair &&other) noexcept
Move constructor.
Definition
Pair.hpp:61
Sleak::Pair::operator=
Pair & operator=(const Pair &other)=default
Copy assignment operator.
Sleak::Pair::Pair
Pair(const Pair &other)=default
Copy constructor.
Sleak::Pair::Pair
Pair(const T1 &first, const T2 &second)
Constructor with initial values.
Definition
Pair.hpp:47
Sleak::Pair::Pair
Pair()=default
Default constructor.
Sleak::Pair::swap
void swap(Pair &other) noexcept
Swaps the contents of this Pair with another Pair.
Definition
Pair.hpp:92
Sleak::Pair::second
T2 second
Definition
Pair.hpp:34
Sleak
Root namespace for everything the engine exposes.
Definition
Camera.hpp:10
Sleak::operator<
bool operator<(const Pair< T1, T2 > &lhs, const Pair< T1, T2 > &rhs)
Definition
Pair.hpp:125
Sleak::makePair
Pair< T1, T2 > makePair(T1 &&first, T2 &&second)
Creates a Pair object with the given values.
Definition
Pair.hpp:109
Sleak::operator>
bool operator>(const Pair< T1, T2 > &lhs, const Pair< T1, T2 > &rhs)
Definition
Pair.hpp:137
Sleak::operator<=
bool operator<=(const Pair< T1, T2 > &lhs, const Pair< T1, T2 > &rhs)
Definition
Pair.hpp:132
Sleak::operator>=
bool operator>=(const Pair< T1, T2 > &lhs, const Pair< T1, T2 > &rhs)
Definition
Pair.hpp:142
Sleak::operator!=
bool operator!=(const Pair< T1, T2 > &lhs, const Pair< T1, T2 > &rhs)
Definition
Pair.hpp:120
Sleak::operator==
bool operator==(const Pair< T1, T2 > &lhs, const Pair< T1, T2 > &rhs)
Definition
Pair.hpp:115
include
public
Utility
Container
Pair.hpp
Generated on
for SleakEngine by
1.18.0