PCL
AutoPointer.h
Go to the documentation of this file.
1 // ____ ______ __
2 // / __ \ / ____// /
3 // / /_/ // / / /
4 // / ____// /___ / /___ PixInsight Class Library
5 // /_/ \____//_____/ PCL 2.6.5
6 // ----------------------------------------------------------------------------
7 // pcl/AutoPointer.h - Released 2024-01-13T15:47:58Z
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-01-13T15:47:58Z
pcl
PCL root namespace.
Definition: AbstractImage.h:76
pcl::AutoPointer::Destroy
void Destroy()
Definition: AutoPointer.h:396
pcl::AutoPointer::Deleter
const deleter & Deleter() const
Definition: AutoPointer.h:513
pcl::AutoPointer::SetPointer
void SetPointer(pointer p)
Definition: AutoPointer.h:362
pcl::AutoPointer::Release
pointer Release()
Definition: AutoPointer.h:410
Utility.h
pcl::AutoPointer::EnableAutoDelete
void EnableAutoDelete(bool enable=true)
Definition: AutoPointer.h:494
pcl::AutoPointer::Ptr
const_pointer Ptr() const
Definition: AutoPointer.h:437
pcl::StandardDeleter::operator()
void operator()(pointer p) const
Definition: AutoPointer.h:113
pcl::AutoPointer::~AutoPointer
virtual ~AutoPointer()
Definition: AutoPointer.h:350
pcl::AutoPointer::IsValid
bool IsValid() const
Definition: AutoPointer.h:462
pcl::AutoPointerCloner::AutoPointerCloner
AutoPointerCloner(pointer p, bool autoDelete=true, const deleter &d=deleter())
Definition: AutoPointer.h:762
pcl::AutoPointer< OpenFileDialogPrivate >::const_pointer
const OpenFileDialogPrivate * const_pointer
Definition: AutoPointer.h:257
pcl::EphemerisFile::Handle
Calculation of ephemerides from data stored in XEPH files.
Definition: EphemerisFile.h:1674
pcl::AutoPointer
A smart pointer with exclusive object ownership and optional automatic object destruction.
Definition: AutoPointer.h:240
pcl::AutoPointerCloner::AutoPointerCloner
AutoPointerCloner(const AutoPointerCloner &x)
Definition: AutoPointer.h:801
pcl::AutoPointer::IsNull
bool IsNull() const
Definition: AutoPointer.h:453
pcl::AutoPointer::Swap
friend void Swap(AutoPointer &x1, AutoPointer &x2)
Definition: AutoPointer.h:661
pcl::AutoPointerCloner::AutoPointerCloner
AutoPointerCloner(base_type &&x)
Definition: AutoPointer.h:813
pcl::StandardDeleter< OpenFileDialogPrivate >::value_type
OpenFileDialogPrivate value_type
Definition: AutoPointer.h:102
pcl::AutoPointer::Pointer
const_pointer Pointer() const
Definition: AutoPointer.h:421
pcl::AutoPointer::AutoPointer
AutoPointer(pointer p, bool autoDelete=true, const deleter &d=deleter())
Definition: AutoPointer.h:305
pcl::AutoPointerCloner::AutoPointerCloner
AutoPointerCloner(const base_type &x)
Definition: AutoPointer.h:780
pcl::AutoPointer::Pointer
pointer Pointer()
Definition: AutoPointer.h:429
pcl::AutoPointer< OpenFileDialogPrivate >::value_type
OpenFileDialogPrivate value_type
Definition: AutoPointer.h:247
pcl::AutoPointer< OpenFileDialogPrivate >::pointer
OpenFileDialogPrivate * pointer
Definition: AutoPointer.h:252
pcl::AutoPointer::Reset
void Reset()
Definition: AutoPointer.h:382
pcl::Swap
void Swap(GenericPoint< T > &p1, GenericPoint< T > &p2) noexcept
Definition: Point.h:1459
pcl::AutoPointer::IsAutoDelete
bool IsAutoDelete() const
Definition: AutoPointer.h:483
pcl::AutoPointer::AutoPointer
AutoPointer(AutoPointer &x)
Definition: AutoPointer.h:326
pcl::StandardDeleter< OpenFileDialogPrivate >::pointer
OpenFileDialogPrivate * pointer
Definition: AutoPointer.h:107
pcl::AutoPointer::Deleter
deleter & Deleter()
Definition: AutoPointer.h:521
pcl::operator*
Complex< T1 > operator*(const Complex< T1 > &c1, const Complex< T2 > &c2) noexcept
Definition: Complex.h:548
pcl::AutoPointerCloner::AutoPointerCloner
AutoPointerCloner(bool autoDelete=true, const deleter &d=deleter())
Definition: AutoPointer.h:739
pcl::StandardDeleter
An object deleter that uses the standard delete operator.
Definition: AutoPointer.h:95
pcl::AutoPointer::AutoPointer
AutoPointer(bool autoDelete=true, const deleter &d=deleter())
Definition: AutoPointer.h:282
Defs.h
pcl::AutoPointer::AutoPointer
AutoPointer(AutoPointer &&x)
Definition: AutoPointer.h:336
pcl::AutoPointer::DisableAutoDelete
void DisableAutoDelete(bool disable=true)
Definition: AutoPointer.h:505
pcl::AutoPointer::Ptr
pointer Ptr()
Definition: AutoPointer.h:445
pcl::AutoPointerCloner
A smart pointer able to generate dynamically allocated copies of the objects pointed to by other smar...
Definition: AutoPointer.h:695