I try to do a little Web-Development with C++ and WebToolkit. My problem so far is that I have no idea how to access my class WebApp from the main function to populate data.
I have a few sensors for oxygen, light etc. In the WebApp-Class I defined some std::vector< OxygenSensor* > OSList which I would like to access from within the main function. How can this be done?
#include <Wt/WApplication.h>
#include <vector>
#include <string>
#include "View/WebApp.h"
//Make some default config during development
std::vector<std::string> make_webapp_config();
std::unique_ptr<Wt::WApplication> createApplication(const Wt::WEnvironment& env)
{
return std::make_unique<WebApp>(env);
}
int main(int argc, char **argv)
{
auto *wa = &createApplication;
return Wt::WRun(argv[0], make_webapp_config(), wa);
}
std::vector<std::string> make_webapp_config() {
std::vector<std::string> conf;
conf.push_back("--docroot");
conf.push_back(".");
conf.push_back("--http-address");
conf.push_back("0.0.0.0");
conf.push_back("--http-port");
conf.push_back("9090");
return conf;
}
WebApp(i.e. aWt::WApplicationobject) is only constructed when a session is started. Every browser session has its ownWt::WApplicationobject. So, it is not possible to access this object from the main function (as no session is constructed yet as the web server is not yet listening for incoming connections).Two possibilities exist:
OSListfrom within the constructor ofWebApp(or inside theWebApp::initialisefunction), which is automatically called by Wt when the session is created.OSListas a global (static) list, but pay attention to possible data races in that case.