C++ libconfig::Setting::add( )














































C++ libconfig::Setting::add( )



C++ libconfig::Setting::add( )

Here is the list of prototype for add method for settings defined under namespace libconfig:
Setting & add (const std::string &name, Setting::Type type)
Setting & add (const char *name, Setting::Type type)
Setting & add (Setting::Type type)

Method on Setting: Setting & add (const std::string &name, Setting::Type type)
Method on Setting: Setting & add (const char *name, Setting::Type type)
  • These methods add a new child setting with the given name and type to the setting, which must be a group. 
  • They return a reference to the new setting. 
  • If the setting already has a child setting with the given name, or if the name is invalid, a SettingNameException is thrown. 
  • If the setting is not a group, a SettingTypeException is thrown.

Method on Setting: Setting & add (Setting::Type type)
  • This method adds a new element to the setting, which must be of type TypeArray or TypeList
  • If the setting is an array which currently has zero elements, the type parameter (which must be TypeIntTypeInt64TypeFloatTypeBool, or TypeString) determines the type for the array; otherwise it must match the type of the existing elements in the array.
  • The method returns the new setting on success.
  • The method throws a SettingTypeException if the setting is not an array or list, or if type is invalid.

NOTE: 
  • Once a setting has been created, neither its name nor type can be changed.
  •  If type is a scalar type, the new setting will have a default value of 0, 0.0, false, or NULL, as appropriate.

Consider the following configuration file:

/*An example configuration file*/

version = "1.0";

application:
{
  window:
  {
    title = "My Application";
    size = { w = 640; h = 480; };
    pos = { x = 350; y = 250; };
  };

  list = ( ( "abc", 123, true ), 1.234, ( /* an empty list */) );

  books = ( { title  = "Treasure Island";
              author = "Robert Louis Stevenson";
              price  = 29.95;
              qty    = 5; },
            { title  = "Snow Crash";
              author = "Neal Stephenson";
              price  = 9.99;
              qty    = 8; } );

  misc:
  {
    pi = 3.141592654;
    bigint = 9223372036854775807L;
    columns = [ "Last Name", "First Name", "MI" ];
    bitmask = 0x1FC3;
  };
};

Now let us see how we can add settings using the above configuration file as an example.
To add a setting e = 2.718 to misc group we should do something like:
/*read setting application.misc*/
/*returns a group as*/
/* misc {
pi = 3.141592654;
bigint = 9223372036854775807L;
columns = [ "Last Name", "First Name", "MI" ];
bitmask = 0x1FC3;
};*/
libconfig::Setting& setting = config.lookup("application.misc");

/*now to add setting e = 2.718 we will write*/
setting.add("e", libconfig::Setting::TypeFloat);
/*by default the setting application.misc["e"] will have 0.0*/
/* misc {
pi = 3.141592654;
bigint = 9223372036854775807L;
columns = [ "Last Name", "First Name", "MI" ];
bitmask = 0x1FC3;
e : 0;
};*/

/*following line prints e : 0*/
std::cout << "e : " << (double)config.lookup("application.misc")["e"] << std::endl;
/*to assign the value 2.718 we need to use assignment operator as*/
setting["e"] = 2.718;
/* misc {
pi = 3.141592654;
bigint = 9223372036854775807L;
columns = [ "Last Name", "First Name", "MI" ];
bitmask = 0x1FC3;
e : 2.718;
};*/
/*following line prints e : 2.718*/
std::cout << "e : " << (double)config.lookup("application.misc")["e"] << std::endl;


 The following example implements the same idea:
/*include necessary header file*/
#include <iostream>
#include <cstdlib>
#include <libconfig.h++>

/*driver function: program execution begins from here*/
int main(){

/*create an instance of config*/
libconfig::Config config;

/*read a configuration file*/
try{
config.readFile ("sample.cfg");
} catch (libconfig::FileIOException &e){
/*inform user about IOException*/
std::cerr << "FileIOException occurred. Could not read \"example.cfg\"!!\n";
/*terminate program*/
exit (EXIT_FAILURE);
} catch (libconfig::ParseException &e){
/*inform user about the parse exception*/
std::cerr << "Parse error at " << e.getFile() << ":" << e.getLine()
<< " - " << e.getError() << std::endl;
/*terminate program*/
return(EXIT_FAILURE);
}

try {

/*read setting application.misc*/
/*returns a group as*/
/* misc {
pi = 3.141592654;
bigint = 9223372036854775807L;
columns = [ "Last Name", "First Name", "MI" ];
bitmask = 0x1FC3;
};*/
libconfig::Setting& setting = config.lookup("application.misc");

/*now to add setting e = 2.718 we will write*/
setting.add("e", libconfig::Setting::TypeFloat);
/*by default the setting application.misc["e"] will have 0.0*/
/*following line prints e : 0*/
std::cout << "e : " << (double)config.lookup("application.misc")["e"] << std::endl;
/*to assign the value 2.718 we need to use assignment operator as*/
setting["e"] = 2.718;
/*following line prints e : 2.718*/
std::cout << "e : " << (double)config.lookup("application.misc")["e"] << std::endl;

} catch(libconfig::SettingNotFoundException &e) {
/*in case if setting does not exist*/
std::cerr << R"(No "name" or "version" setting(s) in configuration file.)" << std::endl;
} catch (libconfig::SettingTypeException &e){
/*in case if type of setting do not match with type*/
/*declared by the programmer*/
std::cerr << "Type mismatch exception occurred!!\n";
}

/*return 0 to caller to indicate normal termination of program*/
return 0;
}

Assuming that you are on Linux platform:
Use the following command to compile the above source code:
g++ `pkg-config --cflags libconfig++` main.cpp -o main `pkg-config --libs libconfig++`
The above command after successful compilation will generate an object/binary file.
Use  the following command to execute the object/binary file:
./main

Here is the output generated by above program:
e : 0
e : 2.718


More Articles of Shailly Jain:

Name Views Likes
C++ cv::subtract 3259 0
C++ libconfig::Setting::setFormat( ) 716 0
Cast operators for Setting provided by config library 477 0
C++ cv::bitwise_or 1812 0
C++ libconfig::Setting::getType( ) 811 0
C++ tinyxml2::XMLNode::LastChild( ) 559 0
C++ libconfig::Setting::isAggregate( ) 416 0
C++ cv::bitwise_not 2434 0
Basics of configuration files supported by libconfig in C++ 1467 0
C++ tinyxml2::XMLNode::ToDeclaration( ) 742 0
C++ libconfig::Config.exists( ) 959 0
C++ cv::trace( ) 693 0
C++ libconfig::Setting::getName( ) 699 0
C++ tinyxml2::XMLNode::GetLineNum( ) 518 0
C++ libconfig::Config.readFile( ) 1925 0
C++ tinxyxml2 Introduction 799 0
C++ libconfig::Setting::add( ) 659 0
C++ tinyxml2::XMLNode::FirstChild( ) 572 0
C++ libconfig::Config::setAutoConvert( ) 579 0
Subscripting Setting objects in C++ - libconfig::Setting 398 0
C++ tinyxml2::XMLNode::ToElement( ) 679 0
C++ libconfig::Setting::isScalar( ) 365 0
C++ tinyxml2::XMLNode::FirstChildElement( ) 1041 0
C++ libconfig::Config::writeFile( ) 845 0
C++ libconfig::Config::getAutoConvert( ) 661 0
C++ libconfig::Setting::exists( ) 563 0
C++ cv::bitwise_and 7173 0
C++ libconfig::Setting::remove( ) 653 0
C++ libconfig::Setting::isNumber( ) 453 0
C++ tinyxml2::XMLNode::ToComment( ) 562 0
C++ libconfig::Setting::getIndex( ) 529 0
C++ libconfig::Setting::isRoot( ) 423 0
C++ libconfig::Setting::getParent( ) 512 0
C++ tinyxml2::XMLNode::ToText( ) 552 0
Exceptions defined by libconfig library 724 0
Assignment operators for Setting provided by libconfig library 358 0
C++ tinyxml2::XMLNode::ToDocument( ) 405 0
C++ libconfig::ParseException - Methods 951 0
C++ libconfig Introduction 1413 0
C++ tinyxml2::XMLNode::ToUnknown( ) 448 0
C++ libconfig::Config.lookup( ) 3439 0
C++ libconfig::Setting::getPath( ) 620 0
C++ libconfig::Setting::getFormat( ) 415 0
C++ cv::bitwise_xor 1235 0
C++ libconfig::Config.lookupValue( ) 3392 0
C++ tinyxml2::XMLNode 420 0
C++ tinyxml2::XMLNode::Parent( ) 612 0
C++ libconfig::Config.getRoot( ) 954 0
C++ tinyxml2::XMLNode::LastChildElement( ) 458 0
C++ libconfig::Setting::isGroup( ) 547 0
C++ tinyxml2::XMLDocument::LoadFile 1713 0
C++ libconfig::Setting::isList( ) 286 0
C++ libconfig::Setting::isArray( ) 587 0
C++ tinyxml2 installation 1369 0

Comments