GenericContainer
GenericContainer a tool for C++ programming
Loading...
Searching...
No Matches
GenericContainer TOML Interface

This module provides interface with TOML files. More...

Functions

bool GC_namespace::file_TOML_to_GC (string_view file_name, GenericContainer &gc)
bool GC_namespace::TOML_to_GC (istream_type &stream, GenericContainer &gc)
bool GC_namespace::TOML_to_GC (string const &DATA, GenericContainer &gc)
void GC_namespace::GC_to_TOML (GenericContainer const &gc, std::string &res)
void GC_namespace::GC_to_TOML (GenericContainer const &gc, ostream_type &stream)

Detailed Description

This module provides interface with TOML files.

The GenericContainer (GC) module offers a flexible container for storing various data types such as booleans, integers, floating-point numbers, complex numbers, and strings. It also supports structured data types such as vectors and maps. The C++ interface allows for seamless interaction with the container, enabling users to perform operations like adding elements, retrieving data, and managing hierarchical structures.

Overview

This tutorial explains how to use the GenericContainer TOML interface for converting between TOML data and the GenericContainer object.

The GenericContainer is a flexible structure that can store various data types like booleans, integers, floating-point numbers, complex numbers, strings, vectors, and maps. The interface provides multiple ways to convert TOML files, streams, or strings into GenericContainer objects and vice versa.

Loading TOML Data into a GenericContainer

There are several ways to load TOML data into a GenericContainer.

Loading TOML from a File

The file_TOML_to_GC function reads TOML data from a file and populates a GenericContainer. This is useful for handling configuration files or other data stored in TOML format.

bool file_TOML_to_GC( std::string const & file_name_TOML, GenericContainer & gc );
bool file_TOML_to_GC(string_view file_name, GenericContainer &gc)
Definition GenericContainerInterface_toml.hh:54

Example:

GenericContainer gc;
std::string file_name = "config.toml";
if (file_TOML_to_GC(file_name, gc)) {
std::cout << "TOML file successfully loaded into GenericContainer." << std::endl;
} else {
std::cerr << "Failed to load TOML file." << std::endl;
}

Loading TOML from an Input Stream

The TOML_to_GC function allows loading TOML data from an input stream, such as a file stream or a string stream.

bool TOML_to_GC( istream_type & file_TOML, GenericContainer & gc );
bool TOML_to_GC(istream_type &stream, GenericContainer &gc)
Definition GenericContainerInterface_toml.hh:75
std::basic_istream< char > istream_type
Alias for a character-based input stream.
Definition GenericContainer.hh:118

Example:

GenericContainer gc;
std::ifstream file_stream("config.toml");
if (TOML_to_GC(file_stream, gc)) {
std::cout << "TOML stream successfully loaded into GenericContainer." << std::endl;
} else {
std::cerr << "Failed to load TOML stream." << std::endl;
}

Loading TOML from a String

You can convert a TOML string directly into a GenericContainer using the TOML_to_GC function.

bool TOML_to_GC( std::string const & TOML, GenericContainer & gc );

Example:

GenericContainer gc;
std::string toml_data = R"(
key1: value1
key2: 42
key3:
- 1
- 2
- 3
)";
if (TOML_to_GC(toml_data, gc)) {
std::cout << "TOML string successfully loaded into GenericContainer." << std::endl;
} else {
std::cerr << "Failed to load TOML string." << std::endl;
}

Loading TOML from a Vector of Strings

If you have multiple TOML strings in a vector, you can load them into a GenericContainer using the TOML_to_GC function.

bool TOML_to_GC( std::vector<std::string> const & TOML, GenericContainer & gc );

Example:

GenericContainer gc;
std::vector<std::string> toml_fragments = {
"key1 = \"value1\"",
"key2 = 42",
"key3 = [1, 2, 3]"
};
if (TOML_to_GC(toml_fragments, gc)) {
std::cout << "TOML fragments successfully loaded into GenericContainer." << std::endl;
} else {
std::cerr << "Failed to load TOML fragments." << std::endl;
}

Converting a GenericContainer to TOML

You can also convert a GenericContainer back to TOML format.

Converting to a TOML File Stream

You can write the contents of a GenericContainer to an output stream (e.g., a file) in TOML format using the GC_to_TOML function.

void GC_to_TOML( GenericContainer const & gc, ostream_type & file_TOML );
void GC_to_TOML(GenericContainer const &gc, std::string &res)
Definition GenericContainerInterface_toml.hh:116
std::basic_ostream< char > ostream_type
Alias for a character-based output stream.
Definition GenericContainer.hh:109

Example:

GenericContainer gc;
gc["key1"] = "value1";
gc["key2"] = 42;
std::ofstream file_stream("output.toml");
GC_to_TOML(gc, file_stream);
std::cout << "TOML data written to output.toml" << std::endl;

Functions

◆ file_TOML_to_GC()

bool GC_namespace::file_TOML_to_GC ( string_view file_name,
GenericContainer & gc )
inline

Convert a TOML file to a GenericContainer.

This function reads TOML data from the provided input stream and populates the given GenericContainer with the parsed data.

Parameters
[in]file_nameInput file name containing TOML data.
[out]gcThe GenericContainer to be populated.
Returns
true if the conversion was successful, false otherwise.

◆ GC_to_TOML() [1/2]

void GC_namespace::GC_to_TOML ( GenericContainer const & gc,
ostream_type & stream )
inline

Convert a GenericContainer to a TOML file stream.

This function converts the contents of the provided GenericContainer into TOML format and writes it to the specified output stream.

Parameters
[in]gcThe GenericContainer to convert.
[out]streamOutput stream to write the TOML data.

◆ GC_to_TOML() [2/2]

void GC_namespace::GC_to_TOML ( GenericContainer const & gc,
std::string & res )
inline

Convert a vector of TOML strings to a GenericContainer.

This function parses each string in the given vector as TOML and populates the specified GenericContainer with the resulting data.

Parameters
[in]gcThe GenericContainer to convert.
[out]DATAString to store the TOML encoded GenericContainer.

◆ TOML_to_GC() [1/2]

bool GC_namespace::TOML_to_GC ( istream_type & stream,
GenericContainer & gc )
inline

Convert a TOML file stream to a GenericContainer.

This function reads TOML data from the provided input stream and populates the given GenericContainer with the parsed data.

Parameters
[in]streamInput stream containing TOML data.
[out]gcThe GenericContainer to be populated.
Returns
true if the conversion was successful, false otherwise.

◆ TOML_to_GC() [2/2]

bool GC_namespace::TOML_to_GC ( string const & DATA,
GenericContainer & gc )
inline

Convert a TOML string to a GenericContainer.

This function parses the given TOML string and populates the specified GenericContainer with the resulting data.

Parameters
[in]DATAThe TOML string to parse.
[out]gcThe GenericContainer to be populated.
Returns
true if the conversion was successful, false otherwise.