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 <MMgt_TShared.hxx>
21 //! Purpose: This template class is used to define Handle adaptor
22 //! for allocated dynamically objects of arbitrary type.
24 //! The advantage is that this handle will automatically destroy
25 //! the object when last referred Handle is destroyed (i.e. it is a
26 //! typical smart pointer), and that it can be handled as
27 //! Handle(Standard_Transient) in OCCT components.
30 class NCollection_Handle : public Handle(Standard_Transient)
34 //! Internal adaptor class wrapping actual type
35 //! and enhancing it by reference counter inherited from
36 //! Standard_Transient
37 class Ptr : public Standard_Transient
41 //! Constructor: stores pointer to the object
42 Ptr (T* theObj) { myPtr = theObj; }
44 //! Destructor deletes the object
45 ~Ptr () { if ( myPtr ) delete myPtr; myPtr = 0; }
52 //! Assignment operator
53 Ptr& operator=(const Ptr&);
56 T* myPtr; //!< Pointer to the object
59 //! Constructor of handle from pointer on newly allocated object.
60 //! Note that additional argument is used to avoid ambiguity with
61 //! public constructor from pointer when Handle is intilialized by 0.
62 NCollection_Handle (Ptr* thePtr, int)
63 : Handle(Standard_Transient) (thePtr) {}
67 typedef T element_type;
69 //! Default constructor; creates null handle
70 NCollection_Handle () {}
72 //! Constructor of handle from pointer on newly allocated object
73 NCollection_Handle (T* theObject)
74 : Handle(Standard_Transient) (theObject ? new Ptr (theObject) : 0) {}
76 //! Cast handle to contained type
77 T* get () { return ((Ptr*)Handle(Standard_Transient)::get())->myPtr; }
79 //! Cast handle to contained type
80 const T* get () const { return ((Ptr*)Handle(Standard_Transient)::get())->myPtr; }
82 //! Cast handle to contained type
83 T* operator -> () { return get(); }
85 //! Cast handle to contained type
86 const T* operator -> () const { return get(); }
88 //! Cast handle to contained type
89 T& operator * () { return *get(); }
91 //! Cast handle to contained type
92 const T& operator * () const { return *get(); }
94 //! Downcast arbitrary Handle to the argument type if contained
95 //! object is Handle for this type; returns null otherwise
96 static NCollection_Handle<T> DownCast (const Handle(Standard_Transient)& theOther)
98 return NCollection_Handle<T>(dynamic_cast<Ptr*>(theOther.get()), 0);