All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UniqueArray.h
Go to the documentation of this file.
1 //=================================================================================================
20 //=================================================================================================
21 
22 #ifndef _BLAZE_UTIL_UNIQUEARRAY_H_
23 #define _BLAZE_UTIL_UNIQUEARRAY_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 //*************************************************************************************************
82 template< typename T // Type of the array elements
83  , typename D = ArrayDelete > // Type of the deleter
84 class UniqueArray : private NonCopyable
85 {
86  public:
87  //**Type definitions****************************************************************************
88  typedef typename RemoveReference<T>::Type* Pointer;
89  typedef typename RemoveReference<T>::Type& Reference;
90  typedef D Deleter;
91  //**********************************************************************************************
92 
93  //**Constructors********************************************************************************
96  inline UniqueArray( Pointer ptr = NULL );
98  //**********************************************************************************************
99 
100  //**Destructor**********************************************************************************
103  inline ~UniqueArray();
105  //**********************************************************************************************
106 
107  //**Access operators****************************************************************************
110  inline Reference operator[]( size_t index ) const /* throw() */;
112  //**********************************************************************************************
113 
114  //**Utility functions***************************************************************************
117  inline Pointer get() const /* throw() */;
118  inline Pointer release() /* throw() */;
119  inline void reset( Pointer ptr = NULL ) /* throw() */;
120  inline void swap ( UniqueArray& up ) /* throw() */;
122  //**********************************************************************************************
123 
124  private:
125  //**Member variables****************************************************************************
130 
131  //**********************************************************************************************
132 
133  //**Compile time checks*************************************************************************
137  //**********************************************************************************************
138 };
139 //*************************************************************************************************
140 
141 
142 
143 
144 //=================================================================================================
145 //
146 // CONSTRUCTOR
147 //
148 //=================================================================================================
149 
150 //*************************************************************************************************
155 template< typename T // Type of the array elements
156  , typename D > // Type of the deleter
158  : ptr_ ( ptr ) // Pointer to the managed array
159  , deleter_( Deleter() ) // Resource deleter
160 {}
161 //*************************************************************************************************
162 
163 
164 
165 
166 //=================================================================================================
167 //
168 // DESTRUCTOR
169 //
170 //=================================================================================================
171 
172 //*************************************************************************************************
175 template< typename T // Type of the array elements
176  , typename D > // Type of the deleter
178 {
179  deleter_( ptr_ );
180 }
181 //*************************************************************************************************
182 
183 
184 
185 
186 //=================================================================================================
187 //
188 // ACCESS OPERATORS
189 //
190 //=================================================================================================
191 
192 //*************************************************************************************************
197 template< typename T // Type of the array elements
198  , typename D > // Type of the deleter
199 inline typename UniqueArray<T,D>::Reference UniqueArray<T,D>::operator[]( size_t index ) const /* throw() */
200 {
201  BLAZE_USER_ASSERT( ptr_, "Uninitialized unique pointer" );
202  return ptr_[index];
203 }
204 //*************************************************************************************************
205 
206 
207 
208 
209 //=================================================================================================
210 //
211 // UTILITY FUNCTIONS
212 //
213 //=================================================================================================
214 
215 //*************************************************************************************************
223 template< typename T // Type of the array elements
224  , typename D > // Type of the deleter
225 inline typename UniqueArray<T,D>::Pointer UniqueArray<T,D>::get() const /* throw() */
226 {
227  return ptr_;
228 }
229 //*************************************************************************************************
230 
231 
232 //*************************************************************************************************
240 template< typename T // Type of the array elements
241  , typename D > // Type of the deleter
243 {
244  Pointer tmp( ptr_ );
245  ptr_ = NULL;
246  return tmp;
247 }
248 //*************************************************************************************************
249 
250 
251 //*************************************************************************************************
257 template< typename T // Type of the array elements
258  , typename D > // Type of the deleter
259 inline void UniqueArray<T,D>::reset( Pointer ptr ) /* throw() */
260 {
261  if( ptr != ptr_ ) {
262  UniqueArray( ptr ).swap( *this );
263  }
264 }
265 //*************************************************************************************************
266 
267 
268 //*************************************************************************************************
275 template< typename T // Type of the array elements
276  , typename D > // Type of the deleter
277 inline void UniqueArray<T,D>::swap( UniqueArray& ptr ) /* throw() */
278 {
279  Pointer tmp( ptr_ );
280  ptr_ = ptr.ptr_;
281  ptr.ptr_ = tmp;
282 }
283 //*************************************************************************************************
284 
285 
286 
287 
288 //=================================================================================================
289 //
290 // GLOBAL OPERATORS
291 //
292 //=================================================================================================
293 
294 //*************************************************************************************************
297 template< typename T1, typename D1, typename T2, typename D2 >
298 inline bool operator==( const UniqueArray<T1,D1>& lhs, const UniqueArray<T2,D2>& rhs );
299 
300 template< typename T1, typename D1, typename T2, typename D2 >
301 inline bool operator!=( const UniqueArray<T1,D1>& lhs, const UniqueArray<T2,D2>& rhs );
302 
303 template< typename T1, typename D1, typename T2, typename D2 >
304 inline bool operator<( const UniqueArray<T1,D1>& lhs, const UniqueArray<T2,D2>& rhs );
305 
306 template< typename T1, typename D1, typename T2, typename D2 >
307 inline bool operator<=( const UniqueArray<T1,D1>& lhs, const UniqueArray<T2,D2>& rhs );
308 
309 template< typename T1, typename D1, typename T2, typename D2 >
310 inline bool operator>( const UniqueArray<T1,D1>& lhs, const UniqueArray<T2,D2>& rhs );
311 
312 template< typename T1, typename D1, typename T2, typename D2 >
313 inline bool operator>=( const UniqueArray<T1,D1>& lhs, const UniqueArray<T2,D2>& rhs );
314 
315 template< typename T, typename D >
316 inline bool operator==( const UniqueArray<T,D>& ptr, const Null& null );
317 
318 template< typename T, typename D >
319 inline bool operator!=( const UniqueArray<T,D>& ptr, const Null& null );
320 
321 template< typename T, typename D >
322 inline bool operator<( const UniqueArray<T,D>& ptr, const Null& null );
323 
324 template< typename T, typename D >
325 inline bool operator>( const UniqueArray<T,D>& ptr, const Null& null );
326 
327 template< typename T, typename D >
328 inline bool operator<=( const UniqueArray<T,D>& ptr, const Null& null );
329 
330 template< typename T, typename D >
331 inline bool operator>=( const UniqueArray<T,D>& ptr, const Null& null );
332 
333 template< typename T, typename D >
334 inline bool operator==( const Null& null, const UniqueArray<T,D>& ptr );
335 
336 template< typename T, typename D >
337 inline bool operator!=( const Null& null, const UniqueArray<T,D>& ptr );
338 
339 template< typename T, typename D >
340 inline bool operator<( const Null& null, const UniqueArray<T,D>& ptr );
341 
342 template< typename T, typename D >
343 inline bool operator>( const Null& null, const UniqueArray<T,D>& ptr );
344 
345 template< typename T, typename D >
346 inline bool operator<=( const Null& null, const UniqueArray<T,D>& ptr );
347 
348 template< typename T, typename D >
349 inline bool operator>=( const Null& null, const UniqueArray<T,D>& ptr );
350 
351 template< typename T, typename D >
352 inline void swap( UniqueArray<T,D>& a, UniqueArray<T,D>& b ) /* throw() */;
354 //*************************************************************************************************
355 
356 
357 //*************************************************************************************************
364 template< typename T1 // Resource type of the left-hand side unique array
365  , typename D1 // Deleter type of the left-hand side unique array
366  , typename T2 // Resource type of the right-hand side unique array
367  , typename D2 > // Deleter type of the right-hand side unique array
368 inline bool operator==( const UniqueArray<T1,D1>& lhs, const UniqueArray<T2,D2>& rhs )
369 {
370  return lhs.get() == rhs.get();
371 }
372 //*************************************************************************************************
373 
374 
375 //*************************************************************************************************
382 template< typename T1 // Resource type of the left-hand side unique array
383  , typename D1 // Deleter type of the left-hand side unique array
384  , typename T2 // Resource type of the right-hand side unique array
385  , typename D2 > // Deleter type of the right-hand side unique array
386 inline bool operator!=( const UniqueArray<T1,D1>& lhs, const UniqueArray<T2,D2>& rhs )
387 {
388  return lhs.get() != rhs.get();
389 }
390 //*************************************************************************************************
391 
392 
393 //*************************************************************************************************
400 template< typename T1 // Resource type of the left-hand side unique array
401  , typename D1 // Deleter type of the left-hand side unique array
402  , typename T2 // Resource type of the right-hand side unique array
403  , typename D2 > // Deleter type of the right-hand side unique array
404 inline bool operator<( const UniqueArray<T1,D1>& lhs, const UniqueArray<T2,D2>& rhs )
405 {
406  return lhs.get() < rhs.get();
407 }
408 //*************************************************************************************************
409 
410 
411 //*************************************************************************************************
418 template< typename T1 // Resource type of the left-hand side unique array
419  , typename D1 // Deleter type of the left-hand side unique array
420  , typename T2 // Resource type of the right-hand side unique array
421  , typename D2 > // Deleter type of the right-hand side unique array
422 inline bool operator>( const UniqueArray<T1,D1>& lhs, const UniqueArray<T2,D2>& rhs )
423 {
424  return rhs < lhs;
425 }
426 //*************************************************************************************************
427 
428 
429 //*************************************************************************************************
436 template< typename T1 // Resource type of the left-hand side unique array
437  , typename D1 // Deleter type of the left-hand side unique array
438  , typename T2 // Resource type of the right-hand side unique array
439  , typename D2 > // Deleter type of the right-hand side unique array
440 inline bool operator<=( const UniqueArray<T1,D1>& lhs, const UniqueArray<T2,D2>& rhs )
441 {
442  return !( rhs < lhs );
443 }
444 //*************************************************************************************************
445 
446 
447 //*************************************************************************************************
454 template< typename T1 // Resource type of the left-hand side unique array
455  , typename D1 // Deleter type of the left-hand side unique array
456  , typename T2 // Resource type of the right-hand side unique array
457  , typename D2 > // Deleter type of the right-hand side unique array
458 inline bool operator>=( const UniqueArray<T1,D1>& lhs, const UniqueArray<T2,D2>& rhs )
459 {
460  return !( lhs < rhs );
461 }
462 //*************************************************************************************************
463 
464 
465 //*************************************************************************************************
472 template< typename T // Resource type of the unique array
473  , typename D > // Deleter type of the unique array
474 inline bool operator==( const UniqueArray<T,D>& ptr, const Null& null )
475 {
476  return ptr.get() == null;
477 }
478 //*************************************************************************************************
479 
480 
481 //*************************************************************************************************
488 template< typename T // Resource type of the unique array
489  , typename D > // Deleter type of the unique array
490 inline bool operator!=( const UniqueArray<T,D>& ptr, const Null& null )
491 {
492  return !( ptr == null );
493 }
494 //*************************************************************************************************
495 
496 
497 //*************************************************************************************************
504 template< typename T // Resource type of the unique array
505  , typename D > // Deleter type of the unique array
506 inline bool operator<( const UniqueArray<T,D>& ptr, const Null& null )
507 {
508  return ptr.get() < null;
509 }
510 //*************************************************************************************************
511 
512 
513 //*************************************************************************************************
520 template< typename T // Resource type of the unique array
521  , typename D > // Deleter type of the unique array
522 inline bool operator>( const UniqueArray<T,D>& ptr, const Null& null )
523 {
524  return ptr.get() > null;
525 }
526 //*************************************************************************************************
527 
528 
529 //*************************************************************************************************
536 template< typename T // Resource type of the unique array
537  , typename D > // Deleter type of the unique array
538 inline bool operator<=( const UniqueArray<T,D>& ptr, const Null& null )
539 {
540  return !( ptr > null );
541 }
542 //*************************************************************************************************
543 
544 
545 //*************************************************************************************************
552 template< typename T // Resource type of the unique array
553  , typename D > // Deleter type of the unique array
554 inline bool operator>=( const UniqueArray<T,D>& ptr, const Null& null )
555 {
556  return !( ptr < null );
557 }
558 //*************************************************************************************************
559 
560 
561 //*************************************************************************************************
568 template< typename T // Resource type of the unique array
569  , typename D > // Deleter type of the unique array
570 inline bool operator==( const Null& null, const UniqueArray<T,D>& ptr )
571 {
572  return ptr == null;
573 }
574 //*************************************************************************************************
575 
576 
577 //*************************************************************************************************
584 template< typename T // Resource type of the unique array
585  , typename D > // Deleter type of the unique array
586 inline bool operator!=( const Null& null, const UniqueArray<T,D>& ptr )
587 {
588  return ptr != null;
589 }
590 //*************************************************************************************************
591 
592 
593 //*************************************************************************************************
600 template< typename T // Resource type of the unique array
601  , typename D > // Deleter type of the unique array
602 inline bool operator<( const Null& null, const UniqueArray<T,D>& ptr )
603 {
604  return ptr > null;
605 }
606 //*************************************************************************************************
607 
608 
609 //*************************************************************************************************
616 template< typename T // Resource type of the unique array
617  , typename D > // Deleter type of the unique array
618 inline bool operator>( const Null& null, const UniqueArray<T,D>& ptr )
619 {
620  return ptr < null;
621 }
622 //*************************************************************************************************
623 
624 
625 //*************************************************************************************************
632 template< typename T // Resource type of the unique array
633  , typename D > // Deleter type of the unique array
634 inline bool operator<=( const Null& null, const UniqueArray<T,D>& ptr )
635 {
636  return ptr >= null;
637 }
638 //*************************************************************************************************
639 
640 
641 //*************************************************************************************************
648 template< typename T // Resource type of the unique array
649  , typename D > // Deleter type of the unique array
650 inline bool operator>=( const Null& null, const UniqueArray<T,D>& ptr )
651 {
652  return ptr <= null;
653 }
654 //*************************************************************************************************
655 
656 
657 //*************************************************************************************************
665 template< typename T // Resource type of the unique array
666  , typename D > // Deleter type of the unique array
667 inline void swap( UniqueArray<T,D>& a, UniqueArray<T,D>& b ) /* throw() */
668 {
669  a.swap( b );
670 }
671 //*************************************************************************************************
672 
673 } // namespace blaze
674 
675 #endif