Skip to content

Instantly share code, notes, and snippets.

@rpavlik
Created January 5, 2011 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rpavlik/5d6ece6101d3695e43c0 to your computer and use it in GitHub Desktop.
Save rpavlik/5d6ece6101d3695e43c0 to your computer and use it in GitHub Desktop.
/** @file osgLuaBind.h
@brief Partial header with template specializations providing Luabind-compatible
conversions to/from osgLua values
@date
2009-2010
@author
Ryan Pavlik
<rpavlik@iastate.edu> and <abiryan@ryand.net>
http://academic.cleardefinition.com/
Iowa State University Virtual Reality Applications Center
Human-Computer Interaction Graduate Program
*/
#pragma once
#ifndef INCLUDED_osgLuaBind_h
#define INCLUDED_osgLuaBind_h
// Local includes
// - none
// Library/third-party includes
#include <luabind/luabind.hpp>
#include <osgLua/Value.h>
#include <osgIntrospection/variant_cast>
// Standard includes
#ifdef BUILD_VERBOSE
#include <iostream>
#endif
namespace luabind
{
/// Base class for converting osg value types to/from osgLua values
template<typename OSG_QUALIFIED_TYPENAME>
struct osglua_val_converter_base : native_converter_base<OSG_QUALIFIED_TYPENAME> {
static int compute_score(lua_State* L, int index) {
osgLua::Value * v = osgLua::Value::get(L, index);
if (!v) {
#ifdef BUILD_VERBOSE
std::cout << "Not a osgLua value" << std::endl;
#endif
return -1;
}
static const osgIntrospection::Type& destType =
osgIntrospection::Reflection::getType(extended_typeid<OSG_QUALIFIED_TYPENAME>());
const osgIntrospection::Type& type = v->get().getType();
#ifdef BUILD_VERBOSE
std::cout << "Destination type: " << destType.getQualifiedName() << std::endl;
std::cout << "Value type: " << type.getQualifiedName() << std::endl;
#endif
if (type == destType) {
#ifdef BUILD_VERBOSE
std::cout << "Exact match for type!" << std::endl;
#endif
return 2;
} else if (type.isSubclassOf(destType)) {
#ifdef BUILD_VERBOSE
std::cout << "Convertible match for type." << std::endl;
#endif
return 1;
} else {
// attempting conversion
try {
OSG_QUALIFIED_TYPENAME temp = osgIntrospection::variant_cast<OSG_QUALIFIED_TYPENAME>(v->get());
} catch (...) {
return -1;
}
return 0; // OK, convertible, so better than nothing
}
}
OSG_QUALIFIED_TYPENAME from(lua_State* L, int index) {
osgLua::Value * v = osgLua::Value::get(L, index);
if (!v) {
return OSG_QUALIFIED_TYPENAME();
}
return osgIntrospection::variant_cast<OSG_QUALIFIED_TYPENAME>(v->get());
}
void to(lua_State* L, OSG_QUALIFIED_TYPENAME const& x) {
osgLua::Value::push(L, x);
}
};
}
#ifndef CREATE_OSGLUA_VALUE_CONVERTER
/// Macro to create converters required to bind functions with osg-typed
/// value arguments with Luabind
#define CREATE_OSGLUA_VALUE_CONVERTER(T) \
namespace luabind { \
template <> \
struct default_converter<T> \
: osglua_val_converter_base<T> \
{}; \
\
template <> \
struct default_converter<T const&> \
: default_converter<T> \
{}; \
\
namespace detail {\
template <> \
struct type_to_string<T> \
{ \
static void get(lua_State* L) \
{ \
lua_pushstring(L, #T); \
} \
}; \
}\
}
#endif
#include <osg/Matrixd>
#include <osg/Vec3d>
#include <osg/Vec4d>
#include <osg/Quat>
CREATE_OSGLUA_VALUE_CONVERTER(osg::Matrixd);
CREATE_OSGLUA_VALUE_CONVERTER(osg::Vec3d);
CREATE_OSGLUA_VALUE_CONVERTER(osg::Vec4d);
CREATE_OSGLUA_VALUE_CONVERTER(osg::Quat);
#endif // INCLUDED_osgLuaBind_h
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment