SyDEVS  v0.6.7
Multiscale Simulation and Systems Modeling Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
identity.h
Go to the documentation of this file.
1 #pragma once
2 #ifndef SYDEVS_IDENTITY_H_
3 #define SYDEVS_IDENTITY_H_
4 
6 #include <ostream>
7 
8 namespace sydevs {
9 
10 
40 template<typename U>
41 class identity
42 {
43 public:
47  constexpr identity();
48 
52  constexpr explicit identity(int64 index);
53 
54  constexpr identity(const identity&) = default;
55  identity& operator=(const identity&) = default;
56  identity(identity&&) = default;
57  identity& operator=(identity&&) = default;
58  ~identity() = default;
59 
60  constexpr bool valid() const;
61  constexpr int64 index() const;
62 
63  identity& operator++();
64  identity operator++(int);
65  identity& operator--();
66  identity operator--(int);
67 
68  identity& operator+=(int64 rhs);
69  identity& operator-=(int64 rhs);
70 
71  constexpr const identity operator+() const;
72 
73  constexpr const identity operator+(int64 rhs) const;
74  constexpr const identity operator-(int64 rhs) const;
75 
76  constexpr bool operator==(identity rhs) const;
77  constexpr bool operator!=(identity rhs) const;
78  constexpr bool operator<(identity rhs) const;
79  constexpr bool operator>(identity rhs) const;
80  constexpr bool operator<=(identity rhs) const;
81  constexpr bool operator>=(identity rhs) const;
82 
83 protected:
84  static constexpr int64 nan_int64 = std::numeric_limits<int64>::min();
85 
86 private:
87  int64 index_;
88 };
89 
90 
91 template<typename U>
93  : index_(nan_int64)
94 {
95 }
96 
97 
98 template<typename U>
99 constexpr identity<U>::identity(int64 index)
100  : index_(index)
101 {
102 }
103 
104 
105 template<typename U>
106 constexpr bool identity<U>::valid() const
107 {
108  return index_ != nan_int64;
109 }
110 
111 
112 template<typename U>
113 constexpr int64 identity<U>::index() const
114 {
115  return index_;
116 }
117 
118 
119 template<typename U>
121 {
122  if (valid()) {
123  ++index_;
124  }
125  return *this;
126 }
127 
128 
129 template<typename U>
131 {
132  auto old = *this;
133  ++(*this);
134  return old;
135 }
136 
137 
138 template<typename U>
140 {
141  if (valid()) {
142  --index_;
143  }
144  return *this;
145 }
146 
147 
148 template<typename U>
150 {
151  auto old = *this;
152  --(*this);
153  return old;
154 }
155 
156 
157 template<typename U>
159 {
160  *this = *this + rhs;
161  return *this;
162 }
163 
164 
165 template<typename U>
167 {
168  *this = *this - rhs;
169  return *this;
170 }
171 
172 
173 template<typename U>
174 constexpr const identity<U> identity<U>::operator+() const
175 {
176  return *this;
177 }
178 
179 
180 template<typename U>
181 constexpr const identity<U> identity<U>::operator+(int64 rhs) const
182 {
183  return valid() ? identity<U>(index_ + rhs) : identity<U>();
184 }
185 
186 
187 template<typename U>
188 constexpr const identity<U> identity<U>::operator-(int64 rhs) const
189 {
190  return valid() ? identity<U>(index_ - rhs) : identity<U>();
191 }
192 
193 
194 template<typename U>
195 constexpr bool identity<U>::operator==(identity rhs) const
196 {
197  return index_ == rhs.index_;
198 }
199 
200 
201 template<typename U>
202 constexpr bool identity<U>::operator!=(identity rhs) const
203 {
204  return index_ != rhs.index_;
205 }
206 
207 
208 template<typename U>
209 constexpr bool identity<U>::operator<(identity rhs) const
210 {
211  return index_ < rhs.index_;
212 }
213 
214 
215 template<typename U>
216 constexpr bool identity<U>::operator>(identity rhs) const
217 {
218  return index_ > rhs.index_;
219 }
220 
221 
222 template<typename U>
223 constexpr bool identity<U>::operator<=(identity rhs) const
224 {
225  return index_ <= rhs.index_;
226 }
227 
228 
229 template<typename U>
230 constexpr bool identity<U>::operator>=(identity rhs) const
231 {
232  return index_ >= rhs.index_;
233 }
234 
235 
236 template<typename U>
237 constexpr const identity<U> operator+(int64 lhs, identity<U> rhs)
238 {
239  return rhs + lhs;
240 }
241 
242 
243 template<typename U>
244 inline std::ostream& operator<<(std::ostream& os, const identity<U>& rhs)
245 {
246  if (!rhs.valid()) {
247  os << "{}";
248  }
249  else {
250  os << "{" << rhs.index() << "}";
251  }
252  return os;
253 }
254 
255 
256 } // namespace
257 
258 #endif
arraynd< T, ndims > operator+(const arraynd< T, ndims > &rhs)
Definition: arraynd.h:724
constexpr identity()
Constructs an invalid identity value.
Definition: identity.h:92
static constexpr int64 nan_int64
Definition: identity.h:84
constexpr bool operator<(identity rhs) const
Returns true if the identity value is less than rhs.
Definition: identity.h:209
constexpr bool operator!=(identity rhs) const
Returns true if the identity value does not equal rhs.
Definition: identity.h:202
constexpr bool operator>(identity rhs) const
Returns true if the identity value is greater than rhs.
Definition: identity.h:216
constexpr int64 index() const
Returns the internal index.
Definition: identity.h:113
constexpr bool operator>=(identity rhs) const
Returns true if the identity value is at least rhs.
Definition: identity.h:230
identity & operator-=(int64 rhs)
Subtracts rhs from the identity value.
Definition: identity.h:166
constexpr bool valid() const
Returns true if the identity value is valid.
Definition: identity.h:106
constexpr const identity operator+() const
Returns a copy of the identity value.
Definition: identity.h:174
constexpr bool operator<=(identity rhs) const
Returns true if the identity value is at most rhs.
Definition: identity.h:223
~identity()=default
Destructor.
identity & operator++()
Increments (prefix) the internal index.
Definition: identity.h:120
A data type which identifies an item by combining an encapsulated integer-valued index with a dimensi...
Definition: identity.h:41
constexpr bool operator==(identity rhs) const
Returns true if the identity value equals rhs.
Definition: identity.h:195
constexpr const identity operator-(int64 rhs) const
Returns a new identity value with rhs subtracted.
Definition: identity.h:188
identity & operator=(const identity &)=default
Copy assignment.
int64_t int64
Definition: number_types.h:14
identity & operator+=(int64 rhs)
Adds rhs to the identity value.
Definition: identity.h:158
identity & operator--()
Decrements (prefix) the internal index.
Definition: identity.h:139