Question

I have a (somewhat exorbitant) number of these:

extern TCODLIB_API const TCOD_color_t TCOD_red;
extern TCODLIB_API const TCOD_color_t TCOD_flame;
extern TCODLIB_API const TCOD_color_t TCOD_orange;
extern TCODLIB_API const TCOD_color_t TCOD_amber;
extern TCODLIB_API const TCOD_color_t TCOD_yellow;
extern TCODLIB_API const TCOD_color_t TCOD_lime;

How can I get at the values on the Ruby side nicely? It feels like there should be an attach_const equivalent to attach_function, as used below:

module TCOD
  extend FFI::Library
  ffi_lib File.join(APP_ROOT, "libtcod-1.5.1/libtcod.so")
  attach_function :color_RGB, 'TCOD_color_RGB', [:uchar, :uchar, :uchar], Color.val
end

I'd prefer not to have to redefine everything from lightest_sepia to desaturated_chartreuse if at all possible...

Was it helpful?

Solution

There is, attach_variable is what you're looking for.

This will work for anything that is actually a global variable (which looks to be your case) but not if the constants are just #define macros.

Quoting the FFI::Library documentation for examples:

module Bar
  extend FFI::Library
  ffi_lib 'my_lib'
  attach_variable :c_myvar, :myvar, :long
end
# now callable via Bar.c_myvar

and:

module Bar
  extend FFI::Library
  ffi_lib 'my_lib'
  attach_variable :myvar, :long
end
# now callable via Bar.myvar
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top