PCL
AutoPointer.h
Go to the documentation of this file.
1 // ____ ______ __
2 // / __ \ / ____// /
3 // / /_/ // / / /
4 // / ____// /___ / /___ PixInsight Class Library
5 // /_/ \____//_____/ PCL 2.8.5
6 // ----------------------------------------------------------------------------
7 // pcl/AutoPointer.h - Released 2024-12-28T16:53:48Z
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 #ifdef __PCL_QT_INTERFACE
63 # include <QtCore/QObject>
64 # include <QtGui/QAction>
65 #endif
66 
67 namespace pcl
68 {
69 
70 // ----------------------------------------------------------------------------
71 
99 template <typename T>
100 class PCL_CLASS StandardDeleter
101 {
102 public:
103 
107  using value_type = T;
108 
112  using pointer = T*;
113 
118  void operator()( pointer p ) const
119  {
120  PCL_PRECONDITION( p != nullptr )
121  delete p;
122  }
123 };
124 
125 // ----------------------------------------------------------------------------
126 
244 template <class T, class D = StandardDeleter<T>>
245 class PCL_CLASS AutoPointer
246 {
247 public:
248 
252  using value_type = T;
253 
257  using pointer = T*;
258 
262  using const_pointer = const T*;
263 
267  using deleter = D;
268 
287  AutoPointer( bool autoDelete = true, const deleter& d = deleter() )
288  : m_deleter( d )
289  , m_autoDelete( autoDelete )
290  {
291  }
292 
310  AutoPointer( pointer p, bool autoDelete = true, const deleter& d = deleter() )
311  : m_deleter( d )
312  , m_autoDelete( autoDelete )
313  {
314  m_pointer = p;
315  }
316 
332  : m_deleter( x.m_deleter )
333  , m_autoDelete( x.m_autoDelete )
334  {
335  m_pointer = x.Release();
336  }
337 
342  : m_deleter( std::move( x.m_deleter ) )
343  , m_autoDelete( x.m_autoDelete )
344  {
345  m_pointer = x.Release();
346  }
347 
355  virtual ~AutoPointer()
356  {
357  Reset();
358  }
359 
367  void SetPointer( pointer p )
368  {
369  if ( m_pointer != p )
370  {
371  if ( m_autoDelete )
372  if ( m_pointer != nullptr )
373  m_deleter( Release() ); // in case m_deleter throws
374  m_pointer = p;
375  }
376  }
377 
387  void Reset()
388  {
389  if ( m_pointer != nullptr )
390  {
391  pointer p = Release(); // in case m_deleter throws
392  if ( m_autoDelete )
393  m_deleter( p );
394  }
395  }
396 
401  void Destroy()
402  {
403  Reset();
404  }
405 
416  {
417  pointer p = m_pointer;
418  m_pointer = nullptr;
419  return p;
420  }
421 
427  {
428  return m_pointer;
429  }
430 
435  {
436  return m_pointer;
437  }
438 
443  {
444  return m_pointer;
445  }
446 
451  {
452  return m_pointer;
453  }
454 
458  bool IsNull() const
459  {
460  return m_pointer == nullptr;
461  }
462 
467  bool IsValid() const
468  {
469  return !IsNull();
470  }
471 
488  bool IsAutoDelete() const
489  {
490  return m_autoDelete;
491  }
492 
499  void EnableAutoDelete( bool enable = true )
500  {
501  m_autoDelete = enable;
502  }
503 
510  void DisableAutoDelete( bool disable = true )
511  {
512  EnableAutoDelete( !disable );
513  }
514 
518  const deleter& Deleter() const
519  {
520  return m_deleter;
521  }
522 
527  {
528  return m_deleter;
529  }
530 
551  AutoPointer& operator =( AutoPointer& x )
552  {
553  SetPointer( x.Release() );
554  m_deleter = x.m_deleter;
555  m_autoDelete = x.m_autoDelete;
556  return *this;
557  }
558 
563  AutoPointer& operator =( AutoPointer&& x )
564  {
565  SetPointer( x.Release() );
566  m_deleter = std::move( x.m_deleter );
567  m_autoDelete = x.m_autoDelete;
568  return *this;
569  }
570 
588  AutoPointer& operator =( pointer p )
589  {
590  SetPointer( p );
591  return *this;
592  }
593 
599  operator const_pointer() const
600  {
601  return m_pointer;
602  }
603 
609  operator pointer()
610  {
611  return m_pointer;
612  }
613 
618  const_pointer operator ->() const
619  {
620  PCL_PRECONDITION( m_pointer != nullptr )
621  return m_pointer;
622  }
623 
628  pointer operator ->()
629  {
630  PCL_PRECONDITION( m_pointer != nullptr )
631  return m_pointer;
632  }
633 
638  const value_type& operator *() const
639  {
640  PCL_PRECONDITION( m_pointer != nullptr )
641  return *m_pointer;
642  }
643 
649  {
650  PCL_PRECONDITION( m_pointer != nullptr )
651  return *m_pointer;
652  }
653 
658  operator bool() const
659  {
660  return !IsNull();
661  }
662 
666  friend void Swap( AutoPointer& x1, AutoPointer& x2 )
667  {
668  pcl::Swap( x1.m_pointer, x2.m_pointer );
669  pcl::Swap( x1.m_deleter, x2.m_deleter );
670  bool b = x1.m_autoDelete; x1.m_autoDelete = x2.m_autoDelete; x2.m_autoDelete = b;
671  }
672 
673 protected:
674 
675  pointer m_pointer = nullptr;
676  deleter m_deleter;
677  bool m_autoDelete = true;
678 };
679 
680 // ----------------------------------------------------------------------------
681 
682 #ifdef __PCL_QT_INTERFACE
683 
694 template <class Q>
695 class PCL_CLASS QObjectDeferredDeleter
696 {
697 public:
698 
702  using value_type = Q;
703 
707  using pointer = Q*;
708 
713  void operator()( pointer p ) const
714  {
715  PCL_PRECONDITION( p != nullptr )
716  if ( p != nullptr )
717  p->deleteLater();
718  }
719 };
720 
728 template <class Q>
729 using AutoQObjectPointer = AutoPointer<Q, QObjectDeferredDeleter<Q>>;
730 
731 using AutoQActionPointer = AutoQObjectPointer<QAction>;
732 
733 #endif // __PCL_QT_INTERFACE
734 
735 // ----------------------------------------------------------------------------
736 
737 #define ASSERT_COPIABLE_T() \
738  static_assert( std::is_copy_constructible<T>::value, "AutoPointerCloner<> requires a copy-constructible type." )
739 
754 template <class T, class D = StandardDeleter<T> >
755 class PCL_CLASS AutoPointerCloner : public AutoPointer<T,D>
756 {
757 public:
758 
759  using base_type = AutoPointer<T,D>;
760 
765 
769  using pointer = typename base_type::pointer;
770 
775 
779  using deleter = typename base_type::deleter;
780 
799  AutoPointerCloner( bool autoDelete = true, const deleter& d = deleter() )
800  : base_type( autoDelete, d )
801  {
802  ASSERT_COPIABLE_T();
803  }
804 
822  AutoPointerCloner( pointer p, bool autoDelete = true, const deleter& d = deleter() )
823  : base_type( p, autoDelete, d )
824  {
825  ASSERT_COPIABLE_T();
826  }
827 
841  : base_type( nullptr )
842  {
843  ASSERT_COPIABLE_T();
844  this->m_pointer = x ? new value_type( *x ) : nullptr;
845  this->m_deleter = x.m_deleter;
846  this->m_autoDelete = x || x.m_autoDelete;
847  }
848 
862  : base_type( nullptr )
863  {
864  ASSERT_COPIABLE_T();
865  this->m_pointer = x ? new value_type( *x ) : nullptr;
866  this->m_deleter = x.m_deleter;
867  this->m_autoDelete = x || x.m_autoDelete;
868  }
869 
874  : base_type( std::move( x ) )
875  {
876  }
877 
899  AutoPointerCloner& operator =( const base_type& x )
900  {
901  this->SetPointer( x ? new value_type( *x ) : nullptr );
902  this->m_deleter = x.m_deleter;
903  this->m_autoDelete = x || x.m_autoDelete;
904  return *this;
905  }
906 
928  AutoPointerCloner& operator =( const AutoPointerCloner& x )
929  {
930  this->SetPointer( x ? new value_type( *x ) : nullptr );
931  this->m_deleter = x.m_deleter;
932  this->m_autoDelete = x || x.m_autoDelete;
933  return *this;
934  }
935 
940  AutoPointerCloner& operator =( base_type&& x )
941  {
942  (void)base_type::operator =( std::move( x ) );
943  return *this;
944  }
945 
955  AutoPointerCloner& operator =( pointer p )
956  {
957  this->SetPointer( p );
958  return *this;
959  }
960 };
961 
962 #undef ASSERT_COPIABLE_T
963 
964 // ----------------------------------------------------------------------------
965 
966 } // pcl
967 
968 #endif // __PCL_AutoPointer_h
969 
970 // ----------------------------------------------------------------------------
971 // EOF pcl/AutoPointer.h - Released 2024-12-28T16:53:48Z
A smart pointer able to generate dynamically allocated copies of the objects pointed to by other smar...
Definition: AutoPointer.h:756
typename base_type::pointer pointer
Definition: AutoPointer.h:769
AutoPointerCloner(pointer p, bool autoDelete=true, const deleter &d=deleter())
Definition: AutoPointer.h:822
typename base_type::deleter deleter
Definition: AutoPointer.h:779
typename base_type::value_type value_type
Definition: AutoPointer.h:764
typename base_type::const_pointer const_pointer
Definition: AutoPointer.h:774
AutoPointerCloner(bool autoDelete=true, const deleter &d=deleter())
Definition: AutoPointer.h:799
AutoPointerCloner(const AutoPointerCloner &x)
Definition: AutoPointer.h:861
AutoPointerCloner(const base_type &x)
Definition: AutoPointer.h:840
AutoPointerCloner(base_type &&x)
Definition: AutoPointer.h:873
A smart pointer with exclusive object ownership and optional automatic object destruction.
Definition: AutoPointer.h:246
AutoPointer(bool autoDelete=true, const deleter &d=deleter())
Definition: AutoPointer.h:287
const_pointer Ptr() const
Definition: AutoPointer.h:442
void SetPointer(pointer p)
Definition: AutoPointer.h:367
void DisableAutoDelete(bool disable=true)
Definition: AutoPointer.h:510
friend void Swap(AutoPointer &x1, AutoPointer &x2)
Definition: AutoPointer.h:666
bool IsNull() const
Definition: AutoPointer.h:458
AutoPointer(pointer p, bool autoDelete=true, const deleter &d=deleter())
Definition: AutoPointer.h:310
const_pointer Pointer() const
Definition: AutoPointer.h:426
AutoPointer(AutoPointer &&x)
Definition: AutoPointer.h:341
deleter & Deleter()
Definition: AutoPointer.h:526
const deleter & Deleter() const
Definition: AutoPointer.h:518
AutoPointer(AutoPointer &x)
Definition: AutoPointer.h:331
virtual ~AutoPointer()
Definition: AutoPointer.h:355
const T * const_pointer
Definition: AutoPointer.h:262
pointer Release()
Definition: AutoPointer.h:415
pointer Pointer()
Definition: AutoPointer.h:434
bool IsAutoDelete() const
Definition: AutoPointer.h:488
bool IsValid() const
Definition: AutoPointer.h:467
void EnableAutoDelete(bool enable=true)
Definition: AutoPointer.h:499
An object deleter that uses the standard delete operator.
Definition: AutoPointer.h:101
void operator()(pointer p) const
Definition: AutoPointer.h:118
Complex< T1 > operator*(const Complex< T1 > &c1, const Complex< T2 > &c2) noexcept
Definition: Complex.h:562
void Swap(GenericPoint< T > &p1, GenericPoint< T > &p2) noexcept
Definition: Point.h:1459
PCL root namespace.
Definition: AbstractImage.h:77