[+] C#-like using with RAII semantics. Use: AU_USING(pPointer/ref, MethodName)

This commit is contained in:
Reece Wilson 2024-07-30 01:51:20 +01:00
parent d2200907da
commit 6b9a364a91
2 changed files with 41 additions and 0 deletions

View File

@ -289,3 +289,5 @@ struct AuStringOwnedException : AuStringException
#define AU_EMIT_FIRST_COMMA_OTHERS(n),n #define AU_EMIT_FIRST_COMMA_OTHERS(n),n
#include "Objects/Objects.hpp" #include "Objects/Objects.hpp"
#include "AU_USING.hpp"

View File

@ -0,0 +1,39 @@
/***
Copyright (C) 2024 Jamie Reece Wilson (a/k/a "Reece"). All rights reserved.
File: AU_USING.hpp
Date: 2024-07-30
Author: Reece
***/
#pragma once
namespace __audetail
{
template <typename T_t>
struct __au_using
{
T_t *pThat;
using CB_t = void (T_t:: *)();
CB_t cb;
__au_using(T_t &that, CB_t cb) :
cb(cb),
pThat(&that)
{ }
__au_using(T_t *that, CB_t cb) :
cb(cb),
pThat(that)
{ }
~__au_using()
{
((pThat)->*(cb))();
}
};
}
#define AU_USING(pPointer, method) \
__audetail::__au_using<AuRemovePointer_t<AuRemoveReference_t<decltype(*pPointer)>>> AU_CONCAT(__stack_using, __COUNTER__)( \
pPointer, \
&AuRemovePointer_t<AuRemoveReference_t<decltype(*pPointer)>>::method);