Binding to static float*

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Binding to static float*

john dunn-3
In my app I have a bunch of static float variables that I'd like to
bind directly to lua variables. I'm able to give access to these
variables via a symbol table with a peek/poke interface but I'd prefer
if lua could talk directly to the variables. What I'd like to do is
something like

float* a = ptr_to_some_float;
float* b = ptr_to_some_other_float;

extern "C" int init( lua_State* L ) {
  luabind::module(L,"mem" ) [
    luabind::def( "a", a ),
    luabind::def( "b", b )
  ];
  return 0;
}

Which of course doesn't work since def only takes function pointers. I
also tried to wrap my pointers in a struct and then expose the
pointers through that

struct data_t {
  float* a;
  float* b;
};

extern "C" int initdsp(lua_State* L) {
  luabind::module(L,"dsp")
  [
    luabind::class_<data_t>("data")
      .def(luabind::constructor<>())
      .def_readwrite( "a", &data_t::a )
      .def_readwrite( "b", &data_t::b )
  ];
  return 0;
}

But that asserts due to an unregistered type - I assume it doesn't
know how to deal with float*.

Basically all I want to do is have a global variable in lua which
mirrors a variable in C++. Setting the variable in lua will directly
set it in C++ and getting the variable in lua will return the value in
C++. If it's not possible with a global variable wrapping it inside a
class would also be ok - but the class has to store pointers to the
data and not the actual data. It's not possible to move the data to be
contained within a class.

Any pointers would be greatly appreciated.

John Dunn

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Binding to static float*

Arvid Norberg-2

On Oct 5, 2006, at 19:27, john dunn wrote:

> In my app I have a bunch of static float variables that I'd like to
> bind directly to lua variables. I'm able to give access to these
> variables via a symbol table with a peek/poke interface but I'd prefer
> if lua could talk directly to the variables. What I'd like to do is
> something like
>
> float* a = ptr_to_some_float;
> float* b = ptr_to_some_other_float;
>
> [...]
>
> struct data_t {
>   float* a;
>   float* b;
> };
>
> extern "C" int initdsp(lua_State* L) {
>   luabind::module(L,"dsp")
>   [
>     luabind::class_<data_t>("data")
>       .def(luabind::constructor<>())
>       .def_readwrite( "a", &data_t::a )
>       .def_readwrite( "b", &data_t::b )
>   ];
>   return 0;
> }
>
> But that asserts due to an unregistered type - I assume it doesn't
> know how to deal with float*.

Right, this approach would have worked if the actual variables was  
inside the struct, so you wouldn't have had pointers to them.

> Basically all I want to do is have a global variable in lua which
> mirrors a variable in C++. Setting the variable in lua will directly
> set it in C++ and getting the variable in lua will return the value in
> C++. If it's not possible with a global variable wrapping it inside a
> class would also be ok - but the class has to store pointers to the
> data and not the actual data. It's not possible to move the data to be
> contained within a class.
>
> Any pointers would be greatly appreciated.

You can go with the approach of binding a struct with set and get  
member functions, and bind them as properties. Then you will have the  
ordinary variable-look and feel in lua, but the C++ getters and  
setters are called.

See the end of the properties section:
http://www.rasterbar.com/products/luabind/docs.html#properties


--
Arvid Norberg



-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user