ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
ecvArray.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 // Local
11 #include "ecvHObject.h"
12 
13 // CV_CORE_LIB
14 #include <CVShareable.h>
15 
16 // System
17 #include <vector>
18 
20 template <class Type, int N, class ComponentType>
21 class ccArray : public std::vector<Type>, public CCShareable, public ccHObject {
22 public:
25 
27  ccArray(QString name = QString()) : ccHObject(name) {
28  setFlagState(CC_LOCKED, true);
29  }
30 
32  virtual Base* clone() {
33  Base* cloneArray = new Base(getName());
34  if (!copy(*cloneArray)) {
35  // error message already issued
36  cloneArray->release();
37  cloneArray = nullptr;
38  }
39  return cloneArray;
40  }
41 
43  bool copy(Base& dest) const {
44  try {
45  // copy only the data
46  static_cast<std::vector<Type>&>(dest) =
47  static_cast<const std::vector<Type>&>(*this);
48  } catch (const std::bad_alloc&) {
49  CVLog::Warning("[ccArray::copy] Not enough memory");
50  return false;
51  }
52  return true;
53  }
54 
56  bool reserveSafe(size_t count) {
57  try {
58  this->reserve(count);
59  } catch (const std::bad_alloc&) {
60  // not enough memory
61  return false;
62  }
63  return true;
64  }
65 
67  inline bool isAllocated() const { return this->capacity() != 0; }
68 
70  bool resizeSafe(size_t count,
71  bool initNewElements = false,
72  const Type* valueForNewElements = nullptr) {
73  try {
74  if (initNewElements) {
75  if (!valueForNewElements) {
77  "[ccArray::resizeSafe] Internal error: no new "
78  "element specified");
79  return false;
80  }
81  this->resize(count, *valueForNewElements);
82  } else {
83  this->resize(count);
84  }
85  } catch (const std::bad_alloc&) {
86  // not enough memory
87  return false;
88  }
89  return true;
90  }
91 
92  // inherited from ccHObject
93  inline virtual CV_CLASS_ENUM getClassID() const override {
94  return CV_TYPES::ARRAY;
95  }
96  inline virtual bool isShareable() const override { return true; }
97  inline virtual bool isSerializable() const override { return true; }
98 
99  // Shortcuts (for backward compatibility)
100  inline Type& getValue(size_t index) { return this->at(index); }
101  inline const Type& getValue(size_t index) const { return this->at(index); }
102  inline void setValue(size_t index, const Type& value) {
103  this->at(index) = value;
104  }
105  inline void addElement(const Type& value) { this->emplace_back(value); }
106  inline void fill(const Type& value) {
107  if (this->empty())
108  this->resize(this->capacity(), value);
109  else
110  std::fill(this->begin(), this->end(), value);
111  }
112  inline unsigned currentSize() const {
113  return static_cast<unsigned>(this->size());
114  }
115  inline void clear(bool releaseMemory = false) {
116  if (releaseMemory)
117  this->resize(0);
118  else
119  this->std::vector<Type>::clear();
120  }
121  inline void swap(size_t i1, size_t i2) {
122  std::swap(this->at(i1), this->at(i2));
123  }
124 
125 protected:
127 
129  virtual ~ccArray() {}
130 
131  // inherited from ccHObject
132  inline bool toFile_MeOnly(QFile& out, short dataVersion) const override {
133  if (dataVersion < 20) {
134  assert(false);
135  return false;
136  }
138  ComponentType>(*this,
139  out);
140  }
141  inline short minimumFileVersion_MeOnly() const override {
143  }
144  inline bool fromFile_MeOnly(QFile& in,
145  short dataVersion,
146  int flags,
147  LoadedIDMap& oldToNewIDMap) override {
149  ComponentType>(
150  *this, in, dataVersion, "array");
151  }
152 };
int64_t CV_CLASS_ENUM
Type of object type flags (64 bits)
Definition: CVTypes.h:97
@ CC_LOCKED
Definition: CVTypes.h:31
int size
std::string name
int count
virtual void release()
Decrease counter and deletes object when 0.
Definition: CVShareable.cpp:35
static bool Warning(const char *format,...)
Prints out a formatted warning message in console.
Definition: CVLog.cpp:133
Shareable array that can be properly inserted in the DB tree.
Definition: ecvArray.h:21
const Type & getValue(size_t index) const
Definition: ecvArray.h:101
Type & getValue(size_t index)
Definition: ecvArray.h:100
ccArray< Type, N, ComponentType > Base
Base type.
Definition: ecvArray.h:24
void fill(const Type &value)
Definition: ecvArray.h:106
bool isAllocated() const
Returns whether some memory has been allocated or not.
Definition: ecvArray.h:67
bool resizeSafe(size_t count, bool initNewElements=false, const Type *valueForNewElements=nullptr)
Resizes memory (no exception thrown)
Definition: ecvArray.h:70
bool reserveSafe(size_t count)
Reserves memory (no exception thrown)
Definition: ecvArray.h:56
void setValue(size_t index, const Type &value)
Definition: ecvArray.h:102
unsigned currentSize() const
Definition: ecvArray.h:112
void addElement(const Type &value)
Definition: ecvArray.h:105
virtual bool isSerializable() const override
Returns whether object is serializable of not.
Definition: ecvArray.h:97
virtual Base * clone()
Duplicates array.
Definition: ecvArray.h:32
virtual ~ccArray()
Destructor (protected)
Definition: ecvArray.h:129
void swap(size_t i1, size_t i2)
Definition: ecvArray.h:121
virtual CV_CLASS_ENUM getClassID() const override
Returns class ID.
Definition: ecvArray.h:93
bool toFile_MeOnly(QFile &out, short dataVersion) const override
Save own object data.
Definition: ecvArray.h:132
virtual bool isShareable() const override
Returns whether object is shareable or not.
Definition: ecvArray.h:96
bool copy(Base &dest) const
Copies the content of this array in another one.
Definition: ecvArray.h:43
bool fromFile_MeOnly(QFile &in, short dataVersion, int flags, LoadedIDMap &oldToNewIDMap) override
Loads own object data.
Definition: ecvArray.h:144
void clear(bool releaseMemory=false)
Definition: ecvArray.h:115
short minimumFileVersion_MeOnly() const override
Definition: ecvArray.h:141
ccArray(QString name=QString())
Default constructor.
Definition: ecvArray.h:27
Hierarchical CLOUDVIEWER Object.
Definition: ecvHObject.h:25
virtual void setFlagState(CV_OBJECT_FLAG flag, bool state)
Sets flag state.
virtual QString getName() const
Returns object name.
Definition: ecvObject.h:72
QMultiMap< unsigned, unsigned > LoadedIDMap
Map of loaded unique IDs (old ID --> new ID)
static bool GenericArrayFromFile(std::vector< Type > &data, QFile &in, short dataVersion, const QString &verboseDescription)
Helper: loads a vector structure from file.
static bool GenericArrayToFile(const std::vector< Type > &data, QFile &out)
Helper: saves a vector to file.
static short GenericArrayToFileMinVersion()
Returns the minimum file version to save/load a 'generic array'.
@ ARRAY
Definition: CVTypes.h:133
Rgb at(size_t color_id)
void swap(cloudViewer::core::SmallVectorImpl< T > &LHS, cloudViewer::core::SmallVectorImpl< T > &RHS)
Implement std::swap in terms of SmallVector swap.
Definition: SmallVector.h:1370