Accessing WebToolkit WApplication class from main

54 Views Asked by At

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;
}
1

There are 1 best solutions below

0
m7913d On

WebApp (i.e. a Wt::WApplication object) is only constructed when a session is started. Every browser session has its own Wt::WApplication object. 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:

  • Initialise the OSList from within the constructor of WebApp (or inside the WebApp::initialise function), which is automatically called by Wt when the session is created.
  • Store the OSList as a global (static) list, but pay attention to possible data races in that case.