0027960: Configuration - fix compilation of OSD_Directory with MinGW-w64
[occt.git] / src / NCollection / NCollection_Handle.hxx
1 // Created on: 2009-01-30
2 // Created by: Andrey BETENEV (abv)
3 // Copyright (c) 2009-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
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.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #ifndef NCollection_Handle_HeaderFile
17 #define NCollection_Handle_HeaderFile
18
19 #include <MMgt_TShared.hxx>
20   
21 //! Purpose: This template class is used to define Handle adaptor
22 //! for allocated dynamically objects of arbitrary type.
23 //!
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.
28
29 template <class T>
30 class NCollection_Handle : public Handle(Standard_Transient)
31 {
32  private:
33
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
38   {
39   public:
40
41     //! Constructor: stores pointer to the object
42     Ptr (T* theObj) { myPtr = theObj; }
43
44     //! Destructor deletes the object
45     ~Ptr () { if ( myPtr ) delete myPtr; myPtr = 0; }
46
47   protected:
48
49     //! Copy constructor
50     Ptr(const Ptr&);
51
52     //! Assignment operator
53     Ptr& operator=(const Ptr&);
54
55   public:
56     T* myPtr; //!< Pointer to the object
57   };
58   
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) {}
64   
65  public:
66
67   typedef T element_type;
68
69   //! Default constructor; creates null handle
70   NCollection_Handle () {}
71   
72   //! Constructor of handle from pointer on newly allocated object
73   NCollection_Handle (T* theObject) 
74     : Handle(Standard_Transient) (theObject ? new Ptr (theObject) : 0) {}
75   
76   //! Cast handle to contained type
77   T* get () { return ((Ptr*)Handle(Standard_Transient)::get())->myPtr; }
78
79   //! Cast handle to contained type
80   const T* get () const { return ((Ptr*)Handle(Standard_Transient)::get())->myPtr; }
81
82   //! Cast handle to contained type
83   T* operator -> () { return get(); }
84   
85   //! Cast handle to contained type
86   const T* operator -> () const { return get(); }
87   
88   //! Cast handle to contained type
89   T& operator * () { return *get(); }
90   
91   //! Cast handle to contained type
92   const T& operator * () const { return *get(); }
93
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)
97   {
98     return NCollection_Handle<T>(dynamic_cast<Ptr*>(theOther.get()), 0);
99   }
100
101 };
102
103 #endif