Skip to content

Latest commit

 

History

History
 
 

cpp

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  • infos =
  • infos/author = Markus Raab elektra@libelektra.org
  • infos/status = maintained nodep
  • infos/provides =
  • infos/description =

The C++ binding is a 1:1 mapping of all C-functions into C++. In this README the advantages are described.

Installation

See installation. The package containing the header files is called libelektra-dev.

No Explicit Delete Necessary

The objects will automatically be freed when they leave the scope. E.g.

int main() {
	Key k;
	KDB kdb;
	KeySet ks;
} // k, kdb and ks will automatically be freed here

The references are automatically tracked whenever a key is appended or removed to a KeySet. This takes away the major causes of memleaks.

Exceptions and IO

No return values need to be checked. Instead on problems an exception is thrown:

try
{
	kdb::KDB kdb(k);
	kdb.get(ks, k);
	kdb.set(ks, k);
  // Continued below ↓
  //
}

We do not have to care here that something might not work, because an exception will be thrown in these cases. Additionally, it is very simple to print a keyset:

std::cout << ks;

At the end we close KDB explicitly, so that we also get the warnings from there. The C++ binding then features a possibility to print the warnings.

  //
  // Continuation of code above ↑
	kdb.close(k);
	printWarnings(std::cout, k, true, true);
}
catch (kdb::KDBException const & e)
{
	std::cout << e.what();
}

See here for the full code. e.what() will print all warnings and errors by default. This can be customized.

It can also be changed which exceptions are thrown as shown here.

Allow Iterations

Next to the C-style fashioned loop:

for (elektraCursor it = 0; it < ks.size (); ++it)
{
	std::cout << ks.at (it).getName() << std::endl;
}

The C++ interface also supports real iterators:

for (KeySet::iterator i = ks3.begin(); i != ks3.end(); ++i)
{
	Key k(*i);
	std::cout << k.getName() << std::endl;
}

Also C++11 iterators are supported, and of course reverse and const iterators, too.

Type Safety

Contextual Values allow a key to be used as native variables, see here how they can be generated.