GenericContainer
GenericContaine a tool for C++ programming
Loading...
Searching...
No Matches
GenericContainer-YAML Interface

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

Functions

bool GC_namespace::file_YAML_to_GC (string const &file_name_YAML, GenericContainer &gc)
 
bool GC_namespace::YAML_to_GC (istream_type &file_YAML, GenericContainer &gc)
 
bool GC_namespace::YAML_to_GC (string const &YAML, GenericContainer &gc)
 
bool GC_namespace::YAML_to_GC (vector< string > const &YAML, GenericContainer &gc)
 
void GC_namespace::GC_to_YAML (GenericContainer const &gc, vector< string > &YAML)
 
void GC_namespace::GC_to_YAML (GenericContainer const &gc, ostream_type &file_YAML)
 

Detailed Description

This module provides interface with YAML 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 YAML interface for converting between YAML 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 YAML files, streams, or strings into GenericContainer objects and vice versa.

Loading YAML Data into a GenericContainer

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

Loading YAML from a File

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

bool file_YAML_to_GC( std::string const & file_name_YAML, GenericContainer & gc );
bool file_YAML_to_GC(string const &file_name_YAML, GenericContainer &gc)

Example:

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

Loading YAML from an Input Stream

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

bool YAML_to_GC( istream_type & file_YAML, GenericContainer & gc );
bool YAML_to_GC(istream_type &file_YAML, GenericContainer &gc)
std::basic_istream< char > istream_type
Alias for a character-based input stream.
Definition GenericContainer.hh:112

Example:

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

Loading YAML from a String

You can convert a YAML string directly into a GenericContainer using the YAML_to_GC function.

bool YAML_to_GC( std::string const & YAML, GenericContainer & gc );

Example:

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

Loading YAML from a Vector of Strings

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

bool YAML_to_GC( std::vector<std::string> const & YAML, GenericContainer & gc );

Example:

GenericContainer gc;
std::vector<std::string> yaml_fragments = {
"key1: value1",
"key2: 42",
"key3:\n - 1\n - 2\n - 3"
};
if (YAML_to_GC(yaml_fragments, gc)) {
std::cout << "YAML fragments successfully loaded into GenericContainer." << std::endl;
} else {
std::cerr << "Failed to load YAML fragments." << std::endl;
}

Converting a GenericContainer to YAML

You can also convert a GenericContainer back to YAML format.

Converting to a Vector of YAML Strings

The GC_to_YAML function converts the contents of a GenericContainer into a vector of YAML strings.

void GC_to_YAML( GenericContainer const & gc, std::vector<std::string> & YAML );
void GC_to_YAML(GenericContainer const &gc, vector< string > &YAML)

Example:

GenericContainer gc;
gc["key1"] = "value1";
gc["key2"] = 42;
std::vector<std::string> yaml_output;
GC_to_YAML(gc, yaml_output);
for (const auto &line : yaml_output) {
std::cout << line << std::endl;
}

Converting to a YAML File Stream

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

void GC_to_YAML( GenericContainer const & gc, ostream_type & file_YAML );
std::basic_ostream< char > ostream_type
Alias for a character-based output stream.
Definition GenericContainer.hh:103

Example:

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

Function Documentation

◆ file_YAML_to_GC()

bool GC_namespace::file_YAML_to_GC ( string const & file_name_YAML,
GenericContainer & gc )

Convert a YAML file to a GenericContainer.

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

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

◆ GC_to_YAML() [1/2]

void GC_namespace::GC_to_YAML ( GenericContainer const & gc,
ostream_type & file_YAML )

Convert a GenericContainer to a YAML file stream.

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

Parameters
[in]gcThe GenericContainer to convert.
[out]file_YAMLOutput stream to write the YAML data.

◆ GC_to_YAML() [2/2]

void GC_namespace::GC_to_YAML ( GenericContainer const & gc,
vector< string > & YAML )

Convert a GenericContainer to a vector of YAML strings.

This function converts the contents of the provided GenericContainer into YAML format and stores it in the specified vector.

Parameters
[in]gcThe GenericContainer to convert.
[out]YAMLVector to store the resulting YAML strings.

◆ YAML_to_GC() [1/3]

bool GC_namespace::YAML_to_GC ( istream_type & file_YAML,
GenericContainer & gc )

Convert a YAML file stream to a GenericContainer.

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

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

◆ YAML_to_GC() [2/3]

bool GC_namespace::YAML_to_GC ( string const & YAML,
GenericContainer & gc )

Convert a YAML string to a GenericContainer.

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

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

◆ YAML_to_GC() [3/3]

bool GC_namespace::YAML_to_GC ( vector< string > const & YAML,
GenericContainer & gc )

Convert a vector of YAML strings to a GenericContainer.

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

Parameters
[in]YAMLVector containing YAML strings to parse.
[out]gcThe GenericContainer to be populated.
Returns
true if the conversion was successful, false otherwise.