PCL
AutoPointer.h
Go to the documentation of this file.
1 // ____ ______ __
2 // / __ \ / ____// /
3 // / /_/ // / / /
4 // / ____// /___ / /___ PixInsight Class Library
5 // /_/ \____//_____/ PCL 2.1.19
6 // ----------------------------------------------------------------------------
7 // pcl/AutoPointer.h - Released 2019-11-07T10:59:34Z
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-2019 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 (http://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  typedef T value_type;
103 
108  typedef T* pointer;
109 
114  void operator()( pointer p ) const
115  {
116  PCL_PRECONDITION( p != nullptr )
117  delete p;
118  }
119 };
120 
121 // ----------------------------------------------------------------------------
122 
240 template <class T, class D = StandardDeleter<T> >
241 class PCL_CLASS AutoPointer
242 {
243 public:
244 
248  typedef T value_type;
249 
253  typedef T* pointer;
254 
258  typedef const T* const_pointer;
259 
263  typedef D deleter;
264 
283  AutoPointer( bool autoDelete = true, const deleter& d = deleter() ) :
284  m_deleter( d ),
285  m_autoDelete( autoDelete )
286  {
287  }
288 
306  AutoPointer( pointer p, bool autoDelete = true, const deleter& d = deleter() ) :
307  m_deleter( d ),
308  m_autoDelete( autoDelete )
309  {
310  m_pointer = p;
311  }
312 
328  m_deleter( x.m_deleter ),
329  m_autoDelete( x.m_autoDelete )
330  {
331  m_pointer = x.Release();
332  }
333 
338  m_deleter( std::move( x.m_deleter ) ),
339  m_autoDelete( x.m_autoDelete )
340  {
341  m_pointer = x.Release();
342  }
343 
351  virtual ~AutoPointer()
352  {
353  Reset();
354  }
355 
363  void SetPointer( pointer p )
364  {
365  if ( m_pointer != p )
366  {
367  if ( m_autoDelete )
368  if ( m_pointer != nullptr )
369  m_deleter( Release() ); // in case m_deleter throws
370  m_pointer = p;
371  }
372  }
373 
383  void Reset()
384  {
385  if ( m_pointer != nullptr )
386  {
387  pointer p = Release(); // in case m_deleter throws
388  if ( m_autoDelete )
389  m_deleter( p );
390  }
391  }
392 
397  void Destroy()
398  {
399  Reset();
400  }
401 
411  pointer Release()
412  {
413  pointer p = m_pointer;
414  m_pointer = nullptr;
415  return p;
416  }
417 
422  const_pointer Pointer() const
423  {
424  return m_pointer;
425  }
426 
430  pointer Pointer()
431  {
432  return m_pointer;
433  }
434 
438  const_pointer Ptr() const
439  {
440  return m_pointer;
441  }
442 
446  pointer Ptr()
447  {
448  return m_pointer;
449  }
450 
454  bool IsNull() const
455  {
456  return m_pointer == nullptr;
457  }
458 
463  bool IsValid() const
464  {
465  return !IsNull();
466  }
467 
484  bool IsAutoDelete() const
485  {
486  return m_autoDelete;
487  }
488 
495  void EnableAutoDelete( bool enable = true )
496  {
497  m_autoDelete = enable;
498  }
499 
506  void DisableAutoDelete( bool disable = true )
507  {
508  EnableAutoDelete( !disable );
509  }
510 
514  const deleter& Deleter() const
515  {
516  return m_deleter;
517  }
518 
522  deleter& Deleter()
523  {
524  return m_deleter;
525  }
526 
547  AutoPointer& operator =( AutoPointer& x )
548  {
549  SetPointer( x.Release() );
550  m_deleter = x.m_deleter;
551  m_autoDelete = x.m_autoDelete;
552  return *this;
553  }
554 
559  AutoPointer& operator =( AutoPointer&& x )
560  {
561  SetPointer( x.Release() );
562  m_deleter = std::move( x.m_deleter );
563  m_autoDelete = x.m_autoDelete;
564  return *this;
565  }
566 
584  AutoPointer& operator =( pointer p )
585  {
586  SetPointer( p );
587  return *this;
588  }
589 
595  operator const_pointer() const
596  {
597  return m_pointer;
598  }
599 
605  operator pointer()
606  {
607  return m_pointer;
608  }
609 
614  const_pointer operator ->() const
615  {
616  PCL_PRECONDITION( m_pointer != nullptr )
617  return m_pointer;
618  }
619 
624  pointer operator ->()
625  {
626  PCL_PRECONDITION( m_pointer != nullptr )
627  return m_pointer;
628  }
629 
634  const value_type& operator *() const
635  {
636  PCL_PRECONDITION( m_pointer != nullptr )
637  return *m_pointer;
638  }
639 
644  value_type& operator *()
645  {
646  PCL_PRECONDITION( m_pointer != nullptr )
647  return *m_pointer;
648  }
649 
654  operator bool() const
655  {
656  return !IsNull();
657  }
658 
662  friend void Swap( AutoPointer& x1, AutoPointer& x2 )
663  {
664  pcl::Swap( x1.m_pointer, x2.m_pointer );
665  pcl::Swap( x1.m_deleter, x2.m_deleter );
666  bool b = x1.m_autoDelete; x1.m_autoDelete = x2.m_autoDelete; x2.m_autoDelete = b;
667  }
668 
669 protected:
670 
671  pointer m_pointer = nullptr;
672  deleter m_deleter;
673  bool m_autoDelete = true;
674 };
675 
676 // ----------------------------------------------------------------------------
677 
678 #define ASSERT_COPIABLE_T() \
679  static_assert( std::is_copy_constructible<T>::value, "AutoPointerCloner<> requires a copy-constructible type." )
680 
695 template <class T, class D = StandardDeleter<T> >
696 class PCL_CLASS AutoPointerCloner : public AutoPointer<T,D>
697 {
698 public:
699 
700  typedef AutoPointer<T,D> base_type;
701 
706 
710  typedef typename base_type::pointer pointer;
711 
716 
720  typedef typename base_type::deleter deleter;
721 
740  AutoPointerCloner( bool autoDelete = true, const deleter& d = deleter() ) :
741  base_type( autoDelete, d )
742  {
743  ASSERT_COPIABLE_T();
744  }
745 
763  AutoPointerCloner( pointer p, bool autoDelete = true, const deleter& d = deleter() ) :
764  base_type( p, autoDelete, d )
765  {
766  ASSERT_COPIABLE_T();
767  }
768 
781  AutoPointerCloner( const base_type& x ) : 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 
801  AutoPointerCloner( const AutoPointerCloner& x ) : base_type( nullptr )
802  {
803  ASSERT_COPIABLE_T();
804  this->m_pointer = x ? new value_type( *x ) : nullptr;
805  this->m_deleter = x.m_deleter;
806  this->m_autoDelete = x || x.m_autoDelete;
807  }
808 
812  AutoPointerCloner( base_type&& x ) : base_type( std::move( x ) )
813  {
814  }
815 
837  AutoPointerCloner& operator =( const base_type& x )
838  {
839  this->SetPointer( x ? new value_type( *x ) : nullptr );
840  this->m_deleter = x.m_deleter;
841  this->m_autoDelete = x || x.m_autoDelete;
842  return *this;
843  }
844 
866  AutoPointerCloner& operator =( const AutoPointerCloner& x )
867  {
868  this->SetPointer( x ? new value_type( *x ) : nullptr );
869  this->m_deleter = x.m_deleter;
870  this->m_autoDelete = x || x.m_autoDelete;
871  return *this;
872  }
873 
878  AutoPointerCloner& operator =( base_type&& x )
879  {
880  (void)base_type::operator =( std::move( x ) );
881  return *this;
882  }
883 
893  AutoPointerCloner& operator =( pointer p )
894  {
895  this->SetPointer( p );
896  return *this;
897  }
898 };
899 
900 #undef ASSERT_COPIABLE_T
901 
902 // ----------------------------------------------------------------------------
903 
904 } // pcl
905 
906 #endif // __PCL_AutoPointer_h
907 
908 // ----------------------------------------------------------------------------
909 // EOF pcl/AutoPointer.h - Released 2019-11-07T10:59:34Z
bool IsNull() const
Definition: AutoPointer.h:454
base_type::pointer pointer
Definition: AutoPointer.h:710
Complex< T1 > operator*(const Complex< T1 > &c1, const Complex< T2 > &c2)
Definition: Complex.h:539
AutoPointer(pointer p, bool autoDelete=true, const deleter &d=deleter())
Definition: AutoPointer.h:306
AutoPointerCloner(const base_type &x)
Definition: AutoPointer.h:781
bool IsValid() const
Definition: AutoPointer.h:463
void Swap(GenericPoint< T > &p1, GenericPoint< T > &p2)
Definition: Point.h:1402
AutoPointerCloner(pointer p, bool autoDelete=true, const deleter &d=deleter())
Definition: AutoPointer.h:763
AutoPointer(AutoPointer &x)
Definition: AutoPointer.h:327
PCL root namespace.
Definition: AbstractImage.h:76
AutoPointerCloner(bool autoDelete=true, const deleter &d=deleter())
Definition: AutoPointer.h:740
A smart pointer with sole object ownership and optional automatic object destruction.
Definition: AutoPointer.h:241
const deleter & Deleter() const
Definition: AutoPointer.h:514
Calculation of ephemerides from data stored in XEPH files.
An object deleter that uses the standard delete operator.
Definition: AutoPointer.h:95
AutoPointerCloner(base_type &&x)
Definition: AutoPointer.h:812
A smart pointer able to clone the objects pointed to by other smart pointers.
Definition: AutoPointer.h:696
pointer Pointer()
Definition: AutoPointer.h:430
base_type::value_type value_type
Definition: AutoPointer.h:705
AutoPointer(AutoPointer &&x)
Definition: AutoPointer.h:337
AutoPointer(bool autoDelete=true, const deleter &d=deleter())
Definition: AutoPointer.h:283
base_type::const_pointer const_pointer
Definition: AutoPointer.h:715
pointer Release()
Definition: AutoPointer.h:411
void EnableAutoDelete(bool enable=true)
Definition: AutoPointer.h:495
const_pointer Ptr() const
Definition: AutoPointer.h:438
const_pointer Pointer() const
Definition: AutoPointer.h:422
void DisableAutoDelete(bool disable=true)
Definition: AutoPointer.h:506
const OpenFileDialogPrivate * const_pointer
Definition: AutoPointer.h:258
virtual ~AutoPointer()
Definition: AutoPointer.h:351
void operator()(pointer p) const
Definition: AutoPointer.h:114
void SetPointer(pointer p)
Definition: AutoPointer.h:363
AutoPointerCloner(const AutoPointerCloner &x)
Definition: AutoPointer.h:801
deleter & Deleter()
Definition: AutoPointer.h:522
base_type::deleter deleter
Definition: AutoPointer.h:720
bool IsAutoDelete() const
Definition: AutoPointer.h:484
friend void Swap(AutoPointer &x1, AutoPointer &x2)
Definition: AutoPointer.h:662