My aim is to set up a data structure to store the settings of my application.
In PHP I would just write...
$settings = array(
"Fullscreen" => true,
"Width" => 1680,
"Height" => 1050,
"Title" => "My Application",
);
Now I tried to create a similar structure in C++ but it can't handle different datatypes yet. By the way if there is a better way of storing such settings data, please let me know.
struct Setting{ string Key, Value; };
Setting Settings[] = {
("Fullscreen", "true"), // it's acceptable to store the boolean as string
("Width", "1680"), // it's not for integers as I want to use them later
("Height", 1050), // would be nice but of course an error
("Title", "My Application") // strings aren't the problem with this implementation
};
How can I model a structure of an associative array
with flexible datatypes
?
An associative data structure with varying data types is exactly what a struct
is...
struct SettingsType
{
bool Fullscreen;
int Width;
int Height;
std::string Title;
} Settings = { true, 1680, 1050, "My Application" };
Now, maybe you want some sort of reflection because the field names will appear in a configuration file? Something like:
SettingsSerializer x[] = { { "Fullscreen", &SettingsType::Fullscreen },
{ "Width", &SettingsType::Width },
{ "Height", &SettingsType::Height },
{ "Title", &Settings::Title } };
will get you there, as long as you give SettingsSerializer
an overloaded constructor with different behavior depending on the pointer-to-member type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With