Skip to content

Commit

Permalink
Refactoring some global singletons.
Browse files Browse the repository at this point in the history
  • Loading branch information
checkroom authored and KaruroChori committed Dec 20, 2024
1 parent 0a63d3c commit d96010a
Show file tree
Hide file tree
Showing 14 changed files with 131 additions and 66 deletions.
4 changes: 0 additions & 4 deletions include/globals.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,4 @@ struct global_ctx_t{
field_models_t value_models;
};

namespace singleton{
extern vs_test_debug_t debug;
}

}
50 changes: 39 additions & 11 deletions include/loader.hpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,48 @@
#pragma once
/**
* @file loader.hpp
* @author karurochari
* @brief Utility function and classes to handle everything related to application loading.
*
* @copyright Copyright (c) 2024
*
*/

/*
Glue logic exposed to the final application to avoid exposing too many libraries.
*/
#pragma once

#include "globals.hpp"

class ui_tree_xml;
namespace vs{
struct app_loader{
ui_tree_xml* root;

class ui_tree;

struct loader_t{
private:
ui_tree* root;

public:
/**
* @brief Load a new application
*
* @param globals The current global context object.
* @param profile Filename of the profile to use for loading. If `nullptr` default will be used.
* @param path The virtual path to the app root file.
*/
loader_t(global_ctx_t& globals, const char* profile, const char* path);

~loader_t();

/**
* @brief After building in the constructor, run the application once ready.
*
* @return int 0 if everything went ok, else error codes.
*/
int run();

app_loader(global_ctx_t& globals, const char* profile, const char* path);
~app_loader();
int run();
int test();
/**
* @brief RUn the application in test mode.
*
* @return int if everything went ok, else error codes.
*/
int test();
};
}
18 changes: 18 additions & 0 deletions include/settings.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @file settings.hpp
* @author karurochari
* @brief Structures to store the instance settings
*
* @copyright Copyright (c) 2024
*
*/

#pragma once

namespace vs{

struct settings_t{

};

}
32 changes: 32 additions & 0 deletions include/singletons.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma once

#include <utils/app-env.hpp>
#include <utils/env.hpp>
#include <utils/paths.hpp>
#include <utils/policies.hpp>
#include <cache/memory-storage.hpp>
#include <cache/res-storage.hpp>
#include <cache/kv-storage.hpp>
#include <cache/secrets.hpp>

namespace vs{

namespace singleton{

#ifdef VS_USE_NETWORKING_CURL
struct curl_t{
curl_t();
~curl_t();
} extern curl;
#endif

struct debug_t{
FILE* fd = nullptr;
debug_t();
~debug_t();

void operator()(const char* field, const char* value);
} extern debug;
}

}
8 changes: 0 additions & 8 deletions include/utils/env.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,6 @@ struct js_rt_t{
void* operator()();
};

struct vs_test_debug_t{
FILE* fd = nullptr;
vs_test_debug_t();
~vs_test_debug_t();

void operator()(const char* field, const char* value);
};

/**
* @brief In case it is not configured already, it prepares the SQLITE file and all the pre-compiled queries needed.
*
Expand Down
2 changes: 1 addition & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,8 @@ vs_fltk = shared_library(
'./src/utils/policies.internal.cpp',
'./src/utils/tcc-wrap.cpp',

'./src/singletons.cpp',
'./src/fetcher.cpp',

'./src/loader.cpp',

src_components,
Expand Down
2 changes: 1 addition & 1 deletion src/app/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ int run(const char* path, const char *entry, const char* profile, const char* te
globals.env.computed_policies.testing=true;
}

app_loader loader(globals,profile,entry);
loader_t loader(globals,profile,entry);

//TODO implement test
if(tests!=nullptr){
Expand Down
2 changes: 1 addition & 1 deletion src/cbindings/vs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <cbindings/vs.h>
#include <iostream>
#include <pugixml.hpp>
#include <globals.hpp>
#include <singletons.hpp>

void vs_hello_world(){
std::cout<<"Hello world!\n";
Expand Down
9 changes: 0 additions & 9 deletions src/globals.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,2 @@
#include <cstdio>
#include <globals.hpp>
#include <cstdlib>

namespace vs{
namespace singleton{

vs_test_debug_t debug;

}
}
22 changes: 5 additions & 17 deletions src/loader.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
#include <iostream>

#ifdef VS_USE_NETWORKING_CURL
#include <curl/curl.h>
#endif


#include <ui-tree.hpp>
#include <ui-tree.xml.hpp>
#include <loader.hpp>

//TODO:Remove

namespace vs {
app_loader::app_loader(global_ctx_t& globals, const char *profile, const char *path) {

#ifdef VS_USE_NETWORKING_CURL
curl_global_init(CURL_GLOBAL_ALL);
#endif
loader_t::loader_t(global_ctx_t& globals, const char *profile, const char *path) {

pugi::xml_document doc;

static constexpr const char *embedded_profile = R"(<root></root>)";
Expand Down Expand Up @@ -48,12 +39,12 @@ app_loader::app_loader(global_ctx_t& globals, const char *profile, const char *p
}
}

int app_loader::test() {
int loader_t::test() {
return root->runtime_testsuite();
// TODO
}

int app_loader::run() {
int loader_t::run() {
root->globals->mem_storage.cleanup();
root->cleanup();
if (!root->globals->env.computed_policies.headless) {
Expand All @@ -67,10 +58,7 @@ int app_loader::run() {
}
}

app_loader::~app_loader() {
#ifdef VS_USE_NETWORKING_CURL
curl_global_cleanup();
#endif
loader_t::~loader_t() {
if (root != nullptr)
delete root;
}
Expand Down
2 changes: 1 addition & 1 deletion src/pipelines/tcc-c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "utils/paths.hpp"
#include <cstddef>

#include <iostream>
#include <singletons.hpp>
#include <pipelines/tcc-c.hpp>
#include <ui-tree.xml.hpp>

Expand Down
32 changes: 32 additions & 0 deletions src/singletons.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <singletons.hpp>
#include <chrono>

#ifdef VS_USE_NETWORKING_CURL
#include <curl/curl.h>
#endif

namespace vs{
namespace singleton{

debug_t::debug_t(){auto file=getenv("VS_DEBUG_FILE");if(file!=nullptr)fd=fopen(file,"w+");}
debug_t::~debug_t(){if(fd!=nullptr)fclose(fd);}

void debug_t::operator()(const char* field, const char* value){
if(fd==nullptr)return;
else{
auto now = std::chrono::system_clock::now();
fprintf(fd,"%s\t%s\t%ld\n",field,value,std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch()).count());
}
}


#ifdef VS_USE_NETWORKING_CURL
curl_t::curl_t(){curl_global_init(CURL_GLOBAL_ALL);}
curl_t::~curl_t(){curl_global_cleanup();}

curl_t curl;
debug_t debug;
#endif

}
}
1 change: 1 addition & 0 deletions src/ui-tree.xml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <ui-tree.xml.hpp>

#include <globals.hpp>
#include <singletons.hpp>
#include <fetcher.hpp>

#include <utils/paths.hpp>
Expand Down
13 changes: 0 additions & 13 deletions src/utils/env.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,19 +100,6 @@ js_rt_t::~js_rt_t(){
void* js_rt_t::operator()(){return rt;}


vs_test_debug_t::vs_test_debug_t(){auto file=getenv("VS_DEBUG_FILE");if(file!=nullptr)fd=fopen(file,"w+");}
vs_test_debug_t::~vs_test_debug_t(){if(fd!=nullptr)fclose(fd);}

void vs_test_debug_t::operator()(const char* field, const char* value){
if(fd==nullptr)return;
else{
auto now = std::chrono::system_clock::now();
fprintf(fd,"%s\t%s\t%ld\n",field,value,std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch()).count());
}
}



void prepare_db(){

try
Expand Down

0 comments on commit d96010a

Please sign in to comment.