0022815: Missing delete operator for placement new
[occt.git] / src / NCollection / NCollection_Haft.h
1 // File:      NCollection_Haft.h
2 // Created:   02.06.11 08:12:42
3 // Author:    Andrey BETENEV
4 // Copyright: OPEN CASCADE SAS 2011
5
6 #if ! defined(_MSC_VER) || ! defined(_MANAGED)
7 #error This file is usable only in C++/CLI (.NET) programs
8 #endif
9
10 #pragma once
11
12 using namespace System;
13 using namespace System::Collections::Generic;
14
15 //! Template CLI class providing the way to encapsulate instance of C++ 
16 //! class as a field in the C++/CLI (ref) class. 
17 //!
18 //! It can be helpful to encapsulate OCCT Handles, maps, arrays, etc.
19 //!
20 //! Use of variable of the Haft type is very similar to that of encapsulated 
21 //! class:
22 //! - Default constructor creates default-constructed C++ instance
23 //! - Non-default construction is possible by copy or by initialization from
24 //!   compatible pointer (e.g. Haft for Handle can be initialized by pointer 
25 //!   returned by operator new for a handled class)
26 //! - Underlying C++ instance is accessed by operator ()
27
28 template <class CPPClass> 
29 public ref class NCollection_Haft 
30 {
31 public:
32   //! Initialize CLI Haft object by default-constructed C++ object
33   NCollection_Haft ()
34   {
35     myPtr = new CPPClass;
36   }
37
38   //! Initialize CLI Haft object by compatible C++ pointer
39   template <class T>
40   NCollection_Haft (const T* aPtr)
41   {
42     myPtr = new CPPClass (aPtr);
43   }
44
45   //! Initialize CLI Haft object by C++ class object
46   NCollection_Haft (const CPPClass& aPtr)
47   {
48     myPtr = new CPPClass (aPtr);
49   }
50
51   //! Destructor - invoked explicitly by delete, or automatically 
52   //! when local variable is scoped out
53   ~NCollection_Haft ()
54   {
55     this->Nullify();
56   }
57
58   //! Finalizer - called undeterministically by garbage collector
59   !NCollection_Haft ()
60   {
61     this->Nullify();
62   }
63
64   //! Function call operator is provided to access underlying C++ object
65   CPPClass& operator () () { return *myPtr; }
66
67 protected:
68   //! Invalidate the haft
69   void Nullify ()
70   {
71     delete myPtr;
72     myPtr = 0;
73   }
74
75 protected:
76   CPPClass* myPtr;
77 };