Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Clang and LLVM have solved your issue by generating tables containing their objects, using a combination of code generation and preprocessor trickery.</p> <p>You can skip either step, depending on your own setup. For example:</p> <pre><code>// records.inc EXPAND_RECORD(Foo, "Foo", 4); EXPAND_RECORD(Bar, "Bar", 18); EXPAND_RECORD(Bar2, "Bar", 19); </code></pre> <p>Now, you can generate your enum:</p> <pre><code>// records.h enum Record { #define EXPAND_RECORD(Name, String, Value) Name, #include "records.inc" #undef EXPAND_RECORD }; char const* getRecordName(Record r); int getRecordValue(Record r); // records.cpp char const* getRecordName(Record r) { switch(r) { #define EXPAND_RECORD(Name, String, Value) case Name: return String; #include "records.inc" #undef EXPAND_RECORD } abort(); // unreachable, or you can return a "null" value } int getRecordValue(Record r) { switch(r) { #define EXPAND_RECORD(Name, String, Value) case Name: return Value; #include "records.inc" #undef EXPAND_RECORD } abort(); // unreachable, or you can return a "null" value } </code></pre> <p>In Clang and LLVM, a code generation phase is used to generate the .inc from more pleasant definition files.</p> <p>It works pretty well... but do be aware that any modification of the enum implies full recompilation. You might wish to go to a "codeset" approach, where the enum is used internally but never leaked outside, and stable values (those of the enum) are provided to the client (<code>unsigned</code>), so that old clients can link to the new libraries without recompilation: they will be limited to use the old set of codes, which is no problem if it's stable.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload