File: ccArray.h

package info (click to toggle)
cloudcompare 2.10.1-2
  • links: PTS
  • area: main
  • in suites: buster
  • size: 55,916 kB
  • sloc: cpp: 219,837; ansic: 29,944; makefile: 67; sh: 45
file content (147 lines) | stat: -rw-r--r-- 4,755 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//##########################################################################
//#                                                                        #
//#                              CLOUDCOMPARE                              #
//#                                                                        #
//#  This program is free software; you can redistribute it and/or modify  #
//#  it under the terms of the GNU General Public License as published by  #
//#  the Free Software Foundation; version 2 or later of the License.      #
//#                                                                        #
//#  This program is distributed in the hope that it will be useful,       #
//#  but WITHOUT ANY WARRANTY; without even the implied warranty of        #
//#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the          #
//#  GNU General Public License for more details.                          #
//#                                                                        #
//#          COPYRIGHT: EDF R&D / TELECOM ParisTech (ENST-TSI)             #
//#                                                                        #
//##########################################################################

#ifndef CC_ARRAY_HEADER
#define CC_ARRAY_HEADER

//Local
#include "ccHObject.h"

//CCLib
#include <CCShareable.h>

//System
#include <vector>

//! Shareable array that can be properly inserted in the DB tree
template <class Type, int N, class ComponentType> class ccArray : public std::vector<Type>, public CCShareable, public ccHObject
{
public:

	//! Base type
	typedef ccArray<Type, N, ComponentType> Base;

	//! Default constructor
	ccArray(QString name = QString())
		: ccHObject(name)
	{
		setFlagState(CC_LOCKED, true);
	}

	//! Duplicates array
	virtual Base* clone()
	{
		Base* cloneArray = new Base(getName());
		if (!copy(*cloneArray))
		{
			//error message already issued
			cloneArray->release();
			cloneArray = nullptr;
		}
		return cloneArray;
	}

	//! Copies the content of this array in another one
	bool copy(Base& dest) const
	{
		try
		{
			//copy only the data
			static_cast<std::vector<Type>&>(dest) = static_cast<const std::vector<Type>&>(*this);
		}
		catch (const std::bad_alloc&)
		{
			ccLog::Warning("[ccArray::copy] Not enough memory");
			return false;
		}
		return true;
	}

	//! Reserves memory (no exception thrown)
	bool reserveSafe(size_t count)
	{
		try
		{
			this->reserve(count);
		}
		catch (const std::bad_alloc&)
		{
			//not enough memory
			return false;
		}
		return true;
	}

	//! Returns whether some memory has been allocated or not
	inline bool isAllocated() const { return this->capacity() != 0; }
	
	//! Resizes memory (no exception thrown)
	bool resizeSafe(size_t count, bool initNewElements = false, const Type* valueForNewElements = nullptr)
	{
		try
		{
			if (initNewElements)
			{
				if (!valueForNewElements)
				{
					ccLog::Warning("[ccArray::resizeSafe] Internal error: no new element specified");
					return false;
				}
				this->resize(count, *valueForNewElements);
			}
			else
			{
				this->resize(count);
			}
		}
		catch (const std::bad_alloc&)
		{
			//not enough memory
			return false;
		}
		return true;
	}

	//inherited from ccHObject
	inline virtual CC_CLASS_ENUM getClassID() const override { return CC_TYPES::ARRAY; }
	inline virtual bool isShareable() const override { return true; }
	inline virtual bool isSerializable() const override { return true; }

	//Shortcuts (for backward compatibility)
	inline Type& getValue(size_t index) { return this->at(index); }
	inline const Type& getValue(size_t index) const { return this->at(index); }
	inline void setValue(size_t index, const Type& value) { this->at(index) = value; }
	inline void addElement(const Type& value) { this->emplace_back(value); }
	inline void fill(const Type& value) { if (this->empty()) this->resize(this->capacity(), value); else std::fill(this->begin(), this->end(), value); }
	inline unsigned currentSize() const { return static_cast<unsigned>(this->size()); }
	inline void clear(bool releaseMemory = false) { if (releaseMemory) this->resize(0); else this->std::vector<Type>::clear(); }
	inline void swap(size_t i1, size_t i2) { std::swap(this->at(i1), this->at(i2)); }

protected:

	//! Destructor (protected)
	/** Use release instead.
	**/
	virtual ~ccArray() {}

	//inherited from ccHObject
	inline virtual bool toFile_MeOnly(QFile& out) const override { return ccSerializationHelper::GenericArrayToFile<Type, N, ComponentType>(*this, out); }
	inline virtual bool fromFile_MeOnly(QFile& in, short dataVersion, int flags) override { return ccSerializationHelper::GenericArrayFromFile<Type, N, ComponentType>(*this, in, dataVersion); }

};

#endif //CC_ARRAY_HEADER