SGPropertyNode: extract enum value with getValue
This commit is contained in:
+50
-1
@@ -20,8 +20,10 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <typeinfo>
|
||||||
|
|
||||||
#include <boost/utility.hpp>
|
#include <boost/utility.hpp>
|
||||||
|
#include <boost/type_traits/is_enum.hpp>
|
||||||
|
|
||||||
#if PROPS_STANDALONE
|
#if PROPS_STANDALONE
|
||||||
#else
|
#else
|
||||||
@@ -1784,7 +1786,8 @@ private:
|
|||||||
|
|
||||||
// Convenience functions for use in templates
|
// Convenience functions for use in templates
|
||||||
template<typename T>
|
template<typename T>
|
||||||
T getValue(const SGPropertyNode*);
|
typename boost::disable_if<boost::is_enum<T>, T>::type
|
||||||
|
getValue(const SGPropertyNode*);
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
inline bool getValue<bool>(const SGPropertyNode* node) { return node->getBoolValue(); }
|
inline bool getValue<bool>(const SGPropertyNode* node) { return node->getBoolValue(); }
|
||||||
@@ -1819,6 +1822,52 @@ inline std::string getValue<std::string>(const SGPropertyNode* node)
|
|||||||
return node->getStringValue();
|
return node->getStringValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
namespace simgear
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Default trait for extracting enum values from SGPropertyNode. Create your
|
||||||
|
* own specialization for specific enum types to enable validation of values.
|
||||||
|
*/
|
||||||
|
template<class T>
|
||||||
|
struct enum_traits
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Typename of the enum
|
||||||
|
*/
|
||||||
|
static const char* name() { return typeid(T).name(); }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Default value (will be used if validation fails)
|
||||||
|
*/
|
||||||
|
static T defVal() { return T(); }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Whether the given integer value has an enum value defined
|
||||||
|
*/
|
||||||
|
static bool validate(int) { return true; }
|
||||||
|
};
|
||||||
|
} // namespace simgear
|
||||||
|
|
||||||
|
/** Extract enum from SGPropertyNode */
|
||||||
|
template<typename T>
|
||||||
|
inline typename boost::enable_if<boost::is_enum<T>, T>::type
|
||||||
|
getValue(const SGPropertyNode* node)
|
||||||
|
{
|
||||||
|
typedef simgear::enum_traits<T> Traits;
|
||||||
|
int val = node->getIntValue();
|
||||||
|
if( !Traits::validate(val) )
|
||||||
|
{
|
||||||
|
SG_LOG
|
||||||
|
(
|
||||||
|
SG_GENERAL,
|
||||||
|
SG_WARN,
|
||||||
|
"Invalid value for enum (" << Traits::name() << ", val = " << val << ")"
|
||||||
|
);
|
||||||
|
return Traits::defVal();
|
||||||
|
}
|
||||||
|
return static_cast<T>(node->getIntValue());
|
||||||
|
}
|
||||||
|
|
||||||
inline bool setValue(SGPropertyNode* node, bool value)
|
inline bool setValue(SGPropertyNode* node, bool value)
|
||||||
{
|
{
|
||||||
return node->setBoolValue(value);
|
return node->setBoolValue(value);
|
||||||
|
|||||||
Reference in New Issue
Block a user