All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UniqueArray.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_UTIL_UNIQUEARRAY_H_
36 #define _BLAZE_UTIL_UNIQUEARRAY_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <blaze/util/Assert.h>
45 #include <blaze/util/NonCopyable.h>
46 #include <blaze/util/Null.h>
48 #include <blaze/util/Types.h>
50 
51 
52 namespace blaze {
53 
54 //=================================================================================================
55 //
56 // CLASS DEFINITION
57 //
58 //=================================================================================================
59 
60 //*************************************************************************************************
95 template< typename T // Type of the array elements
96  , typename D = ArrayDelete > // Type of the deleter
97 class UniqueArray : private NonCopyable
98 {
99  public:
100  //**Type definitions****************************************************************************
101  typedef typename RemoveReference<T>::Type* Pointer;
103  typedef D Deleter;
104  //**********************************************************************************************
105 
106  //**Constructors********************************************************************************
109  inline UniqueArray( Pointer ptr = NULL );
111  //**********************************************************************************************
112 
113  //**Destructor**********************************************************************************
116  inline ~UniqueArray();
118  //**********************************************************************************************
119 
120  //**Access operators****************************************************************************
123  inline Reference operator[]( size_t index ) const /* throw() */;
125  //**********************************************************************************************
126 
127  //**Utility functions***************************************************************************
130  inline Pointer get() const /* throw() */;
131  inline Pointer release() /* throw() */;
132  inline void reset( Pointer ptr = NULL ) /* throw() */;
133  inline void swap ( UniqueArray& up ) /* throw() */;
135  //**********************************************************************************************
136 
137  private:
138  //**Member variables****************************************************************************
143 
144  //**********************************************************************************************
145 
146  //**Compile time checks*************************************************************************
150  //**********************************************************************************************
151 };
152 //*************************************************************************************************
153 
154 
155 
156 
157 //=================================================================================================
158 //
159 // CONSTRUCTOR
160 //
161 //=================================================================================================
162 
163 //*************************************************************************************************
168 template< typename T // Type of the array elements
169  , typename D > // Type of the deleter
171  : ptr_ ( ptr ) // Pointer to the managed array
172  , deleter_( Deleter() ) // Resource deleter
173 {}
174 //*************************************************************************************************
175 
176 
177 
178 
179 //=================================================================================================
180 //
181 // DESTRUCTOR
182 //
183 //=================================================================================================
184 
185 //*************************************************************************************************
188 template< typename T // Type of the array elements
189  , typename D > // Type of the deleter
191 {
192  deleter_( ptr_ );
193 }
194 //*************************************************************************************************
195 
196 
197 
198 
199 //=================================================================================================
200 //
201 // ACCESS OPERATORS
202 //
203 //=================================================================================================
204 
205 //*************************************************************************************************
210 template< typename T // Type of the array elements
211  , typename D > // Type of the deleter
212 inline typename UniqueArray<T,D>::Reference UniqueArray<T,D>::operator[]( size_t index ) const /* throw() */
213 {
214  BLAZE_USER_ASSERT( ptr_, "Uninitialized unique pointer" );
215  return ptr_[index];
216 }
217 //*************************************************************************************************
218 
219 
220 
221 
222 //=================================================================================================
223 //
224 // UTILITY FUNCTIONS
225 //
226 //=================================================================================================
227 
228 //*************************************************************************************************
236 template< typename T // Type of the array elements
237  , typename D > // Type of the deleter
238 inline typename UniqueArray<T,D>::Pointer UniqueArray<T,D>::get() const /* throw() */
239 {
240  return ptr_;
241 }
242 //*************************************************************************************************
243 
244 
245 //*************************************************************************************************
253 template< typename T // Type of the array elements
254  , typename D > // Type of the deleter
256 {
257  Pointer tmp( ptr_ );
258  ptr_ = NULL;
259  return tmp;
260 }
261 //*************************************************************************************************
262 
263 
264 //*************************************************************************************************
270 template< typename T // Type of the array elements
271  , typename D > // Type of the deleter
272 inline void UniqueArray<T,D>::reset( Pointer ptr ) /* throw() */
273 {
274  if( ptr != ptr_ ) {
275  UniqueArray( ptr ).swap( *this );
276  }
277 }
278 //*************************************************************************************************
279 
280 
281 //*************************************************************************************************
288 template< typename T // Type of the array elements
289  , typename D > // Type of the deleter
290 inline void UniqueArray<T,D>::swap( UniqueArray& ptr ) /* throw() */
291 {
292  Pointer tmp( ptr_ );
293  ptr_ = ptr.ptr_;
294  ptr.ptr_ = tmp;
295 }
296 //*************************************************************************************************
297 
298 
299 
300 
301 //=================================================================================================
302 //
303 // GLOBAL OPERATORS
304 //
305 //=================================================================================================
306 
307 //*************************************************************************************************
310 template< typename T1, typename D1, typename T2, typename D2 >
311 inline bool operator==( const UniqueArray<T1,D1>& lhs, const UniqueArray<T2,D2>& rhs );
312 
313 template< typename T1, typename D1, typename T2, typename D2 >
314 inline bool operator!=( const UniqueArray<T1,D1>& lhs, const UniqueArray<T2,D2>& rhs );
315 
316 template< typename T1, typename D1, typename T2, typename D2 >
317 inline bool operator<( const UniqueArray<T1,D1>& lhs, const UniqueArray<T2,D2>& rhs );
318 
319 template< typename T1, typename D1, typename T2, typename D2 >
320 inline bool operator<=( const UniqueArray<T1,D1>& lhs, const UniqueArray<T2,D2>& rhs );
321 
322 template< typename T1, typename D1, typename T2, typename D2 >
323 inline bool operator>( const UniqueArray<T1,D1>& lhs, const UniqueArray<T2,D2>& rhs );
324 
325 template< typename T1, typename D1, typename T2, typename D2 >
326 inline bool operator>=( const UniqueArray<T1,D1>& lhs, const UniqueArray<T2,D2>& rhs );
327 
328 template< typename T, typename D >
329 inline bool operator==( const UniqueArray<T,D>& ptr, const Null& null );
330 
331 template< typename T, typename D >
332 inline bool operator!=( const UniqueArray<T,D>& ptr, const Null& null );
333 
334 template< typename T, typename D >
335 inline bool operator<( const UniqueArray<T,D>& ptr, const Null& null );
336 
337 template< typename T, typename D >
338 inline bool operator>( const UniqueArray<T,D>& ptr, const Null& null );
339 
340 template< typename T, typename D >
341 inline bool operator<=( const UniqueArray<T,D>& ptr, const Null& null );
342 
343 template< typename T, typename D >
344 inline bool operator>=( const UniqueArray<T,D>& ptr, const Null& null );
345 
346 template< typename T, typename D >
347 inline bool operator==( const Null& null, const UniqueArray<T,D>& ptr );
348 
349 template< typename T, typename D >
350 inline bool operator!=( const Null& null, const UniqueArray<T,D>& ptr );
351 
352 template< typename T, typename D >
353 inline bool operator<( const Null& null, const UniqueArray<T,D>& ptr );
354 
355 template< typename T, typename D >
356 inline bool operator>( const Null& null, const UniqueArray<T,D>& ptr );
357 
358 template< typename T, typename D >
359 inline bool operator<=( const Null& null, const UniqueArray<T,D>& ptr );
360 
361 template< typename T, typename D >
362 inline bool operator>=( const Null& null, const UniqueArray<T,D>& ptr );
363 
364 template< typename T, typename D >
365 inline void swap( UniqueArray<T,D>& a, UniqueArray<T,D>& b ) /* throw() */;
367 //*************************************************************************************************
368 
369 
370 //*************************************************************************************************
377 template< typename T1 // Resource type of the left-hand side unique array
378  , typename D1 // Deleter type of the left-hand side unique array
379  , typename T2 // Resource type of the right-hand side unique array
380  , typename D2 > // Deleter type of the right-hand side unique array
381 inline bool operator==( const UniqueArray<T1,D1>& lhs, const UniqueArray<T2,D2>& rhs )
382 {
383  return lhs.get() == rhs.get();
384 }
385 //*************************************************************************************************
386 
387 
388 //*************************************************************************************************
395 template< typename T1 // Resource type of the left-hand side unique array
396  , typename D1 // Deleter type of the left-hand side unique array
397  , typename T2 // Resource type of the right-hand side unique array
398  , typename D2 > // Deleter type of the right-hand side unique array
399 inline bool operator!=( const UniqueArray<T1,D1>& lhs, const UniqueArray<T2,D2>& rhs )
400 {
401  return lhs.get() != rhs.get();
402 }
403 //*************************************************************************************************
404 
405 
406 //*************************************************************************************************
413 template< typename T1 // Resource type of the left-hand side unique array
414  , typename D1 // Deleter type of the left-hand side unique array
415  , typename T2 // Resource type of the right-hand side unique array
416  , typename D2 > // Deleter type of the right-hand side unique array
417 inline bool operator<( const UniqueArray<T1,D1>& lhs, const UniqueArray<T2,D2>& rhs )
418 {
419  return lhs.get() < rhs.get();
420 }
421 //*************************************************************************************************
422 
423 
424 //*************************************************************************************************
431 template< typename T1 // Resource type of the left-hand side unique array
432  , typename D1 // Deleter type of the left-hand side unique array
433  , typename T2 // Resource type of the right-hand side unique array
434  , typename D2 > // Deleter type of the right-hand side unique array
435 inline bool operator>( const UniqueArray<T1,D1>& lhs, const UniqueArray<T2,D2>& rhs )
436 {
437  return rhs < lhs;
438 }
439 //*************************************************************************************************
440 
441 
442 //*************************************************************************************************
449 template< typename T1 // Resource type of the left-hand side unique array
450  , typename D1 // Deleter type of the left-hand side unique array
451  , typename T2 // Resource type of the right-hand side unique array
452  , typename D2 > // Deleter type of the right-hand side unique array
453 inline bool operator<=( const UniqueArray<T1,D1>& lhs, const UniqueArray<T2,D2>& rhs )
454 {
455  return !( rhs < lhs );
456 }
457 //*************************************************************************************************
458 
459 
460 //*************************************************************************************************
467 template< typename T1 // Resource type of the left-hand side unique array
468  , typename D1 // Deleter type of the left-hand side unique array
469  , typename T2 // Resource type of the right-hand side unique array
470  , typename D2 > // Deleter type of the right-hand side unique array
471 inline bool operator>=( const UniqueArray<T1,D1>& lhs, const UniqueArray<T2,D2>& rhs )
472 {
473  return !( lhs < rhs );
474 }
475 //*************************************************************************************************
476 
477 
478 //*************************************************************************************************
485 template< typename T // Resource type of the unique array
486  , typename D > // Deleter type of the unique array
487 inline bool operator==( const UniqueArray<T,D>& ptr, const Null& null )
488 {
489  return ptr.get() == null;
490 }
491 //*************************************************************************************************
492 
493 
494 //*************************************************************************************************
501 template< typename T // Resource type of the unique array
502  , typename D > // Deleter type of the unique array
503 inline bool operator!=( const UniqueArray<T,D>& ptr, const Null& null )
504 {
505  return !( ptr == null );
506 }
507 //*************************************************************************************************
508 
509 
510 //*************************************************************************************************
517 template< typename T // Resource type of the unique array
518  , typename D > // Deleter type of the unique array
519 inline bool operator<( const UniqueArray<T,D>& ptr, const Null& null )
520 {
521  return ptr.get() < null;
522 }
523 //*************************************************************************************************
524 
525 
526 //*************************************************************************************************
533 template< typename T // Resource type of the unique array
534  , typename D > // Deleter type of the unique array
535 inline bool operator>( const UniqueArray<T,D>& ptr, const Null& null )
536 {
537  return ptr.get() > null;
538 }
539 //*************************************************************************************************
540 
541 
542 //*************************************************************************************************
549 template< typename T // Resource type of the unique array
550  , typename D > // Deleter type of the unique array
551 inline bool operator<=( const UniqueArray<T,D>& ptr, const Null& null )
552 {
553  return !( ptr > null );
554 }
555 //*************************************************************************************************
556 
557 
558 //*************************************************************************************************
565 template< typename T // Resource type of the unique array
566  , typename D > // Deleter type of the unique array
567 inline bool operator>=( const UniqueArray<T,D>& ptr, const Null& null )
568 {
569  return !( ptr < null );
570 }
571 //*************************************************************************************************
572 
573 
574 //*************************************************************************************************
581 template< typename T // Resource type of the unique array
582  , typename D > // Deleter type of the unique array
583 inline bool operator==( const Null& null, const UniqueArray<T,D>& ptr )
584 {
585  return ptr == null;
586 }
587 //*************************************************************************************************
588 
589 
590 //*************************************************************************************************
597 template< typename T // Resource type of the unique array
598  , typename D > // Deleter type of the unique array
599 inline bool operator!=( const Null& null, const UniqueArray<T,D>& ptr )
600 {
601  return ptr != null;
602 }
603 //*************************************************************************************************
604 
605 
606 //*************************************************************************************************
613 template< typename T // Resource type of the unique array
614  , typename D > // Deleter type of the unique array
615 inline bool operator<( const Null& null, const UniqueArray<T,D>& ptr )
616 {
617  return ptr > null;
618 }
619 //*************************************************************************************************
620 
621 
622 //*************************************************************************************************
629 template< typename T // Resource type of the unique array
630  , typename D > // Deleter type of the unique array
631 inline bool operator>( const Null& null, const UniqueArray<T,D>& ptr )
632 {
633  return ptr < null;
634 }
635 //*************************************************************************************************
636 
637 
638 //*************************************************************************************************
645 template< typename T // Resource type of the unique array
646  , typename D > // Deleter type of the unique array
647 inline bool operator<=( const Null& null, const UniqueArray<T,D>& ptr )
648 {
649  return ptr >= null;
650 }
651 //*************************************************************************************************
652 
653 
654 //*************************************************************************************************
661 template< typename T // Resource type of the unique array
662  , typename D > // Deleter type of the unique array
663 inline bool operator>=( const Null& null, const UniqueArray<T,D>& ptr )
664 {
665  return ptr <= null;
666 }
667 //*************************************************************************************************
668 
669 
670 //*************************************************************************************************
678 template< typename T // Resource type of the unique array
679  , typename D > // Deleter type of the unique array
680 inline void swap( UniqueArray<T,D>& a, UniqueArray<T,D>& b ) /* throw() */
681 {
682  a.swap( b );
683 }
684 //*************************************************************************************************
685 
686 } // namespace blaze
687 
688 #endif
#define BLAZE_USER_ASSERT(expr, msg)
Run time assertion macro for user checks.In case of an invalid run time expression, the program execution is terminated. The BLAZE_USER_ASSERT macro can be disabled by setting the BLAZE_USER_ASSERT flag to zero or by defining NDEBUG during the compilation.
Definition: Assert.h:117
RemoveReference< T >::Type & Reference
Reference type of the managed array elements.
Definition: UniqueArray.h:102
RemoveReference< T >::Type * Pointer
Pointer type of the managed array elements.
Definition: UniqueArray.h:101
const blaze::Null NULL
Global NULL pointer.This instance of the Null class replaces the NULL macro to ensure a type-safe NUL...
Definition: Null.h:300
UniqueArray(Pointer ptr=NULL)
The default constructor for the UniqueArray specialization.
Definition: UniqueArray.h:170
Pointer get() const
Returns a pointer to the managed array.
Definition: UniqueArray.h:238
Base class for non-copyable class instances.
Header file for a safe C++ NULL pointer implementation.
bool operator>(const NegativeAccuracy< A > &lhs, const T &rhs)
Greater-than comparison between a NegativeAccuracy object and a floating point value.
Definition: Accuracy.h:366
bool operator>=(const NegativeAccuracy< A > &, const T &rhs)
Greater-or-equal-than comparison between a NegativeAccuracy object and a floating point value...
Definition: Accuracy.h:442
Safe C++ NULL pointer implementation.This implementation offers a remedy for the use of the NULL poin...
Definition: Null.h:62
Constraint on the data type.
Deleter deleter_
Resource deleter.
Definition: UniqueArray.h:142
void reset(Pointer ptr=NULL)
Resets the unique array and replaces the managed array with the given array.
Definition: UniqueArray.h:272
Base class for non-copyable class instances.The NonCopyable class is intended to work as a base class...
Definition: NonCopyable.h:63
Pointer release()
Releases the ownership of the managed array to the caller.
Definition: UniqueArray.h:255
~UniqueArray()
The destructor for the UniqueArray specialization.
Definition: UniqueArray.h:190
#define BLAZE_CONSTRAINT_MUST_NOT_BE_ARRAY_TYPE(T)
Constraint on the data type.In case the given data type T is an array type, a compilation error is cr...
Definition: Array.h:116
Removal of reference modifiers.The RemoveCV type trait removes any reference modifiers from the given...
Definition: RemoveReference.h:69
D Deleter
Type of the resource deleter.
Definition: UniqueArray.h:103
Header file for run time assertion macros.
Pointer ptr_
Pointer to the managed array.
Definition: UniqueArray.h:141
void swap(DynamicMatrix< Type, SO > &a, DynamicMatrix< Type, SO > &b)
Swapping the contents of two matrices.
Definition: DynamicMatrix.h:4671
Header file for the RemoveReference type trait.
Reference operator[](size_t index) const
Direct access to the array elements.
Definition: UniqueArray.h:212
bool operator==(const NegativeAccuracy< A > &lhs, const T &rhs)
Equality comparison between a NegativeAccuracy object and a floating point value. ...
Definition: Accuracy.h:249
bool operator!=(const NegativeAccuracy< A > &lhs, const T &rhs)
Inequality comparison between a NegativeAccuracy object and a floating point value.
Definition: Accuracy.h:289
Header file for basic type definitions.
Header file for the ArrayDelete policy classes.
void swap(UniqueArray &up)
Swapping the contents of two unique arrays.
Definition: UniqueArray.h:290
Scope-limited management of dynamically allocated arrays.The UniqueArray class implements a scope-res...
Definition: UniqueArray.h:97