Flecs v3.1
A fast entity component system (ECS) for C & C++
Loading...
Searching...
No Matches
pair.hpp
Go to the documentation of this file.
1
6#pragma once
7
8namespace flecs {
9
10namespace _ {
11 struct pair_base { };
12} // _
13
14
35template <typename First, typename Second>
36struct pair : _::pair_base {
37 using type = conditional_t<!is_empty<First>::value || is_empty<Second>::value, First, Second>;
38 using first = First;
39 using second = Second;
40
41 pair(type& v) : ref_(v) { }
42
43 // This allows the class to be used as a temporary object
44 pair(const type& v) : ref_(const_cast<type&>(v)) { }
45
46 operator type&() {
47 return ref_;
48 }
49
50 operator const type&() const {
51 return ref_;
52 }
53
54 type* operator->() {
55 return &ref_;
56 }
57
58 const type* operator->() const {
59 return &ref_;
60 }
61
62 type& operator*() {
63 return ref_;
64 }
65
66 const type& operator*() const {
67 return ref_;
68 }
69
70private:
71 type& ref_;
72};
73
74template <typename First, typename Second, if_t<is_empty<First>::value> = 0>
76
77
79template <typename T>
80struct is_pair {
81 static constexpr bool value = is_base_of<_::pair_base, remove_reference_t<T> >::value;
82};
83
84
86template <typename P>
87using pair_first_t = transcribe_cv_t<remove_reference_t<P>, typename remove_reference_t<P>::first>;
88
90template <typename P>
91using pair_second_t = transcribe_cv_t<remove_reference_t<P>, typename remove_reference_t<P>::second>;
92
94template <typename P>
95using pair_type_t = transcribe_cv_t<remove_reference_t<P>, typename remove_reference_t<P>::type>;
96
98template <typename T, typename U = int>
100
101template <typename T>
102struct actual_type<T, if_not_t< is_pair<T>::value >> {
103 using type = T;
104};
105
106template <typename T>
107struct actual_type<T, if_t< is_pair<T>::value >> {
108 using type = pair_type_t<T>;
109};
110
111template <typename T>
112using actual_type_t = typename actual_type<T>::type;
113
114
115// Get type without const, *, &
116template<typename T>
117struct base_type {
118 using type = decay_t< remove_pointer_t< actual_type_t<T> > >;
119};
120
121template <typename T>
122using base_type_t = typename base_type<T>::type;
123
124
125// Get type without *, & (retains const which is useful for function args)
126template<typename T>
128 using type = remove_pointer_t< remove_reference_t< actual_type_t<T> > >;
129};
130
131template <typename T>
132using base_arg_type_t = typename base_arg_type<T>::type;
133
134
135// Test if type is the same as its actual type
136template <typename T>
137struct is_actual {
138 static constexpr bool value =
139 std::is_same<T, actual_type_t<T> >::value && !is_enum<T>::value;
140};
141
142} // flecs
transcribe_cv_t< remove_reference_t< P >, typename remove_reference_t< P >::second > pair_second_t
Get pair::second from pair while preserving cv qualifiers.
Definition: pair.hpp:91
transcribe_cv_t< remove_reference_t< P >, typename remove_reference_t< P >::first > pair_first_t
Get pair::first from pair while preserving cv qualifiers.
Definition: pair.hpp:87
transcribe_cv_t< remove_reference_t< P >, typename remove_reference_t< P >::type > pair_type_t
Get pair::type type from pair while preserving cv qualifiers.
Definition: pair.hpp:95
Get actual type from a regular type or pair.
Definition: pair.hpp:99
Test if type is a pair.
Definition: pair.hpp:80
Type that represents a pair.
Definition: pair.hpp:36