NetOceanDirect  3.1.1
OceanDirect .NET API
ManagedObject.h
1 #pragma once
2 // bizarrely std::swap is defined in string_view from C++17 onwards
3 //#include <string_view>
4 using namespace System;
5 
6 namespace NetOceanDirect {
7 
8  // Use a template parameter "pointerDelete" to determine if the managed object will delete the
9  // supplied pointer. This allows for pointers that are owned elsewhere e.g. by smart pointers.
10  // pointerDelete is defaulted to doDeletion so that existing code that used the previous version of this
11  // template will "just work" unchanged.
12  enum MemoryCleanup { doDeletion, noDeletion };
13 
14  template<typename T, typename MemoryCleanup pointerDelete = MemoryCleanup::doDeletion>
15  public ref class ManagedObject
16  {
17  protected:
18  T* m_Instance;
19  public:
20  ManagedObject(T* instance) :
21  m_Instance(instance) {
22  }
23  virtual ~ManagedObject() {
24  // This is where managed resources should be cleaned up followed
25  // by the finalizer to clean up unmanaged resources
26  this->!ManagedObject();
27  }
28  !ManagedObject() {
29  // Clean up unmanaged resources.
30  // This template is normally used to access native code through the
31  // pointer...
32  if (pointerDelete == MemoryCleanup::doDeletion && m_Instance)
33  {
34  delete m_Instance;
35  m_Instance = nullptr;
36  }
37  }
38  T* GetInstance() {
39  return m_Instance;
40  }
41  };
42 }
Definition: ManagedObject.h:16