feat add __attribute__((construcotr))

This commit is contained in:
tqcq
2024-04-29 21:45:37 +08:00
parent fc1ad57bbb
commit 563a1bfcfe
2 changed files with 36 additions and 37 deletions

View File

@@ -37,38 +37,38 @@
#include "base/logging.h"
class GoogleInitializer {
public:
typedef void (*VoidFunction)(void);
GoogleInitializer(const char* name, VoidFunction ctor, VoidFunction dtor)
: name_(name), destructor_(dtor) {
RAW_VLOG(10, "<GoogleModuleObject> constructing: %s\n", name_);
if (ctor)
ctor();
}
~GoogleInitializer() {
RAW_VLOG(10, "<GoogleModuleObject> destroying: %s\n", name_);
if (destructor_)
destructor_();
}
public:
typedef void (*VoidFunction)(void);
private:
const char* const name_;
const VoidFunction destructor_;
GoogleInitializer(const char *name, VoidFunction ctor, VoidFunction dtor) : name_(name), destructor_(dtor)
{
RAW_VLOG(10, "<GoogleModuleObject> constructing: %s\n", name_);
if (ctor) ctor();
}
~GoogleInitializer()
{
RAW_VLOG(10, "<GoogleModuleObject> destroying: %s\n", name_);
if (destructor_) destructor_();
}
const char *name() const { return name_; }
private:
const char *const name_;
const VoidFunction destructor_;
};
#define REGISTER_MODULE_INITIALIZER(name, body) \
namespace { \
static void google_init_module_##name () { body; } \
GoogleInitializer google_initializer_module_##name(#name, \
google_init_module_##name, NULL); \
}
#define REGISTER_MODULE_DESTRUCTOR(name, body) \
namespace { \
static void google_destruct_module_##name () { body; } \
GoogleInitializer google_destructor_module_##name(#name, \
NULL, google_destruct_module_##name); \
}
#define REGISTER_MODULE_INITIALIZER(name, body) \
namespace { \
static void google_init_module_##name() __attribute__((constructor)) { body; } \
GoogleInitializer google_initializer_module_##name(#name, google_init_module_##name, NULL); \
}
#define REGISTER_MODULE_DESTRUCTOR(name, body) \
namespace { \
static void google_destruct_module_##name() __attribute__((constructor)) { body; } \
GoogleInitializer google_destructor_module_##name(#name, NULL, google_destruct_module_##name); \
}
#endif /* _GOOGLEINIT_H */