2012-10-12 17:04:03 +00:00
|
|
|
/*
|
|
|
|
* Yaml.h
|
|
|
|
*
|
|
|
|
* Created on: 06.10.2012
|
|
|
|
* Author: Felix
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef DG_YAML_H_
|
|
|
|
#define DG_YAML_H_
|
|
|
|
|
2012-12-22 14:10:26 +00:00
|
|
|
#include <string>
|
2012-10-12 17:04:03 +00:00
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
#include <yaml-cpp/yaml.h>
|
|
|
|
|
2013-03-30 12:38:25 +00:00
|
|
|
#include "Log.h"
|
2013-08-07 15:39:43 +00:00
|
|
|
#include "Vector.h"
|
2013-03-30 12:38:25 +00:00
|
|
|
|
2012-10-12 17:04:03 +00:00
|
|
|
/**
|
|
|
|
* Interface to a YAML file.
|
|
|
|
*/
|
|
|
|
class Yaml {
|
|
|
|
public:
|
2013-03-30 15:39:46 +00:00
|
|
|
explicit Yaml(const std::string& filename);
|
2012-10-14 17:15:45 +00:00
|
|
|
~Yaml();
|
2012-10-12 17:04:03 +00:00
|
|
|
|
2012-12-22 14:10:26 +00:00
|
|
|
std::string getFilename() const;
|
2012-12-20 13:59:05 +00:00
|
|
|
|
2012-12-22 14:10:26 +00:00
|
|
|
static void setFolder(const std::string& folder);
|
2012-10-13 10:22:18 +00:00
|
|
|
|
2012-10-12 17:04:03 +00:00
|
|
|
template <typename T>
|
2012-12-22 14:10:26 +00:00
|
|
|
T get(const std::string& key, const T& defaultValue) const;
|
2012-10-12 17:04:03 +00:00
|
|
|
|
|
|
|
private:
|
2012-12-22 14:10:26 +00:00
|
|
|
static std::string mFolder;
|
2012-12-20 13:59:05 +00:00
|
|
|
|
2012-12-22 14:10:26 +00:00
|
|
|
std::string mFilename;
|
2012-10-14 17:15:45 +00:00
|
|
|
std::ifstream mFile;
|
2012-10-12 17:04:03 +00:00
|
|
|
YAML::Node mNode;
|
|
|
|
};
|
|
|
|
|
2012-10-13 10:22:18 +00:00
|
|
|
/**
|
|
|
|
* Stream output operators to specialize Yaml::get for other types.
|
2013-03-30 12:38:25 +00:00
|
|
|
* Error handling is done in Yaml::get.
|
2012-10-13 10:22:18 +00:00
|
|
|
*/
|
|
|
|
namespace {
|
2013-08-07 15:39:43 +00:00
|
|
|
void operator>>(const YAML::Node& node, Vector2i& vector) {
|
2012-10-13 10:22:18 +00:00
|
|
|
node[0] >> vector.x;
|
|
|
|
node[1] >> vector.y;
|
|
|
|
}
|
|
|
|
|
2013-08-07 15:39:43 +00:00
|
|
|
void operator>>(const YAML::Node& node, Vector2f& vector) {
|
2012-10-13 10:22:18 +00:00
|
|
|
node[0] >> vector.x;
|
|
|
|
node[1] >> vector.y;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-10-13 11:11:43 +00:00
|
|
|
/**
|
2013-03-30 12:38:25 +00:00
|
|
|
* Gets a value of a specified type by key. Returns default value on error.
|
2012-10-13 11:11:43 +00:00
|
|
|
*
|
|
|
|
* @param key The string by which to select the return value.
|
|
|
|
* @tparam T The type of the return value.
|
|
|
|
* @return The value of the specified key.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
2012-12-22 14:10:26 +00:00
|
|
|
T Yaml::get(const std::string& key, const T& defaultValue) const {
|
2013-03-30 12:38:25 +00:00
|
|
|
try {
|
2013-04-04 19:49:38 +00:00
|
|
|
if (const YAML::Node* node = mNode.FindValue(key)) {
|
|
|
|
T value;
|
|
|
|
*node >> value;
|
|
|
|
return value;
|
|
|
|
}
|
2013-04-04 21:13:08 +00:00
|
|
|
else
|
2013-04-04 19:49:38 +00:00
|
|
|
return defaultValue;
|
2012-12-20 13:59:05 +00:00
|
|
|
}
|
2013-04-04 19:49:38 +00:00
|
|
|
catch(YAML::Exception&) {
|
2012-12-20 13:59:05 +00:00
|
|
|
return defaultValue;
|
|
|
|
}
|
2012-10-13 11:11:43 +00:00
|
|
|
};
|
|
|
|
|
2012-10-12 17:04:03 +00:00
|
|
|
#endif /* DG_YAML_H_ */
|