Skip to content

Commit

Permalink
Add "SafeADLXWrapper" class
Browse files Browse the repository at this point in the history
Wraps native code with try catch, return ADLX_FAIL on exception
  • Loading branch information
Rem0o committed Nov 9, 2024
1 parent ea1ce7f commit 08688e3
Show file tree
Hide file tree
Showing 9 changed files with 238 additions and 14 deletions.
2 changes: 2 additions & 0 deletions ADLXBindings/ADLXBindings.i
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "../ADLX/SDK/Include/IPerformanceMonitoring.h"
#include "../ADLX/SDK/ADLXHelper/Windows/Cpp/ADLXHelper.h"
#include "ADLXExt.h"
#include "SafeADLXHelper.h"

typedef int64_t adlx_int64;
typedef int32_t adlx_int32;
Expand Down Expand Up @@ -127,6 +128,7 @@ typedef WCHAR TCHAR;
%include "../ADLX/SDK/Include/IPerformanceMonitoring.h"
%include "../ADLX/SDK/ADLXHelper/Windows/Cpp/ADLXHelper.h"
%include "ADLXExt.h"
%include "SafeADLXHelper.h"

using namespace adlx;

Expand Down
2 changes: 2 additions & 0 deletions ADLXBindings/ADLXBindings.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@
<ClInclude Include="ADLXExt.h" />
<ClInclude Include="framework.h" />
<ClInclude Include="pch.h" />
<ClInclude Include="SafeADLXHelper.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\ADLX\SDK\ADLXHelper\Windows\Cpp\ADLXHelper.cpp" />
Expand All @@ -169,6 +170,7 @@
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader>
</ClCompile>
<ClCompile Include="SafeADLXHelper.cpp" />
</ItemGroup>
<ItemGroup>
<CustomBuild Include="ADLXBindings.i">
Expand Down
26 changes: 19 additions & 7 deletions ADLXBindings/ADLXBindings.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,19 @@
<ClInclude Include="pch.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="ADLXCSharpBind_wrap.h">
<ClInclude Include="framework.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\..\..\SDK\ADLXHelper\Windows\Cpp\ADLXHelper.h">
<ClInclude Include="..\ADLX\SDK\ADLXHelper\Windows\Cpp\ADLXHelper.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="framework.h">
<ClInclude Include="ADLXBindings_wrap.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="ADLXExt.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="SafeADLXHelper.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
Expand All @@ -35,18 +41,24 @@
<ClCompile Include="pch.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="ADLXCSharpBind_wrap.cxx">
<ClCompile Include="..\ADLX\SDK\ADLXHelper\Windows\Cpp\ADLXHelper.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\ADLX\SDK\Platform\Windows\WinAPIs.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="ADLXBindings_wrap.cxx">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\SDK\ADLXHelper\Windows\Cpp\ADLXHelper.cpp">
<ClCompile Include="ADLXExt.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\..\..\..\SDK\Platform\Windows\WinAPIs.cpp">
<ClCompile Include="SafeADLXHelper.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<CustomBuild Include="ADLXCSharpBind.i" />
<CustomBuild Include="ADLXBindings.i" />
</ItemGroup>
<ItemGroup>
<None Include="cs_struct.i" />
Expand Down
55 changes: 55 additions & 0 deletions ADLXBindings/ADLXBindings_wrap.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ template <typename T> T SwigValueInit() {
#include "../ADLX/SDK/Include/IPerformanceMonitoring.h"
#include "../ADLX/SDK/ADLXHelper/Windows/Cpp/ADLXHelper.h"
#include "ADLXExt.h"
#include "SafeADLXHelper.h"

typedef int64_t adlx_int64;
typedef int32_t adlx_int32;
Expand Down Expand Up @@ -7876,6 +7877,60 @@ SWIGEXPORT int SWIGSTDCALL CSharp_ADLXWrapperfBindings_ADLXExt_GetCurrentMetrics
}


SWIGEXPORT void * SWIGSTDCALL CSharp_ADLXWrapperfBindings_new_SafeADLXHelper___() {
void * jresult ;
SafeADLXHelper *result = 0 ;

result = (SafeADLXHelper *)new SafeADLXHelper();
jresult = (void *)result;
return jresult;
}


SWIGEXPORT void SWIGSTDCALL CSharp_ADLXWrapperfBindings_delete_SafeADLXHelper___(void * jarg1) {
SafeADLXHelper *arg1 = (SafeADLXHelper *) 0 ;

arg1 = (SafeADLXHelper *)jarg1;
delete arg1;
}


SWIGEXPORT int SWIGSTDCALL CSharp_ADLXWrapperfBindings_SafeADLXHelper_Initialize___(void * jarg1) {
int jresult ;
SafeADLXHelper *arg1 = (SafeADLXHelper *) 0 ;
ADLX_RESULT result;

arg1 = (SafeADLXHelper *)jarg1;
result = (ADLX_RESULT)(arg1)->Initialize();
jresult = (int)result;
return jresult;
}


SWIGEXPORT int SWIGSTDCALL CSharp_ADLXWrapperfBindings_SafeADLXHelper_Terminate___(void * jarg1) {
int jresult ;
SafeADLXHelper *arg1 = (SafeADLXHelper *) 0 ;
ADLX_RESULT result;

arg1 = (SafeADLXHelper *)jarg1;
result = (ADLX_RESULT)(arg1)->Terminate();
jresult = (int)result;
return jresult;
}


SWIGEXPORT void * SWIGSTDCALL CSharp_ADLXWrapperfBindings_SafeADLXHelper_GetSystemServices___(void * jarg1) {
void * jresult ;
SafeADLXHelper *arg1 = (SafeADLXHelper *) 0 ;
adlx::IADLXSystem *result = 0 ;

arg1 = (SafeADLXHelper *)jarg1;
result = (adlx::IADLXSystem *)(arg1)->GetSystemServices();
jresult = (void *)result;
return jresult;
}


SWIGEXPORT void * SWIGSTDCALL CSharp_ADLXWrapperfBindings_new_intP___() {
void * jresult ;
adlx_int *result = 0 ;
Expand Down
39 changes: 39 additions & 0 deletions ADLXBindings/SafeADLXHelper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "SafeADLXHelper.h"

SafeADLXHelper::SafeADLXHelper()
{
}

SafeADLXHelper::~SafeADLXHelper()
{
Terminate();
}

ADLX_RESULT SafeADLXHelper::Initialize()
{
try
{
return _adlxHelper.Initialize();
}
catch (...)
{
return ADLX_FAIL;
}
}

ADLX_RESULT SafeADLXHelper::Terminate()
{
try
{
return _adlxHelper.Terminate();
}
catch (...)
{
return ADLX_FAIL;
}
}

adlx::IADLXSystem* SafeADLXHelper::GetSystemServices()
{
return _adlxHelper.GetSystemServices();
}
21 changes: 21 additions & 0 deletions ADLXBindings/SafeADLXHelper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include "../ADLX/SDK/Include/ADLX.h"
#include "../ADLX/SDK/ADLXHelper/Windows/Cpp/ADLXHelper.h"

class SafeADLXHelper
{
public:

SafeADLXHelper();
virtual ~SafeADLXHelper();

ADLX_RESULT Initialize();
ADLX_RESULT Terminate();

adlx::IADLXSystem* GetSystemServices();

private:
ADLXHelper _adlxHelper;

}; //class SafeADLXHelper
11 changes: 4 additions & 7 deletions ADLXWrapper/ADLXWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace ADLXWrapper
{
public class ADLXWrapper : Wrapper<ADLXHelper>
public class ADLXWrapper : Wrapper<SafeADLXHelper>
{
private bool _initialized;
private ADLXExt _ext;

public ADLXWrapper() : base(new ADLXHelper())
public ADLXWrapper() : base(new SafeADLXHelper())
{
_ext = new ADLXExt();
}
Expand All @@ -30,11 +30,8 @@ public SystemServices GetSystemServices()

public void Terminate()
{
if (_initialized)
{
_initialized = false;
NativeInterface.Terminate().ThrowIfError("Couldn't terminate ADLX");
}
_initialized = false;
NativeInterface.Terminate().ThrowIfError("Couldn't terminate ADLX");
}

public override void Dispose()
Expand Down
15 changes: 15 additions & 0 deletions ADLXWrapper/Bindings/ADLXPINVOKE.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1804,6 +1804,21 @@ static ADLXPINVOKE() {
[global::System.Runtime.InteropServices.DllImport("ADLXBindings", EntryPoint="CSharp_ADLXWrapperfBindings_ADLXExt_GetCurrentMetrics___")]
public static extern int ADLXExt_GetCurrentMetrics(global::System.Runtime.InteropServices.HandleRef jarg1, global::System.Runtime.InteropServices.HandleRef jarg2, global::System.Runtime.InteropServices.HandleRef jarg3, ref GPUMetricsStruct jarg4);

[global::System.Runtime.InteropServices.DllImport("ADLXBindings", EntryPoint="CSharp_ADLXWrapperfBindings_new_SafeADLXHelper___")]
public static extern global::System.IntPtr new_SafeADLXHelper();

[global::System.Runtime.InteropServices.DllImport("ADLXBindings", EntryPoint="CSharp_ADLXWrapperfBindings_delete_SafeADLXHelper___")]
public static extern void delete_SafeADLXHelper(global::System.Runtime.InteropServices.HandleRef jarg1);

[global::System.Runtime.InteropServices.DllImport("ADLXBindings", EntryPoint="CSharp_ADLXWrapperfBindings_SafeADLXHelper_Initialize___")]
public static extern int SafeADLXHelper_Initialize(global::System.Runtime.InteropServices.HandleRef jarg1);

[global::System.Runtime.InteropServices.DllImport("ADLXBindings", EntryPoint="CSharp_ADLXWrapperfBindings_SafeADLXHelper_Terminate___")]
public static extern int SafeADLXHelper_Terminate(global::System.Runtime.InteropServices.HandleRef jarg1);

[global::System.Runtime.InteropServices.DllImport("ADLXBindings", EntryPoint="CSharp_ADLXWrapperfBindings_SafeADLXHelper_GetSystemServices___")]
public static extern global::System.IntPtr SafeADLXHelper_GetSystemServices(global::System.Runtime.InteropServices.HandleRef jarg1);

[global::System.Runtime.InteropServices.DllImport("ADLXBindings", EntryPoint="CSharp_ADLXWrapperfBindings_new_intP___")]
public static extern global::System.IntPtr new_intP();

Expand Down
81 changes: 81 additions & 0 deletions ADLXWrapper/Bindings/SafeADLXHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//------------------------------------------------------------------------------
// <auto-generated />
//
// This file was automatically generated by SWIG (https://www.swig.org).
// Version 4.2.1
//
// Do not make changes to this file unless you know what you are doing - modify
// the SWIG interface file instead.
//------------------------------------------------------------------------------

namespace ADLXWrapper.Bindings {

public class SafeADLXHelper : global::System.IDisposable {
private global::System.Runtime.InteropServices.HandleRef swigCPtr;
protected bool swigCMemOwn;

internal SafeADLXHelper(global::System.IntPtr cPtr, bool cMemoryOwn) {
swigCMemOwn = cMemoryOwn;
swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
}

internal static global::System.Runtime.InteropServices.HandleRef getCPtr(SafeADLXHelper obj) {
return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
}

internal static global::System.Runtime.InteropServices.HandleRef swigRelease(SafeADLXHelper obj) {
if (obj != null) {
if (!obj.swigCMemOwn)
throw new global::System.ApplicationException("Cannot release ownership as memory is not owned");
global::System.Runtime.InteropServices.HandleRef ptr = obj.swigCPtr;
obj.swigCMemOwn = false;
obj.Dispose();
return ptr;
} else {
return new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
}
}

~SafeADLXHelper() {
Dispose(false);
}

public void Dispose() {
Dispose(true);
global::System.GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing) {
lock(this) {
if (swigCPtr.Handle != global::System.IntPtr.Zero) {
if (swigCMemOwn) {
swigCMemOwn = false;
ADLXPINVOKE.delete_SafeADLXHelper(swigCPtr);
}
swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
}
}
}

public SafeADLXHelper() : this(ADLXPINVOKE.new_SafeADLXHelper(), true) {
}

public ADLX_RESULT Initialize() {
ADLX_RESULT ret = (ADLX_RESULT)ADLXPINVOKE.SafeADLXHelper_Initialize(swigCPtr);
return ret;
}

public ADLX_RESULT Terminate() {
ADLX_RESULT ret = (ADLX_RESULT)ADLXPINVOKE.SafeADLXHelper_Terminate(swigCPtr);
return ret;
}

public IADLXSystem GetSystemServices() {
global::System.IntPtr cPtr = ADLXPINVOKE.SafeADLXHelper_GetSystemServices(swigCPtr);
IADLXSystem ret = (cPtr == global::System.IntPtr.Zero) ? null : new IADLXSystem(cPtr, false);
return ret;
}

}

}

0 comments on commit 08688e3

Please sign in to comment.