Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ persisting data

Tags:

c++

templates

what I want to archive is a simple way to make some variables persistent. For this I wrote a PeristenceProvider Class which wraps the boost property tree functionality to store data in xml / ini files.

At the moment I need to do things like this:

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

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

But is there a chance to hide this in a way like this:

class ClassA
{
     Persist<int, "valueI"> m_ValueI;
}
like image 732
Steve Avatar asked Jun 25 '12 15:06

Steve


People also ask

What is data persistence in C?

1. Persistent data. When a C program exits, all of its global variables, local variables, and heap-allocated blocks are lost. Its memory is reclaimed by the operating system, erased, and handed out to other programs.

What is meant by persisting data?

Persistence is "the continuance of an effect after its cause is removed". In the context of storing data in a computer system, this means that the data survives after the process with which it was created has ended. In other words, for a data store to be considered persistent, it must write to non-volatile storage.

What is persistent data example?

Persistent data is static and does not change with time (not dynamic). Persistent data stores core information. For example, an organization's financial data must be persistent. Persistent data cannot be deleted by external processes or objects until the user deletes it, meaning it's stable.

What is persistent and non persistent data?

Persistence data: The data which is available after fully closing the application. This type of data must be save into shared preference or database or internal or external memory. Non- persistence data: The data which is not available after fully closing the application.


1 Answers

It's possible but not exactly that way. You cannot use string literals to instantiate template. String objects with external linkage are only allowed to be non-type arguments. So string constant must be defined as extern and be char[], not just char*.

See example (it will print "Hello" and "World", really cool, isn't it?):

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;
like image 108
Rost Avatar answered Sep 20 '22 18:09

Rost