UniquePtr.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_UTIL_UNIQUEPTR_H_
36 #define _BLAZE_UTIL_UNIQUEPTR_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 //*************************************************************************************************
96 template< typename T // Type of the resource
97  , typename D = PtrDelete > // Type of the deleter
98 class UniquePtr : private NonCopyable
99 {
100  public:
101  //**Type definitions****************************************************************************
102  typedef typename RemoveReference<T>::Type* Pointer;
104  typedef D Deleter;
105  //**********************************************************************************************
106 
107  //**Constructors********************************************************************************
110  inline UniquePtr( Pointer ptr = NULL );
112  //**********************************************************************************************
113 
114  //**Destructor**********************************************************************************
117  inline ~UniquePtr();
119  //**********************************************************************************************
120 
121  //**Access operators****************************************************************************
124  inline Reference operator* () const /* throw() */;
125  inline Pointer operator->() const /* throw() */;
127  //**********************************************************************************************
128 
129  //**Utility functions***************************************************************************
132  inline Pointer get() const /* throw() */;
133  inline Pointer release() /* throw() */;
134  inline void reset( Pointer ptr = NULL ) /* throw() */;
135  inline void swap ( UniquePtr& up ) /* throw() */;
137  //**********************************************************************************************
138 
139  //**Conversion operator*************************************************************************
142  inline operator bool() const /* throw() */;
144  //**********************************************************************************************
145 
146  private:
147  //**Member variables****************************************************************************
150  Pointer ptr_;
151  Deleter deleter_;
152 
153  //**********************************************************************************************
154 
155  //**Compile time checks*************************************************************************
159  //**********************************************************************************************
160 };
161 //*************************************************************************************************
162 
163 
164 
165 
166 //=================================================================================================
167 //
168 // CONSTRUCTOR
169 //
170 //=================================================================================================
171 
172 //*************************************************************************************************
177 template< typename T // Type of the resource
178  , typename D > // Type of the deleter
179 inline UniquePtr<T,D>::UniquePtr( Pointer ptr )
180  : ptr_ ( ptr ) // Pointer to the managed resource
181  , deleter_( Deleter() ) // Resource deleter.
182 {}
183 //*************************************************************************************************
184 
185 
186 
187 
188 //=================================================================================================
189 //
190 // DESTRUCTOR
191 //
192 //=================================================================================================
193 
194 //*************************************************************************************************
197 template< typename T // Type of the resource
198  , typename D > // Type of the deleter
200 {
201  deleter_( ptr_ );
202 }
203 //*************************************************************************************************
204 
205 
206 
207 
208 //=================================================================================================
209 //
210 // ACCESS OPERATORS
211 //
212 //=================================================================================================
213 
214 //*************************************************************************************************
219 template< typename T // Type of the resource
220  , typename D > // Type of the deleter
221 inline typename UniquePtr<T,D>::Reference UniquePtr<T,D>::operator*() const /* throw() */
222 {
223  BLAZE_USER_ASSERT( ptr_, "Uninitialized unique pointer" );
224  return *ptr_;
225 }
226 //*************************************************************************************************
227 
228 
229 //*************************************************************************************************
234 template< typename T // Type of the resource
235  , typename D > // Type of the deleter
236 inline typename UniquePtr<T,D>::Pointer UniquePtr<T,D>::operator->() const /* throw() */
237 {
238  BLAZE_USER_ASSERT( ptr_, "Uninitialized unique pointer" );
239  return ptr_;
240 }
241 //*************************************************************************************************
242 
243 
244 
245 
246 //=================================================================================================
247 //
248 // UTILITY FUNCTIONS
249 //
250 //=================================================================================================
251 
252 //*************************************************************************************************
260 template< typename T // Type of the resource
261  , typename D > // Type of the deleter
262 inline typename UniquePtr<T,D>::Pointer UniquePtr<T,D>::get() const /* throw() */
263 {
264  return ptr_;
265 }
266 //*************************************************************************************************
267 
268 
269 //*************************************************************************************************
277 template< typename T // Type of the resource
278  , typename D > // Type of the deleter
279 inline typename UniquePtr<T,D>::Pointer UniquePtr<T,D>::release() /* throw() */
280 {
281  Pointer tmp( ptr_ );
282  ptr_ = NULL;
283  return tmp;
284 }
285 //*************************************************************************************************
286 
287 
288 //*************************************************************************************************
294 template< typename T // Type of the resource
295  , typename D > // Type of the deleter
296 inline void UniquePtr<T,D>::reset( Pointer ptr ) /* throw() */
297 {
298  if( ptr != ptr_ ) {
299  UniquePtr( ptr ).swap( *this );
300  }
301 }
302 //*************************************************************************************************
303 
304 
305 //*************************************************************************************************
312 template< typename T // Type of the resource
313  , typename D > // Type of the deleter
314 inline void UniquePtr<T,D>::swap( UniquePtr& ptr ) /* throw() */
315 {
316  Pointer tmp( ptr_ );
317  ptr_ = ptr.ptr_;
318  ptr.ptr_ = tmp;
319 }
320 //*************************************************************************************************
321 
322 
323 
324 
325 //=================================================================================================
326 //
327 // CONVERSION OPERATOR
328 //
329 //=================================================================================================
330 
331 //*************************************************************************************************
336 template< typename T // Type of the resource
337  , typename D > // Type of the deleter
338 inline UniquePtr<T,D>::operator bool() const /* throw() */
339 {
340  return ( ptr_ != NULL );
341 }
342 //*************************************************************************************************
343 
344 
345 
346 
347 //=================================================================================================
348 //
349 // GLOBAL OPERATORS
350 //
351 //=================================================================================================
352 
353 //*************************************************************************************************
356 template< typename T1, typename D1, typename T2, typename D2 >
357 inline bool operator==( const UniquePtr<T1,D1>& lhs, const UniquePtr<T2,D2>& rhs );
358 
359 template< typename T1, typename D1, typename T2, typename D2 >
360 inline bool operator!=( const UniquePtr<T1,D1>& lhs, const UniquePtr<T2,D2>& rhs );
361 
362 template< typename T1, typename D1, typename T2, typename D2 >
363 inline bool operator<( const UniquePtr<T1,D1>& lhs, const UniquePtr<T2,D2>& rhs );
364 
365 template< typename T1, typename D1, typename T2, typename D2 >
366 inline bool operator<=( const UniquePtr<T1,D1>& lhs, const UniquePtr<T2,D2>& rhs );
367 
368 template< typename T1, typename D1, typename T2, typename D2 >
369 inline bool operator>( const UniquePtr<T1,D1>& lhs, const UniquePtr<T2,D2>& rhs );
370 
371 template< typename T1, typename D1, typename T2, typename D2 >
372 inline bool operator>=( const UniquePtr<T1,D1>& lhs, const UniquePtr<T2,D2>& rhs );
373 
374 template< typename T, typename D >
375 inline bool operator==( const UniquePtr<T,D>& ptr, const Null& null );
376 
377 template< typename T, typename D >
378 inline bool operator!=( const UniquePtr<T,D>& ptr, const Null& null );
379 
380 template< typename T, typename D >
381 inline bool operator<( const UniquePtr<T,D>& ptr, const Null& null );
382 
383 template< typename T, typename D >
384 inline bool operator>( const UniquePtr<T,D>& ptr, const Null& null );
385 
386 template< typename T, typename D >
387 inline bool operator<=( const UniquePtr<T,D>& ptr, const Null& null );
388 
389 template< typename T, typename D >
390 inline bool operator>=( const UniquePtr<T,D>& ptr, const Null& null );
391 
392 template< typename T, typename D >
393 inline bool operator==( const Null& null, const UniquePtr<T,D>& ptr );
394 
395 template< typename T, typename D >
396 inline bool operator!=( const Null& null, const UniquePtr<T,D>& ptr );
397 
398 template< typename T, typename D >
399 inline bool operator<( const Null& null, const UniquePtr<T,D>& ptr );
400 
401 template< typename T, typename D >
402 inline bool operator>( const Null& null, const UniquePtr<T,D>& ptr );
403 
404 template< typename T, typename D >
405 inline bool operator<=( const Null& null, const UniquePtr<T,D>& ptr );
406 
407 template< typename T, typename D >
408 inline bool operator>=( const Null& null, const UniquePtr<T,D>& ptr );
409 
410 template< typename T, typename D >
411 inline void swap( UniquePtr<T,D>& a, UniquePtr<T,D>& b ) /* throw() */;
413 //*************************************************************************************************
414 
415 
416 //*************************************************************************************************
423 template< typename T1 // Resource type of the left-hand side unique pointer
424  , typename D1 // Deleter type of the left-hand side unique pointer
425  , typename T2 // Resource type of the right-hand side unique pointer
426  , typename D2 > // Deleter type of the right-hand side unique pointer
427 inline bool operator==( const UniquePtr<T1,D1>& lhs, const UniquePtr<T2,D2>& rhs )
428 {
429  return lhs.get() == rhs.get();
430 }
431 //*************************************************************************************************
432 
433 
434 //*************************************************************************************************
441 template< typename T1 // Resource type of the left-hand side unique pointer
442  , typename D1 // Deleter type of the left-hand side unique pointer
443  , typename T2 // Resource type of the right-hand side unique pointer
444  , typename D2 > // Deleter type of the right-hand side unique pointer
445 inline bool operator!=( const UniquePtr<T1,D1>& lhs, const UniquePtr<T2,D2>& rhs )
446 {
447  return lhs.get() != rhs.get();
448 }
449 //*************************************************************************************************
450 
451 
452 //*************************************************************************************************
459 template< typename T1 // Resource type of the left-hand side unique pointer
460  , typename D1 // Deleter type of the left-hand side unique pointer
461  , typename T2 // Resource type of the right-hand side unique pointer
462  , typename D2 > // Deleter type of the right-hand side unique pointer
463 inline bool operator<( const UniquePtr<T1,D1>& lhs, const UniquePtr<T2,D2>& rhs )
464 {
465  return lhs.get() < rhs.get();
466 }
467 //*************************************************************************************************
468 
469 
470 //*************************************************************************************************
477 template< typename T1 // Resource type of the left-hand side unique pointer
478  , typename D1 // Deleter type of the left-hand side unique pointer
479  , typename T2 // Resource type of the right-hand side unique pointer
480  , typename D2 > // Deleter type of the right-hand side unique pointer
481 inline bool operator>( const UniquePtr<T1,D1>& lhs, const UniquePtr<T2,D2>& rhs )
482 {
483  return rhs < lhs;
484 }
485 //*************************************************************************************************
486 
487 
488 //*************************************************************************************************
495 template< typename T1 // Resource type of the left-hand side unique pointer
496  , typename D1 // Deleter type of the left-hand side unique pointer
497  , typename T2 // Resource type of the right-hand side unique pointer
498  , typename D2 > // Deleter type of the right-hand side unique pointer
499 inline bool operator<=( const UniquePtr<T1,D1>& lhs, const UniquePtr<T2,D2>& rhs )
500 {
501  return !( rhs < lhs );
502 }
503 //*************************************************************************************************
504 
505 
506 //*************************************************************************************************
513 template< typename T1 // Resource type of the left-hand side unique pointer
514  , typename D1 // Deleter type of the left-hand side unique pointer
515  , typename T2 // Resource type of the right-hand side unique pointer
516  , typename D2 > // Deleter type of the right-hand side unique pointer
517 inline bool operator>=( const UniquePtr<T1,D1>& lhs, const UniquePtr<T2,D2>& rhs )
518 {
519  return !( lhs < rhs );
520 }
521 //*************************************************************************************************
522 
523 
524 //*************************************************************************************************
531 template< typename T // Resource type of the unique pointer
532  , typename D > // Deleter type of the unique pointer
533 inline bool operator==( const UniquePtr<T,D>& ptr, const Null& null )
534 {
535  return ptr.get() == null;
536 }
537 //*************************************************************************************************
538 
539 
540 //*************************************************************************************************
547 template< typename T // Resource type of the unique pointer
548  , typename D > // Deleter type of the unique pointer
549 inline bool operator!=( const UniquePtr<T,D>& ptr, const Null& null )
550 {
551  return !( ptr == null );
552 }
553 //*************************************************************************************************
554 
555 
556 //*************************************************************************************************
563 template< typename T // Resource type of the unique pointer
564  , typename D > // Deleter type of the unique pointer
565 inline bool operator<( const UniquePtr<T,D>& ptr, const Null& null )
566 {
567  return ptr.get() < null;
568 }
569 //*************************************************************************************************
570 
571 
572 //*************************************************************************************************
579 template< typename T // Resource type of the unique pointer
580  , typename D > // Deleter type of the unique pointer
581 inline bool operator>( const UniquePtr<T,D>& ptr, const Null& null )
582 {
583  return ptr.get() > null;
584 }
585 //*************************************************************************************************
586 
587 
588 //*************************************************************************************************
595 template< typename T // Resource type of the unique pointer
596  , typename D > // Deleter type of the unique pointer
597 inline bool operator<=( const UniquePtr<T,D>& ptr, const Null& null )
598 {
599  return !( ptr > null );
600 }
601 //*************************************************************************************************
602 
603 
604 //*************************************************************************************************
611 template< typename T // Resource type of the unique pointer
612  , typename D > // Deleter type of the unique pointer
613 inline bool operator>=( const UniquePtr<T,D>& ptr, const Null& null )
614 {
615  return !( ptr < null );
616 }
617 //*************************************************************************************************
618 
619 
620 //*************************************************************************************************
627 template< typename T // Resource type of the unique pointer
628  , typename D > // Deleter type of the unique pointer
629 inline bool operator==( const Null& null, const UniquePtr<T,D>& ptr )
630 {
631  return ptr == null;
632 }
633 //*************************************************************************************************
634 
635 
636 //*************************************************************************************************
643 template< typename T // Resource type of the unique pointer
644  , typename D > // Deleter type of the unique pointer
645 inline bool operator!=( const Null& null, const UniquePtr<T,D>& ptr )
646 {
647  return ptr != null;
648 }
649 //*************************************************************************************************
650 
651 
652 //*************************************************************************************************
659 template< typename T // Resource type of the unique pointer
660  , typename D > // Deleter type of the unique pointer
661 inline bool operator<( const Null& null, const UniquePtr<T,D>& ptr )
662 {
663  return ptr > null;
664 }
665 //*************************************************************************************************
666 
667 
668 //*************************************************************************************************
675 template< typename T // Resource type of the unique pointer
676  , typename D > // Deleter type of the unique pointer
677 inline bool operator>( const Null& null, const UniquePtr<T,D>& ptr )
678 {
679  return ptr < null;
680 }
681 //*************************************************************************************************
682 
683 
684 //*************************************************************************************************
691 template< typename T // Resource type of the unique pointer
692  , typename D > // Deleter type of the unique pointer
693 inline bool operator<=( const Null& null, const UniquePtr<T,D>& ptr )
694 {
695  return ptr >= null;
696 }
697 //*************************************************************************************************
698 
699 
700 //*************************************************************************************************
707 template< typename T // Resource type of the unique pointer
708  , typename D > // Deleter type of the unique pointer
709 inline bool operator>=( const Null& null, const UniquePtr<T,D>& ptr )
710 {
711  return ptr <= null;
712 }
713 //*************************************************************************************************
714 
715 
716 //*************************************************************************************************
724 template< typename T // Resource type of the unique pointer
725  , typename D > // Deleter type of the unique pointer
726 inline void swap( UniquePtr<T,D>& a, UniquePtr<T,D>& b ) /* throw() */
727 {
728  a.swap( b );
729 }
730 //*************************************************************************************************
731 
732 } // namespace blaze
733 
734 #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.
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
Pointer get() const
Returns a pointer to the managed resource.
Definition: UniquePtr.h:262
Deleter deleter_
Resource deleter.
Definition: UniquePtr.h:151
Scope-limited management of dynamically allocated resourses.The UniquePtr class implements a scope-re...
Definition: UniquePtr.h:98
UniquePtr(Pointer ptr=NULL)
The default constructor for UniquePtr.
Definition: UniquePtr.h:179
Safe C++ NULL pointer implementation.This implementation offers a remedy for the use of the NULL poin...
Definition: Null.h:69
Header file for the PtrDelete policy classes.
Constraint on the data type.
Namespace of the Blaze C++ math library.
Definition: Blaze.h:57
RemoveReference< T >::Type & Reference
Reference type of the managed resource.
Definition: UniquePtr.h:103
Base class for non-copyable class instances.The NonCopyable class is intended to work as a base class...
Definition: NonCopyable.h:63
RemoveReference< T >::Type * Pointer
Pointer type of the managed resource.
Definition: UniquePtr.h:102
Reference operator*() const
Direct access to the managed resource.
Definition: UniquePtr.h:221
D Deleter
Type of the resource deleter.
Definition: UniquePtr.h:104
#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
Header file for run time assertion macros.
void reset(Pointer ptr=NULL)
Resets the unique pointer and replaces the managed resource with the given resource.
Definition: UniquePtr.h:296
Header file for the RemoveReference type trait.
Pointer release()
Releases the ownership of the managed resource to the caller.
Definition: UniquePtr.h:279
Pointer operator->() const
Direct access to the managed resource.
Definition: UniquePtr.h:236
bool operator==(const NegativeAccuracy< A > &lhs, const T &rhs)
Equality comparison between a NegativeAccuracy object and a floating point value. ...
Definition: Accuracy.h:249
Pointer ptr_
Pointer to the managed resource.
Definition: UniquePtr.h:150
bool operator!=(const NegativeAccuracy< A > &lhs, const T &rhs)
Inequality comparison between a NegativeAccuracy object and a floating point value.
Definition: Accuracy.h:289
~UniquePtr()
The destructor for UniquePtr.
Definition: UniquePtr.h:199
void swap(UniquePtr &up)
Swapping the contents of two unique pointers.
Definition: UniquePtr.h:314
Header file for a safe C++ NULL pointer implementation.