Apply to-Lua and from-Lua functionality to TemplateProperty as well

This commit is contained in:
Alexander Bock
2014-09-02 20:59:29 +02:00
parent d90e35372a
commit be3f727706
9 changed files with 303 additions and 71 deletions

View File

@@ -27,58 +27,120 @@
namespace openspace {
namespace properties {
#define REGISTER_NUMERICALPROPERTY_HEADER(CLASS_NAME, TYPE) \
typedef NumericalProperty<TYPE> CLASS_NAME; \
template <> std::string PropertyDelegate<NumericalProperty<TYPE>>::className(); \
template <> std::string PropertyDelegate<TemplateProperty<TYPE>>::className(); \
template <> template <> \
TYPE PropertyDelegate<NumericalProperty<TYPE>>::defaultValue<TYPE>(); \
template <> template <> \
TYPE PropertyDelegate<NumericalProperty<TYPE>>::defaultMinimumValue<TYPE>(); \
template <> template <> \
TYPE PropertyDelegate<NumericalProperty<TYPE>>::defaultMaximumValue<TYPE>(); \
template <> template <> \
TYPE PropertyDelegate<NumericalProperty<TYPE>>::fromLuaValue( \
lua_State* state, bool& success); \
template <> template <> \
bool PropertyDelegate<NumericalProperty<TYPE>>::toLuaValue( \
lua_State* state, TYPE value);
#define REGISTER_NUMERICALPROPERTY_HEADER(CLASS_NAME, TYPE) \
typedef NumericalProperty<TYPE> CLASS_NAME; \
template <> \
std::string PropertyDelegate<NumericalProperty<TYPE>>::className(); \
template <> \
std::string PropertyDelegate<TemplateProperty<TYPE>>::className(); \
template <> \
template <> \
TYPE PropertyDelegate<NumericalProperty<TYPE>>::defaultValue<TYPE>(); \
template <> \
template <> \
TYPE PropertyDelegate<NumericalProperty<TYPE>>::defaultMinimumValue<TYPE>(); \
template <> \
template <> \
TYPE PropertyDelegate<NumericalProperty<TYPE>>::defaultMaximumValue<TYPE>(); \
template <> \
template <> \
TYPE PropertyDelegate<TemplateProperty<TYPE>>::fromLuaValue(lua_State* state, \
bool& success); \
template <> \
template <> \
TYPE PropertyDelegate<NumericalProperty<TYPE>>::fromLuaValue(lua_State* state, \
bool& success); \
template <> \
template <> \
bool PropertyDelegate<TemplateProperty<TYPE>>::toLuaValue(lua_State* state, \
TYPE value); \
template <> \
template <> \
bool PropertyDelegate<NumericalProperty<TYPE>>::toLuaValue(lua_State* state, \
TYPE value); \
template <> \
int PropertyDelegate<TemplateProperty<TYPE>>::typeLua(); \
template <> \
int PropertyDelegate<NumericalProperty<TYPE>>::typeLua();
#define REGISTER_NUMERICALPROPERTY_SOURCE(CLASS_NAME, TYPE, \
DEFAULT_VALUE, DEFAULT_MIN_VALUE, DEFAULT_MAX_VALUE, DEFAULT_STEPPING, \
FROM_LUA_LAMBDA_EXPRESSION, TO_LUA_LAMBDA_EXPRESSION) \
template <> \
std::string PropertyDelegate<NumericalProperty<TYPE>>::className() { \
return #CLASS_NAME; \
} \
template <> \
std::string PropertyDelegate<TemplateProperty<TYPE>>::className() { \
return #CLASS_NAME; \
} \
template <> template <> \
TYPE PropertyDelegate<NumericalProperty<TYPE>>::defaultValue<TYPE>() { \
return DEFAULT_VALUE; \
} \
template <> template <> \
TYPE PropertyDelegate<NumericalProperty<TYPE>>::defaultMinimumValue<TYPE>() { \
return DEFAULT_MIN_VALUE; \
} \
template <> template <> \
TYPE PropertyDelegate<NumericalProperty<TYPE>>::defaultMaximumValue<TYPE>() { \
return DEFAULT_MAX_VALUE; \
} \
template <> template <> \
TYPE PropertyDelegate<NumericalProperty<TYPE>>::fromLuaValue<TYPE>( \
lua_State* state, bool& success) { \
return FROM_LUA_LAMBDA_EXPRESSION(state, success); \
} \
template <> template <> \
bool PropertyDelegate<NumericalProperty<TYPE>>::toLuaValue<TYPE>( \
lua_State* state, TYPE value) { \
return TO_LUA_LAMBDA_EXPRESSION(state, value); \
}
#define REGISTER_NUMERICALPROPERTY_SOURCE(CLASS_NAME, TYPE, DEFAULT_VALUE, \
DEFAULT_MIN_VALUE, DEFAULT_MAX_VALUE, \
DEFAULT_STEPPING, FROM_LUA_LAMBDA_EXPRESSION, \
TO_LUA_LAMBDA_EXPRESSION) \
template <> \
std::string PropertyDelegate<TemplateProperty<TYPE>>::className() \
{ \
return #CLASS_NAME; \
\
} \
template <> \
std::string PropertyDelegate<NumericalProperty<TYPE>>::className() \
{ \
return PropertyDelegate<TemplateProperty<TYPE>>::className(); \
\
} \
template <> \
template <> \
TYPE PropertyDelegate<NumericalProperty<TYPE>>::defaultValue<TYPE>() \
{ \
return DEFAULT_VALUE; \
\
} \
template <> \
template <> \
TYPE PropertyDelegate<NumericalProperty<TYPE>>::defaultMinimumValue<TYPE>() \
{ \
return DEFAULT_MIN_VALUE; \
\
} \
template <> \
template <> \
TYPE PropertyDelegate<NumericalProperty<TYPE>>::defaultMaximumValue<TYPE>() \
{ \
return DEFAULT_MAX_VALUE; \
\
} \
template <> \
template <> \
TYPE PropertyDelegate<TemplateProperty<TYPE>>::fromLuaValue<TYPE>(lua_State * state, \
bool& success) \
{ \
return FROM_LUA_LAMBDA_EXPRESSION(state, success); \
\
} \
template <> \
template <> \
TYPE PropertyDelegate<NumericalProperty<TYPE>>::fromLuaValue<TYPE>( \
lua_State * state, bool& success) \
{ \
return PropertyDelegate<TemplateProperty<TYPE>>::fromLuaValue<TYPE>(state, \
success); \
\
} \
template <> \
template <> \
bool PropertyDelegate<TemplateProperty<TYPE>>::toLuaValue<TYPE>(lua_State * state, \
TYPE value) \
{ \
return TO_LUA_LAMBDA_EXPRESSION(state, value); \
} \
template <> \
template <> \
bool PropertyDelegate<NumericalProperty<TYPE>>::toLuaValue<TYPE>(lua_State * state, \
TYPE value) \
{ \
return PropertyDelegate<TemplateProperty<TYPE>>::toLuaValue<TYPE>(state, value); \
} \
template <> \
int PropertyDelegate<TemplateProperty<TYPE>>::typeLua() \
{ \
return LUA_TNUMBER; \
} \
template <> \
int PropertyDelegate<NumericalProperty<TYPE>>::typeLua() \
{ \
return PropertyDelegate<TemplateProperty<TYPE>>::typeLua(); \
}
// Delegating constructors are necessary; automatic template deduction cannot
// deduce template argument for 'U' if 'default' methods are used as default values in
@@ -133,8 +195,8 @@ bool NumericalProperty<T>::getLua(lua_State* state) const
}
template <typename T>
int openspace::properties::NumericalProperty<T>::typeLua() const {
return LUA_TNUMBER;
int NumericalProperty<T>::typeLua() const {
return PropertyDelegate<NumericalProperty<T>>::typeLua();
}

View File

@@ -51,6 +51,8 @@ public:
template <typename U>
static bool toLuaValue(lua_State* state, U value);
static int typeLua();
};
} // namespace properties

View File

@@ -71,5 +71,11 @@ bool PropertyDelegate<T>::toLuaValue(lua_State* state, U value) {
"Unimplemented PropertyDelegate::toLuaValue specialization");
}
template <typename T>
int PropertyDelegate<T>::typeLua() {
static_assert(sizeof(T) == 0,
"Unimplemented PropertyDelegate::luaType specialization");
}
} // namespace properties
} // namespace openspace

View File

@@ -45,7 +45,19 @@ template <>
template <>
std::string PropertyDelegate<TemplateProperty<std::string>>::defaultValue<std::string>();
//REGISTER_TEMPLATEPROPERTY_HEADER(StringProperty, std::string);
template <>
template <>
std::string PropertyDelegate<TemplateProperty<std::string>>::fromLuaValue<std::string>(
lua_State* state, bool& success);
template <>
template <>
bool PropertyDelegate<TemplateProperty<std::string>>::toLuaValue<std::string>(
lua_State* state, std::string value);
template <>
int PropertyDelegate<TemplateProperty<std::string>>::typeLua();
} // namespace properties

View File

@@ -41,6 +41,11 @@ public:
virtual void set(boost::any value) override;
virtual const std::type_info& type() const override;
bool getLua(lua_State* state) const override;
bool setLua(lua_State* state) override;
int typeLua() const override;
operator T();
TemplateProperty<T>& operator=(T val);

View File

@@ -31,20 +31,52 @@ namespace properties {
std::string PropertyDelegate<TemplateProperty<TYPE>>::className(); \
template <> \
template <> \
TYPE PropertyDelegate<TemplateProperty<TYPE>>::defaultValue<TYPE>();
#define REGISTER_TEMPLATEPROPERTY_SOURCE(CLASS_NAME, TYPE, DEFAULT_VALUE) \
TYPE PropertyDelegate<TemplateProperty<TYPE>>::defaultValue<TYPE>(); \
template <> \
std::string PropertyDelegate<TemplateProperty<TYPE>>::className() { \
template <> \
TYPE PropertyDelegate<TemplateProperty<TYPE>>::fromLuaValue(lua_State* state, \
bool& success); \
template <> \
template <> \
bool PropertyDelegate<TemplateProperty<TYPE>>::toLuaValue(lua_State* state, \
TYPE value); \
template <> \
int PropertyDelegate<TemplateProperty<TYPE>>::typeLua();
#define REGISTER_TEMPLATEPROPERTY_SOURCE(CLASS_NAME, TYPE, DEFAULT_VALUE, \
FROM_LUA_LAMBDA_EXPRESSION, \
TO_LUA_LAMBDA_EXPRESSION, LUA_TYPE) \
template <> \
std::string PropertyDelegate<TemplateProperty<TYPE>>::className() \
{ \
return #CLASS_NAME; \
\
} \
template <> \
template <> \
TYPE PropertyDelegate<TemplateProperty<TYPE>>::defaultValue<TYPE>() { \
TYPE PropertyDelegate<TemplateProperty<TYPE>>::defaultValue<TYPE>() \
{ \
return DEFAULT_VALUE; \
\
}
} \
template <> \
template <> \
TYPE PropertyDelegate<TemplateProperty<TYPE>>::fromLuaValue<TYPE>(lua_State * state, \
bool& success) \
{ \
return FROM_LUA_LAMBDA_EXPRESSION(state, success); \
} \
template <> \
template <> \
bool PropertyDelegate<TemplateProperty<TYPE>>::toLuaValue<TYPE>(lua_State * state, \
TYPE value) \
{ \
return TO_LUA_LAMBDA_EXPRESSION(state, value); \
} \
template <> \
int PropertyDelegate<TemplateProperty<TYPE>>::typeLua() \
{ \
return LUA_TYPE; \
}
// Delegating constructors are necessary; automatic template deduction cannot
// deduce template argument for 'U' if 'default' methods are used as default values in
@@ -114,5 +146,27 @@ const std::type_info& TemplateProperty<T>::type() const {
return typeid(T);
}
template <typename T>
bool TemplateProperty<T>::setLua(lua_State* state)
{
bool success;
T value = PropertyDelegate<TemplateProperty<T>>::fromLuaValue<T>(state, success);
if (success)
set(value);
return success;
}
template <typename T>
bool TemplateProperty<T>::getLua(lua_State* state) const
{
bool success = PropertyDelegate<TemplateProperty<T>>::toLuaValue<T>(state, _value);
return success;
}
template <typename T>
int TemplateProperty<T>::typeLua() const {
return PropertyDelegate<TemplateProperty<T>>::typeLua();
}
} // namespace properties
} // namespace openspace

View File

@@ -51,7 +51,20 @@ namespace properties {
// char16_t and char32_t are not supported on Visual Studio 2013 and are defined to
// be equal to unsigned short and unsigned int which causes a compile error
REGISTER_TEMPLATEPROPERTY_SOURCE(BoolProperty, bool, false);
REGISTER_TEMPLATEPROPERTY_SOURCE(BoolProperty, bool, false,
[](lua_State* state, bool& success) -> bool {
success = (lua_isboolean(state, -1) == 1);
if (success)
return lua_toboolean(state, -1) == 1;
else
return false;
},
[](lua_State* state, bool value) -> bool {
lua_pushboolean(state, value);
return true;
},
LUA_TBOOLEAN
);
REGISTER_NUMERICALPROPERTY_SOURCE(CharProperty, char, char(0),
numeric_limits<char>::lowest(), numeric_limits<char>::max(), char(1),

View File

@@ -24,6 +24,8 @@
#include <openspace/properties/stringproperty.h>
#include <ghoul/lua/ghoul_lua.h>
namespace openspace {
namespace properties {
@@ -50,12 +52,31 @@ std::string PropertyDelegate<TemplateProperty<std::string>>::defaultValue<std::s
return "";
}
template <>
template <>
std::string PropertyDelegate<TemplateProperty<std::string>>::fromLuaValue<std::string>(
lua_State* state, bool& success)
{
success = lua_isstring(state, -1) == 1;
if (success)
return lua_tostring(state, -1);
else
return "";
}
//REGISTER_TEMPLATEPROPERTY_SOURCE(StringProperty, std::string, "");
//
//std::string openspace::properties::StringProperty::className() const {
// return "StringProperty";
//}
template <>
template <>
bool PropertyDelegate<TemplateProperty<std::string>>::toLuaValue<std::string>(
lua_State* state, std::string value)
{
lua_pushstring(state, value.c_str());
return true;
}
template <>
int PropertyDelegate<TemplateProperty<std::string>>::typeLua() {
return LUA_TSTRING;
}
} // namespace properties
} // namespace openspace

View File

@@ -102,14 +102,71 @@ namespace properties {
\
}
REGISTER_TEMPLATEPROPERTY_SOURCE(BVec2Property, glm::bvec2, glm::bvec2(false));
REGISTER_TEMPLATEPROPERTY_SOURCE(BVec3Property, glm::bvec3, glm::bvec3(false));
REGISTER_TEMPLATEPROPERTY_SOURCE(BVec4Property, glm::bvec4, glm::bvec4(false));
REGISTER_TEMPLATEPROPERTY_SOURCE(BVec2Property, glm::bvec2, glm::bvec2(false),
[](lua_State* state, bool& success) -> glm::bvec2 {
success = (lua_isboolean(state, -1) == 1) && (lua_isboolean(state, -2) == 1);
if (success)
return glm::bvec2(lua_toboolean(state, -1), lua_toboolean(state, -2));
else
return glm::bvec2(false);
},
[](lua_State* state, glm::bvec2 value) -> bool {
lua_pushboolean(state, value.x);
lua_pushboolean(state, value.y);
return true;
},
LUA_TTABLE
);
REGISTER_TEMPLATEPROPERTY_SOURCE(BVec3Property, glm::bvec3, glm::bvec3(false),
[](lua_State* state, bool& success) -> glm::bvec3 {
success = (lua_isboolean(state, -1) == 1) &&
(lua_isboolean(state, -2) == 1) &&
(lua_isboolean(state, -3) == 1);
if (success)
return glm::bvec3(lua_toboolean(state, -1),
lua_toboolean(state, -2),
lua_toboolean(state, -3));
else
return glm::bvec3(false);
},
[](lua_State* state, glm::bvec3 value) -> bool {
lua_pushboolean(state, value.x);
lua_pushboolean(state, value.y);
lua_pushboolean(state, value.z);
return true;
},
LUA_TTABLE
);
REGISTER_TEMPLATEPROPERTY_SOURCE(BVec4Property, glm::bvec4, glm::bvec4(false),
[](lua_State* state, bool& success) -> glm::bvec4 {
success = (lua_isboolean(state, -1) == 1) &&
(lua_isboolean(state, -2) == 1) &&
(lua_isboolean(state, -3) == 1) &&
(lua_isboolean(state, -4) == 1);
if (success)
return glm::bvec4(lua_toboolean(state, -1),
lua_toboolean(state, -2),
lua_toboolean(state, -3),
lua_toboolean(state, -4));
else
return glm::bvec4(false);
},
[](lua_State* state, glm::bvec4 value) -> bool {
lua_pushboolean(state, value.x);
lua_pushboolean(state, value.y);
lua_pushboolean(state, value.z);
lua_pushboolean(state, value.w);
return true;
},
LUA_TTABLE
);
REGISTER_NUMERICALPROPERTY_SOURCE(Vec2Property, glm::vec2, glm::vec2(0),
glm::vec2(numeric_limits<float>::lowest()), glm::vec2(numeric_limits<float>::max()),
glm::vec2(0.01f), DEFAULT_FROM_LUA_LAMBDA_2(glm::vec2, glm::vec2(0)),
DEFAULT_TO_LUA_LAMBDA_2(glm::vec2));
DEFAULT_TO_LUA_LAMBDA_2(glm::vec2)); // , LUA_TTABLE
REGISTER_NUMERICALPROPERTY_SOURCE(Vec3Property, glm::vec3, glm::vec3(0),
glm::vec3(numeric_limits<float>::lowest()), glm::vec3(numeric_limits<float>::max()),