1 // Created on: 2009-01-30
2 // Created by: Andrey BETENEV (abv)
3 // Copyright (c) 2009-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #ifndef NCollection_Handle_HeaderFile
17 #define NCollection_Handle_HeaderFile
19 #include <Standard_Transient.hxx>
20 #include <Standard_Handle.hxx>
22 //! Purpose: This template class is used to define Handle adaptor
23 //! for allocated dynamically objects of arbitrary type.
25 //! The advantage is that this handle will automatically destroy
26 //! the object when last referred Handle is destroyed (i.e. it is a
27 //! typical smart pointer), and that it can be handled as
28 //! Handle(Standard_Transient) in OCCT components.
31 class NCollection_Handle : public opencascade::handle<Standard_Transient>
35 //! Internal adaptor class wrapping actual type
36 //! and enhancing it by reference counter inherited from
37 //! Standard_Transient
38 class Ptr : public Standard_Transient
42 //! Constructor: stores pointer to the object
43 Ptr (T* theObj) : myPtr (theObj) {}
45 //! Destructor deletes the object
46 ~Ptr () { if ( myPtr ) delete myPtr; myPtr = 0; }
53 //! Assignment operator
54 Ptr& operator=(const Ptr&);
57 T* myPtr; //!< Pointer to the object
60 //! Constructor of handle from pointer on newly allocated object.
61 //! Note that additional argument is used to avoid ambiguity with
62 //! public constructor from pointer when Handle is intilialized by 0.
63 NCollection_Handle (Ptr* thePtr, int)
64 : opencascade::handle<Standard_Transient> (thePtr) {}
68 typedef T element_type;
70 //! Default constructor; creates null handle
71 NCollection_Handle () {}
73 //! Constructor of handle from pointer on newly allocated object
74 NCollection_Handle (T* theObject)
75 : opencascade::handle<Standard_Transient> (theObject ? new Ptr (theObject) : 0) {}
77 //! Cast handle to contained type
78 T* get () { return ((Ptr*)opencascade::handle<Standard_Transient>::get())->myPtr; }
80 //! Cast handle to contained type
81 const T* get () const { return ((Ptr*)opencascade::handle<Standard_Transient>::get())->myPtr; }
83 //! Cast handle to contained type
84 T* operator -> () { return get(); }
86 //! Cast handle to contained type
87 const T* operator -> () const { return get(); }
89 //! Cast handle to contained type
90 T& operator * () { return *get(); }
92 //! Cast handle to contained type
93 const T& operator * () const { return *get(); }
95 //! Downcast arbitrary Handle to the argument type if contained
96 //! object is Handle for this type; returns null otherwise
97 static NCollection_Handle<T> DownCast (const opencascade::handle<Standard_Transient>& theOther)
99 return NCollection_Handle<T>(dynamic_cast<Ptr*>(theOther.get()), 0);