Artec 3D Scanning SDK  2.0
TArrayRef.h
Go to the documentation of this file.
1 /********************************************************************
2  *
3  * Project Artec 3D Scanning SDK
4  *
5  * Purpose: Helper class for safe and array intefrace storage
6  * and fast indexing
7  *
8  * Copyright: Artec Group
9  *
10  ********************************************************************/
11 
12 #ifndef _TARRAYREF_H_
13 #define _TARRAYREF_H_
14 
15 #include <artec/sdk/base/TRef.h>
16 #include <artec/sdk/base/IArray.h>
17 #include <artec/sdk/base/Types.h>
18 #include <artec/sdk/base/Errors.h>
19 
20 namespace artec { namespace sdk { namespace base
21 {
22 
23 /**
24  * This class is intended for safe manipulation of arrays in terms of their life time.
25  * Those arrays should implement one of the IArrayXXX interfaces that provides reference counting support.
26  * TArrayRef objects should hold pointers to the instances of such classes.
27  * Below is an example of how to create an instance of the IArrayInt
28  * implementation:
29  * TArrayRef<IArrayInt> arrayInt;
30  * createArrayInt( &arrayInt, 100 );
31  * Operator &() is overloaded so that createArrayInt() function can fill the passed TArrayRef
32  * with a newly created instance. At the very moment of definition TArrayRef object has a count
33  * of references equal to 1.
34  *
35  * This class is similar to TRef, however, some important differences should be noted.
36  * TArrayRef class has the []() operator. In order to optimize and avoid excessive
37  * virtual calls, the array size and the data pointer are cached once
38  * the array is created. This is why the array instance cannot be replaced inside.
39  * Some TArrayRef operators are also private in order to prohibit their usage.
40  */
41 
42 template <class T>
43 class TArrayRef : public TRef<T>
44 {
45 public:
46  typedef typename T::elementType elementType;
47 
48  TArrayRef() : TRef<T>()
49  {
50  keep();
51  }
52 
53  TArrayRef(T* p) : TRef<T>(p)
54  {
55  keep();
56  }
57 
58  TArrayRef(const TRef<T>& p) : TRef<T>(p)
59  {
60  keep();
61  }
62 
63  TArrayRef(const TArrayRef& p) : TRef<T>(p)
64  {
65  keep();
66  }
67 
68  ~TArrayRef() { }
69 
70  int size() const { return dataSize_; }
71 
72  elementType& operator[](int i) const { return dataPointer_[i]; }
73 
74  elementType* getPointer() const { return dataPointer_; }
75 
77  {
79  keep();
80  return *this;
81  }
82 
84  {
86  keep();
87  return *this;
88  }
89 
91  {
93  keep();
94  return *this;
95  }
96 
97 protected:
98  void keep()
99  {
100  if (TRef<T>::tObject_)
101  {
102  dataSize_ = TRef<T>::tObject_->getSize();
103  dataPointer_ = TRef<T>::tObject_->getPointer();
104  }
105  else
106  {
107  dataSize_ = 0;
108  dataPointer_ = NULL;
109  }
110  }
111 
113  elementType* dataPointer_;
114 
115 private:
116  /// Operator -> is prohibited due to optimization
117  /// Use only . member function size() and getPointer() operations, not the ones from <T> type
118  T* operator ->() const
119  {
120  assert(false && "Do not use this function!");
121  return NULL;
122  }
123 
124  T** operator&()
125  {
126  assert(false && "Do not use this function!");
127  return NULL;
128  }
129 
130  template <class T2>
131  friend ErrorCode createArray(TArrayRef<T2>& src, int elementsCount, bool zeroFill);
132  template <class T2>
133  friend ErrorCode resizeArray(TArrayRef<T2>& src, int elementsCount);
134  template <class T2>
135  friend ErrorCode resizeArray(TArrayRef<T2>& src, int elementsCount, bool zeroFill);
136 };
137 
146 
155 
156 /// Assign new array to TArrayRef
157 template <class T>
158 inline ErrorCode createArray(TArrayRef<T>& src, int elementsCount, bool zeroFill = false);
159 
160 template <>
161 inline ErrorCode createArray<IArrayByte>(TArrayRef<IArrayByte>& src, int elementsCount, bool zeroFill)
162 {
163  ErrorCode error = createArrayByte(&src.tObject_, elementsCount, zeroFill);
164  if (error != ErrorCode_OK)
165  return error;
166 
167  src.keep();
168 
169  return error;
170 }
171 
172 template <>
173 inline ErrorCode createArray<IArrayInt>(TArrayRef<IArrayInt>& src, int elementsCount, bool zeroFill)
174 {
175  ErrorCode error = createArrayInt(&src.tObject_, elementsCount, zeroFill);
176  if (error != ErrorCode_OK)
177  return error;
178 
179  src.keep();
180 
181  return error;
182 }
183 
184 template <>
185 inline ErrorCode createArray<IArrayFloat>(TArrayRef<IArrayFloat>& src, int elementsCount, bool zeroFill)
186 {
187  ErrorCode error = createArrayFloat(&src.tObject_, elementsCount, zeroFill);
188  if (error != ErrorCode_OK)
189  return error;
190 
191  src.keep();
192 
193  return error;
194 }
195 
196 template <>
197 inline ErrorCode createArray<IArrayUVCoordinates>(TArrayRef<IArrayUVCoordinates>& src, int elementsCount, bool zeroFill)
198 {
199  ErrorCode error = createArrayUVCoordinates(&src.tObject_, elementsCount, zeroFill);
200  if (error != ErrorCode_OK)
201  return error;
202 
203  src.keep();
204 
205  return error;
206 }
207 
208 template <>
209 inline ErrorCode createArray<IArrayPoint3F>(TArrayRef<IArrayPoint3F>& src, int elementsCount, bool zeroFill)
210 {
211  ErrorCode error = createArrayPoint3F(&src.tObject_, elementsCount, zeroFill);
212  if (error != ErrorCode_OK)
213  return error;
214 
215  src.keep();
216 
217  return error;
218 }
219 
220 template <>
221 inline ErrorCode createArray<IArrayIndexTriplet>(TArrayRef<IArrayIndexTriplet>& src, int elementsCount, bool zeroFill)
222 {
223  ErrorCode error = createArrayIndexTriplet(&src.tObject_, elementsCount, zeroFill);
224  if (error != ErrorCode_OK)
225  return error;
226 
227  src.keep();
228 
229  return error;
230 }
231 
232 /// Release the array assigned to TArrayRef and create a new one
233 template <class T>
234 ErrorCode resizeArray(TArrayRef<T>& src, int elementsCount)
235 {
236  if(elementsCount < 0)
238 
239  if (src.size() != elementsCount)
240  {
241  src = 0;
242 
243  return createArray(src, elementsCount);
244  }
245 
246  return ErrorCode_OK;
247 }
248 
249 /// Release the array assigned to TArrayRef and create a new one
250 template <class T>
251 ErrorCode resizeArray(TArrayRef<T>& src, int elementsCount, bool zeroFill)
252 {
253  ErrorCode error = resizeArray(src, elementsCount);
254 
255  if (error != ErrorCode_OK)
256  return error;
257 
258  if (zeroFill)
259  memset(src.dataPointer_, 0, elementsCount * sizeof(typename TArrayRef<T>::elementType));
260 
261  return ErrorCode_OK;
262 }
263 
264 } } } // namespace artec::sdk::base
265 
266 #endif // _TARRAYREF_H_
ErrorCode createArray(TArrayRef< T > &src, int elementsCount, bool zeroFill=false)
Assign new array to TArrayRef.
TArrayRef< IArrayPoint3F > TArrayPoint3F
Definition: TArrayRef.h:142
TArrayRef< const IArrayFloat > const_TArrayFloat
Definition: TArrayRef.h:149
ErrorCode createArray< IArrayUVCoordinates >(TArrayRef< IArrayUVCoordinates > &src, int elementsCount, bool zeroFill)
Definition: TArrayRef.h:197
elementType * getPointer() const
Definition: TArrayRef.h:74
TArrayRef< const IArrayUVCoordinates > const_TArrayUVCoordinates
Definition: TArrayRef.h:150
TArrayRef< IArrayString > TArrayString
Definition: TArrayRef.h:144
ErrorCode ABASESDK_LINK_SPEC createArrayFloat(IArrayFloat **pArray, int elementsCount, bool zeroFill=false)
ErrorCode resizeArray(TArrayRef< T > &src, int elementsCount)
Release the array assigned to TArrayRef and create a new one.
Definition: TArrayRef.h:234
elementType & operator[](int i) const
Definition: TArrayRef.h:72
TRef< T > & operator=(T *p)
Definition: TRef.h:101
friend ErrorCode resizeArray(TArrayRef< T2 > &src, int elementsCount)
ErrorCode createArray< IArrayIndexTriplet >(TArrayRef< IArrayIndexTriplet > &src, int elementsCount, bool zeroFill)
Definition: TArrayRef.h:221
ErrorCode ABASESDK_LINK_SPEC createArrayIndexTriplet(IArrayIndexTriplet **pArray, int elementsCount, bool zeroFill=false)
ErrorCode ABASESDK_LINK_SPEC createArrayInt(IArrayInt **pArray, int elementsCount, bool zeroFill=false)
TArrayRef< const IArrayInt > const_TArrayInt
Definition: TArrayRef.h:148
[WARNING] 0x80010201: Provided argument is invalid
Definition: Errors.h:147
TArrayRef(const TRef< T > &p)
Definition: TArrayRef.h:58
ErrorCode createArray< IArrayByte >(TArrayRef< IArrayByte > &src, int elementsCount, bool zeroFill)
Definition: TArrayRef.h:161
TArrayRef< IArrayUVCoordinates > TArrayUVCoordinates
Definition: TArrayRef.h:141
TArrayRef< IArrayByte > TArrayByte
Definition: TArrayRef.h:138
ErrorCode createArray< IArrayFloat >(TArrayRef< IArrayFloat > &src, int elementsCount, bool zeroFill)
Definition: TArrayRef.h:185
TArrayRef< IArrayImage > TArrayImage
Definition: TArrayRef.h:145
ErrorCode createArray< IArrayPoint3F >(TArrayRef< IArrayPoint3F > &src, int elementsCount, bool zeroFill)
Definition: TArrayRef.h:209
ErrorCode ABASESDK_LINK_SPEC createArrayByte(IArrayByte **pArray, int elementsCount, bool zeroFill=false)
ErrorCode createArray< IArrayInt >(TArrayRef< IArrayInt > &src, int elementsCount, bool zeroFill)
Definition: TArrayRef.h:173
[SUCCESS] 0x00000000: Success
Definition: Errors.h:61
TArrayRef< const IArrayPoint3F > const_TArrayPoint3F
Definition: TArrayRef.h:151
This class is intended for safe manipulation of supported classes in terms of their time frame...
Definition: TRef.h:59
TArrayRef< const IArrayByte > const_TArrayByte
Definition: TArrayRef.h:147
TArrayRef & operator=(T *p)
Definition: TArrayRef.h:76
TArrayRef< IArrayIndexTriplet > TArrayIndexTriplet
Definition: TArrayRef.h:143
TArrayRef & operator=(const TArrayRef &p)
Definition: TArrayRef.h:90
TArrayRef< const IArrayString > const_TArrayString
Definition: TArrayRef.h:153
friend ErrorCode createArray(TArrayRef< T2 > &src, int elementsCount, bool zeroFill)
This class is intended for safe manipulation of arrays in terms of their life time.
Definition: TArrayRef.h:43
TArrayRef< IArrayInt > TArrayInt
Definition: TArrayRef.h:139
TArrayRef & operator=(const TRef< T > &p)
Definition: TArrayRef.h:83
TArrayRef< IArrayFloat > TArrayFloat
Definition: TArrayRef.h:140
ErrorCode ABASESDK_LINK_SPEC createArrayUVCoordinates(IArrayUVCoordinates **pArray, int elementsCount, bool zeroFill=false)
ErrorCode ABASESDK_LINK_SPEC createArrayPoint3F(IArrayPoint3F **pArray, int elementsCount, bool zeroFill=false)
T::elementType elementType
Definition: TArrayRef.h:46
elementType * dataPointer_
Definition: TArrayRef.h:113
TArrayRef< const IArrayImage > const_TArrayImage
Definition: TArrayRef.h:154
TArrayRef< const IArrayIndexTriplet > const_TArrayIndexTriplet
Definition: TArrayRef.h:152
TArrayRef(const TArrayRef &p)
Definition: TArrayRef.h:63