All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UniquePtr.h
Go to the documentation of this file.
1 //=================================================================================================
20 //=================================================================================================
21 
22 #ifndef _BLAZE_UTIL_UNIQUEPTR_H_
23 #define _BLAZE_UTIL_UNIQUEPTR_H_
24 
25 
26 //*************************************************************************************************
27 // Includes
28 //*************************************************************************************************
29 
30 #include <blaze/util/Assert.h>
32 #include <blaze/util/NonCopyable.h>
33 #include <blaze/util/Null.h>
35 #include <blaze/util/Types.h>
37 
38 
39 namespace blaze {
40 
41 //=================================================================================================
42 //
43 // CLASS DEFINITION
44 //
45 //=================================================================================================
46 
47 //*************************************************************************************************
83 template< typename T // Type of the resource
84  , typename D = PtrDelete > // Type of the deleter
85 class UniquePtr : private NonCopyable
86 {
87  public:
88  //**Type definitions****************************************************************************
89  typedef typename RemoveReference<T>::Type* Pointer;
90  typedef typename RemoveReference<T>::Type& Reference;
91  typedef D Deleter;
92  //**********************************************************************************************
93 
94  //**Constructors********************************************************************************
97  inline UniquePtr( Pointer ptr = NULL );
99  //**********************************************************************************************
100 
101  //**Destructor**********************************************************************************
104  inline ~UniquePtr();
106  //**********************************************************************************************
107 
108  //**Access operators****************************************************************************
111  inline Reference operator* () const /* throw() */;
112  inline Pointer operator->() const /* throw() */;
114  //**********************************************************************************************
115 
116  //**Utility functions***************************************************************************
119  inline Pointer get() const /* throw() */;
120  inline Pointer release() /* throw() */;
121  inline void reset( Pointer ptr = NULL ) /* throw() */;
122  inline void swap ( UniquePtr& up ) /* throw() */;
124  //**********************************************************************************************
125 
126  private:
127  //**Member variables****************************************************************************
132 
133  //**********************************************************************************************
134 
135  //**Compile time checks*************************************************************************
139  //**********************************************************************************************
140 };
141 //*************************************************************************************************
142 
143 
144 
145 
146 //=================================================================================================
147 //
148 // CONSTRUCTOR
149 //
150 //=================================================================================================
151 
152 //*************************************************************************************************
157 template< typename T // Type of the resource
158  , typename D > // Type of the deleter
159 inline UniquePtr<T,D>::UniquePtr( Pointer ptr )
160  : ptr_ ( ptr ) // Pointer to the managed resource
161  , deleter_( Deleter() ) // Resource deleter.
162 {}
163 //*************************************************************************************************
164 
165 
166 
167 
168 //=================================================================================================
169 //
170 // DESTRUCTOR
171 //
172 //=================================================================================================
173 
174 //*************************************************************************************************
177 template< typename T // Type of the resource
178  , typename D > // Type of the deleter
180 {
181  deleter_( ptr_ );
182 }
183 //*************************************************************************************************
184 
185 
186 
187 
188 //=================================================================================================
189 //
190 // ACCESS OPERATORS
191 //
192 //=================================================================================================
193 
194 //*************************************************************************************************
199 template< typename T // Type of the resource
200  , typename D > // Type of the deleter
201 inline typename UniquePtr<T,D>::Reference UniquePtr<T,D>::operator*() const /* throw() */
202 {
203  BLAZE_USER_ASSERT( ptr_, "Uninitialized unique pointer" );
204  return *ptr_;
205 }
206 //*************************************************************************************************
207 
208 
209 //*************************************************************************************************
214 template< typename T // Type of the resource
215  , typename D > // Type of the deleter
216 inline typename UniquePtr<T,D>::Pointer UniquePtr<T,D>::operator->() const /* throw() */
217 {
218  BLAZE_USER_ASSERT( ptr_, "Uninitialized unique pointer" );
219  return ptr_;
220 }
221 //*************************************************************************************************
222 
223 
224 
225 
226 //=================================================================================================
227 //
228 // UTILITY FUNCTIONS
229 //
230 //=================================================================================================
231 
232 //*************************************************************************************************
240 template< typename T // Type of the resource
241  , typename D > // Type of the deleter
242 inline typename UniquePtr<T,D>::Pointer UniquePtr<T,D>::get() const /* throw() */
243 {
244  return ptr_;
245 }
246 //*************************************************************************************************
247 
248 
249 //*************************************************************************************************
257 template< typename T // Type of the resource
258  , typename D > // Type of the deleter
259 inline typename UniquePtr<T,D>::Pointer UniquePtr<T,D>::release() /* throw() */
260 {
261  Pointer tmp( ptr_ );
262  ptr_ = NULL;
263  return tmp;
264 }
265 //*************************************************************************************************
266 
267 
268 //*************************************************************************************************
274 template< typename T // Type of the resource
275  , typename D > // Type of the deleter
276 inline void UniquePtr<T,D>::reset( Pointer ptr ) /* throw() */
277 {
278  if( ptr != ptr_ ) {
279  UniquePtr( ptr ).swap( *this );
280  }
281 }
282 //*************************************************************************************************
283 
284 
285 //*************************************************************************************************
292 template< typename T // Type of the resource
293  , typename D > // Type of the deleter
294 inline void UniquePtr<T,D>::swap( UniquePtr& ptr ) /* throw() */
295 {
296  Pointer tmp( ptr_ );
297  ptr_ = ptr.ptr_;
298  ptr.ptr_ = tmp;
299 }
300 //*************************************************************************************************
301 
302 
303 
304 
305 //=================================================================================================
306 //
307 // GLOBAL OPERATORS
308 //
309 //=================================================================================================
310 
311 //*************************************************************************************************
314 template< typename T1, typename D1, typename T2, typename D2 >
315 inline bool operator==( const UniquePtr<T1,D1>& lhs, const UniquePtr<T2,D2>& rhs );
316 
317 template< typename T1, typename D1, typename T2, typename D2 >
318 inline bool operator!=( const UniquePtr<T1,D1>& lhs, const UniquePtr<T2,D2>& rhs );
319 
320 template< typename T1, typename D1, typename T2, typename D2 >
321 inline bool operator<( const UniquePtr<T1,D1>& lhs, const UniquePtr<T2,D2>& rhs );
322 
323 template< typename T1, typename D1, typename T2, typename D2 >
324 inline bool operator<=( const UniquePtr<T1,D1>& lhs, const UniquePtr<T2,D2>& rhs );
325 
326 template< typename T1, typename D1, typename T2, typename D2 >
327 inline bool operator>( const UniquePtr<T1,D1>& lhs, const UniquePtr<T2,D2>& rhs );
328 
329 template< typename T1, typename D1, typename T2, typename D2 >
330 inline bool operator>=( const UniquePtr<T1,D1>& lhs, const UniquePtr<T2,D2>& rhs );
331 
332 template< typename T, typename D >
333 inline bool operator==( const UniquePtr<T,D>& ptr, const Null& null );
334 
335 template< typename T, typename D >
336 inline bool operator!=( const UniquePtr<T,D>& ptr, const Null& null );
337 
338 template< typename T, typename D >
339 inline bool operator<( const UniquePtr<T,D>& ptr, const Null& null );
340 
341 template< typename T, typename D >
342 inline bool operator>( const UniquePtr<T,D>& ptr, const Null& null );
343 
344 template< typename T, typename D >
345 inline bool operator<=( const UniquePtr<T,D>& ptr, const Null& null );
346 
347 template< typename T, typename D >
348 inline bool operator>=( const UniquePtr<T,D>& ptr, const Null& null );
349 
350 template< typename T, typename D >
351 inline bool operator==( const Null& null, const UniquePtr<T,D>& ptr );
352 
353 template< typename T, typename D >
354 inline bool operator!=( const Null& null, const UniquePtr<T,D>& ptr );
355 
356 template< typename T, typename D >
357 inline bool operator<( const Null& null, const UniquePtr<T,D>& ptr );
358 
359 template< typename T, typename D >
360 inline bool operator>( const Null& null, const UniquePtr<T,D>& ptr );
361 
362 template< typename T, typename D >
363 inline bool operator<=( const Null& null, const UniquePtr<T,D>& ptr );
364 
365 template< typename T, typename D >
366 inline bool operator>=( const Null& null, const UniquePtr<T,D>& ptr );
367 
368 template< typename T, typename D >
369 inline void swap( UniquePtr<T,D>& a, UniquePtr<T,D>& b ) /* throw() */;
371 //*************************************************************************************************
372 
373 
374 //*************************************************************************************************
381 template< typename T1 // Resource type of the left-hand side unique pointer
382  , typename D1 // Deleter type of the left-hand side unique pointer
383  , typename T2 // Resource type of the right-hand side unique pointer
384  , typename D2 > // Deleter type of the right-hand side unique pointer
385 inline bool operator==( const UniquePtr<T1,D1>& lhs, const UniquePtr<T2,D2>& rhs )
386 {
387  return lhs.get() == rhs.get();
388 }
389 //*************************************************************************************************
390 
391 
392 //*************************************************************************************************
399 template< typename T1 // Resource type of the left-hand side unique pointer
400  , typename D1 // Deleter type of the left-hand side unique pointer
401  , typename T2 // Resource type of the right-hand side unique pointer
402  , typename D2 > // Deleter type of the right-hand side unique pointer
403 inline bool operator!=( const UniquePtr<T1,D1>& lhs, const UniquePtr<T2,D2>& rhs )
404 {
405  return lhs.get() != rhs.get();
406 }
407 //*************************************************************************************************
408 
409 
410 //*************************************************************************************************
417 template< typename T1 // Resource type of the left-hand side unique pointer
418  , typename D1 // Deleter type of the left-hand side unique pointer
419  , typename T2 // Resource type of the right-hand side unique pointer
420  , typename D2 > // Deleter type of the right-hand side unique pointer
421 inline bool operator<( const UniquePtr<T1,D1>& lhs, const UniquePtr<T2,D2>& rhs )
422 {
423  return lhs.get() < rhs.get();
424 }
425 //*************************************************************************************************
426 
427 
428 //*************************************************************************************************
435 template< typename T1 // Resource type of the left-hand side unique pointer
436  , typename D1 // Deleter type of the left-hand side unique pointer
437  , typename T2 // Resource type of the right-hand side unique pointer
438  , typename D2 > // Deleter type of the right-hand side unique pointer
439 inline bool operator>( const UniquePtr<T1,D1>& lhs, const UniquePtr<T2,D2>& rhs )
440 {
441  return rhs < lhs;
442 }
443 //*************************************************************************************************
444 
445 
446 //*************************************************************************************************
453 template< typename T1 // Resource type of the left-hand side unique pointer
454  , typename D1 // Deleter type of the left-hand side unique pointer
455  , typename T2 // Resource type of the right-hand side unique pointer
456  , typename D2 > // Deleter type of the right-hand side unique pointer
457 inline bool operator<=( const UniquePtr<T1,D1>& lhs, const UniquePtr<T2,D2>& rhs )
458 {
459  return !( rhs < lhs );
460 }
461 //*************************************************************************************************
462 
463 
464 //*************************************************************************************************
471 template< typename T1 // Resource type of the left-hand side unique pointer
472  , typename D1 // Deleter type of the left-hand side unique pointer
473  , typename T2 // Resource type of the right-hand side unique pointer
474  , typename D2 > // Deleter type of the right-hand side unique pointer
475 inline bool operator>=( const UniquePtr<T1,D1>& lhs, const UniquePtr<T2,D2>& rhs )
476 {
477  return !( lhs < rhs );
478 }
479 //*************************************************************************************************
480 
481 
482 //*************************************************************************************************
489 template< typename T // Resource type of the unique pointer
490  , typename D > // Deleter type of the unique pointer
491 inline bool operator==( const UniquePtr<T,D>& ptr, const Null& null )
492 {
493  return ptr.get() == null;
494 }
495 //*************************************************************************************************
496 
497 
498 //*************************************************************************************************
505 template< typename T // Resource type of the unique pointer
506  , typename D > // Deleter type of the unique pointer
507 inline bool operator!=( const UniquePtr<T,D>& ptr, const Null& null )
508 {
509  return !( ptr == null );
510 }
511 //*************************************************************************************************
512 
513 
514 //*************************************************************************************************
521 template< typename T // Resource type of the unique pointer
522  , typename D > // Deleter type of the unique pointer
523 inline bool operator<( const UniquePtr<T,D>& ptr, const Null& null )
524 {
525  return ptr.get() < null;
526 }
527 //*************************************************************************************************
528 
529 
530 //*************************************************************************************************
537 template< typename T // Resource type of the unique pointer
538  , typename D > // Deleter type of the unique pointer
539 inline bool operator>( const UniquePtr<T,D>& ptr, const Null& null )
540 {
541  return ptr.get() > null;
542 }
543 //*************************************************************************************************
544 
545 
546 //*************************************************************************************************
553 template< typename T // Resource type of the unique pointer
554  , typename D > // Deleter type of the unique pointer
555 inline bool operator<=( const UniquePtr<T,D>& ptr, const Null& null )
556 {
557  return !( ptr > null );
558 }
559 //*************************************************************************************************
560 
561 
562 //*************************************************************************************************
569 template< typename T // Resource type of the unique pointer
570  , typename D > // Deleter type of the unique pointer
571 inline bool operator>=( const UniquePtr<T,D>& ptr, const Null& null )
572 {
573  return !( ptr < null );
574 }
575 //*************************************************************************************************
576 
577 
578 //*************************************************************************************************
585 template< typename T // Resource type of the unique pointer
586  , typename D > // Deleter type of the unique pointer
587 inline bool operator==( const Null& null, const UniquePtr<T,D>& ptr )
588 {
589  return ptr == null;
590 }
591 //*************************************************************************************************
592 
593 
594 //*************************************************************************************************
601 template< typename T // Resource type of the unique pointer
602  , typename D > // Deleter type of the unique pointer
603 inline bool operator!=( const Null& null, const UniquePtr<T,D>& ptr )
604 {
605  return ptr != null;
606 }
607 //*************************************************************************************************
608 
609 
610 //*************************************************************************************************
617 template< typename T // Resource type of the unique pointer
618  , typename D > // Deleter type of the unique pointer
619 inline bool operator<( const Null& null, const UniquePtr<T,D>& ptr )
620 {
621  return ptr > null;
622 }
623 //*************************************************************************************************
624 
625 
626 //*************************************************************************************************
633 template< typename T // Resource type of the unique pointer
634  , typename D > // Deleter type of the unique pointer
635 inline bool operator>( const Null& null, const UniquePtr<T,D>& ptr )
636 {
637  return ptr < null;
638 }
639 //*************************************************************************************************
640 
641 
642 //*************************************************************************************************
649 template< typename T // Resource type of the unique pointer
650  , typename D > // Deleter type of the unique pointer
651 inline bool operator<=( const Null& null, const UniquePtr<T,D>& ptr )
652 {
653  return ptr >= null;
654 }
655 //*************************************************************************************************
656 
657 
658 //*************************************************************************************************
665 template< typename T // Resource type of the unique pointer
666  , typename D > // Deleter type of the unique pointer
667 inline bool operator>=( const Null& null, const UniquePtr<T,D>& ptr )
668 {
669  return ptr <= null;
670 }
671 //*************************************************************************************************
672 
673 
674 //*************************************************************************************************
682 template< typename T // Resource type of the unique pointer
683  , typename D > // Deleter type of the unique pointer
684 inline void swap( UniquePtr<T,D>& a, UniquePtr<T,D>& b ) /* throw() */
685 {
686  a.swap( b );
687 }
688 //*************************************************************************************************
689 
690 } // namespace blaze
691 
692 #endif