DaoAI World C# SDK INDUSTRIAL 2025.3.0
Loading...
Searching...
No Matches
managed_object.h
Go to the documentation of this file.
1#pragma once
2#include <Windows.h>
3using namespace System;
4using namespace System::Runtime::InteropServices;
5
6namespace DaoAI
7{
8 namespace DeepLearningCLI
9 {
10 // Adapted from ChatGPT
11 static const char* string_to_char_array(String^ string)
12 {
13 if (string == nullptr) return nullptr; // Handle null input
14
15 // Convert managed String^ to unmanaged UTF-16 memory
16 IntPtr ptr = Marshal::StringToHGlobalAuto(string);
17 if (ptr == IntPtr::Zero) return nullptr;
18
19 const wchar_t* wideStr = static_cast<const wchar_t*>(ptr.ToPointer());
20
21 // Ensure string is not empty
22 if (*wideStr == L'\0')
23 {
24 Marshal::FreeHGlobal(ptr);
25 return nullptr;
26 }
27
28 // Determine the required buffer size for UTF-8 conversion
29 int sizeNeeded = WideCharToMultiByte(CP_UTF8, 0, wideStr, -1, nullptr, 0, nullptr, nullptr);
30 if (sizeNeeded <= 0)
31 {
32 Marshal::FreeHGlobal(ptr);
33 return nullptr;
34 }
35
36 // Allocate memory for UTF-8 string
37 char* utf8Str = new char[sizeNeeded];
38 if (!utf8Str)
39 {
40 Marshal::FreeHGlobal(ptr);
41 return nullptr;
42 }
43
44 // Convert UTF-16 to UTF-8
45 if (WideCharToMultiByte(CP_UTF8, 0, wideStr, -1, utf8Str, sizeNeeded, nullptr, nullptr) == 0)
46 {
47 delete[] utf8Str;
48 Marshal::FreeHGlobal(ptr);
49 return nullptr;
50 }
51
52 // Free the unmanaged UTF-16 memory
53 Marshal::FreeHGlobal(ptr);
54
55 return utf8Str; // Caller must free this memory
56 }
57
58 template<class T>
59 public ref class ManagedObject
60 {
61 protected:
63 public:
64 ManagedObject(T* instance)
65 : m_Instance(instance)
66 {
67 }
69 : m_Instance(nullptr)
70 {
71 }
73 {
74 if (m_Instance != nullptr)
75 {
76 delete m_Instance;
77 }
78 }
80 {
81 if (m_Instance != nullptr)
82 {
83 delete m_Instance;
84 }
85 }
87 {
88 return m_Instance;
89 }
90 };
91 }
92}
Definition managed_object.h:60
ManagedObject(T *instance)
Definition managed_object.h:64
T * m_Instance
Definition managed_object.h:62
ManagedObject()
Definition managed_object.h:68
T * GetInstance()
Definition managed_object.h:86
virtual ~ManagedObject()
Definition managed_object.h:72
Definition common.cpp:6