C ++ persistent data

what I want to archive is an easy way to make some variables permanent. To do this, I wrote the PeristenceProvider class, which wraps the functionality of the boost property tree for storing data in xml / ini files.

At the moment, I need to do such things:

ClassA::ClassA() { m_valueI = PersistenceProvider::getInstance.get<int>("valueI"); } ClassA::~ClassA() { PeristenceProvider::getInstance.set<int>("valueI", m_valueI); } 

But is there any way to hide it as follows:

 class ClassA { Persist<int, "valueI"> m_ValueI; } 
+6
source share
2 answers

Perhaps, but not quite. You cannot use string literals to create a template. External objects with string constraints are allowed only for nonpig arguments. Therefore, the string constant must be defined as extern and be char[] , and not just char* .

See an example (it will print "Hello" and "World", really cool, right?):

 extern const char hello[] = "Hello"; extern const char world[] = "World"; template<const char* s> struct X { X() { std::cout << s << std::endl; } }; X<hello> z1; X<world> z2; 
+1
source

It seems that you do not save tons of information - just a few options. If so, then simply wrap the function calls in your own functions that take two arguments - a std :: string or const char * and the type of element to save. If the number of stored types is limited (for example, int, double, std :: string), this will work fine.

0
source

Source: https://habr.com/ru/post/918956/


All Articles