3

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...

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

3 Comments

Pity there isn't a solution for #define constants too, but this helps :D
@Mispy: There is a solution for #define constants; see FFI::ConstGenerator.
@stopanko questions go in questions, not comments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.