Queuing a call in headers - c++

The goal is to allow header files to "register" an initializer function so that main can just iterate over those functions and call them. I've stumbled upon a solution which uses __attribute__, but it seems to be GCC-only (https://stackoverflow.com/a/37082249/7867841).
// header1.h
void myInitializer(){}
REGISTER_THIS(&myInitializer);
// header2.h
void myInitializer2(){}
REGISTER_THIS(&myInitializer2);
// main.cpp
...
for_each_registered_ptr(){ call_ptr(); } // calls myInitializer and myInitializer2
...
Is there a universal solution to this? Functions can be switched with classes or types if that's easier to implement.

You can abuse static function locals to do this, avoiding the static initialization order fiasco.
In init.h, we have this:
#ifndef INIT_H
#define INIT_H
#include <vector>
// Can be changed to std::function<...> or whatever you need.
typedef void (*init_fn)();
// Returns int so it can easily be used in a variable initializer.
int register_initializer(init_fn fn);
std::vector<init_fn> & get_initializers();
#endif
Then, in init.cpp:
#include "init.h"
int register_initializer(init_fn fn)
{
get_initializers().push_back(fn);
return 0;
}
std::vector<init_fn> & get_initializers()
{
static std::vector<init_fn> ip;
return ip;
}
A few notes, before we move on to the rest:
The static local is only initialized once, the first time the function is called.
The "global" vector is kind-of-leaked. It's unlikely this will be a problem unless you are adding tens of thousands of entries to this vector. You can always get_initializers().clear() to empty it out after using it.
We'd use it like so, in a.cpp:
#include <iostream>
#include "init.h"
static void a_init() { std::cout << "a_init()\n"; }
static auto dummy = register_initializer(a_init);
And, finally, we have our (rather simple) main.cpp:
#include "init.h"
int main() {
for (auto fn : get_initializers()) {
fn();
}
return 0;
}

Related

Could I write a library such that when loaded it first generates a dataset which could later be accessed by other functions?

Normally libraries' functions are only executed when called (unlike normal program which has an entry point like main()), but in this case I would like to ship a library that requires a data set, which the data set is too big that's it'll be better to generate on its own.
Which makes me wonder could I write a library that when loaded, executes a function that generates the data set (perhaps saving in global variable or an extern variable) which can later be used by other functions, something like following:
// lib.hh
#pragma once
struct Data{}; // Data is a custom class for holding the data set
extern Data dataset;
// lib.cc
#include"./lib.hh"
Data gen() {
// Generates the data here
return result;
}
double find(double key) {
// Searches data within `dataset` that has `key` as its key
}
So when loaded in another program, gen() should run automatically (and prior to any other functions), which then the program that loaded the library could call find(double).
Is such library implementation possible? Or should I better just summon a child process that generates the data, then retrieve results through pipe?
For cases like this I would recommend to use a static local variable
//header
struct Data{
Data() {
//Complex intialization code goes here
}
};
Data& data_gen() {
static Data data;
return data;
}
//caller/main
auto Data = data_gen(); //Initialization code run once
auto Data2 = data_gen(); //Reuse the variable no more runs
Even different threads can call that data_gen function, but of course they should only read it never write something unsynchronized. So I would generally give out a const reference to it, since sharing mutable data i.e. state via globals is very very bad style.
So prefer
const Data& data_gen() {
static Data data;
return data;
}
But although that approach is the most robust one, often you can just let it run before the main function runs, if you are a so called statically linked (the classic/default/standardized way in C++), but not if you are a .so/.dll/.dylib:
//Header lib.hpp
#pragma once
#include <iostream>
namespace lib_name {
struct Data{
Data() {
std::cout << "Ctor\n";
}
double find(double key) const{
return 0;
}
};
extern const Data dataset;
void foo();
}
//lib.cpp
#include <lib.hpp>
namespace lib_name {
const Data dataset;
void foo() {
dataset.find(5);
}
}
//main
#include <lib.hpp>
int main() {
lib_name::dataset.find(4.2);
lib_name::dataset.find(4.2);
lib_name::foo();
}
You will get only one "Ctor", before the main, but if your data generating is complex but can be done with std::array why not make it even easier?
//header
#include <array>
namespace lib_name {
struct Data{
constexpr Data() {
d_[0] = 42;
}
double find(size_t key) const{
return d_.at(key);
}
std::array<double,5> d_{};
};
inline constexpr Data dataset;
void foo();//just for demonstration optional of course
}
//lib.cpp optional
#include <lib.hpp>
#include <iostream>
namespace lib_name {
void foo() {
std::cout << dataset.find(0) << "\n";
}
}
Then your initialization code is run once, but by the compiler on compilation. It then just saves the result.

initializing a static (non-constant) variable of a class.

I have TestMethods.h
#pragma once
// strings and c-strings
#include <iostream>
#include <cstring>
#include <string>
class TestMethods
{
private:
static int nextNodeID;
// I tried the following line instead ...it says the in-class initializer must be constant ... but this is not a constant...it needs to increment.
//static int nextNodeID = 0;
int nodeID;
std::string fnPFRfile; // Name of location data file for this node.
public:
TestMethods();
~TestMethods();
int currentNodeID();
};
// Initialize the nextNodeID
int TestMethods::nextNodeID = 0;
// I tried this down here ... it says the variable is multiply defined.
I have TestMethods.cpp
#include "stdafx.h"
#include "TestMethods.h"
TestMethods::TestMethods()
{
nodeID = nextNodeID;
++nextNodeID;
}
TestMethods::~TestMethods()
{
}
int TestMethods::currentNodeID()
{
return nextNodeID;
}
I've looked at this example here: Unique id of class instance
It looks almost identical to mine. I tried both the top solutions. Neither works for me. Obviously I'm missing something. Can anyone point out what it is?
You need to move the definition of TestMethods::nextNodeID into the cpp file. If you have it in the header file then every file that includes the header will get it defined in them leading to multiple defenitions.
If you have C++17 support you can use the inline keyword to declare the static variable in the class like
class ExampleClass {
private:
inline static int counter = 0;
public:
ExampleClass() {
++counter;
}
};

Best practice for a global config

I have several c++ programs that are all reading a YAML configuration file in /etc/foo/config.yml. I have written a function that reads the config from the file
YAML::Node load_config();
(using the yaml-cpp library).
I would like this configuration to be loaded once, at the beginning of the main() function of my program, and then accessible everywhere as some kind of global variable.
Currently, many of my functions have extra parameters that are just values read from the configuration file. It could be avoided by having this global configuration, making my function definitions and calls much simpler and readable.
Side note: I am also using OpenMP for distributing computation, which means that the configuration must be accessible to all parallel processes.
Could someone give a tiny example of what this would look like when done the right way?
Thanks!
here's one way. It's a variation on the idea of the schwartz counter to manage a global singleton (for example, std::cout itself)
// globals.hpp
#include <istream>
struct globals_object
{
globals_object()
{
// record number of source files which instanciate a globals_object
++init_count_;
}
~globals_object()
{
// The last source file cleans up at program exit
if(--init_count_ == 0)
{
if (pimpl_)
{
delete pimpl_;
}
}
}
// internal implementation
struct impl
{
void load(std::istream& is)
{
// do loading code here
}
int get_param_a() const {
return a_;
}
int a_;
};
// (re)load global state
void load(std::istream&& is)
{
if (pimpl_) delete pimpl_;
pimpl_ = new impl;
pimpl_->load(is);
}
// public parameter accessor
int get_param_a() const {
return get_impl().get_param_a();
}
private:
static int init_count_;
static impl* pimpl_;
static impl& get_impl()
{
return *pimpl_;
}
};
// one of these per translation unit
static globals_object globals;
// globals.cpp
// note - not initialised - will be zero-initialised
// before global constructors are called
// you need one of these in a cpp file
int globals_object::init_count_;
globals_object::impl* globals_object::pimpl_;
// main file
// #include "globals.hpp"
#include <fstream>
int main()
{
globals.load(std::ifstream("settings.yml"));
}
// any other file
// #include "globals.hpp"
#include <iostream>
void foo()
{
std::cout << globals.get_param_a() << std::endl;
}

How can I generate a compile-time array of interrupt handlers in C++?

I'd like to be able to write my ISR in one place:
some_collection TimerHandlers;
// added to ISR table in linker script
void rawTimerIRQHandler() {
call_each_handler_in(handlers);
}
Such that I can then register handlers in other files
// file1.cpp
void ledTimerHandler1() {
}
register(ledTimerHandler1); //or in an init function if not possible here
// file2.cpp
void ledTimerHandler2() {
}
register(ledTimerHandler2); //or in an init function if not possible here
And when the hardware jumps to rawTimerIRQHandler, it executes ledTimerHandler1 and ledTimerHandler2 in some arbitrary order.
Obviously, I can implement this using something similar to a vector<void(*)()>, but since the number of these handlers is known at compile-time, is there any way I can generate an array (or template linked list) at compile-time? I'd like to avoid the dynamic memory allocation that comes with vector.
I'm open to using template<>, #define, or even GCC-specific attributes to acheive this goal.
The scaffolding's a bit tedious but once it's done the usage couldn't be simpler:
// example.h:
#include "Registered.h"
struct example : Registered<example> {};
// main.cc:
#include <iostream>
#include "example.h"
int main ()
{
for ( auto p = example::registry; p; p=p->chain )
std::cout << p << '\n';
}
// Registered.h :
template<class registered>
struct Registered {
static registered *registry;
registered *chain;
Registered() : chain(registry) {registry=static_cast<registered*>(this);}
};
// example.cc:
#include "example.h"
template<> example *Registered<example>::registry = 0;
static struct example first, second, third; // these can be defined anywhere w/ static duration
edit: moved the first,second,third declaration/definitions to satisfy my inner pedant
Absolutley. If I understand correctly, you just want a fixed array of function pointers to your handlers. Using C++11 syntax, and assuming 3 handlers just for the sake of the example,
#include <array>
const std::array<HandlerPtr, 3> handlers= {&ledTimerHandler1, &ledTimerHandler2, &ledTimerHandler3};
or using more classic C/C++ syntax
const HandlerPtr handlers[] = {&ledTimerHandler1, &ledTimerHandler2, &ledTimerHandler3};
Based off jthill's answer, here's what I'll probably end up using (since I don't need a generic form):
struct timer_handler {
static timer_handler *first = 0;
timer_handler *next;
void (*f)();
public:
timer_handler(void (*f)()) : next(first), f(f) { first = this;}
// connect this to the interrupt vector
static inline void executeAll() {
auto p = first;
while(p) {
p->f();
p = p->next;
}
}
};
//a.cpp
void foo() {
}
timer_handler tfoo = foo;
//b.cpp
void bar() {
}
timer_handler tbar = bar;

Function calls with class members?

Before I present the code which is found at the bottom of this post I would like to talk about the issue and the fix's that I do not desire. Okay basically I've created a GUI from scratch sort of and one requirement I wanted for this was allow components to have their own click executions so if i click a button or tab etc.. It would call Component->Execute(); Well normally you would do something like a switch statement of ids and if that components ID equaled n number then it would perform this action. Well that seemed kinda dumb to me and I thought there has to be a better way. I eventually tried to incorporate a feature in JAVA where you would do like Component.AddActionListener(new ActionListener( public void execute(ActionEvent ae) { })); or something like that and I thought that this feature has to be possible in C++. I eventually came across storing void functions into a variable in which could be executed at any time and modified at any time. However I hadn't noticed an issue and that was this only worked with static functions. So below you'll see my problem. I've patched the problem by using a pointer to SomeClass however this would mean having an individual function call for every class type is there no way to store a function callback to a non-static class member without doing the below strategy? and instead doing a strategy like the commented out code?
//Main.cpp
#include <iostream> //system requires this.
#include "SomeClass.h"
void DoSomething1(void)
{
std::cout << "We Called Static DoSomething1\n";
}
void DoSomething2(void)
{
std::cout << "We Called Static DoSomething2\n";
}
int main()
{
void (*function_call2)(SomeClass*);
void (*function_call)() = DoSomething1; //This works No Problems!
function_call(); //Will Call the DoSomething1(void);
function_call = DoSomething2; //This works No Problems!
function_call(); //Will Call the DoSomething2(void);
SomeClass *some = new SomeClass(); //Create a SomeClass pointer;
function_call = SomeClass::DoSomething3; //Static SomeClass::DoSomething3();
function_call(); //Will Call the SomeClass::DoSomething3(void);
//function_call = some->DoSomething4; //Non-Static SomeClass::DoSomething4 gives an error.
//function_call(); //Not used because of error above.
function_call2 = SomeClass::DoSomething5; //Store the SomeClass::DoSomething(SomeClass* some);
function_call2(some); //Call out SomeClass::DoSomething5 which calls on SomeClass::DoSomething4's non static member.
system("pause");
return 0;
}
//SomeClass.hpp
#pragma once
#include <iostream>
class SomeClass
{
public:
SomeClass();
~SomeClass();
public:
static void DoSomething3(void);
void DoSomething4(void);
static void DoSomething5(SomeClass* some);
};
//SomeClass.cpp
#include "SomeClass.h"
SomeClass::SomeClass(void)
{
}
SomeClass::~SomeClass(void)
{
}
void SomeClass::DoSomething3(void)
{
std::cout << "We Called Static DoSomething3\n";
}
void SomeClass::DoSomething4(void)
{
std::cout << "We Called Non-Static DoSomething4\n";
}
void SomeClass::DoSomething5(SomeClass *some)
{
some->DoSomething4();
}
Secondary Fix for what I'll do not an exact answer I wanted but it meets my needs for now along with allowing additional features which would have become overly complicate had this not existed.
//Component.hpp
#pragma once
#include <iostream>
#include <windows.h>
#include <d3dx9.h>
#include <d3d9.h>
#include "Constants.hpp"
#include "ScreenState.hpp"
#include "ComponentType.hpp"
using namespace std;
class Component
{
static void EMPTY(void) { }
static void EMPTY(int i) { }
public:
Component(void)
{
callback = EMPTY;
callback2 = EMPTY;
callback_id = -1;
}
Component* SetFunction(void (*callback)())
{
this->callback = callback;
return this;
}
Component* SetFunction(void (*callback2)(int), int id)
{
this->callback_id = id;
this->callback2 = callback2;
return this;
}
void execute(void)
{
callback();
callback2(callback_id);
}
}
The syntax for pointers-to-member-functions is as follows:
struct Foo
{
void bar(int, int);
void zip(int, int);
};
Foo x;
void (Foo::*p)(int, int) = &Foo::bar; // pointer
(x.*p)(1, 2); // invocation
p = &Foo::zip;
(x.*p)(3, 4); // invocation
Mind the additional parentheses in the function invocation, which is needed to get the correct operator precedence. The member-dereference operator is .* (and there's also ->* from an instance pointer).