On Wed 22 Apr 2015, Frank Henigman wrote: > Use the new WAFFLE_ENUM_LIST macro to implement waffle_enum_to_string() > instead of specifying all the enums again. > Now it will no longer be necessary to update waffle_enum_to_string() > when adding an enum. > Use bsearch() instead of a case statement, not because it's better, > but to be symmetric with a new waffle_string_to_enum() function. > > Signed-off-by: Frank Henigman <fjhenig...@google.com> > --- > src/waffle/core/wcore_util.c | 94 > ++++++++++++++++++++++---------------------- > 1 file changed, 47 insertions(+), 47 deletions(-)
[snip] > const char* > wcore_enum_to_string(int32_t e) > { > - switch (e) { > - #define CASE(x) case x: return #x > - > - CASE(WAFFLE_DONT_CARE); > - CASE(WAFFLE_NONE); > - CASE(WAFFLE_PLATFORM); > - CASE(WAFFLE_PLATFORM_ANDROID); > - CASE(WAFFLE_PLATFORM_CGL); > - CASE(WAFFLE_PLATFORM_GLX); > - CASE(WAFFLE_PLATFORM_WAYLAND); > - CASE(WAFFLE_PLATFORM_X11_EGL); > - CASE(WAFFLE_PLATFORM_GBM); > - CASE(WAFFLE_PLATFORM_WGL); > - CASE(WAFFLE_PLATFORM_NACL); > - CASE(WAFFLE_CONTEXT_API); > - CASE(WAFFLE_CONTEXT_OPENGL); > - CASE(WAFFLE_CONTEXT_OPENGL_ES1); > - CASE(WAFFLE_CONTEXT_OPENGL_ES2); > - CASE(WAFFLE_CONTEXT_OPENGL_ES3); > - CASE(WAFFLE_CONTEXT_MAJOR_VERSION); > - CASE(WAFFLE_CONTEXT_MINOR_VERSION); > - CASE(WAFFLE_CONTEXT_PROFILE); > - CASE(WAFFLE_CONTEXT_CORE_PROFILE); > - CASE(WAFFLE_CONTEXT_COMPATIBILITY_PROFILE); > - CASE(WAFFLE_CONTEXT_FORWARD_COMPATIBLE); > - CASE(WAFFLE_CONTEXT_DEBUG); > - CASE(WAFFLE_RED_SIZE); > - CASE(WAFFLE_GREEN_SIZE); > - CASE(WAFFLE_BLUE_SIZE); > - CASE(WAFFLE_ALPHA_SIZE); > - CASE(WAFFLE_DEPTH_SIZE); > - CASE(WAFFLE_STENCIL_SIZE); > - CASE(WAFFLE_SAMPLE_BUFFERS); > - CASE(WAFFLE_SAMPLES); > - CASE(WAFFLE_DOUBLE_BUFFERED); > - CASE(WAFFLE_ACCUM_BUFFER); > - CASE(WAFFLE_DL_OPENGL); > - CASE(WAFFLE_DL_OPENGL_ES1); > - CASE(WAFFLE_DL_OPENGL_ES2); > - CASE(WAFFLE_DL_OPENGL_ES3); > - CASE(WAFFLE_WINDOW_WIDTH); > - CASE(WAFFLE_WINDOW_HEIGHT); > - > - default: return NULL; > - > - #undef CASE > - } > + enum_sort(); > + struct enum_map_entry key = { .value = e }; > + struct enum_map_entry *found = bsearch(&key, > + enum_map_value, > + ARRAY_SIZE(enum_map_value), > + sizeof(enum_map_value[0]), > + enum_cmp_value); > + if (!found) > + return NULL; > + > + return found->name; > } Frank, for generating code like this, you may want to refer to very similar enum_to_string code in Piglit for which I wrote a generator script: http://cgit.freedesktop.org/piglit/tree/tests/util/piglit-util-gl-enum-gen.c.mako?id=6122d1dba102b53602e5b19dd46d0975d28b6cd4 And another more complex script that generates piglit's GL dispatch: http://cgit.freedesktop.org/piglit/tree/tests/util/piglit-dispatch-gen.c.mako?id=6122d1dba102b53602e5b19dd46d0975d28b6cd4 There's *lot* of complex generated code in Piglit that heavily uses Mako templates. Adding Mako as a Waffle buildtime dependency might be overkill for the little amount of code generation that Waffle needs to do. On the other hand, for generating code, Mako templates are more concise than ad-hoc Python scripts. Mako, ad-hoc Python, or bash+sed --- It doesn't really matter to me which gets used to generate this function. But I wanted to introduce you to Piglit's Mako files anyway, just in case you ever needed to do crazy complex code generation for some other purpose, for Waffle or any other project. _______________________________________________ waffle mailing list waffle@lists.freedesktop.org http://lists.freedesktop.org/mailman/listinfo/waffle