Best practice for a global config - c++

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

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.

Queuing a call in headers

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

making consistency of data member in the whole programme without using static keyword

In the below programme i use one boolean variable named check , which is being accessed inside main function by two objects of Tst1 and Test2 . But the value of check variable is not maintained in the programme . we can use static but i want to know some alternative way ..could anyone give me some hints on it ?
Thanks in advance .
Inside jointdeatils.h
#pragma once
class Jointdetails
{
public:
Jointdetails(void);
~Jointdetails(void);
bool check;
};
Inside jointdeatils.cpp
#include "Jointdetails.h"
Jointdetails::Jointdetails(void)
{
check = false ;
}
Jointdetails::~Jointdetails(void)
{
}
Inside analyzer.h
#pragma once
#include "Jointdetails.h"
class Analyzer
{
public:
Analyzer(void);
Jointdetails* GetJointDetails();
Jointdetails* m_ptheCTJointDetails;
~Analyzer(void);
};
Inside analyzer.cpp
#include "Analyzer.h"
#include "stddef.h"
Analyzer::Analyzer(void)
{
m_ptheCTJointDetails = new Jointdetails();
}
Analyzer::~Analyzer(void)
{
}
Jointdetails* Analyzer::GetJointDetails()
{
if(m_ptheCTJointDetails)
return m_ptheCTJointDetails;
else
return NULL;
}
Inside Test1.h
#pragma once
#include "Analyzer.h"
class Tst1
{
public:
Tst1(void);
Analyzer *analyzer1 ;
public:
~Tst1(void);
};
Inside Test1.cpp
#include "Tst1.h"
Tst1::Tst1(void)
{
analyzer1 = new Analyzer ;
}
Tst1::~Tst1(void)
{
}
Inside Test2.h
#pragma once
#include "Analyzer.h"
class Test2
{
public:
Test2(void);
Analyzer *analyzer2 ;
public:
~Test2(void);
};
Inside Test2.cpp
#include "Test2.h"
Test2::Test2(void)
{
analyzer2 = new Analyzer ;
}
Test2::~Test2(void)
{
}
Inside main.cpp
#include "Test2.h"
#include "Tst1.h"
#include "stdio.h"
int main()
{
Tst1 *test1 = new Tst1 ; //check = false
Test2 *test2 = new Test2 ; //check = false
test1->analyzer1->GetJointDetails()->check = true ;
if(test2->analyzer2->GetJointDetails()->check )
printf("Check value is changed");
else
printf("Check value is not changed");
return 0 ;
}
There are only two possible ways to do so:
Use static storage data
Pass automatic or dynamic storage data as parameters to destination functions/ctors
Way #1 is more handy as you can access such data directly from any function. But it shall be considered as bad design because it almost the same thing as evil global variables.
Way #2 is more correct (see answer by justin for example) but could be a bit irritating - you will need to pass required data as parameter to each required function and/or store data as class data member. Not a pleasant work in case of many classes/nested calls.
Nevertheless, if you don't care on drawbacks of way #1 consider singleton-on-demand concept. It allows to use static data in more dynamic way - create on demand, share access by several users and destroy when nobody use it anymore. See example (several details, includes etc skipped for brevity):
JointDetails.h
class JointDetails
{
// Actual class definition
// ...
public:
// Function accessing to JointDetails instance
static std::shared_ptr<JointDetails> Get();
};
JointDetails.cpp
std::shared_ptr<JointDetails> JointDetails::Get()
{
static std::weak_ptr<JointDetails> s_trackInstance;
if(s_trackInstance.expired())
{
auto instance = std::make_shared<JointDetails>();
s_trackInstance = instance;
return instance;
}
return s_trackInstance.lock();
}
Analyzer.h
// All instances of Analyzer share the same instance of JointDetails.
// But JointDetails instance is created dynamically only when first instance of
// Analyzer is created and destoyed when the last instance of Analyzer is destroyed.
class Analyzer
{
std::shared_ptr<JointDetails> m_details;
public:
Analyzer(): m_details(JointDetails::Get()) {}
const JointDetails& GetDetails() const { return *m_details; }
};
You're either going to have to make check static, or JointDetails a singleton (which also uses the static keyword).
If you make check static, you are saying that all instances of JointDetails have the same check.
If you make JointDetails a singleton, then you're saying that every reference to a JointDetails object is the same object, so your Tst1 and your Test2 will both have a pointer to the same object.
I think the latter is what you're looking for:
Jointdetails.h
#pragma once
class Jointdetails
{
public:
~Jointdetails(void);
bool check;
static Jointdetails* getInstance();
private:
Jointdetails(void);
};
Jointdetails.cpp
#include "Jointdetails.h"
Jointdetails::Jointdetails(void)
{
check = false ;
}
Jointdetails::~Jointdetails(void)
{
}
Jointdetails* Jointdetails::getInstance() {
static Jointdetails s_instance;
return &s_instance;
}
Analyzer.cpp
Analyzer::Analyzer(void)
{
m_ptheCTJointDetails = Jointdetails::getInstance();
}
0) you needn't use new so often.
1) you can construct your objects with the joint details as a parameter in your constructor.
To illustrate:
class Tst1 {
public:
Tst1(Jointdetails& pJointdetails) : analyzer1(pJointdetails) {
}
Analyzer analyzer1;
public:
~Tst1(void);
};
int RunProgram(Jointdetails& pJointdetails) {
Tst1(pJointdetails);
...
}
int main() {
Jointdetails jointdetails;
const int result(RunProgram(jointdetails));
return result;
}

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).

How do I implement convenient logging without a Singleton?

My current implementation, simplified:
#include <string>
#include <memory>
class Log
{
public:
~Log() {
// closing file-descriptors, etc...
}
static void LogMsg( const std::string& msg )
{
static std::unique_ptr<Log> g_singleton;
if ( !g_singleton.get() )
g_singleton.reset( new Log );
g_singleton->logMsg( msg );
}
private:
Log() { }
void logMsg( const std::string& msg ) {
// do work
}
};
In general, I am satisfied with this implementation because:
lazy instantiation means I don't pay unless I use it
use of unique_ptr means automatic cleanup so valgrind is happy
relatively simple, easy-to-understand implementation
However, the negatives are:
singletons aren't conducive to unit-testing
dissonance in the back of my mind for introducing a pseudo-global (a bit of a code smell)
So here are my questions directed towards those developers who are successful in exorcising all singletons from their C++ code:
What kind of non-Singleton implementation do you use for application-wide logging?
Is the interface as simple and accessible as a Log::LogMsg() call above?
I want to avoid passing a Log instance all over my code, if at all possible - note: I am asking because, I, too, want to exorcise all Singletons from my code if there is a good, reasonable alternative.
First: the use of std::unique_ptr is unnecessary:
void Log::LogMsg(std::string const& s) {
static Log L;
L.log(s);
}
Produces exactly the same lazy initialization and cleanup semantics without introducing all the syntax noise (and redundant test).
Now that is out of the way...
Your class is extremely simple. You might want to build a slightly more complicated version, typical requirements for log messages are:
timestamp
level
file
line
function
process name / thread id (if relevant)
on top of the message itself.
As such, it is perfectly conceivable to have several objects with different parameters:
// LogSink is a backend consuming preformatted messages
// there can be several different instances depending on where
// to send the data
class Logger {
public:
Logger(Level l, LogSink& ls);
void operator()(std::string const& message,
char const* function,
char const* file,
int line);
private:
Level _level;
LogSink& _sink;
};
And you usually wrap the access inside a macro for convenience:
#define LOG(Logger_, Message_) \
Logger_( \
static_cast<std::ostringstream&>( \
std::ostringstream().flush() << Message_ \
).str(), \
__FUNCTION__, \
__FILE__, \
__LINE__ \
);
Now, we can create a simple verbose logger:
Logger& Debug() {
static Logger logger(Level::Debug, Console);
return logger;
}
#ifdef NDEBUG
# define LOG_DEBUG(_) do {} while(0)
#else
# define LOG_DEBUG(Message_) LOG(Debug(), Message_)
#endif
And use it conveniently:
int foo(int a, int b) {
int result = a + b;
LOG_DEBUG("a = " << a << ", b = " << b << " --> result = " << result)
return result;
}
The purpose of this rant ? Not all that is a global need be unique. The uniqueness of Singletons is generally useless.
Note: if the bit of magic involving std::ostringstream scares you, this is normal, see this question
I'd go with the simple, pragmatic solution:
you want a solution that is globally accessible. For the most part, I try to avoid globals, but for loggers, let's face it, it's usually impractical.
So, we do need something to be globally accessible.
But, we don't want the additional "there can be only one" restriction that a singleton confers. Some of your unit tests might want to instantiate their own private logger. Others might want to replace the global logger, perhaps.
So make it a global. A plain old simple global variable.
This still doesn't fully solve the problem with unit testing, admittedly, but we can't always have everything we want. ;)
As pointed out in the comment, you need to consider the initialization order for globals, which, in C++, is partly undefined.
In my code, that is generally not a problem, because I rarely have more than one global (my logger), and I stick rigidly to a rule of never allowing globals to depend on each others.
But it's something you have to consider, at least.
I really like the following interface since it uses streaming. Of course you can add channels, time and thread information to it. Another possible extension is to use the __FILE__ and __LINE__ macros and add it as parameters to the constructor. You could even add a variadic template function if you do not like the stream syntax. If you want to store some configuration you could add them to some static variables.
#include <iostream>
#include <sstream>
class LogLine {
public:
LogLine(std::ostream& out = std::cout) : m_Out(out) {}
~LogLine() {
m_Stream << "\n";
m_Out << m_Stream.rdbuf();
m_Out.flush();
}
template <class T>
LogLine& operator<<(const T& thing) { m_Stream << thing; return *this; }
private:
std::stringstream m_Stream;
std::ostream& m_Out;
//static LogFilter...
};
int main(int argc, char *argv[])
{
LogLine() << "LogLine " << 4 << " the win....";
return 0;
}
// file ILoggerImpl.h
struct ILoggerImpl
{
virtual ~ILoggerImpl() {}
virtual void Info(std::string s) = 0;
virtual void Warning(std::string s) = 0;
virtual void Error(std::string s) = 0;
};
// file logger.h //
#include "ILoggerImpl.h"
class CLogger: public ILoggerImpl
{
public:
CLogger():log(NULL) { }
//interface
void Info(std::string s) {if (NULL==log) return; log->Info(s); }
void Warning(std::string s) {if (NULL==log) return; log->Warning(s); }
void Error(std::string s) {if (NULL==log) return; log->Error(s); }
//
void BindImplementation(ILoggerImpl &ilog) { log = &ilog; }
void UnbindImplementation(){ log = NULL; }
private:
ILoggerImpl *log;
};
// file: loggers.h //
#include "logger.h"
extern CLogger Log1;
extern CLogger Log2;
extern CLogger Log3;
extern CLogger Log4;
extern CLogger LogB;
/// file: A.h //
#include "loggers.h"
class A
{
public:
void foo()
{
Log1.Info("asdhoj");
Log2.Info("asdhoj");
Log3.Info("asdhoj");
}
private:
};
/// file: B.h //
#include "loggers.h"
class B
{
public:
void bar()
{
Log1.Info("asdhoj");
Log2.Info("asdhoj");
LogB.Info("asdhoj");
a.foo();
}
private:
A a;
};
////// file: main.cpp ////////////////
#include "loggers.h"
#include "A.h"
#include "B.h"
#include "fileloger.h"
#include "xmllogger.h"
CLogger Log1;
CLogger Log2;
CLogger Log3;
CLogger Log4;
CLogger LogB;
// client code
int main()
{
std::unique_ptr<ILoggerImpl> filelog1(new CFileLogger("C:\\log1.txt"));
Log1.BindImplementation(*filelog1.get());
std::unique_ptr<ILoggerImpl> xmllogger2(new CXmlLogger("C:\\log2.xml"));
Log2.BindImplementation(*xmllogger2.get());
std::unique_ptr<ILoggerImpl> xmllogger3(new CXmlLogger("C:\\logB.xml"));
LogB.BindImplementation(*xmllogger3.get());
B b;
b.bar();
return 0;
};
// testing code
///////file: test.cpp /////////////////////////////////
#include "loggers.h"
CLogger Log1;
CLogger Log2;
CLogger Log3;
CLogger Log4;
int main()
{
run_all_tests();
}
///////file: test_a.cpp /////////////////////////////////
#include "A.h"
TEST(test1)
{
A a;
}
TEST(test2, A_logs_to_Log1_when_foo_is_called())
{
A a;
std::unique_ptr<ILoggerImpl> filelog1Mock(new CFileLoggerMock("C:\\log1.txt"));
Log1.BindImplementation(*filelog1.get());
EXPECT_CALL(filelog1Mock Info...);
a.foo();
Log1.UnbindImplementation();
}