NetOceanDirect 3.1.3
OceanDirect .NET API
ManagedObject.h
Go to the documentation of this file.
1#pragma once
2// bizarrely std::swap is defined in string_view from C++17 onwards
3//#include <string_view>
4using namespace System;
5
6namespace 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.
13
14 template<typename T, typename MemoryCleanup pointerDelete = MemoryCleanup::doDeletion>
15 public ref class ManagedObject
16 {
17 protected:
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 }
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 }
39 return m_Instance;
40 }
41 };
42}
Definition ManagedObject.h:16
ManagedObject(T *instance)
Definition ManagedObject.h:20
T * m_Instance
Definition ManagedObject.h:18
T * GetInstance()
Definition ManagedObject.h:38
virtual ~ManagedObject()
Definition ManagedObject.h:23
Definition Advanced.h:6
MemoryCleanup
Definition ManagedObject.h:12
@ noDeletion
Definition ManagedObject.h:12
@ doDeletion
Definition ManagedObject.h:12