handle - untested, first revision for an idea
This commit is contained in:
parent
8f852036ed
commit
3b24e85bb0
57
handle.h
Normal file
57
handle.h
Normal file
@ -0,0 +1,57 @@
|
||||
#ifndef MAG_HANDLE_H
|
||||
#define MAG_HANDLE_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
#error "handle.h does not work with C++ compilers, you must compile these files with GCC directly!"
|
||||
#endif
|
||||
|
||||
/** Tells your constructor/destructor handler function about the state */
|
||||
enum HANDLE_STATE {
|
||||
HANDLE_CREAT,
|
||||
HANDLE_DESTR,
|
||||
};
|
||||
|
||||
/**
|
||||
* This creates a "handle". Basically a struct with given constructor and destructor + RAII.
|
||||
*
|
||||
* Usage:
|
||||
*
|
||||
* struct Meaning {
|
||||
* int a;
|
||||
* int b;
|
||||
* };
|
||||
* // You can typedef if you want, change code accordingly!
|
||||
*
|
||||
* handle(struct Meaning) {
|
||||
* if(state == HANDLE_CREAT) {
|
||||
* this->a = *(int*) data;
|
||||
* this->b = 2
|
||||
* } else {
|
||||
* printf("%d\n", this->a + this->b);
|
||||
* }
|
||||
* }
|
||||
*
|
||||
* // Now you can use a handle in your local scopes
|
||||
* // Should print 42:
|
||||
* int main() {
|
||||
* int initializer = 40;
|
||||
* creat(struct Data, d, &initializer);
|
||||
* }
|
||||
*
|
||||
* Rem.: Unrelated trick, but you can typedef struct A A; // both typedef and forward declare
|
||||
*/
|
||||
|
||||
#define creat(TNAME, VNAME, VINIT) \
|
||||
[[gnu::always_inline]] inline void F(TNAME *v) { \
|
||||
TNAME##_lifetime_handler(HANDLE_DESTR, v, NULL); \
|
||||
} \
|
||||
[[gnu::cleanup(F)]] TNAME VNAME; \
|
||||
TNAME##_lifetime_handler(HANDLE_CREAT, VNAME, VINIT);
|
||||
|
||||
#define handle(TNAME) \
|
||||
static void [[gnu::always_inline]] inline TNAME##_lifetime_handler( \
|
||||
HANDLE_STATE state, \
|
||||
TNAME *this, \
|
||||
void *data)
|
||||
|
||||
#endif // MAG_HANDLE_H
|
4
handle_test.c
Normal file
4
handle_test.c
Normal file
@ -0,0 +1,4 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
}
|
3
innerf.c
3
innerf.c
@ -5,12 +5,13 @@ int main() {
|
||||
int b = 40;
|
||||
int c = 2;
|
||||
|
||||
// See: https://gcc.gnu.org/onlinedocs/gcc-4.3.0/gcc/Nested-Functions.html
|
||||
inline void lf(register int* r) {
|
||||
a += *r;
|
||||
}; // semicolon(;) not needeed!
|
||||
|
||||
/*
|
||||
// Same as above...
|
||||
// Same as above... but need auto storage class if you pre-declare
|
||||
inline auto void lf(register int* r);
|
||||
inline void lf(register int* r) {
|
||||
a += *r;
|
||||
|
Loading…
x
Reference in New Issue
Block a user