I have created a function template for loading my settings from a binary file.
template<class T> T LoadSettings(const std::string &fileName)
{
// Load settings
T settings;
std::string filePath(LOCAL_FILE_DIR);
filePath.append(fileName);
std::ifstream file(filePath, std::ios::in | std::ios::binary);
if (file.is_open())
{
if (!settings.ParseFromIstream(&file))
{
throw ExceptionMessage("Failed to parse TextureAtlasSettings");
}
}
else
{
throw ExceptionMessage("Failed to open file");
}
return settings;
};
I would like to be able to invoke the function and return the appropriate settings class.
Coming from C# I would do the following.
MySettingsClass settings = LoadSettings<MySettingsClass>("FileName.bin");
How can I do the same thing in C++?
EDIT:
I should be more generic!
throw std::runtime_error("Failed to parse " + typeid(T).name());
Then use this syntax in C++