libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
pair.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <cstddef>
11#include <utility>
12#include <tuple>
14
16
31template <typename A, typename B>
32class Pair {
33 public:
35 using FirstType = A;
36
38 using SecondType = B;
39
41 Pair() = default;
42
48 Pair(const A &a, const B &b) : d(a, b) {}
49
55 Pair(A &&a, B &&b) : d(std::move(a), std::move(b)) {}
56
61 Pair(const std::pair<A, B> &p) : d(p) {}
62
67 Pair(std::pair<A, B> &&p) : d(std::move(p)) {}
68
70 Pair(const Pair &other) = default;
71
73 Pair(Pair &&other) noexcept = default;
74
76 ~Pair() = default;
77
79 Pair &operator=(const Pair &other) = default;
80
82 Pair &operator=(Pair &&other) noexcept = default;
83
85 A &first() { return d.first; }
86
88 const A &first() const { return d.first; }
89
91 B &second() { return d.second; }
92
94 const B &second() const { return d.second; }
95
100 void setFirst(const A &a) { d.first = a; }
101
106 void setSecond(const B &b) { d.second = b; }
107
112 const std::pair<A, B> &toStdPair() const { return d; }
113
118 void swap(Pair &other) noexcept {
119 d.swap(other.d);
120 return;
121 }
122
129 static Pair make(A a, B b) {
130 return Pair(std::move(a), std::move(b));
131 }
132
134 friend bool operator==(const Pair &lhs, const Pair &rhs) {
135 return lhs.d == rhs.d;
136 }
137
139 friend bool operator!=(const Pair &lhs, const Pair &rhs) {
140 return lhs.d != rhs.d;
141 }
142
144 friend bool operator<(const Pair &lhs, const Pair &rhs) {
145 return lhs.d < rhs.d;
146 }
147
153 template <std::size_t I>
154 auto &get() & {
155 if constexpr (I == 0) return d.first;
156 else return d.second;
157 }
158
160 template <std::size_t I>
161 const auto &get() const & {
162 if constexpr (I == 0) return d.first;
163 else return d.second;
164 }
165
167 template <std::size_t I>
168 auto &&get() && {
169 if constexpr (I == 0) return std::move(d.first);
170 else return std::move(d.second);
171 }
172
173 private:
174 std::pair<A, B> d;
175};
176
178
179// Structured bindings support
180namespace std {
181template <typename A, typename B>
182struct tuple_size<promeki::Pair<A, B>> : std::integral_constant<std::size_t, 2> {};
183
184template <typename A, typename B>
185struct tuple_element<0, promeki::Pair<A, B>> { using type = A; };
186
187template <typename A, typename B>
188struct tuple_element<1, promeki::Pair<A, B>> { using type = B; };
189} // namespace std
Dynamic array container wrapping std::vector.
Definition list.h:40
Typed pair container wrapping std::pair.
Definition pair.h:32
Pair(std::pair< A, B > &&p)
Constructs from an existing std::pair (move overload).
Definition pair.h:67
const std::pair< A, B > & toStdPair() const
Returns a const reference to the underlying std::pair.
Definition pair.h:112
void setFirst(const A &a)
Sets the first element.
Definition pair.h:100
Pair(Pair &&other) noexcept=default
Move constructor.
Pair & operator=(Pair &&other) noexcept=default
Move assignment operator.
B & second()
Returns a mutable reference to the second element.
Definition pair.h:91
A & first()
Returns a mutable reference to the first element.
Definition pair.h:85
Pair(const Pair &other)=default
Copy constructor.
auto && get() &&
Structured bindings support: element access by index.
Definition pair.h:168
Pair(const std::pair< A, B > &p)
Constructs from an existing std::pair.
Definition pair.h:61
friend bool operator==(const Pair &lhs, const Pair &rhs)
Returns true if both pairs have identical contents.
Definition pair.h:134
friend bool operator<(const Pair &lhs, const Pair &rhs)
Lexicographic less-than comparison.
Definition pair.h:144
Pair & operator=(const Pair &other)=default
Copy assignment operator.
void setSecond(const B &b)
Sets the second element.
Definition pair.h:106
const auto & get() const &
Structured bindings support: element access by index.
Definition pair.h:161
static Pair make(A a, B b)
Factory function to create a Pair.
Definition pair.h:129
~Pair()=default
Destructor.
Pair()=default
Default constructor. Value-initializes both elements.
Pair(const A &a, const B &b)
Constructs a pair from two values.
Definition pair.h:48
const A & first() const
Returns a const reference to the first element.
Definition pair.h:88
auto & get() &
Structured bindings support: element access by index.
Definition pair.h:154
Pair(A &&a, B &&b)
Constructs a pair by moving two values.
Definition pair.h:55
void swap(Pair &other) noexcept
Swaps contents with another pair.
Definition pair.h:118
friend bool operator!=(const Pair &lhs, const Pair &rhs)
Returns true if the pairs differ.
Definition pair.h:139
const B & second() const
Returns a const reference to the second element.
Definition pair.h:94
#define PROMEKI_NAMESPACE_BEGIN
Starts a promeki namespace block.
Definition namespace.h:14
#define PROMEKI_NAMESPACE_END
Ends a promeki namespace block.
Definition namespace.h:19