refactor this with self + add c++ compat
This commit is contained in:
parent
bf5ad27064
commit
91b20bc366
@ -7,10 +7,10 @@ struct Vektor {
|
|||||||
int *v;
|
int *v;
|
||||||
}; handle(Vektor) {
|
}; handle(Vektor) {
|
||||||
if(state == HANDLE_CREAT) {
|
if(state == HANDLE_CREAT) {
|
||||||
this->count = *(int*) data;
|
self->count = *(int*) data;
|
||||||
this->v = calloc(this->count, sizeof(int));
|
self->v = (int*) calloc(self->count, sizeof(int));
|
||||||
} else if (state == HANDLE_DESTR){
|
} else if (state == HANDLE_DESTR){
|
||||||
if(this->v) free(this->v);
|
if(self->v) free(self->v);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
43
handle.h
43
handle.h
@ -1,9 +1,6 @@
|
|||||||
#ifndef MAG_HANDLE_H
|
#ifndef MAG_HANDLE_H
|
||||||
#define MAG_HANDLE_H
|
#define MAG_HANDLE_H
|
||||||
|
/* Simple single-header library that makes you have RAII in gcc (also works in C++ compilers) */
|
||||||
#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 */
|
/** Tells your constructor/destructor handler function about the state */
|
||||||
enum HANDLE_STATE {
|
enum HANDLE_STATE {
|
||||||
@ -11,6 +8,8 @@ enum HANDLE_STATE {
|
|||||||
HANDLE_DESTR
|
HANDLE_DESTR
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#ifndef __cplusplus
|
||||||
|
|
||||||
#define creat(TNAME, VNAME, VINIT) \
|
#define creat(TNAME, VNAME, VINIT) \
|
||||||
[[gnu::always_inline]] inline void VNAME##_cleanuper(struct TNAME *v) { \
|
[[gnu::always_inline]] inline void VNAME##_cleanuper(struct TNAME *v) { \
|
||||||
TNAME##_lifetime_handler(HANDLE_DESTR, v, NULL); \
|
TNAME##_lifetime_handler(HANDLE_DESTR, v, NULL); \
|
||||||
@ -21,8 +20,34 @@ enum HANDLE_STATE {
|
|||||||
#define handle(TNAME) \
|
#define handle(TNAME) \
|
||||||
static inline void TNAME##_lifetime_handler( \
|
static inline void TNAME##_lifetime_handler( \
|
||||||
enum HANDLE_STATE state, \
|
enum HANDLE_STATE state, \
|
||||||
struct TNAME *this, \
|
struct TNAME *self, \
|
||||||
void *data)
|
void *data)
|
||||||
|
|
||||||
|
#else /* C++ compilers better be able to compile code written with us */
|
||||||
|
|
||||||
|
#define creat(TNAME, VNAME, VINIT) \
|
||||||
|
TNAME##_raiicpp VNAME(VINIT);
|
||||||
|
|
||||||
|
#define handle(TNAME) \
|
||||||
|
struct TNAME##_raiicpp; \
|
||||||
|
static inline void TNAME##_lifetime_handler( \
|
||||||
|
enum HANDLE_STATE state, \
|
||||||
|
struct TNAME##_raiicpp *self, \
|
||||||
|
void *data); \
|
||||||
|
struct TNAME##_raiicpp : public TNAME { \
|
||||||
|
inline TNAME##_raiicpp(void *data) { \
|
||||||
|
TNAME##_lifetime_handler(HANDLE_CREAT, this, data); \
|
||||||
|
} \
|
||||||
|
inline ~TNAME##_raiicpp() { \
|
||||||
|
TNAME##_lifetime_handler(HANDLE_DESTR, this, nullptr); \
|
||||||
|
} \
|
||||||
|
}; \
|
||||||
|
static inline void TNAME##_lifetime_handler( \
|
||||||
|
enum HANDLE_STATE state, \
|
||||||
|
struct TNAME##_raiicpp *self, \
|
||||||
|
void *data)
|
||||||
|
|
||||||
|
#endif /* __cplusplus */
|
||||||
/*
|
/*
|
||||||
* EXAMPLE:
|
* EXAMPLE:
|
||||||
*
|
*
|
||||||
@ -34,10 +59,10 @@ enum HANDLE_STATE {
|
|||||||
*
|
*
|
||||||
* handle(Meaning) {
|
* handle(Meaning) {
|
||||||
* if(state == HANDLE_CREAT) {
|
* if(state == HANDLE_CREAT) {
|
||||||
* this->a = *(int*) data;
|
* self->a = *(int*) data;
|
||||||
* this->b = 2;
|
* self->b = 2;
|
||||||
* } else {
|
* } else {
|
||||||
* printf("%d\n", this->a + this->b);
|
* printf("%d\n", self->a + self->b);
|
||||||
* }
|
* }
|
||||||
* }
|
* }
|
||||||
*
|
*
|
||||||
@ -50,4 +75,4 @@ enum HANDLE_STATE {
|
|||||||
*
|
*
|
||||||
* Rem.: Unrelated trick, but you can typedef struct A A; // both typedef and forward declare
|
* Rem.: Unrelated trick, but you can typedef struct A A; // both typedef and forward declare
|
||||||
*/
|
*/
|
||||||
#endif // MAG_HANDLE_H
|
#endif /* MAG_HANDLE_H */
|
||||||
|
@ -8,11 +8,11 @@ struct Meaning {
|
|||||||
|
|
||||||
handle(Meaning) {
|
handle(Meaning) {
|
||||||
if(state == HANDLE_CREAT) {
|
if(state == HANDLE_CREAT) {
|
||||||
this->a = *(int*) data;
|
self->a = *(int*) data;
|
||||||
this->b = 2;
|
self->b = 2;
|
||||||
} else {
|
} else {
|
||||||
// Destructor
|
// Destructor
|
||||||
printf("%d\n", this->a + this->b);
|
printf("%d\n", self->a + self->b);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
2
makefile
2
makefile
@ -8,6 +8,8 @@ gcc:
|
|||||||
gcc raii_test.c -o raii_test
|
gcc raii_test.c -o raii_test
|
||||||
g++ raii_test.cpp -o raii_test_cpp
|
g++ raii_test.cpp -o raii_test_cpp
|
||||||
gcc handle_test.c -o handle_test
|
gcc handle_test.c -o handle_test
|
||||||
|
gcc array_handle_test.c -o array_handle_test
|
||||||
|
g++ array_handle_test.c -o array_handle_test_gpp
|
||||||
clang:
|
clang:
|
||||||
clang macroname.c -o macroname
|
clang macroname.c -o macroname
|
||||||
clang count.c -o count
|
clang count.c -o count
|
||||||
|
Loading…
x
Reference in New Issue
Block a user