All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Array.h
Go to the documentation of this file.
1 /*
2  * Array.h
3  *
4  * This file is part of the HausmiSEP project
5  *
6  * Copyright (C) 2012, 2013 Marco Alvarado (malvcr@gmail.com)
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #ifndef ARRAY_H_
23 #define ARRAY_H_
24 
25 #include <HSEP/HSEPObject.h>
26 #include <HSEP/Dictionary.h>
27 
28 #include <vector>
29 #include <string>
30 #include <cstring>
31 #include <functional>
32 
33 using namespace std;
34 
35 namespace HSEP {
36 
45  template<class T>
46  class Array : public HSEPObject {
47  vector<T*> aCatalog;
48  public:
49 
54  typedef function<void (T* pValuePtr)> ForEachWorker;
55 
56  Array() : HSEPObject("Array") {
57  aCatalog.clear();
58  }
59 
60  Array(Array& pReference) {
61  pReference.forEach([](T* xValue){
62  T* vItem = new T(xValue);
63  push(vItem);
64  });
65  }
66 
67  virtual ~Array() {
68  clear();
69  } // destructor
70 
71 
72  void operator()(Array& pReference) {
73  clear();
74  pReference.forEach([&](T* xValue){
75  T* vItem = new T(*xValue);
76  push(vItem);
77  });
78  }
79 
86  T* ptr(size_t pPos) {
87  T* vResultPtr = nullptr;
88  if (pPos < aCatalog.size()) {
89  vResultPtr = aCatalog.at(pPos);
90  }
91  return vResultPtr;
92  } // ref
93 
98  void push(T& pValueRef) {
99 
100  T* vValuePtr = new T(pValueRef);
101  aCatalog.push_back(vValuePtr);
102 
103  } // add
104 
109  void push(T* pValuePtr) {
110  aCatalog.push_back(pValuePtr);
111  } // add
112 
117  T* pop() {
118  string* vElementPtr = aCatalog.back();
119  aCatalog.pop_back();
120  return vElementPtr;
121  }
122 
126  void clear() {
127 
128  forEach([](T* xValuePtr) {
129  delete xValuePtr;
130  xValuePtr = nullptr;
131  });
132  aCatalog.clear();
133 
134  } // clear
135 
143  bool forEach(ForEachWorker pWorker) {
144 
145  // Normally, we define the result as true only when everything was processed. However,
146  // we work the "forEach" as an exception, because the result "false" is intended only
147  // to declare that the "pWorker" function performed an illegal operation and that the
148  // forEach was aborted because of that.
149  //
150  bool vResult = true;
151 
152  for (T* vItemPtr : aCatalog) {
153  try {
154  pWorker(vItemPtr);
155  }
156  catch(exception& vExc) {
157  setLastError(vExc.what());
158  vResult = false;
159  break; // unmanaged error ... let's stop the cycle
160  }
161  }
162 
163  return vResult;
164 
165  } // forEach
166 
171  size_t size() {
172  return aCatalog.size();
173  } // size
174 
175  }; // Array class
176 
188  template <class T>
189  void fillArray(Array<T>& pArray, function<T*()> pWorker, int pSize) {
190  for (int vIdx=0;vIdx<pSize; vIdx++) {
191  T* vItem = pWorker();
192  pArray.push(vItem);
193  }
194  }
195 
196 } // HSEP namespace
197 
198 #endif /* ARRAY_H_ */