Rework the RCPPFramework class to be a bit more user friendly.

This commit is contained in:
Relintai 2022-02-05 18:17:36 +01:00
parent 3757114a21
commit b833a3ee3a
2 changed files with 68 additions and 21 deletions

View File

@ -11,32 +11,49 @@
// Backends
#include "crypto_backends/hash_hashlib/setup.h"
void RCPPFramework::initialize() {
if (get_singleton() != nullptr) {
RLOG_ERR("RCPPFramework: has already beed initialized!");
void RCPPFramework::create() {
new RCPPFramework();
}
void RCPPFramework::destroy() {
delete _instance;
}
void RCPPFramework::create_and_init() {
new RCPPFramework();
#if DATABASES_ENABLED
initialize_database_backends();
#endif
backend_hash_hashlib_install_providers();
PlatformInitializer::allocate_all();
RCPPFramework::get_singleton()->initialize();
}
void RCPPFramework::initialize(int argc, char **argv, char **envp) {
initialize();
void RCPPFramework::create_and_init(int argc, char **argv, char **envp) {
new RCPPFramework();
RCPPFramework::get_singleton()->initialize();
RCPPFramework::get_singleton()->setup_args(argc, argv, envp);
}
void RCPPFramework::initialize() {
if (_initialized) {
RLOG_ERR("RCPPFramework: has already beed initialized!");
return;
}
_initialized = true;
_do_initialize();
}
void RCPPFramework::setup_args(int argc, char **argv, char **envp) {
// Don't use the error macros here, they might not work before initialization
if (!_initialized) {
printf("ERROR! RCPPFramework::set_args: You have to call initialize() first!\n");
return;
}
PlatformInitializer::arg_setup(argc, argv, envp);
}
void RCPPFramework::uninitialize() {
delete _instance;
PlatformInitializer::free_all();
}
void RCPPFramework::manage_object(Object *obj) {
@ -45,6 +62,8 @@ void RCPPFramework::manage_object(Object *obj) {
RCPPFramework::RCPPFramework() {
_instance = this;
_initialized = false;
}
RCPPFramework::~RCPPFramework() {
@ -63,3 +82,17 @@ RCPPFramework *RCPPFramework::get_singleton() {
}
RCPPFramework *RCPPFramework::_instance = nullptr;
void RCPPFramework::_do_initialize() {
#if DATABASES_ENABLED
initialize_database_backends();
#endif
backend_hash_hashlib_install_providers();
PlatformInitializer::allocate_all();
}
void RCPPFramework::_do_uninitialize() {
PlatformInitializer::free_all();
}

View File

@ -10,9 +10,18 @@ class RCPPFramework : Object {
RCPP_OBJECT(RCPPFramework, Object);
public:
static void initialize();
static void initialize(int argc, char **argv, char **envp);
static void uninitialize();
// Helper methods to allocate and destroy the singleton.
// Note that creating / deleting an instance manually in your app will also work.
static void create();
static void destroy();
//Usie these if you don't want to mess with the default settings.
static void create_and_init();
static void create_and_init(int argc, char **argv, char **envp);
void initialize();
void uninitialize();
void setup_args(int argc, char **argv, char **envp);
void manage_object(Object* obj);
@ -22,6 +31,11 @@ public:
static RCPPFramework *get_singleton();
protected:
virtual void _do_initialize();
virtual void _do_uninitialize();
bool _initialized;
Vector<Object *> _managed_objects;
static RCPPFramework *_instance;