ccArray.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //##########################################################################
  2. //# #
  3. //# CLOUDCOMPARE #
  4. //# #
  5. //# This program is free software; you can redistribute it and/or modify #
  6. //# it under the terms of the GNU General Public License as published by #
  7. //# the Free Software Foundation; version 2 or later of the License. #
  8. //# #
  9. //# This program is distributed in the hope that it will be useful, #
  10. //# but WITHOUT ANY WARRANTY; without even the implied warranty of #
  11. //# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
  12. //# GNU General Public License for more details. #
  13. //# #
  14. //# COPYRIGHT: EDF R&D / TELECOM ParisTech (ENST-TSI) #
  15. //# #
  16. //##########################################################################
  17. #ifndef CC_ARRAY_HEADER
  18. #define CC_ARRAY_HEADER
  19. //Local
  20. #include "ccHObject.h"
  21. //CCCoreLib
  22. #include <CCShareable.h>
  23. //System
  24. #include <vector>
  25. //! Shareable array that can be properly inserted in the DB tree
  26. template <class Type, int N, class ComponentType> class ccArray : public std::vector<Type>, public CCShareable, public ccHObject
  27. {
  28. public:
  29. //! Base type
  30. typedef ccArray<Type, N, ComponentType> Base;
  31. //! Default constructor
  32. ccArray(QString name = QString())
  33. : ccHObject(name)
  34. {
  35. setFlagState(CC_LOCKED, true);
  36. }
  37. //! Duplicates array
  38. virtual Base* clone()
  39. {
  40. Base* cloneArray = new Base(getName());
  41. if (!copy(*cloneArray))
  42. {
  43. //error message already issued
  44. cloneArray->release();
  45. cloneArray = nullptr;
  46. }
  47. return cloneArray;
  48. }
  49. //! Copies the content of this array in another one
  50. bool copy(Base& dest) const
  51. {
  52. try
  53. {
  54. //copy only the data
  55. static_cast<std::vector<Type>&>(dest) = static_cast<const std::vector<Type>&>(*this);
  56. }
  57. catch (const std::bad_alloc&)
  58. {
  59. ccLog::Warning("[ccArray::copy] Not enough memory");
  60. return false;
  61. }
  62. return true;
  63. }
  64. //! Reserves memory (no exception thrown)
  65. bool reserveSafe(size_t count)
  66. {
  67. try
  68. {
  69. this->reserve(count);
  70. }
  71. catch (const std::bad_alloc&)
  72. {
  73. //not enough memory
  74. return false;
  75. }
  76. return true;
  77. }
  78. //! Returns whether some memory has been allocated or not
  79. inline bool isAllocated() const { return this->capacity() != 0; }
  80. //! Resizes memory (no exception thrown)
  81. bool resizeSafe(size_t count, bool initNewElements = false, const Type* valueForNewElements = nullptr)
  82. {
  83. try
  84. {
  85. if (initNewElements)
  86. {
  87. if (!valueForNewElements)
  88. {
  89. ccLog::Warning("[ccArray::resizeSafe] Internal error: no new element specified");
  90. return false;
  91. }
  92. this->resize(count, *valueForNewElements);
  93. }
  94. else
  95. {
  96. this->resize(count);
  97. }
  98. }
  99. catch (const std::bad_alloc&)
  100. {
  101. //not enough memory
  102. return false;
  103. }
  104. return true;
  105. }
  106. //inherited from ccHObject
  107. inline virtual CC_CLASS_ENUM getClassID() const override { return CC_TYPES::ARRAY; }
  108. inline virtual bool isShareable() const override { return true; }
  109. inline virtual bool isSerializable() const override { return true; }
  110. //Shortcuts (for backward compatibility)
  111. inline Type& getValue(size_t index) { return this->at(index); }
  112. inline const Type& getValue(size_t index) const { return this->at(index); }
  113. inline void setValue(size_t index, const Type& value) { this->at(index) = value; }
  114. inline void addElement(const Type& value) { this->emplace_back(value); }
  115. inline void fill(const Type& value) { if (this->empty()) this->resize(this->capacity(), value); else std::fill(this->begin(), this->end(), value); }
  116. inline unsigned currentSize() const { return static_cast<unsigned>(this->size()); }
  117. inline void clear(bool releaseMemory = false) { if (releaseMemory) this->resize(0); else this->std::vector<Type>::clear(); }
  118. inline void swap(size_t i1, size_t i2) { std::swap(this->at(i1), this->at(i2)); }
  119. protected:
  120. //! Destructor (protected)
  121. /** Use release instead.
  122. **/
  123. virtual ~ccArray() {}
  124. //inherited from ccHObject
  125. inline bool toFile_MeOnly(QFile& out, short dataVersion) const override
  126. {
  127. return ccSerializationHelper::GenericArrayToFile<Type, N, ComponentType>(*this, out);
  128. }
  129. inline bool fromFile_MeOnly(QFile& in, short dataVersion, int flags, LoadedIDMap& oldToNewIDMap) override
  130. {
  131. return ccSerializationHelper::GenericArrayFromFile<Type, N, ComponentType>(*this, in, dataVersion, "array");
  132. }
  133. };
  134. #endif //CC_ARRAY_HEADER