Flecs: include/flecs/addons/cpp/pair.hpp Source File
Description: A fast entity component system (ECS) for C & C++

| Flecs v4.0 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>
36 struct 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>
75using pair_object = pair<First, Second>;
76
77template <typename T>
78using raw_type_t = remove_pointer_t<remove_reference_t<T>>;
79
81template <typename T>
82 struct is_pair {
83 static constexpr bool value = is_base_of<_::pair_base, raw_type_t<T> >::value;
84};
85
87template <typename P>
88 using pair_first_t = transcribe_cv_t<remove_reference_t<P>, typename raw_type_t<P>::first>;
89
91template <typename P>
92 using pair_second_t = transcribe_cv_t<remove_reference_t<P>, typename raw_type_t<P>::second>;
93
95template <typename P>
96 using pair_type_t = transcribe_cvp_t<remove_reference_t<P>, typename raw_type_t<P>::type>;
97
99template <typename T, typename U = int>
100 struct actual_type;
101
102template <typename T>
103 struct actual_type<T, if_not_t< is_pair<T>::value >> {
104 using type = T;
105};
106
107template <typename T>
108 struct actual_type<T, if_t< is_pair<T>::value >> {
109 using type = pair_type_t<T>;
110};
111
112template <typename T>
113using actual_type_t = typename actual_type<T>::type;
114
115
116// Get type without const, *, &
117template<typename T>
118 struct base_type {
119 using type = decay_t< actual_type_t<T> >;
120};
121
122template <typename T>
123using base_type_t = typename base_type<T>::type;
124
125
126// Get type without *, & (retains const which is useful for function args)
127template<typename T>
128 struct base_arg_type {
129 using type = remove_pointer_t< remove_reference_t< actual_type_t<T> > >;
130};
131
132template <typename T>
133using base_arg_type_t = typename base_arg_type<T>::type;
134
135
136// Test if type is the same as its actual type
137template <typename T>
138 struct is_actual {
139 static constexpr bool value =
140 std::is_same<T, actual_type_t<T> >::value && !is_enum<T>::value;
141};
142
143} // flecs
flecs::pair_type_t
transcribe_cvp_t< remove_reference_t< P >, typename raw_type_t< P >::type > pair_type_t
Get pair::type type from pair while preserving cv qualifiers and pointer type.
Definition pair.hpp:96
flecs::pair_second_t
transcribe_cv_t< remove_reference_t< P >, typename raw_type_t< P >::second > pair_second_t
Get pair::second from pair while preserving cv qualifiers.
Definition pair.hpp:92
flecs::pair_first_t
transcribe_cv_t< remove_reference_t< P >, typename raw_type_t< P >::first > pair_first_t
Get pair::first from pair while preserving cv qualifiers.
Definition pair.hpp:88
flecs::_::pair_base
Definition pair.hpp:11
flecs::actual_type
Get actual type from a regular type or pair.
Definition pair.hpp:100
flecs::base_arg_type
Definition pair.hpp:128
flecs::base_type
Definition pair.hpp:118
flecs::is_actual
Definition pair.hpp:138
flecs::is_pair
Test if type is a pair.
Definition pair.hpp:82
flecs::pair
Type that represents a pair.
Definition pair.hpp:36