PCL
AutoPointer.h
Go to the documentation of this file.
1 // ____ ______ __
2 // / __ \ / ____// /
3 // / /_/ // / / /
4 // / ____// /___ / /___ PixInsight Class Library
5 // /_/ \____//_____/ PCL 2.7.0
6 // ----------------------------------------------------------------------------
7 // pcl/AutoPointer.h - Released 2024-06-18T15:48:54Z
8 // ----------------------------------------------------------------------------
9 // This file is part of the PixInsight Class Library (PCL).
10 // PCL is a multiplatform C++ framework for development of PixInsight modules.
11 //
12 // Copyright (c) 2003-2024 Pleiades Astrophoto S.L. All Rights Reserved.
13 //
14 // Redistribution and use in both source and binary forms, with or without
15 // modification, is permitted provided that the following conditions are met:
16 //
17 // 1. All redistributions of source code must retain the above copyright
18 // notice, this list of conditions and the following disclaimer.
19 //
20 // 2. All redistributions in binary form must reproduce the above copyright
21 // notice, this list of conditions and the following disclaimer in the
22 // documentation and/or other materials provided with the distribution.
23 //
24 // 3. Neither the names "PixInsight" and "Pleiades Astrophoto", nor the names
25 // of their contributors, may be used to endorse or promote products derived
26 // from this software without specific prior written permission. For written
27 // permission, please contact info@pixinsight.com.
28 //
29 // 4. All products derived from this software, in any form whatsoever, must
30 // reproduce the following acknowledgment in the end-user documentation
31 // and/or other materials provided with the product:
32 //
33 // "This product is based on software from the PixInsight project, developed
34 // by Pleiades Astrophoto and its contributors (https://pixinsight.com/)."
35 //
36 // Alternatively, if that is where third-party acknowledgments normally
37 // appear, this acknowledgment must be reproduced in the product itself.
38 //
39 // THIS SOFTWARE IS PROVIDED BY PLEIADES ASTROPHOTO AND ITS CONTRIBUTORS
40 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
41 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PLEIADES ASTROPHOTO OR ITS
43 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
44 // EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, BUSINESS
45 // INTERRUPTION; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; AND LOSS OF USE,
46 // DATA OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49 // POSSIBILITY OF SUCH DAMAGE.
50 // ----------------------------------------------------------------------------
51 
52 #ifndef __PCL_AutoPointer_h
53 #define __PCL_AutoPointer_h
54 
56 
57 #include <pcl/Defs.h>
58 #include <pcl/Diagnostics.h>
59 
60 #include <pcl/Utility.h>
61 
62 namespace pcl
63 {
64 
65 // ----------------------------------------------------------------------------
66 
94 template <typename T>
95 class PCL_CLASS StandardDeleter
96 {
97 public:
98 
102  using value_type = T;
103 
107  using pointer = T*;
108 
113  void operator()( pointer p ) const
114  {
115  PCL_PRECONDITION( p != nullptr )
116  delete p;
117  }
118 };
119 
120 // ----------------------------------------------------------------------------
121 
239 template <class T, class D = StandardDeleter<T> >
240 class PCL_CLASS AutoPointer
241 {
242 public:
243 
247  using value_type = T;
248 
252  using pointer = T*;
253 
257  using const_pointer = const T*;
258 
262  using deleter = D;
263 
282  AutoPointer( bool autoDelete = true, const deleter& d = deleter() )
283  : m_deleter( d )
284  , m_autoDelete( autoDelete )
285  {
286  }
287 
305  AutoPointer( pointer p, bool autoDelete = true, const deleter& d = deleter() )
306  : m_deleter( d )
307  , m_autoDelete( autoDelete )
308  {
309  m_pointer = p;
310  }
311 
327  : m_deleter( x.m_deleter )
328  , m_autoDelete( x.m_autoDelete )
329  {
330  m_pointer = x.Release();
331  }
332 
337  : m_deleter( std::move( x.m_deleter ) )
338  , m_autoDelete( x.m_autoDelete )
339  {
340  m_pointer = x.Release();
341  }
342 
350  virtual ~AutoPointer()
351  {
352  Reset();
353  }
354 
362  void SetPointer( pointer p )
363  {
364  if ( m_pointer != p )
365  {
366  if ( m_autoDelete )
367  if ( m_pointer != nullptr )
368  m_deleter( Release() ); // in case m_deleter throws
369  m_pointer = p;
370  }
371  }
372 
382  void Reset()
383  {
384  if ( m_pointer != nullptr )
385  {
386  pointer p = Release(); // in case m_deleter throws
387  if ( m_autoDelete )
388  m_deleter( p );
389  }
390  }
391 
396  void Destroy()
397  {
398  Reset();
399  }
400 
411  {
412  pointer p = m_pointer;
413  m_pointer = nullptr;
414  return p;
415  }
416 
422  {
423  return m_pointer;
424  }
425 
430  {
431  return m_pointer;
432  }
433 
438  {
439  return m_pointer;
440  }
441 
446  {
447  return m_pointer;
448  }
449 
453  bool IsNull() const
454  {
455  return m_pointer == nullptr;
456  }
457 
462  bool IsValid() const
463  {
464  return !IsNull();
465  }
466 
483  bool IsAutoDelete() const
484  {
485  return m_autoDelete;
486  }
487 
494  void EnableAutoDelete( bool enable = true )
495  {
496  m_autoDelete = enable;
497  }
498 
505  void DisableAutoDelete( bool disable = true )
506  {
507  EnableAutoDelete( !disable );
508  }
509 
513  const deleter& Deleter() const
514  {
515  return m_deleter;
516  }
517 
522  {
523  return m_deleter;
524  }
525 
546  AutoPointer& operator =( AutoPointer& x )
547  {
548  SetPointer( x.Release() );
549  m_deleter = x.m_deleter;
550  m_autoDelete = x.m_autoDelete;
551  return *this;
552  }
553 
558  AutoPointer& operator =( AutoPointer&& x )
559  {
560  SetPointer( x.Release() );
561  m_deleter = std::move( x.m_deleter );
562  m_autoDelete = x.m_autoDelete;
563  return *this;
564  }
565 
583  AutoPointer& operator =( pointer p )
584  {
585  SetPointer( p );
586  return *this;
587  }
588 
594  operator const_pointer() const
595  {
596  return m_pointer;
597  }
598 
604  operator pointer()
605  {
606  return m_pointer;
607  }
608 
613  const_pointer operator ->() const
614  {
615  PCL_PRECONDITION( m_pointer != nullptr )
616  return m_pointer;
617  }
618 
623  pointer operator ->()
624  {
625  PCL_PRECONDITION( m_pointer != nullptr )
626  return m_pointer;
627  }
628 
633  const value_type& operator *() const
634  {
635  PCL_PRECONDITION( m_pointer != nullptr )
636  return *m_pointer;
637  }
638 
644  {
645  PCL_PRECONDITION( m_pointer != nullptr )
646  return *m_pointer;
647  }
648 
653  operator bool() const
654  {
655  return !IsNull();
656  }
657 
661  friend void Swap( AutoPointer& x1, AutoPointer& x2 )
662  {
663  pcl::Swap( x1.m_pointer, x2.m_pointer );
664  pcl::Swap( x1.m_deleter, x2.m_deleter );
665  bool b = x1.m_autoDelete; x1.m_autoDelete = x2.m_autoDelete; x2.m_autoDelete = b;
666  }
667 
668 protected:
669 
670  pointer m_pointer = nullptr;
671  deleter m_deleter;
672  bool m_autoDelete = true;
673 };
674 
675 // ----------------------------------------------------------------------------
676 
677 #define ASSERT_COPIABLE_T() \
678  static_assert( std::is_copy_constructible<T>::value, "AutoPointerCloner<> requires a copy-constructible type." )
679 
694 template <class T, class D = StandardDeleter<T> >
695 class PCL_CLASS AutoPointerCloner : public AutoPointer<T,D>
696 {
697 public:
698 
699  using base_type = AutoPointer<T,D>;
700 
705 
709  using pointer = typename base_type::pointer;
710 
715 
719  using deleter = typename base_type::deleter;
720 
739  AutoPointerCloner( bool autoDelete = true, const deleter& d = deleter() )
740  : base_type( autoDelete, d )
741  {
742  ASSERT_COPIABLE_T();
743  }
744 
762  AutoPointerCloner( pointer p, bool autoDelete = true, const deleter& d = deleter() )
763  : base_type( p, autoDelete, d )
764  {
765  ASSERT_COPIABLE_T();
766  }
767 
781  : base_type( nullptr )
782  {
783  ASSERT_COPIABLE_T();
784  this->m_pointer = x ? new value_type( *x ) : nullptr;
785  this->m_deleter = x.m_deleter;
786  this->m_autoDelete = x || x.m_autoDelete;
787  }
788 
802  : base_type( nullptr )
803  {
804  ASSERT_COPIABLE_T();
805  this->m_pointer = x ? new value_type( *x ) : nullptr;
806  this->m_deleter = x.m_deleter;
807  this->m_autoDelete = x || x.m_autoDelete;
808  }
809 
814  : base_type( std::move( x ) )
815  {
816  }
817 
839  AutoPointerCloner& operator =( const base_type& x )
840  {
841  this->SetPointer( x ? new value_type( *x ) : nullptr );
842  this->m_deleter = x.m_deleter;
843  this->m_autoDelete = x || x.m_autoDelete;
844  return *this;
845  }
846 
868  AutoPointerCloner& operator =( const AutoPointerCloner& x )
869  {
870  this->SetPointer( x ? new value_type( *x ) : nullptr );
871  this->m_deleter = x.m_deleter;
872  this->m_autoDelete = x || x.m_autoDelete;
873  return *this;
874  }
875 
880  AutoPointerCloner& operator =( base_type&& x )
881  {
882  (void)base_type::operator =( std::move( x ) );
883  return *this;
884  }
885 
895  AutoPointerCloner& operator =( pointer p )
896  {
897  this->SetPointer( p );
898  return *this;
899  }
900 };
901 
902 #undef ASSERT_COPIABLE_T
903 
904 // ----------------------------------------------------------------------------
905 
906 } // pcl
907 
908 #endif // __PCL_AutoPointer_h
909 
910 // ----------------------------------------------------------------------------
911 // EOF pcl/AutoPointer.h - Released 2024-06-18T15:48:54Z
A smart pointer able to generate dynamically allocated copies of the objects pointed to by other smar...
Definition: AutoPointer.h:696
typename base_type::pointer pointer
Definition: AutoPointer.h:709
AutoPointerCloner(pointer p, bool autoDelete=true, const deleter &d=deleter())
Definition: AutoPointer.h:762
typename base_type::deleter deleter
Definition: AutoPointer.h:719
typename base_type::value_type value_type
Definition: AutoPointer.h:704
typename base_type::const_pointer const_pointer
Definition: AutoPointer.h:714
AutoPointerCloner(bool autoDelete=true, const deleter &d=deleter())
Definition: AutoPointer.h:739
AutoPointerCloner(const AutoPointerCloner &x)
Definition: AutoPointer.h:801
AutoPointerCloner(const base_type &x)
Definition: AutoPointer.h:780
AutoPointerCloner(base_type &&x)
Definition: AutoPointer.h:813
A smart pointer with exclusive object ownership and optional automatic object destruction.
Definition: AutoPointer.h:241
AutoPointer(bool autoDelete=true, const deleter &d=deleter())
Definition: AutoPointer.h:282
const_pointer Ptr() const
Definition: AutoPointer.h:437
void SetPointer(pointer p)
Definition: AutoPointer.h:362
void DisableAutoDelete(bool disable=true)
Definition: AutoPointer.h:505
friend void Swap(AutoPointer &x1, AutoPointer &x2)
Definition: AutoPointer.h:661
bool IsNull() const
Definition: AutoPointer.h:453
AutoPointer(pointer p, bool autoDelete=true, const deleter &d=deleter())
Definition: AutoPointer.h:305
const_pointer Pointer() const
Definition: AutoPointer.h:421
AutoPointer(AutoPointer &&x)
Definition: AutoPointer.h:336
deleter & Deleter()
Definition: AutoPointer.h:521
const deleter & Deleter() const
Definition: AutoPointer.h:513
AutoPointer(AutoPointer &x)
Definition: AutoPointer.h:326
virtual ~AutoPointer()
Definition: AutoPointer.h:350
const T * const_pointer
Definition: AutoPointer.h:257
pointer Release()
Definition: AutoPointer.h:410
pointer Pointer()
Definition: AutoPointer.h:429
bool IsAutoDelete() const
Definition: AutoPointer.h:483
bool IsValid() const
Definition: AutoPointer.h:462
void EnableAutoDelete(bool enable=true)
Definition: AutoPointer.h:494
An object deleter that uses the standard delete operator.
Definition: AutoPointer.h:96
void operator()(pointer p) const
Definition: AutoPointer.h:113
Complex< T1 > operator*(const Complex< T1 > &c1, const Complex< T2 > &c2) noexcept
Definition: Complex.h:548
void Swap(GenericPoint< T > &p1, GenericPoint< T > &p2) noexcept
Definition: Point.h:1459
PCL root namespace.
Definition: AbstractImage.h:77