Stride Reference Manual  - generated for commit 9643b11
IdSubscriptArray.h
Go to the documentation of this file.
1 /*
2  * This is free software: you can redistribute it and/or modify it
3  * under the terms of the GNU General Public License as published by
4  * the Free Software Foundation, either version 3 of the License, or
5  * any later version.
6  * The software is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9  * GNU General Public License for more details.
10  * You should have received a copy of the GNU General Public License
11  * along with the software. If not, see <http://www.gnu.org/licenses/>.
12  *
13  * Copyright 2018, Kuylen E, Willem L, Broeckhove J
14  */
15 
21 #pragma once
22 
23 #include "contact/ContactType.h"
24 
25 #include <array>
26 #include <bitset>
27 #include <initializer_list>
28 #include <iostream>
29 #include <stdexcept>
30 
31 namespace stride {
32 namespace ContactType {
33 
47 template <class T>
48 class IdSubscriptArray : public std::array<T, NumOfTypes()>
49 {
50 public:
54  explicit IdSubscriptArray(T t)
55  {
56  for (auto typ : IdList) {
57  this->operator[](typ) = t;
58  }
59  }
60 
61  explicit IdSubscriptArray()
62  {
63  for (auto typ : IdList) {
64  this->operator[](typ) = T();
65  }
66  }
67 
72  IdSubscriptArray(std::initializer_list<T> l)
73  {
74  auto it = l.begin();
75  for (auto typ : IdList) {
76  if (it != l.end()) {
77  this->operator[](typ) = *it;
78  ++it;
79  } else {
80  this->operator[](typ) = T();
81  }
82  }
83  }
84 
89  explicit IdSubscriptArray(std::array<T, NumOfTypes()>&& l) : std::array<T, NumOfTypes()>(l) {}
90 
97 
99  typename std::array<T, NumOfTypes()>::reference operator[](ContactType::Id id)
100  {
101  return this->std::template array<T, NumOfTypes()>::operator[](ContactType::ToSizeT(id));
102  }
103 
105  typename std::array<T, NumOfTypes()>::const_reference operator[](ContactType::Id id) const
106  {
107  return this->std::template array<T, NumOfTypes()>::operator[](ContactType::ToSizeT(id));
108  }
109 
111  typename std::array<T, NumOfTypes()>::reference at(ContactType::Id id)
112  {
113  if (ToSizeT(id) >= NumOfTypes()) {
114  throw std::out_of_range("IdSubscriptArray::at> Id out of range");
115  }
116  return this->std::template array<T, NumOfTypes()>::operator[](ContactType::ToSizeT(id));
117  }
118 
120  typename std::array<T, NumOfTypes()>::const_reference at(ContactType::Id id) const
121  {
122  if (ToSizeT(id) >= NumOfTypes()) {
123  throw std::out_of_range("IdSubscriptArray::at> Id out of range");
124  }
125  return this->std::template array<T, NumOfTypes()>::operator[](ContactType::ToSizeT(id));
126  }
127 };
128 
132 template <>
133 class IdSubscriptArray<bool> : public std::bitset<NumOfTypes()>
134 {
135 public:
139  explicit IdSubscriptArray(bool t = bool())
140  {
141  if (t)
142  this->set();
143  else
144  this->reset();
145  }
146 
151  IdSubscriptArray(std::initializer_list<bool> l)
152  {
153  auto it = l.begin();
154  for (auto typ : IdList) {
155  if (it != l.end()) {
156  this->operator[](typ) = *it;
157  ++it;
158  } else {
159  this->operator[](typ) = bool();
160  }
161  }
162  }
163 
165  typename std::bitset<NumOfTypes()>::reference operator[](ContactType::Id id)
166  {
167  return this->std::template bitset<NumOfTypes()>::operator[](ContactType::ToSizeT(id));
168  }
169 
172  {
173  return this->std::template bitset<NumOfTypes()>::operator[](ContactType::ToSizeT(id));
174  }
175 
177  typename bitset<NumOfTypes()>::reference at(ContactType::Id id)
178  {
179  if (ToSizeT(id) >= NumOfTypes()) {
180  throw std::out_of_range("IdSubscriptArray<bool>::at> Id out of range");
181  }
182  return this->std::template bitset<NumOfTypes()>::operator[](ContactType::ToSizeT(id));
183  }
184 
186  bool at(ContactType::Id id) const
187  {
188  if (ToSizeT(id) >= NumOfTypes()) {
189  throw std::out_of_range("IdSubscriptArray::at> Id out of range");
190  }
191  return this->std::template bitset<NumOfTypes()>::operator[](ContactType::ToSizeT(id));
192  }
193 };
194 
195 } // namespace ContactType
196 } // namespace stride
IdSubscriptArray(std::initializer_list< T > l)
When we want to use an initializer list the elements is the (possibly emty) initializer list are appl...
Id
Enumerates the ContactPool types.
Definition: ContactType.h:34
constexpr std::size_t ToSizeT(Id id)
Cast to size_t for indexing.
Definition: ContactType.h:54
constexpr std::initializer_list< Id > IdList
To allow iteration over the type ids.
Definition: ContactType.h:75
bool at(ContactType::Id id) const
Subscripting with pool typ id as argument.
bool operator[](ContactType::Id id) const
Subscripting with pool typ id as argument.
bitset< NumOfTypes()>::reference at(ContactType::Id id)
Subscripting with pool typ id as argument.
IdSubscriptArray(std::initializer_list< bool > l)
When we want to use an initializer list the elements is the (possibly emty) initializer list are appl...
constexpr unsigned int NumOfTypes()
Number of ContactPool types.
Definition: ContactType.h:45
std::bitset< NumOfTypes()>::reference operator[](ContactType::Id id)
Subscripting with pool typ id as argument.
STL namespace.
Definition of ContactPool Id Type.
std::array< T, NumOfTypes()>::const_reference operator[](ContactType::Id id) const
Subscripting with pool typ id as argument.
IdSubscriptArray(bool t=bool())
What we &#39;ll use most often and where we can have a default and initialize all array elements to the s...
An std::array modified to enable subscripting with the constact pool type indentifiers.
IdSubscriptArray(std::array< T, NumOfTypes()> &&l)
Initialize with an array of the right dimensions.
std::array< T, NumOfTypes()>::const_reference at(ContactType::Id id) const
Subscripting with pool typ id as argument.
Namespace for the simulator and related classes.
Definition: Calendar.cpp:28
std::array< T, NumOfTypes()>::reference operator[](ContactType::Id id)
This actually works in itself but interferes annoyingly with the first constructor above and is for p...
IdSubscriptArray(T t)
What we &#39;ll use most often and where we can have a default and initialize all array elements to the s...
std::array< T, NumOfTypes()>::reference at(ContactType::Id id)
Subscripting with pool typ id as argument.