OSFI-C++  3.10.0
OpenSF Integration Library
DynamicArray.h
1 /*
2  * openSF Integration Libraries (OSFI)
3  * Deimos Space, S.L.U.
4  *
5  * This file is part of OSFI. OSFI is free software; you can redistribute it
6  * and/or modify it under the terms of the 'ESA Software Community Licence Permissive' as
7  * published by the European Space Agency; either version 2.4 of the License,
8  * or (at your option) any later version. You should have received a
9  * copy of the 'ESA Software Community Licence Permissive - v2.4' along with this program
10  * or one can be found at <http://eop-cfi.esa.int/index.php/docs-and-mission-data/licensing-documents>.
11  *
12  */
13 #ifndef OSFI_CONFM_DYNAMIC_ARRAY
14 #define OSFI_CONFM_DYNAMIC_ARRAY
15 
16 #include <vector>
17 #include <exception>
18 #include <stdexcept>
19 
20 #include "ConFM/base.h"
21 
26 template<typename T> class DynamicArray
27 {
28 public:
30  DynamicArray() = default;
31 
38  DynamicArray(int rows, int columns){
39  for (int i = 0; i < rows; ++i)
40  {
41  data.push_back(std::vector<T> (columns));
42  }
43  }
44 
51  OSFI_DEPRECATED("Use operator() instead to access elements like mat(i,j)")
52  const std::vector<T> & operator[](int i) const {
53  return data[i];
54  }
55 
62  typename std::vector<T>::reference operator()(int i, int j) {
63  return data[i][j];
64  }
65 
72  typename std::vector<T>::const_reference operator()(int i, int j) const {
73  return data[i][j];
74  }
75 
83  typename std::vector<T>::reference at(int i, int j){
84  return checkIndices(this->data, i, j);
85  }
86 
94  typename std::vector<T>::const_reference at(int i, int j) const {
95  return checkIndices(this->data, i, j);
96  }
97 
103  void resize(int rows, int columns){
104  data.resize(rows);
105  for (int i = 0; i < rows; ++i)
106  data[i].resize(columns);
107  }
108 
112  int getRows() const{
113  return data.size();
114  }
115 
119  int getColumns() const{
120  if (data.size() > 0)
121  return data[0].size();
122  else
123  return 0;
124  }
125 
126 private:
127  std::vector<std::vector<T>> data;
128 
130  template<typename V>
131  static auto derefChecked(V& vec, int i, const char* indexName) -> decltype(vec[i]) {
132  try{
133  return vec.at(i);
134  } catch(...) {
135  std::throw_with_nested(std::out_of_range(
136  std::string("While accessing ") + indexName + std::to_string(i)));
137  }
138  }
144  template<typename C>
145  static auto checkIndices(C& mat, int i, int j) -> decltype(mat[i][j]) {
146  auto& row = derefChecked(mat, i, "row");
147  return derefChecked(row, j, "column");
148  }
149 
157  friend bool operator==(const DynamicArray& lhs, const DynamicArray& rhs) {
158  return lhs.data == rhs.data;
159  }
160  // TODO in C++20, == can be replaced with just "= default", and != removed entirely
161 
163  friend bool operator!=(const DynamicArray& lhs, const DynamicArray& rhs) {
164  return !(lhs == rhs);
165  }
166 };
167 
168 #endif // OSFI_CONFM_DYNAMIC_ARRAY
OSFI-C++ header common to many classes in the ConFM module.
Definition: DynamicArray.h:27
std::vector< T >::const_reference operator()(int i, int j) const
Definition: DynamicArray.h:72
std::vector< T >::const_reference at(int i, int j) const
Definition: DynamicArray.h:94
friend bool operator==(const DynamicArray &lhs, const DynamicArray &rhs)
Definition: DynamicArray.h:157
std::vector< T >::reference at(int i, int j)
Definition: DynamicArray.h:83
void resize(int rows, int columns)
Definition: DynamicArray.h:103
DynamicArray()=default
OSFI_DEPRECATED("Use operator() instead to access elements like mat(i,j)") const std
Definition: DynamicArray.h:51
friend bool operator!=(const DynamicArray &lhs, const DynamicArray &rhs)
Definition: DynamicArray.h:163
int getRows() const
Definition: DynamicArray.h:112
DynamicArray(int rows, int columns)
Definition: DynamicArray.h:38
int getColumns() const
Definition: DynamicArray.h:119
std::string to_string(ElementType et)