All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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  private:
140  //**Member variables****************************************************************************
145 
146  //**********************************************************************************************
147 
148  //**Compile time checks*************************************************************************
152  //**********************************************************************************************
153 };
154 //*************************************************************************************************
155 
156 
157 
158 
159 //=================================================================================================
160 //
161 // CONSTRUCTOR
162 //
163 //=================================================================================================
164 
165 //*************************************************************************************************
170 template< typename T // Type of the resource
171  , typename D > // Type of the deleter
172 inline UniquePtr<T,D>::UniquePtr( Pointer ptr )
173  : ptr_ ( ptr ) // Pointer to the managed resource
174  , deleter_( Deleter() ) // Resource deleter.
175 {}
176 //*************************************************************************************************
177 
178 
179 
180 
181 //=================================================================================================
182 //
183 // DESTRUCTOR
184 //
185 //=================================================================================================
186 
187 //*************************************************************************************************
190 template< typename T // Type of the resource
191  , typename D > // Type of the deleter
193 {
194  deleter_( ptr_ );
195 }
196 //*************************************************************************************************
197 
198 
199 
200 
201 //=================================================================================================
202 //
203 // ACCESS OPERATORS
204 //
205 //=================================================================================================
206 
207 //*************************************************************************************************
212 template< typename T // Type of the resource
213  , typename D > // Type of the deleter
214 inline typename UniquePtr<T,D>::Reference UniquePtr<T,D>::operator*() const /* throw() */
215 {
216  BLAZE_USER_ASSERT( ptr_, "Uninitialized unique pointer" );
217  return *ptr_;
218 }
219 //*************************************************************************************************
220 
221 
222 //*************************************************************************************************
227 template< typename T // Type of the resource
228  , typename D > // Type of the deleter
229 inline typename UniquePtr<T,D>::Pointer UniquePtr<T,D>::operator->() const /* throw() */
230 {
231  BLAZE_USER_ASSERT( ptr_, "Uninitialized unique pointer" );
232  return ptr_;
233 }
234 //*************************************************************************************************
235 
236 
237 
238 
239 //=================================================================================================
240 //
241 // UTILITY FUNCTIONS
242 //
243 //=================================================================================================
244 
245 //*************************************************************************************************
253 template< typename T // Type of the resource
254  , typename D > // Type of the deleter
255 inline typename UniquePtr<T,D>::Pointer UniquePtr<T,D>::get() const /* throw() */
256 {
257  return ptr_;
258 }
259 //*************************************************************************************************
260 
261 
262 //*************************************************************************************************
270 template< typename T // Type of the resource
271  , typename D > // Type of the deleter
272 inline typename UniquePtr<T,D>::Pointer UniquePtr<T,D>::release() /* throw() */
273 {
274  Pointer tmp( ptr_ );
275  ptr_ = NULL;
276  return tmp;
277 }
278 //*************************************************************************************************
279 
280 
281 //*************************************************************************************************
287 template< typename T // Type of the resource
288  , typename D > // Type of the deleter
289 inline void UniquePtr<T,D>::reset( Pointer ptr ) /* throw() */
290 {
291  if( ptr != ptr_ ) {
292  UniquePtr( ptr ).swap( *this );
293  }
294 }
295 //*************************************************************************************************
296 
297 
298 //*************************************************************************************************
305 template< typename T // Type of the resource
306  , typename D > // Type of the deleter
307 inline void UniquePtr<T,D>::swap( UniquePtr& ptr ) /* throw() */
308 {
309  Pointer tmp( ptr_ );
310  ptr_ = ptr.ptr_;
311  ptr.ptr_ = tmp;
312 }
313 //*************************************************************************************************
314 
315 
316 
317 
318 //=================================================================================================
319 //
320 // GLOBAL OPERATORS
321 //
322 //=================================================================================================
323 
324 //*************************************************************************************************
327 template< typename T1, typename D1, typename T2, typename D2 >
328 inline bool operator==( const UniquePtr<T1,D1>& lhs, const UniquePtr<T2,D2>& rhs );
329 
330 template< typename T1, typename D1, typename T2, typename D2 >
331 inline bool operator!=( const UniquePtr<T1,D1>& lhs, const UniquePtr<T2,D2>& rhs );
332 
333 template< typename T1, typename D1, typename T2, typename D2 >
334 inline bool operator<( const UniquePtr<T1,D1>& lhs, const UniquePtr<T2,D2>& rhs );
335 
336 template< typename T1, typename D1, typename T2, typename D2 >
337 inline bool operator<=( const UniquePtr<T1,D1>& lhs, const UniquePtr<T2,D2>& rhs );
338 
339 template< typename T1, typename D1, typename T2, typename D2 >
340 inline bool operator>( const UniquePtr<T1,D1>& lhs, const UniquePtr<T2,D2>& rhs );
341 
342 template< typename T1, typename D1, typename T2, typename D2 >
343 inline bool operator>=( const UniquePtr<T1,D1>& lhs, const UniquePtr<T2,D2>& rhs );
344 
345 template< typename T, typename D >
346 inline bool operator==( const UniquePtr<T,D>& ptr, const Null& null );
347 
348 template< typename T, typename D >
349 inline bool operator!=( const UniquePtr<T,D>& ptr, const Null& null );
350 
351 template< typename T, typename D >
352 inline bool operator<( const UniquePtr<T,D>& ptr, const Null& null );
353 
354 template< typename T, typename D >
355 inline bool operator>( const UniquePtr<T,D>& ptr, const Null& null );
356 
357 template< typename T, typename D >
358 inline bool operator<=( const UniquePtr<T,D>& ptr, const Null& null );
359 
360 template< typename T, typename D >
361 inline bool operator>=( const UniquePtr<T,D>& ptr, const Null& null );
362 
363 template< typename T, typename D >
364 inline bool operator==( const Null& null, const UniquePtr<T,D>& ptr );
365 
366 template< typename T, typename D >
367 inline bool operator!=( const Null& null, const UniquePtr<T,D>& ptr );
368 
369 template< typename T, typename D >
370 inline bool operator<( const Null& null, const UniquePtr<T,D>& ptr );
371 
372 template< typename T, typename D >
373 inline bool operator>( const Null& null, const UniquePtr<T,D>& ptr );
374 
375 template< typename T, typename D >
376 inline bool operator<=( const Null& null, const UniquePtr<T,D>& ptr );
377 
378 template< typename T, typename D >
379 inline bool operator>=( const Null& null, const UniquePtr<T,D>& ptr );
380 
381 template< typename T, typename D >
382 inline void swap( UniquePtr<T,D>& a, UniquePtr<T,D>& b ) /* throw() */;
384 //*************************************************************************************************
385 
386 
387 //*************************************************************************************************
394 template< typename T1 // Resource type of the left-hand side unique pointer
395  , typename D1 // Deleter type of the left-hand side unique pointer
396  , typename T2 // Resource type of the right-hand side unique pointer
397  , typename D2 > // Deleter type of the right-hand side unique pointer
398 inline bool operator==( const UniquePtr<T1,D1>& lhs, const UniquePtr<T2,D2>& rhs )
399 {
400  return lhs.get() == rhs.get();
401 }
402 //*************************************************************************************************
403 
404 
405 //*************************************************************************************************
412 template< typename T1 // Resource type of the left-hand side unique pointer
413  , typename D1 // Deleter type of the left-hand side unique pointer
414  , typename T2 // Resource type of the right-hand side unique pointer
415  , typename D2 > // Deleter type of the right-hand side unique pointer
416 inline bool operator!=( const UniquePtr<T1,D1>& lhs, const UniquePtr<T2,D2>& rhs )
417 {
418  return lhs.get() != rhs.get();
419 }
420 //*************************************************************************************************
421 
422 
423 //*************************************************************************************************
430 template< typename T1 // Resource type of the left-hand side unique pointer
431  , typename D1 // Deleter type of the left-hand side unique pointer
432  , typename T2 // Resource type of the right-hand side unique pointer
433  , typename D2 > // Deleter type of the right-hand side unique pointer
434 inline bool operator<( const UniquePtr<T1,D1>& lhs, const UniquePtr<T2,D2>& rhs )
435 {
436  return lhs.get() < rhs.get();
437 }
438 //*************************************************************************************************
439 
440 
441 //*************************************************************************************************
448 template< typename T1 // Resource type of the left-hand side unique pointer
449  , typename D1 // Deleter type of the left-hand side unique pointer
450  , typename T2 // Resource type of the right-hand side unique pointer
451  , typename D2 > // Deleter type of the right-hand side unique pointer
452 inline bool operator>( const UniquePtr<T1,D1>& lhs, const UniquePtr<T2,D2>& rhs )
453 {
454  return rhs < lhs;
455 }
456 //*************************************************************************************************
457 
458 
459 //*************************************************************************************************
466 template< typename T1 // Resource type of the left-hand side unique pointer
467  , typename D1 // Deleter type of the left-hand side unique pointer
468  , typename T2 // Resource type of the right-hand side unique pointer
469  , typename D2 > // Deleter type of the right-hand side unique pointer
470 inline bool operator<=( const UniquePtr<T1,D1>& lhs, const UniquePtr<T2,D2>& rhs )
471 {
472  return !( rhs < lhs );
473 }
474 //*************************************************************************************************
475 
476 
477 //*************************************************************************************************
484 template< typename T1 // Resource type of the left-hand side unique pointer
485  , typename D1 // Deleter type of the left-hand side unique pointer
486  , typename T2 // Resource type of the right-hand side unique pointer
487  , typename D2 > // Deleter type of the right-hand side unique pointer
488 inline bool operator>=( const UniquePtr<T1,D1>& lhs, const UniquePtr<T2,D2>& rhs )
489 {
490  return !( lhs < rhs );
491 }
492 //*************************************************************************************************
493 
494 
495 //*************************************************************************************************
502 template< typename T // Resource type of the unique pointer
503  , typename D > // Deleter type of the unique pointer
504 inline bool operator==( const UniquePtr<T,D>& ptr, const Null& null )
505 {
506  return ptr.get() == null;
507 }
508 //*************************************************************************************************
509 
510 
511 //*************************************************************************************************
518 template< typename T // Resource type of the unique pointer
519  , typename D > // Deleter type of the unique pointer
520 inline bool operator!=( const UniquePtr<T,D>& ptr, const Null& null )
521 {
522  return !( ptr == null );
523 }
524 //*************************************************************************************************
525 
526 
527 //*************************************************************************************************
534 template< typename T // Resource type of the unique pointer
535  , typename D > // Deleter type of the unique pointer
536 inline bool operator<( const UniquePtr<T,D>& ptr, const Null& null )
537 {
538  return ptr.get() < null;
539 }
540 //*************************************************************************************************
541 
542 
543 //*************************************************************************************************
550 template< typename T // Resource type of the unique pointer
551  , typename D > // Deleter type of the unique pointer
552 inline bool operator>( const UniquePtr<T,D>& ptr, const Null& null )
553 {
554  return ptr.get() > null;
555 }
556 //*************************************************************************************************
557 
558 
559 //*************************************************************************************************
566 template< typename T // Resource type of the unique pointer
567  , typename D > // Deleter type of the unique pointer
568 inline bool operator<=( const UniquePtr<T,D>& ptr, const Null& null )
569 {
570  return !( ptr > null );
571 }
572 //*************************************************************************************************
573 
574 
575 //*************************************************************************************************
582 template< typename T // Resource type of the unique pointer
583  , typename D > // Deleter type of the unique pointer
584 inline bool operator>=( const UniquePtr<T,D>& ptr, const Null& null )
585 {
586  return !( ptr < null );
587 }
588 //*************************************************************************************************
589 
590 
591 //*************************************************************************************************
598 template< typename T // Resource type of the unique pointer
599  , typename D > // Deleter type of the unique pointer
600 inline bool operator==( const Null& null, const UniquePtr<T,D>& ptr )
601 {
602  return ptr == null;
603 }
604 //*************************************************************************************************
605 
606 
607 //*************************************************************************************************
614 template< typename T // Resource type of the unique pointer
615  , typename D > // Deleter type of the unique pointer
616 inline bool operator!=( const Null& null, const UniquePtr<T,D>& ptr )
617 {
618  return ptr != null;
619 }
620 //*************************************************************************************************
621 
622 
623 //*************************************************************************************************
630 template< typename T // Resource type of the unique pointer
631  , typename D > // Deleter type of the unique pointer
632 inline bool operator<( const Null& null, const UniquePtr<T,D>& ptr )
633 {
634  return ptr > null;
635 }
636 //*************************************************************************************************
637 
638 
639 //*************************************************************************************************
646 template< typename T // Resource type of the unique pointer
647  , typename D > // Deleter type of the unique pointer
648 inline bool operator>( const Null& null, const UniquePtr<T,D>& ptr )
649 {
650  return ptr < null;
651 }
652 //*************************************************************************************************
653 
654 
655 //*************************************************************************************************
662 template< typename T // Resource type of the unique pointer
663  , typename D > // Deleter type of the unique pointer
664 inline bool operator<=( const Null& null, const UniquePtr<T,D>& ptr )
665 {
666  return ptr >= null;
667 }
668 //*************************************************************************************************
669 
670 
671 //*************************************************************************************************
678 template< typename T // Resource type of the unique pointer
679  , typename D > // Deleter type of the unique pointer
680 inline bool operator>=( const Null& null, const UniquePtr<T,D>& ptr )
681 {
682  return ptr <= null;
683 }
684 //*************************************************************************************************
685 
686 
687 //*************************************************************************************************
695 template< typename T // Resource type of the unique pointer
696  , typename D > // Deleter type of the unique pointer
697 inline void swap( UniquePtr<T,D>& a, UniquePtr<T,D>& b ) /* throw() */
698 {
699  a.swap( b );
700 }
701 //*************************************************************************************************
702 
703 } // namespace blaze
704 
705 #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
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:255
Deleter deleter_
Resource deleter.
Definition: UniquePtr.h:144
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:172
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.
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:214
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:289
Header file for the RemoveReference type trait.
Pointer release()
Releases the ownership of the managed resource to the caller.
Definition: UniquePtr.h:272
Pointer operator->() const
Direct access to the managed resource.
Definition: UniquePtr.h:229
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:143
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:192
Header file for basic type definitions.
void swap(UniquePtr &up)
Swapping the contents of two unique pointers.
Definition: UniquePtr.h:307
Header file for a safe C++ NULL pointer implementation.