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  //**Conversion operator*************************************************************************
140  inline operator bool() const /* throw() */;
142  //**********************************************************************************************
143 
144  private:
145  //**Member variables****************************************************************************
148  Pointer ptr_;
149  Deleter deleter_;
150 
151  //**********************************************************************************************
152 
153  //**Compile time checks*************************************************************************
157  //**********************************************************************************************
158 };
159 //*************************************************************************************************
160 
161 
162 
163 
164 //=================================================================================================
165 //
166 // CONSTRUCTOR
167 //
168 //=================================================================================================
169 
170 //*************************************************************************************************
175 template< typename T // Type of the array elements
176  , typename D > // Type of the deleter
177 inline UniqueArray<T,D>::UniqueArray( Pointer ptr )
178  : ptr_ ( ptr ) // Pointer to the managed array
179  , deleter_( Deleter() ) // Resource deleter
180 {}
181 //*************************************************************************************************
182 
183 
184 
185 
186 //=================================================================================================
187 //
188 // DESTRUCTOR
189 //
190 //=================================================================================================
191 
192 //*************************************************************************************************
195 template< typename T // Type of the array elements
196  , typename D > // Type of the deleter
198 {
199  deleter_( ptr_ );
200 }
201 //*************************************************************************************************
202 
203 
204 
205 
206 //=================================================================================================
207 //
208 // ACCESS OPERATORS
209 //
210 //=================================================================================================
211 
212 //*************************************************************************************************
217 template< typename T // Type of the array elements
218  , typename D > // Type of the deleter
219 inline typename UniqueArray<T,D>::Reference UniqueArray<T,D>::operator[]( size_t index ) const /* throw() */
220 {
221  BLAZE_USER_ASSERT( ptr_, "Uninitialized unique pointer" );
222  return ptr_[index];
223 }
224 //*************************************************************************************************
225 
226 
227 
228 
229 //=================================================================================================
230 //
231 // UTILITY FUNCTIONS
232 //
233 //=================================================================================================
234 
235 //*************************************************************************************************
243 template< typename T // Type of the array elements
244  , typename D > // Type of the deleter
245 inline typename UniqueArray<T,D>::Pointer UniqueArray<T,D>::get() const /* throw() */
246 {
247  return ptr_;
248 }
249 //*************************************************************************************************
250 
251 
252 //*************************************************************************************************
260 template< typename T // Type of the array elements
261  , typename D > // Type of the deleter
263 {
264  Pointer tmp( ptr_ );
265  ptr_ = NULL;
266  return tmp;
267 }
268 //*************************************************************************************************
269 
270 
271 //*************************************************************************************************
277 template< typename T // Type of the array elements
278  , typename D > // Type of the deleter
279 inline void UniqueArray<T,D>::reset( Pointer ptr ) /* throw() */
280 {
281  if( ptr != ptr_ ) {
282  UniqueArray( ptr ).swap( *this );
283  }
284 }
285 //*************************************************************************************************
286 
287 
288 //*************************************************************************************************
295 template< typename T // Type of the array elements
296  , typename D > // Type of the deleter
297 inline void UniqueArray<T,D>::swap( UniqueArray& ptr ) /* throw() */
298 {
299  Pointer tmp( ptr_ );
300  ptr_ = ptr.ptr_;
301  ptr.ptr_ = tmp;
302 }
303 //*************************************************************************************************
304 
305 
306 
307 
308 //=================================================================================================
309 //
310 // CONVERSION OPERATOR
311 //
312 //=================================================================================================
313 
314 //*************************************************************************************************
319 template< typename T // Type of the array elements
320  , typename D > // Type of the deleter
321 inline UniqueArray<T,D>::operator bool() const /* throw() */
322 {
323  return ( ptr_ != NULL );
324 }
325 //*************************************************************************************************
326 
327 
328 
329 
330 //=================================================================================================
331 //
332 // GLOBAL OPERATORS
333 //
334 //=================================================================================================
335 
336 //*************************************************************************************************
339 template< typename T1, typename D1, typename T2, typename D2 >
340 inline bool operator==( const UniqueArray<T1,D1>& lhs, const UniqueArray<T2,D2>& rhs );
341 
342 template< typename T1, typename D1, typename T2, typename D2 >
343 inline bool operator!=( const UniqueArray<T1,D1>& lhs, const UniqueArray<T2,D2>& rhs );
344 
345 template< typename T1, typename D1, typename T2, typename D2 >
346 inline bool operator<( const UniqueArray<T1,D1>& lhs, const UniqueArray<T2,D2>& rhs );
347 
348 template< typename T1, typename D1, typename T2, typename D2 >
349 inline bool operator<=( const UniqueArray<T1,D1>& lhs, const UniqueArray<T2,D2>& rhs );
350 
351 template< typename T1, typename D1, typename T2, typename D2 >
352 inline bool operator>( const UniqueArray<T1,D1>& lhs, const UniqueArray<T2,D2>& rhs );
353 
354 template< typename T1, typename D1, typename T2, typename D2 >
355 inline bool operator>=( const UniqueArray<T1,D1>& lhs, const UniqueArray<T2,D2>& rhs );
356 
357 template< typename T, typename D >
358 inline bool operator==( const UniqueArray<T,D>& ptr, const Null& null );
359 
360 template< typename T, typename D >
361 inline bool operator!=( const UniqueArray<T,D>& ptr, const Null& null );
362 
363 template< typename T, typename D >
364 inline bool operator<( const UniqueArray<T,D>& ptr, const Null& null );
365 
366 template< typename T, typename D >
367 inline bool operator>( const UniqueArray<T,D>& ptr, const Null& null );
368 
369 template< typename T, typename D >
370 inline bool operator<=( const UniqueArray<T,D>& ptr, const Null& null );
371 
372 template< typename T, typename D >
373 inline bool operator>=( const UniqueArray<T,D>& ptr, const Null& null );
374 
375 template< typename T, typename D >
376 inline bool operator==( const Null& null, const UniqueArray<T,D>& ptr );
377 
378 template< typename T, typename D >
379 inline bool operator!=( const Null& null, const UniqueArray<T,D>& ptr );
380 
381 template< typename T, typename D >
382 inline bool operator<( const Null& null, const UniqueArray<T,D>& ptr );
383 
384 template< typename T, typename D >
385 inline bool operator>( const Null& null, const UniqueArray<T,D>& ptr );
386 
387 template< typename T, typename D >
388 inline bool operator<=( const Null& null, const UniqueArray<T,D>& ptr );
389 
390 template< typename T, typename D >
391 inline bool operator>=( const Null& null, const UniqueArray<T,D>& ptr );
392 
393 template< typename T, typename D >
394 inline void swap( UniqueArray<T,D>& a, UniqueArray<T,D>& b ) /* throw() */;
396 //*************************************************************************************************
397 
398 
399 //*************************************************************************************************
406 template< typename T1 // Resource type of the left-hand side unique array
407  , typename D1 // Deleter type of the left-hand side unique array
408  , typename T2 // Resource type of the right-hand side unique array
409  , typename D2 > // Deleter type of the right-hand side unique array
410 inline bool operator==( const UniqueArray<T1,D1>& lhs, const UniqueArray<T2,D2>& rhs )
411 {
412  return lhs.get() == rhs.get();
413 }
414 //*************************************************************************************************
415 
416 
417 //*************************************************************************************************
424 template< typename T1 // Resource type of the left-hand side unique array
425  , typename D1 // Deleter type of the left-hand side unique array
426  , typename T2 // Resource type of the right-hand side unique array
427  , typename D2 > // Deleter type of the right-hand side unique array
428 inline bool operator!=( const UniqueArray<T1,D1>& lhs, const UniqueArray<T2,D2>& rhs )
429 {
430  return lhs.get() != rhs.get();
431 }
432 //*************************************************************************************************
433 
434 
435 //*************************************************************************************************
442 template< typename T1 // Resource type of the left-hand side unique array
443  , typename D1 // Deleter type of the left-hand side unique array
444  , typename T2 // Resource type of the right-hand side unique array
445  , typename D2 > // Deleter type of the right-hand side unique array
446 inline bool operator<( const UniqueArray<T1,D1>& lhs, const UniqueArray<T2,D2>& rhs )
447 {
448  return lhs.get() < rhs.get();
449 }
450 //*************************************************************************************************
451 
452 
453 //*************************************************************************************************
460 template< typename T1 // Resource type of the left-hand side unique array
461  , typename D1 // Deleter type of the left-hand side unique array
462  , typename T2 // Resource type of the right-hand side unique array
463  , typename D2 > // Deleter type of the right-hand side unique array
464 inline bool operator>( const UniqueArray<T1,D1>& lhs, const UniqueArray<T2,D2>& rhs )
465 {
466  return rhs < lhs;
467 }
468 //*************************************************************************************************
469 
470 
471 //*************************************************************************************************
478 template< typename T1 // Resource type of the left-hand side unique array
479  , typename D1 // Deleter type of the left-hand side unique array
480  , typename T2 // Resource type of the right-hand side unique array
481  , typename D2 > // Deleter type of the right-hand side unique array
482 inline bool operator<=( const UniqueArray<T1,D1>& lhs, const UniqueArray<T2,D2>& rhs )
483 {
484  return !( rhs < lhs );
485 }
486 //*************************************************************************************************
487 
488 
489 //*************************************************************************************************
496 template< typename T1 // Resource type of the left-hand side unique array
497  , typename D1 // Deleter type of the left-hand side unique array
498  , typename T2 // Resource type of the right-hand side unique array
499  , typename D2 > // Deleter type of the right-hand side unique array
500 inline bool operator>=( const UniqueArray<T1,D1>& lhs, const UniqueArray<T2,D2>& rhs )
501 {
502  return !( lhs < rhs );
503 }
504 //*************************************************************************************************
505 
506 
507 //*************************************************************************************************
514 template< typename T // Resource type of the unique array
515  , typename D > // Deleter type of the unique array
516 inline bool operator==( const UniqueArray<T,D>& ptr, const Null& null )
517 {
518  return ptr.get() == null;
519 }
520 //*************************************************************************************************
521 
522 
523 //*************************************************************************************************
530 template< typename T // Resource type of the unique array
531  , typename D > // Deleter type of the unique array
532 inline bool operator!=( const UniqueArray<T,D>& ptr, const Null& null )
533 {
534  return !( ptr == null );
535 }
536 //*************************************************************************************************
537 
538 
539 //*************************************************************************************************
546 template< typename T // Resource type of the unique array
547  , typename D > // Deleter type of the unique array
548 inline bool operator<( const UniqueArray<T,D>& ptr, const Null& null )
549 {
550  return ptr.get() < null;
551 }
552 //*************************************************************************************************
553 
554 
555 //*************************************************************************************************
562 template< typename T // Resource type of the unique array
563  , typename D > // Deleter type of the unique array
564 inline bool operator>( const UniqueArray<T,D>& ptr, const Null& null )
565 {
566  return ptr.get() > null;
567 }
568 //*************************************************************************************************
569 
570 
571 //*************************************************************************************************
578 template< typename T // Resource type of the unique array
579  , typename D > // Deleter type of the unique array
580 inline bool operator<=( const UniqueArray<T,D>& ptr, const Null& null )
581 {
582  return !( ptr > null );
583 }
584 //*************************************************************************************************
585 
586 
587 //*************************************************************************************************
594 template< typename T // Resource type of the unique array
595  , typename D > // Deleter type of the unique array
596 inline bool operator>=( const UniqueArray<T,D>& ptr, const Null& null )
597 {
598  return !( ptr < null );
599 }
600 //*************************************************************************************************
601 
602 
603 //*************************************************************************************************
610 template< typename T // Resource type of the unique array
611  , typename D > // Deleter type of the unique array
612 inline bool operator==( const Null& null, const UniqueArray<T,D>& ptr )
613 {
614  return ptr == null;
615 }
616 //*************************************************************************************************
617 
618 
619 //*************************************************************************************************
626 template< typename T // Resource type of the unique array
627  , typename D > // Deleter type of the unique array
628 inline bool operator!=( const Null& null, const UniqueArray<T,D>& ptr )
629 {
630  return ptr != null;
631 }
632 //*************************************************************************************************
633 
634 
635 //*************************************************************************************************
642 template< typename T // Resource type of the unique array
643  , typename D > // Deleter type of the unique array
644 inline bool operator<( const Null& null, const UniqueArray<T,D>& ptr )
645 {
646  return ptr > null;
647 }
648 //*************************************************************************************************
649 
650 
651 //*************************************************************************************************
658 template< typename T // Resource type of the unique array
659  , typename D > // Deleter type of the unique array
660 inline bool operator>( const Null& null, const UniqueArray<T,D>& ptr )
661 {
662  return ptr < null;
663 }
664 //*************************************************************************************************
665 
666 
667 //*************************************************************************************************
674 template< typename T // Resource type of the unique array
675  , typename D > // Deleter type of the unique array
676 inline bool operator<=( const Null& null, const UniqueArray<T,D>& ptr )
677 {
678  return ptr >= null;
679 }
680 //*************************************************************************************************
681 
682 
683 //*************************************************************************************************
690 template< typename T // Resource type of the unique array
691  , typename D > // Deleter type of the unique array
692 inline bool operator>=( const Null& null, const UniqueArray<T,D>& ptr )
693 {
694  return ptr <= null;
695 }
696 //*************************************************************************************************
697 
698 
699 //*************************************************************************************************
707 template< typename T // Resource type of the unique array
708  , typename D > // Deleter type of the unique array
709 inline void swap( UniqueArray<T,D>& a, UniqueArray<T,D>& b ) /* throw() */
710 {
711  a.swap( b );
712 }
713 //*************************************************************************************************
714 
715 } // namespace blaze
716 
717 #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
Header file for basic type definitions.
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
UniqueArray(Pointer ptr=NULL)
The default constructor for the UniqueArray specialization.
Definition: UniqueArray.h:177
Pointer get() const
Returns a pointer to the managed array.
Definition: UniqueArray.h:245
Base class for non-copyable class instances.
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:69
Constraint on the data type.
Namespace of the Blaze C++ math library.
Definition: Blaze.h:57
Deleter deleter_
Resource deleter.
Definition: UniqueArray.h:149
void reset(Pointer ptr=NULL)
Resets the unique array and replaces the managed array with the given array.
Definition: UniqueArray.h:279
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:262
~UniqueArray()
The destructor for the UniqueArray specialization.
Definition: UniqueArray.h:197
#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:148
Header file for the RemoveReference type trait.
Reference operator[](size_t index) const
Direct access to the array elements.
Definition: UniqueArray.h:219
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 the ArrayDelete policy classes.
void swap(UniqueArray &up)
Swapping the contents of two unique arrays.
Definition: UniqueArray.h:297
Scope-limited management of dynamically allocated arrays.The UniqueArray class implements a scope-res...
Definition: UniqueArray.h:97
Header file for a safe C++ NULL pointer implementation.