neoGFX
Cross-platform C++ app/game engine
Loading...
Searching...
No Matches
pair.hpp
Go to the documentation of this file.
1// pair.hpp
2/*
3 * Copyright (c) 2007 Leigh Johnston.
4 *
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * * Neither the name of Leigh Johnston nor the names of any
19 * other contributors to this software may be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
24 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
25 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34*/
35
36#pragma once
37
38#include <neolib/neolib.hpp>
39#include <utility>
41
42namespace neolib
43{
44 template <typename T1, typename T2>
45 class pair : public i_pair<abstract_t<T1>, abstract_t<T2>>, public std::pair<T1, T2>
46 {
47 typedef pair<T1, T2> self_type;
49 public:
50 using typename base_type::abstract_type;
53 typedef T1 first_type;
54 typedef T2 second_type;
55 private:
56 typedef std::pair<first_type, second_type> concrete_type;
57 public:
58 pair() : concrete_type{ first_type{}, second_type{} } {}
59 pair(const abstract_type& aPair) : concrete_type{ first_type{ aPair.first() }, second_type{ aPair.second() } } {}
60 pair(const concrete_type& aPair) : concrete_type{ aPair } {}
61 template <typename T3, typename T4>
62 pair(T3&& aFirst, T4&& aSecond) : concrete_type{ std::forward<T3>(aFirst), std::forward<T4>(aSecond) } {}
63 public:
64 self_type& operator=(const self_type& aOther) { return assign(aOther); }
65 abstract_type& operator=(const abstract_type& aOther) final { return assign(aOther); }
66 public:
67 const first_type& first() const final { return concrete_type::first; }
68 first_type& first() final { return concrete_type::first; }
69 const second_type& second() const final { return concrete_type::second; }
70 second_type& second() final { return concrete_type::second; }
71 public:
73 {
74 if constexpr (!std::is_const_v<first_type> && !std::is_const_v<second_type>)
75 {
76 concrete_type::operator=(concrete_type{ first_type{ aOther.first() }, second_type{ aOther.second() } });
77 return *this;
78 }
79 else
80 throw std::logic_error("neolib::pair isn't assignable (const value_type)");
81 }
82 public:
83 friend void swap(self_type& a, self_type& b)
84 {
85 using std::swap;
86 swap(a.first(), b.first());
87 swap(a.second(), b.second());
88 }
89 public:
90 constexpr bool operator==(const self_type& that) const noexcept
91 {
92 return first() == that.first() && second() == that.second();
93 }
94 constexpr std::partial_ordering operator<=>(const self_type& that) const noexcept
95 {
96 if (*this == that)
97 return std::partial_ordering::equivalent;
98 else if (std::forward_as_tuple(first(), second()) < std::forward_as_tuple(that.first(), that.second()))
99 return std::partial_ordering::less;
100 else
101 return std::partial_ordering::greater;
102 }
103 };
104
105 template <typename T1, typename T2>
106 inline pair<std::decay_t<T1>, std::decay_t<T2>> make_pair(T1&& aFirst, T2&& aSecond)
107 {
108 return pair<std::decay_t<T1>, std::decay_t<T2>>{ std::forward<T1>(aFirst), std::forward<T2>(aSecond) };
109 }
110}
111
virtual const second_type & second() const =0
virtual const first_type & first() const =0
abstract_type & operator=(const abstract_type &aOther) final
Definition pair.hpp:65
first_type & first() final
Definition pair.hpp:68
abstract_t< T2 > second_abstract_type
Definition pair.hpp:52
pair(const abstract_type &aPair)
Definition pair.hpp:59
constexpr bool operator==(const self_type &that) const noexcept
Definition pair.hpp:90
second_type & second() final
Definition pair.hpp:70
self_type & assign(const abstract_type &aOther)
Definition pair.hpp:72
constexpr std::partial_ordering operator<=>(const self_type &that) const noexcept
Definition pair.hpp:94
T2 second_type
Definition pair.hpp:54
const second_type & second() const final
Definition pair.hpp:69
const first_type & first() const final
Definition pair.hpp:67
friend void swap(self_type &a, self_type &b)
Definition pair.hpp:83
self_type & operator=(const self_type &aOther)
Definition pair.hpp:64
pair(T3 &&aFirst, T4 &&aSecond)
Definition pair.hpp:62
abstract_t< T1 > first_abstract_type
Definition pair.hpp:51
pair(const concrete_type &aPair)
Definition pair.hpp:60
T1 first_type
Definition pair.hpp:53
typename detail::abstract_type< T >::type abstract_t
Definition neolib.hpp:178
pair< std::decay_t< T1 >, std::decay_t< T2 > > make_pair(T1 &&aFirst, T2 &&aSecond)
Definition pair.hpp:106
Definition plf_hive.h:79
void swap(plf::hive< element_type, allocator_type > &a, plf::hive< element_type, allocator_type > &b) noexcept(std::allocator_traits< allocator_type >::propagate_on_container_swap::value||std::allocator_traits< allocator_type >::is_always_equal::value)
Definition plf_hive.h:4776