// Must match the value of DIR_MODULE in base/base_paths.h! public static final int DIR_MODULE = 3;
What about defining enums in a .proto file and generating code for both C++ and Java?
[ long email summary: how about we auto generate java enums from generic include files like net_error_list.h? ]
[ long email summary: how about we auto generate java enums from generic include files like net_error_list.h? ]
Howdy,In the android port, we have a bunch of places where we need to synchronize the values of integer constants / enums across java and C++ code.To date, the convention has been to duplicate a list of explicitly valued constants, and have a cross-ref comment stating the two should be kept in sync(trivial example: http://src.chromium.org/viewvc/chrome/trunk/src/base/android/java/src/org/chromium/base/PathService.java?revision=149842&view=markup// Must match the value of DIR_MODULE in base/base_paths.h! public static final int DIR_MODULE = 3;This has obviously brittleness and is fairly unattractive, so we've played with alternatives like using bootstrapping the constant values at runtime (a-la swig), but these tend to be worse: much more boiler plate code, cause bloat by forcing the compiler to including all the string *names* of the constants in *both* binaries (java and C++) where before it may have been able to inline the numeric literal values, slows startup (not only wiring up the constants, but also registering the JNI methods needed in order to wire up the constants), and means one side or other has some not-really-constant constants (if they need initializing at runtime), and can still leave random runtime errors for what should be a compile time issue.So, looking at the gargantuan list of codes in net_error_list.h gave me inspiration to auto-generate the java side constants from the same source file. Example patch to do this: http://crrev.com/10870050 example output: http://pastebin.com/CWMCGqqtThis is so naively simple it's untrue, but AFAICT it addresses all the previous drawbacks completely. (no RAM/ROM/runtime/startup/code maintenance costs. generated consts have no overhead at all if not used, assuming we proguard the jar).It has a couple implications though:- locks in the "DECLARE_CONST(name, value)" format of net_error_list.h, as we'll have some less conventional tools reading it- likely promotes use of this multiple-include header style in more places in the code (and especially in android-specific code) as everyone will want to make their constants easily accessible in java too(- deepens our use of SHOUTING_CASE constants rather than kCamelConstants, as this is the common denominator style that works nicely in java and C++. but really, we've got this already even with manually synchronized constants)
So, for those that read this far, wdyt? Is that a acceptable impact on rest of the project? alternatives?Some answers to other FAQs- we're not looking at swig etc at this time, as we have java <-> native invocation working very nicely using a simple java->C code generation tool- the above tool can't handle constants so well, as in many of places the authoritative value already lives in the native side, and we don't particularly fancy foisting a java->C conversion step on non-java platforms just to get some constants- the use of gcc preprocessor in my patch above is really for illustration only :) but, yeah, it works pretty well for now- a more advanced tool could render the need for a checked-in net_error_java.template file redundant too. The java class name and package, and (optional) constant name prefix (ERR_...) would be easy to set in gyp params. (actually, we could do that with preprocessor define too, if we really wanted)
On Thu, Aug 23, 2012 at 5:57 PM, Jonathan Dixon <jo...@google.com> wrote:
[ long email summary: how about we auto generate java enums from generic include files like net_error_list.h? ]SGTM. Even better, generate actual java enums to get the additional benefits of enum-value-name-as-string as well as an all-legal-values accessor?
Plus, once we decide this is the way the project does constants, we can get a single technique for stringifying enum value names even in C++ (like net does, but for the rest of the codebase).
-a
Plus, once we decide this is the way the project does constants, we can get a single technique for stringifying enum value names even in C++ (like net does, but for the rest of the codebase).What would be the benefit of this exactly? Just curious.
What about defining enums in a .proto file and generating code for both C++ and Java?
It has a couple implications though:- locks in the "DECLARE_CONST(name, value)" format of net_error_list.h, as we'll have some less conventional tools reading it- likely promotes use of this multiple-include header style in more places in the code (and especially in android-specific code) as everyone will want to make their constants easily accessible in java too(- deepens our use of SHOUTING_CASE constants rather than kCamelConstants, as this is the common denominator style that works nicely in java and C++. but really, we've got this already even with manually synchronized constants)If you are going to autogenerate these anyway, could you have the script generate "kCamelCase" for C++ and SHOUTING_CASE for Java?