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 template <class T>
24 class TArrayRef : public TRef<T>
25 {
26 public:
27  typedef typename T::elementType elementType;
28 
29  TArrayRef() : TRef<T>()
30  {
31  keep();
32  }
33 
34  TArrayRef(T* p) : TRef<T>(p)
35  {
36  keep();
37  }
38 
39  TArrayRef(const TRef<T>& p) : TRef<T>(p)
40  {
41  keep();
42  }
43 
44  TArrayRef(const TArrayRef& p) : TRef<T>(p)
45  {
46  keep();
47  }
48 
49  ~TArrayRef() { }
50 
51  int size() const { return dataSize_; }
52 
53  elementType& operator[](int i) const { return dataPointer_[i]; }
54 
55  elementType* getPointer() const { return dataPointer_; }
56 
58  {
60  keep();
61  return *this;
62  }
63 
65  {
67  keep();
68  return *this;
69  }
70 
72  {
74  keep();
75  return *this;
76  }
77 
78 protected:
79  void keep()
80  {
82  {
83  dataSize_ = TRef<T>::tObject_->getSize();
84  dataPointer_ = TRef<T>::tObject_->getPointer();
85  }
86  else
87  {
88  dataSize_ = 0;
89  dataPointer_ = NULL;
90  }
91  }
92 
93  int dataSize_;
94  elementType* dataPointer_;
95 
96 private:
97  /// operator -> is prohibited due to optimization
98  /// use only . member function size() and getPointer() operations, not ones from <T> type
99  T* operator ->() const
100  {
101  assert(false && "Do not use this function!");
102  return NULL;
103  }
104 
105  T** operator&()
106  {
107  assert(false && "Do not use this function!");
108  return NULL;
109  }
110 
111  template <class T2>
112  friend ErrorCode createArray(TArrayRef<T2>& src, int elementsCount, bool zeroFill);
113  template <class T2>
114  friend ErrorCode resizeArray(TArrayRef<T2>& src, int elementsCount);
115  template <class T2>
116  friend ErrorCode resizeArray(TArrayRef<T2>& src, int elementsCount, bool zeroFill);
117 };
118 
127 
136 
137 /// assign to TArrayRef new array
138 template <class T>
139 inline ErrorCode createArray(TArrayRef<T>& src, int elementsCount, bool zeroFill = false);
140 
141 template <>
142 inline ErrorCode createArray<IArrayByte>(TArrayRef<IArrayByte>& src, int elementsCount, bool zeroFill)
143 {
144  ErrorCode error = createArrayByte(&src.tObject_, elementsCount, zeroFill);
145  if (error != ErrorCode_OK)
146  return error;
147 
148  src.keep();
149 
150  return error;
151 }
152 
153 template <>
154 inline ErrorCode createArray<IArrayInt>(TArrayRef<IArrayInt>& src, int elementsCount, bool zeroFill)
155 {
156  ErrorCode error = createArrayInt(&src.tObject_, elementsCount, zeroFill);
157  if (error != ErrorCode_OK)
158  return error;
159 
160  src.keep();
161 
162  return error;
163 }
164 
165 template <>
166 inline ErrorCode createArray<IArrayFloat>(TArrayRef<IArrayFloat>& src, int elementsCount, bool zeroFill)
167 {
168  ErrorCode error = createArrayFloat(&src.tObject_, elementsCount, zeroFill);
169  if (error != ErrorCode_OK)
170  return error;
171 
172  src.keep();
173 
174  return error;
175 }
176 
177 template <>
178 inline ErrorCode createArray<IArrayUVCoordinates>(TArrayRef<IArrayUVCoordinates>& src, int elementsCount, bool zeroFill)
179 {
180  ErrorCode error = createArrayUVCoordinates(&src.tObject_, elementsCount, zeroFill);
181  if (error != ErrorCode_OK)
182  return error;
183 
184  src.keep();
185 
186  return error;
187 }
188 
189 template <>
190 inline ErrorCode createArray<IArrayPoint3F>(TArrayRef<IArrayPoint3F>& src, int elementsCount, bool zeroFill)
191 {
192  ErrorCode error = createArrayPoint3F(&src.tObject_, elementsCount, zeroFill);
193  if (error != ErrorCode_OK)
194  return error;
195 
196  src.keep();
197 
198  return error;
199 }
200 
201 template <>
202 inline ErrorCode createArray<IArrayIndexTriplet>(TArrayRef<IArrayIndexTriplet>& src, int elementsCount, bool zeroFill)
203 {
204  ErrorCode error = createArrayIndexTriplet(&src.tObject_, elementsCount, zeroFill);
205  if (error != ErrorCode_OK)
206  return error;
207 
208  src.keep();
209 
210  return error;
211 }
212 
213 /// release assigned to TArrayRef array and create new one
214 template <class T>
215 ErrorCode resizeArray(TArrayRef<T>& src, int elementsCount)
216 {
217  if(elementsCount < 0)
219 
220  if (src.size() != elementsCount)
221  {
222  src = 0;
223 
224  return createArray(src, elementsCount);
225  }
226 
227  return ErrorCode_OK;
228 }
229 
230 /// release assigned to TArrayRef array and create new one
231 template <class T>
232 ErrorCode resizeArray(TArrayRef<T>& src, int elementsCount, bool zeroFill)
233 {
234  ErrorCode error = resizeArray(src, elementsCount);
235 
236  if (error != ErrorCode_OK)
237  return error;
238 
239  if (zeroFill)
240  memset(src.dataPointer_, 0, elementsCount * sizeof(typename TArrayRef<T>::elementType));
241 
242  return ErrorCode_OK;
243 }
244 
245 } } } // namespace artec::sdk::base
246 
247 #endif // _TARRAYREF_H_
ErrorCode createArray(TArrayRef< T > &src, int elementsCount, bool zeroFill=false)
assign to TArrayRef new array
TArrayRef< IArrayPoint3F > TArrayPoint3F
Definition: TArrayRef.h:123
TArrayRef< const IArrayFloat > const_TArrayFloat
Definition: TArrayRef.h:130
ErrorCode createArray< IArrayUVCoordinates >(TArrayRef< IArrayUVCoordinates > &src, int elementsCount, bool zeroFill)
Definition: TArrayRef.h:178
elementType * getPointer() const
Definition: TArrayRef.h:55
TArrayRef< const IArrayUVCoordinates > const_TArrayUVCoordinates
Definition: TArrayRef.h:131
TArrayRef< IArrayString > TArrayString
Definition: TArrayRef.h:125
ErrorCode ABASESDK_LINK_SPEC createArrayFloat(IArrayFloat **pArray, int elementsCount, bool zeroFill=false)
ErrorCode resizeArray(TArrayRef< T > &src, int elementsCount)
release assigned to TArrayRef array and create new one
Definition: TArrayRef.h:215
elementType & operator[](int i) const
Definition: TArrayRef.h:53
TRef< T > & operator=(T *p)
Definition: TRef.h:64
friend ErrorCode resizeArray(TArrayRef< T2 > &src, int elementsCount)
ErrorCode createArray< IArrayIndexTriplet >(TArrayRef< IArrayIndexTriplet > &src, int elementsCount, bool zeroFill)
Definition: TArrayRef.h:202
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:129
[WARNING] 0x80010201: Provided argument is invalid
Definition: Errors.h:147
TArrayRef(const TRef< T > &p)
Definition: TArrayRef.h:39
ErrorCode createArray< IArrayByte >(TArrayRef< IArrayByte > &src, int elementsCount, bool zeroFill)
Definition: TArrayRef.h:142
TArrayRef< IArrayUVCoordinates > TArrayUVCoordinates
Definition: TArrayRef.h:122
TArrayRef< IArrayByte > TArrayByte
Definition: TArrayRef.h:119
ErrorCode createArray< IArrayFloat >(TArrayRef< IArrayFloat > &src, int elementsCount, bool zeroFill)
Definition: TArrayRef.h:166
TArrayRef< IArrayImage > TArrayImage
Definition: TArrayRef.h:126
ErrorCode createArray< IArrayPoint3F >(TArrayRef< IArrayPoint3F > &src, int elementsCount, bool zeroFill)
Definition: TArrayRef.h:190
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:154
[SUCCESS] 0x00000000: Success
Definition: Errors.h:61
TArrayRef< const IArrayPoint3F > const_TArrayPoint3F
Definition: TArrayRef.h:132
TArrayRef< const IArrayByte > const_TArrayByte
Definition: TArrayRef.h:128
TArrayRef & operator=(T *p)
Definition: TArrayRef.h:57
TArrayRef< IArrayIndexTriplet > TArrayIndexTriplet
Definition: TArrayRef.h:124
TArrayRef & operator=(const TArrayRef &p)
Definition: TArrayRef.h:71
TArrayRef< const IArrayString > const_TArrayString
Definition: TArrayRef.h:134
friend ErrorCode createArray(TArrayRef< T2 > &src, int elementsCount, bool zeroFill)
TArrayRef< IArrayInt > TArrayInt
Definition: TArrayRef.h:120
TArrayRef & operator=(const TRef< T > &p)
Definition: TArrayRef.h:64
TArrayRef< IArrayFloat > TArrayFloat
Definition: TArrayRef.h:121
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:27
elementType * dataPointer_
Definition: TArrayRef.h:94
TArrayRef< const IArrayImage > const_TArrayImage
Definition: TArrayRef.h:135
TArrayRef< const IArrayIndexTriplet > const_TArrayIndexTriplet
Definition: TArrayRef.h:133
TArrayRef(const TArrayRef &p)
Definition: TArrayRef.h:44