TBTK
Need a break? Support the development by playing Polarity Puzzles
CArray.h
Go to the documentation of this file.
1 /* Copyright 2019 Kristofer Björnson
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
23 #ifndef COM_DAFER45_TBTK_C_ARRAY
24 #define COM_DAFER45_TBTK_C_ARRAY
25 
26 #include "TBTK/Serializable.h"
27 #include "TBTK/TBTKMacros.h"
28 
29 #include "TBTK/json.hpp"
30 
31 namespace TBTK{
32 
43 template<typename DataType>
44 class CArray{
45 public:
46  //TBTKFeature Utilities.CArray.construction.1 2019-10-30
48  CArray();
49 
50  //TBTKFeature Utilities.CArray.construction.2 2019-10-30
54  CArray(unsigned int size);
55 
56  //TBTKFeature Utilities.CArray.copyConstruction.1.C++ 2019-10-30
60  CArray(const CArray &carray);
61 
62  //TBTKFeature Utilities.CArray.moveConstruction.1.C++ 2019-10-30
66  CArray(CArray &&carray);
67 
74  CArray(const std::string &serialization, Serializable::Mode mode);
75 
77  ~CArray();
78 
84  CArray& operator=(const CArray &carray);
85 
91  CArray& operator=(CArray &&carray);
92 
93  //TBTKFeature Utilities.CArray.operatorArraySubscript.1 2019-10-30
99  DataType& operator[](unsigned int n);
100 
101  //TBTKFeature Utilities.CArray.operatorArraySubscript.2 2019-10-30
107  const DataType& operator[](unsigned int n) const;
108 
109  //TBTKFeature Utilities.CArray.getData.1.C++ 2019-10-31
113  DataType* getData();
114 
115  //TBTKFeature Utilities.CArray.getData.2.C++ 2019-10-31
119  const DataType* getData() const;
120 
124  unsigned int getSize() const;
125 
133  std::string serialize(Serializable::Mode mode) const;
134 private:
136  unsigned int size;
137 
139  DataType *data;
140 };
141 
142 template<typename DataType>
144  data = nullptr;
145 }
146 
147 template<typename DataType>
148 CArray<DataType>::CArray(unsigned int size){
149  this->size = size;
150  data = new DataType[size];
151 }
152 
153 template<typename DataType>
155  size = carray.size;
156  if(carray.data == nullptr){
157  data = nullptr;
158  }
159  else{
160  data = new DataType[size];
161  for(unsigned int n = 0; n < size; n++)
162  data[n] = carray.data[n];
163  }
164 }
165 
166 template<typename DataType>
168  size = carray.size;
169  if(carray.data == nullptr){
170  data = nullptr;
171  }
172  else{
173  data = carray.data;
174  carray.data = nullptr;
175  }
176 }
177 
178 template<typename DataType>
180  const std::string &serialization,
181  Serializable::Mode mode
182 ){
183  TBTKAssert(
185  serialization,
186  "CArray",
187  mode
188  ),
189  "CArray::CArray()",
190  "Unable to parse string as CArray '" << serialization << "'.",
191  ""
192  );
193 
194  switch(mode){
195  case Serializable::Mode::JSON:
196  {
197  try{
198  nlohmann::json j
199  = nlohmann::json::parse(serialization);
200  size = j.at("size").get<unsigned int>();
201  nlohmann::json d = j.at("data");
202  std::vector<DataType> tempData;
203  for(
204  nlohmann::json::iterator iterator = d.begin();
205  iterator != d.end();
206  ++iterator
207  ){
208  tempData.push_back(
209  Serializable::deserialize<DataType>(
210  *iterator,
211  mode
212  )
213  );
214  }
215  TBTKAssert(
216  size == tempData.size(),
217  "CArray::CArray()",
218  "Unable to deserialize CArray. The number of"
219  << "data elements does not agree with the size"
220  << " '" << serialization << "'.",
221  ""
222  );
223  data = new DataType[size];
224  for(unsigned int n = 0; n < size; n++)
225  data[n] = tempData[n];
226  }
227  catch(nlohmann::json::exception &e){
228  TBTKExit(
229  "CArray::CArray()",
230  "Unable to parse string as CArray '"
231  << serialization << "'.",
232  ""
233  );
234  }
235 
236  break;
237  }
238  default:
239  TBTKExit(
240  "CArray::CArray()",
241  "Only Serializable::Mode::JSON is supported yet.",
242  ""
243  );
244  }
245 }
246 
247 template<typename DataType>
249  if(data != nullptr)
250  delete [] data;
251 }
252 
253 template<typename DataType>
255  if(this != &rhs){
256  size = rhs.size;
257  if(data != nullptr)
258  delete [] data;
259 
260  if(rhs.data == nullptr){
261  data = nullptr;
262  }
263  else{
264  data = new DataType[size];
265  for(unsigned int n = 0; n < size; n++)
266  data[n] = rhs.data[n];
267  }
268  }
269 
270  return *this;
271 }
272 
273 template<typename DataType>
275  if(this != &rhs){
276  size = rhs.size;
277  if(data != nullptr)
278  delete [] data;
279 
280  if(rhs.data == nullptr){
281  data = nullptr;
282  }
283  else{
284  data = rhs.data;
285  rhs.data = nullptr;
286  }
287  }
288 
289  return *this;
290 }
291 
292 template<typename DataType>
293 DataType& CArray<DataType>::operator[](unsigned int n){
294  return data[n];
295 }
296 
297 template<typename DataType>
298 const DataType& CArray<DataType>::operator[](unsigned int n) const{
299  return data[n];
300 }
301 
302 template<typename DataType>
304  return data;
305 }
306 
307 template<typename DataType>
308 const DataType* CArray<DataType>::getData() const{
309  return data;
310 }
311 
312 template<typename DataType>
313 unsigned int CArray<DataType>::getSize() const{
314  return size;
315 }
316 
317 template<typename DataType>
319  switch(mode){
320  case Serializable::Mode::JSON:
321  {
322  nlohmann::json j;
323  j["id"] = "CArray";
324  j["size"] = size;
325  j["data"] = nlohmann::json();
326  for(unsigned int n = 0; n < size; n++){
327  j["data"].push_back(
328  Serializable::serialize(data[n], mode)
329  );
330  }
331 
332  return j.dump();
333  }
334  default:
335  TBTKExit(
336  "CArray::serialize()",
337  "Only Serializable::Mode::JSON is supported yet.",
338  ""
339  );
340  }
341 }
342 
343 }; //End of namesapce TBTK
344 
345 #endif
TBTK::Serializable::validate
static bool validate(const std::string &serialization, const std::string &id, Mode mode)
TBTK::CArray::operator=
CArray & operator=(const CArray &carray)
Definition: CArray.h:254
TBTK::CArray::getSize
unsigned int getSize() const
Definition: CArray.h:313
TBTK::CArray
Container for a C style array.
Definition: CArray.h:44
TBTK::CArray::getData
DataType * getData()
Definition: CArray.h:303
TBTK::Serializable::serialize
virtual std::string serialize(Mode mode) const =0
Serializable.h
Abstract base class for serializable objects.
TBTK::CArray::CArray
CArray()
Definition: CArray.h:143
TBTK::CArray::operator[]
DataType & operator[](unsigned int n)
Definition: CArray.h:293
TBTK::CArray::~CArray
~CArray()
Definition: CArray.h:248
TBTKMacros.h
Precompiler macros.
TBTK::Serializable::Mode
Mode
Definition: Serializable.h:47
TBTK::CArray::serialize
std::string serialize(Serializable::Mode mode) const
Definition: CArray.h:318