1 /*
2  Created on: 2011-06-02
3  Created by: Andrey BETENEV
4  Copyright (c) 2011-2014 OPEN CASCADE SAS
5 
6  This file is part of Open CASCADE Technology software library.
7 
8  This library is free software; you can redistribute it and/or modify it under
9  the terms of the GNU Lesser General Public License version 2.1 as published
10  by the Free Software Foundation, with special exception defined in the file
11  OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12  distribution for complete text of the license and disclaimer of any warranty.
13 
14  Alternatively, this file may be used under the terms of Open CASCADE
15  commercial license or contractual agreement.
16 */
17 
18 #if ! defined(_MSC_VER) || ! defined(_MANAGED)
19 #error This file is usable only in C++/CLI (.NET) programs
20 #endif
21 
22 #pragma once
23 
24 using namespace System;
25 using namespace System::Collections::Generic;
26 
27 //! Template CLI class providing the way to encapsulate instance of C++
28 //! class as a field in the C++/CLI (ref) class.
29 //!
30 //! It can be helpful to encapsulate OCCT Handles, maps, arrays, etc.
31 //!
32 //! Use of variable of the Haft type is very similar to that of encapsulated
33 //! class:
34 //! - Default constructor creates default-constructed C++ instance
35 //! - Non-default construction is possible by copy or by initialization from
36 //!   compatible pointer (e.g. Haft for Handle can be initialized by pointer
37 //!   returned by operator new for a handled class)
38 //! - Underlying C++ instance is accessed by operator ()
39 
40 template <class CPPClass>
41 public ref class NCollection_Haft
42 {
43 public:
44   //! Initialize CLI Haft object by default-constructed C++ object
NCollection_Haft()45   NCollection_Haft ()
46   {
47     myPtr = new CPPClass;
48   }
49 
50   //! Initialize CLI Haft object by compatible C++ pointer
51   template <class T>
NCollection_Haft(const T * aPtr)52   NCollection_Haft (const T* aPtr)
53   {
54     myPtr = new CPPClass (aPtr);
55   }
56 
57   //! Initialize CLI Haft object by C++ class object
NCollection_Haft(const CPPClass & aPtr)58   NCollection_Haft (const CPPClass& aPtr)
59   {
60     myPtr = new CPPClass (aPtr);
61   }
62 
63   //! Destructor - invoked explicitly by delete, or automatically
64   //! when local variable is scoped out
~NCollection_Haft()65   ~NCollection_Haft ()
66   {
67     this->Nullify();
68   }
69 
70   //! Finalizer - called undeterministically by garbage collector
71   !NCollection_Haft ()
72   {
73     this->Nullify();
74   }
75 
76   //! Function call operator is provided to access underlying C++ object
operator()77   CPPClass& operator () () { return *myPtr; }
78 
79 protected:
80   //! Invalidate the haft
Nullify()81   void Nullify ()
82   {
83     delete myPtr;
84     myPtr = 0;
85   }
86 
87 protected:
88   CPPClass* myPtr;
89 };
90