From b833a3ee3a6b1362a6278eaa104b56b30b1b9f9d Mon Sep 17 00:00:00 2001 From: Relintai Date: Sat, 5 Feb 2022 18:17:36 +0100 Subject: [PATCH] Rework the RCPPFramework class to be a bit more user friendly. --- rcpp_framework.cpp | 69 ++++++++++++++++++++++++++++++++++------------ rcpp_framework.h | 20 ++++++++++++-- 2 files changed, 68 insertions(+), 21 deletions(-) diff --git a/rcpp_framework.cpp b/rcpp_framework.cpp index b9aa6fe..404513b 100644 --- a/rcpp_framework.cpp +++ b/rcpp_framework.cpp @@ -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(); - -#if DATABASES_ENABLED - initialize_database_backends(); -#endif - - backend_hash_hashlib_install_providers(); - - PlatformInitializer::allocate_all(); +} +void RCPPFramework::destroy() { + delete _instance; } -void RCPPFramework::initialize(int argc, char **argv, char **envp) { - initialize(); +void RCPPFramework::create_and_init() { + new RCPPFramework(); + + RCPPFramework::get_singleton()->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,10 +62,12 @@ void RCPPFramework::manage_object(Object *obj) { RCPPFramework::RCPPFramework() { _instance = this; + + _initialized = false; } RCPPFramework::~RCPPFramework() { - //delete in reverse order added + // delete in reverse order added for (int i = _managed_objects.size() - 1; i >= 0; --i) { delete _managed_objects[i]; } @@ -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(); +} \ No newline at end of file diff --git a/rcpp_framework.h b/rcpp_framework.h index 80c7a86..a502d85 100644 --- a/rcpp_framework.h +++ b/rcpp_framework.h @@ -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 _managed_objects; static RCPPFramework *_instance;