0

I have a project in which there are many names that, at the moment are hardcoded. The project is 2/5th C, 2/5th Python and 1/5th Python C-extension code.

I would like to "define" a lot of names for the project. I was thinking in two general formats:

// global.h
static const char[] FIELD_NAME = "__random_fields";
static const char[] DETERMINISTIC_NAME = "__non_random_fields";
static const uint32_t THISTHAT = 0x234;
static const float VERSION = 24.5;

for the C, and for the Python:

# global.py
FIELD_NAME = '__random_fields'
DETERMINISTIC_NAME = '__non_random_fields'
THISTHAT = 0x234
VERSION = 24.5

I would prefer to use static const instead of #define (for the C code), but it is only a personal preference in this project.

One option is to use a intermediate representation and add some script magic make-time parsing to generate both .h and .py. Would that be reinventing the wheel? Are there some general strategies or tips for achieving this? There is some Java in the project, so using .properties would be ok, but... the problem persists, I need to do some magic conversion between formats.

MariusSiuram
  • 3,380
  • 1
  • 21
  • 40
  • Can't the Python code use the C constants as an extension module? The same could be done for the Java code via the JNI. – 5gon12eder Jan 13 '15 at 11:08
  • those should be compile-time constants (and seldom changed). I cannot afford JNI calls. Using C constants as an extension module... I thought a little bit, but seemed to require a lot of boilerplate code. – MariusSiuram Jan 13 '15 at 11:10
  • Well, if you don't want to have the languages talk to each other for sharing a single definition of the constants, then the only other option I can see is using code generation to generate three (hopefully) identical versions of parameter definition files. If those are just variable assignments, it shouldn't be too hard to do. Maybe use the Python version as the authoritative source (that you edit) and write a simple Python script that outputs the C and Java version. – 5gon12eder Jan 13 '15 at 11:15

0 Answers0