Reputation: 163
I'm having problem with parsing yaml
files using yaml-cpp
, I'm making a application using wxWidgets
and I'm trying to read the frame size from a yaml
file which looks like,
---
This is the configuration file for the Sample Browser,
feel free to edit this file as needed
...
Window:
SizeW: 1280
SizeH: 720
Media:
Autoplay: false
And this the code that should handle the parsing,
int sizeH, sizeW;
try
{
YAML::Node config = YAML::LoadFile("/home/apoorv/repos/cpp-projects/wxWidgets/SampleBrowser/build/config.yaml");
if (!config["Window"])
{
wxLogDebug("Error! Cannot fetch values.");
}
sizeH = config["SizeH"].as<int>();
sizeW = config["SizeW"].as<int>();
}
catch(const YAML::ParserException& ex)
{
std::cout << ex.what() << std::endl;
}
this->SetSize(sizeW, sizeH);
But when I try to parse this file and set the frame size this->SetSize()
it errors out saying *** Caught unhandled unknown exception; terminating
.
Upvotes: 0
Views: 1838
Reputation: 22688
It's fine to try to do this for learning yaml-cpp, but if you really want to save/restore your frame geometry, you should use wxPersistentTLW instead. To use it, just call wxPersistentRegisterAndRestore(frame, "NameToIdentifyYourFrame")
after creating your frame, see the manual for more details.
Upvotes: 1
Reputation: 6267
In the cross-platform environment it is better to save the client size of the window, as for GTK what's important is the client size. There might be other OSes/toolkits where this is the case.
Upvotes: 0
Reputation: 39738
Since SizeH
and SizeW
are children of Window
, your two lines should look like
sizeH = config["Window"]["SizeH"].as<int>();
sizeW = config["Window"]["SizeW"].as<int>();
or, merged with the previous check,
if (auto window = config["Window"]) {
sizeH = window["SizeH"].as<int>();
sizeW = window["SizeW"].as<int>();
} else {
wxLogDebug("Error! Cannot fetch values.");
}
Generally, the error handling is bad. In your code, if an error is encountered, sizeH
and sizeW
are not set but are still given to SetSize
. This is undefined behavior. You should initialize them with some default values, e.g.
int sizeH = 480, sizeW = 640;
Also, since you keep us in the dark about what this
is, there may be other errors.
Upvotes: 1