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
8 // under the terms of the GNU Lesser General Public 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 //! Standard type function allowing to check that contained object is Handle
22 Standard_EXPORT const Handle(Standard_Type)& STANDARD_TYPE(NCollection_Handle);
24 //! Purpose: This template class is used to define Handle adaptor
25 //! for allocated dynamically objects of arbitrary type.
27 //! The advantage is that this handle will automatically destroy
28 //! the object when last referred Handle is destroyed (i.e. it is a
29 //! typical smart pointer), and that it can be handled as
30 //! Handle(Standard_Transient) in OCCT components.
32 //! Use it as follows:
34 //! NCollection_Handle<T> aPtr = new T (...);
36 //! aPtr->Method(...);
38 //! Handle(Standard_Transient) aBase = aPtr;
39 //! if ( aBase->IsKind(STANDARD_TYPE(NCollection_Handle)) )
41 //! NCollection_Handle<T> aPtr2 = NCollection_Handle<T>::DownCast (aBase);
42 //! if ( ! aPtr2.IsNull() )
47 class NCollection_Handle : public Handle_Standard_Transient
51 //! Internal adaptor class wrapping actual type
52 //! and enhancing it by reference counter inherited from
53 //! Standard_Transient
54 class Ptr : public Standard_Transient
58 //! Constructor: stores pointer to the object
59 Ptr (T* theObj) { myPtr = theObj; }
61 //! Destructor deletes the object
62 ~Ptr () { if ( myPtr ) delete myPtr; myPtr = 0; }
64 //! Implementation of DynamicType() method
65 const Handle(Standard_Type)& DynamicType() const
66 { return STANDARD_TYPE(NCollection_Handle); }
69 T* myPtr; //!< Pointer to the object
72 //! Constructor of handle from pointer on newly allocated object.
73 //! Note that additional argument is used to avoid ambiguity with
74 //! public constructor from pointer when Handle is intilialized by 0.
75 NCollection_Handle (Ptr* thePtr, int)
76 : Handle_Standard_Transient (thePtr) {}
80 //! Default constructor; creates null handle
81 NCollection_Handle () {}
83 //! Constructor of handle from pointer on newly allocated object
84 NCollection_Handle (T* theObject)
85 : Handle_Standard_Transient (theObject ? new Ptr (theObject) : 0) {}
87 //! Cast handle to contained type
88 T* operator -> () { return ((Ptr*)ControlAccess())->myPtr; }
90 //! Cast handle to contained type
91 const T* operator -> () const { return ((Ptr*)ControlAccess())->myPtr; }
93 //! Cast handle to contained type
94 T& operator * () { return *((Ptr*)ControlAccess())->myPtr; }
96 //! Cast handle to contained type
97 const T& operator * () const { return *((Ptr*)ControlAccess())->myPtr; }
99 //! Downcast arbitrary Handle to the argument type if contained
100 //! object is Handle for this type; returns null otherwise
101 static NCollection_Handle<T> DownCast (const Handle(Standard_Transient)& theOther)
103 return NCollection_Handle<T> (theOther.IsNull() ? 0 : dynamic_cast<Ptr*> (theOther.operator->()), 0);