Radium Engine  1.5.20
Loading...
Searching...
No Matches
Index.hpp
1#pragma once
2
3#include <Core/RaCore.hpp>
4#include <limits>
5
6namespace Ra {
7namespace Core {
8namespace Utils {
9
10class RA_CORE_API Index
11{
12 public:
13 using IntegerType = int;
14
17 constexpr Index( IntegerType i = s_invalid );
18 constexpr Index( const Index& i );
19
22 template <typename Integer>
23 explicit constexpr Index( Integer i );
24
27 // ~Index() { }
28
30 constexpr bool isValid() const;
31
33 constexpr bool isInvalid() const;
34 constexpr void setInvalid();
35
37 static constexpr Index Invalid();
38 static constexpr Index Max();
39
41 constexpr IntegerType getValue() const;
42 constexpr void setValue( const IntegerType i );
43
45 constexpr operator IntegerType() const; // required for `array[idx]`
46
48 constexpr Index& operator=( const Index& id );
49 constexpr Index& operator++();
50 constexpr Index& operator--();
51
52 constexpr Index operator+( const Index& id );
53 template <typename Integer>
54 constexpr Index operator+( const Integer& i );
55
56 constexpr Index operator-( const Index& id );
57 template <typename Integer>
58 constexpr Index operator-( const Integer& i );
59
60 constexpr bool operator==( const Index& id );
61 template <typename Integer>
62 constexpr bool operator==( const Integer& i );
63
64 constexpr bool operator!=( const Index& id );
65 template <typename Integer>
66 constexpr bool operator!=( const Integer& i );
67
68 constexpr bool operator<( const Index& id1 );
69 template <typename Integer>
70 constexpr bool operator<( const Integer& i );
71
72 constexpr bool operator<=( const Index& id1 );
73 template <typename Integer>
74 constexpr bool operator<=( const Integer& i );
75
76 constexpr bool operator>( const Index& id1 );
77 template <typename Integer>
78 constexpr bool operator>( const Integer& i );
79
80 constexpr bool operator>=( const Index& id1 );
81 template <typename Integer>
82 constexpr bool operator>=( const Integer& i );
83
84 protected:
86 IntegerType m_idx { s_invalid };
87
88 private:
90 static const IntegerType s_invalid { -1 };
91 static const IntegerType s_maxIdx { std::numeric_limits<IntegerType>::max() };
92};
93
95constexpr Index::Index( IntegerType i ) : m_idx( ( i < 0 ) ? s_invalid : i ) {}
96
97template <typename Integer>
98constexpr Index::Index( Integer i ) : m_idx( ( ( i < 0 ) || ( i > s_maxIdx ) ) ? s_invalid : i ) {
99 static_assert( std::is_integral<Integer>::value, "Integral required." );
100}
101
102constexpr Index::Index( const Index& i ) : m_idx( i.m_idx ) {}
103
105constexpr bool Index::isValid() const {
106 return ( m_idx != s_invalid );
107}
108
110constexpr bool Index::isInvalid() const {
111 return ( m_idx < 0 );
112}
113constexpr void Index::setInvalid() {
114 m_idx = s_invalid;
115}
116constexpr Index Index::Invalid() {
117 return Index( s_invalid );
118}
119constexpr Index Index::Max() {
120 return Index( s_maxIdx );
121}
122
124constexpr Index::IntegerType Index::getValue() const {
125 return m_idx;
126}
127constexpr void Index::setValue( const IntegerType i ) {
128 m_idx = ( i < 0 ) ? s_invalid : i;
129}
130
133constexpr Index::operator IntegerType() const {
134 return m_idx;
135}
136
138constexpr Index& Index::operator=( const Index& id ) {
139 m_idx = id.m_idx;
140 return *this;
141}
142constexpr Index& Index::operator++() {
143 m_idx++;
144 if ( m_idx < 0 ) { m_idx = s_invalid; }
145 return *this;
146}
147constexpr Index& Index::operator--() {
148 if ( m_idx != s_invalid ) { m_idx--; }
149 return *this;
150}
151
152constexpr Index Index::operator+( const Index& id ) {
153 if ( isInvalid() || id.isInvalid() ) { return Index::Invalid(); }
154 return Index( m_idx + id.m_idx );
155}
156template <typename Integer>
157constexpr Index Index::operator+( const Integer& id ) {
158 static_assert( std::is_integral<Integer>::value, "Integral required." );
159 return ( *this ) + Index( id );
160}
161
162constexpr Index Index::operator-( const Index& id ) {
163 if ( isInvalid() || id.isInvalid() ) { return Index::Invalid(); }
164 return Index( m_idx - id.m_idx );
165}
166template <typename Integer>
167constexpr Index Index::operator-( const Integer& id ) {
168 static_assert( std::is_integral<Integer>::value, "Integral required." );
169 return ( *this ) - Index( id );
170}
171
172constexpr bool Index::operator==( const Index& id ) {
173 return ( m_idx == id.m_idx );
174}
175template <typename Integer>
176constexpr bool Index::operator==( const Integer& i ) {
177 static_assert( std::is_integral<Integer>::value, "Integral required." );
178 return ( *this == Index( IntegerType( i ) ) );
179}
180
181constexpr bool Index::operator!=( const Index& id ) {
182 return ( !( *this == id ) );
183}
184template <typename Integer>
185constexpr bool Index::operator!=( const Integer& i ) {
186 static_assert( std::is_integral<Integer>::value, "Integral required." );
187 return ( !( *this == Index( IntegerType( i ) ) ) );
188}
189
190constexpr bool Index::operator<( const Index& id ) {
191 if ( isInvalid() || id.isInvalid() ) { return false; }
192 return ( m_idx < id.m_idx );
193}
194template <typename Integer>
195constexpr bool Index::operator<( const Integer& i ) {
196 return ( *this < Index( IntegerType( i ) ) );
197}
198
199constexpr bool Index::operator<=( const Index& id ) {
200 if ( isInvalid() || id.isInvalid() ) { return false; }
201 return ( m_idx <= id.m_idx );
202}
203template <typename Integer>
204constexpr bool Index::operator<=( const Integer& i ) {
205 static_assert( std::is_integral<Integer>::value, "Integral required." );
206 return ( *this <= Index( IntegerType( i ) ) );
207}
208
209constexpr bool Index::operator>( const Index& id ) {
210 if ( isInvalid() || id.isInvalid() ) { return false; }
211 return ( m_idx > id.m_idx );
212}
213template <typename Integer>
214constexpr bool Index::operator>( const Integer& i ) {
215 static_assert( std::is_integral<Integer>::value, "Integral required." );
216 return ( *this > Index( IntegerType( i ) ) );
217}
218
219constexpr bool Index::operator>=( const Index& id ) {
220 if ( isInvalid() || id.isInvalid() ) { return false; }
221 return ( m_idx >= id.m_idx );
222}
223template <typename Integer>
224constexpr bool Index::operator>=( const Integer& i ) {
225 static_assert( std::is_integral<Integer>::value, "Integral required." );
226 return ( *this >= Index( IntegerType( i ) ) );
227}
228
229} // namespace Utils
230} // namespace Core
231} // namespace Ra
T max(T... args)
hepler function to manage enum as underlying types in VariableSet
Definition Cage.cpp:3
T operator!=(T... args)