Re: [Vala] VAPI-Bindings to obscure c-types.



----- Original Message -----

From: Daniel Brendle <grindhold skarphed org>
Sent: Monday, 5 December 2016, 7:53
Subject: [Vala] VAPI-Bindings to obscure c-types.

In libode's code, there is a type called dReal. Depending on some
compiler flags, dReal might be a double or a float:

#if defined(dSINGLE)
typedef float dReal;
#ifdef dDOUBLE
#error You can only #define dSINGLE or dDOUBLE, not both.
#endif /* dDOUBLE */
#elif defined(dDOUBLE)
typedef double dReal;
#else
#error You must #define dSINGLE or dDOUBLE
#endif

The library then uses dReal in various method signatures that I have to

bind against. Is there any possibility to cover this in vala?


These are macros that are defined without a value and are used by the C pre-processor. To define such a macro 
from valac, as you probably know, you use the -X option:
valac --pkg libode6 -X -DdDOUBLE example.vala

Vala has a similiar, but much simpler, pre-processor:
https://wiki.gnome.org/Projects/Vala/Manual/Preprocessor
https://wiki.gnome.org/Projects/Vala/ConditionalCompilationSample

The Vala pre-processor can also be used in a VAPI file:
#if ODE_FLOAT
  [SimpleType]

  [CCode (cname = "dReal", has_type_id = false)]
  public struct dReal : float {
  }
#else
  [SimpleType]
  [CCode (cname = "dReal", has_type_id = false)]
  public struct dReal : double {
  }
#endif

So for double you would compile with:
valac --pkg libode6 -X -DdDOUBLE example.vala


and for float:
valac --pkg libode6 -X -DdSINGLE -D ODE_FLOAT example.vala

You will have to use separate declarations for both the C pre-processor and the Vala pre-processor. While you 
can define a macro in a .vala file by using const, this cannot currently be emitted before the #include in 
the generated C file. So it is not set at the time when the C header is processed.

This is untested and probably doesn't reflect your binding, but should convey the method.

Al


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]