Wireshark-dev: Re: [Wireshark-dev] const'ness of value_string_ext

Date: Fri, 25 Jul 2014 09:01:03 +0200
Hi,

On Thu, Jul 24, 2014 at 04:35:07PM -0400, Kevin Cox wrote:
> 2: Change everything.
> Pros:
> - Full compiler error checking and type safety.
> Cons:
> - Hard
> - May change dissector API.
> 
> I was wondering what everyone else thought and what should be done to
> improve the safety of this code.

a/ For C90 (C99?) compilers (gcc/clang) use structure^W union initializer:
 instead of const void *strings;

   union {
     const void *data;
     const value_string *vs;
     value_string_ext *vse;
     const true_false_string *tfs;
     ...
   } u;

   add new macros to initialize this union correctly:

#ifdef C_SUPPORTING_STRUCTURE_INITIALIZER
   #define VS_INIT(x) { .vs = x; }
   #define VSE_INIT(x) { .vse = x; }
#else
   #define VS_INIT(x) { x }
   #define VSE_INIT(x) { x }
#endif

   change VALS()/ TFS() macros to access union instead of cast.

b/ For any compiler:
  instead of const void *strings;

  do: const struct field_extension *field_ext;

  struct field_extension { int type; }

#define FIELD_EXT(vs) &(vs).base

  struct field_extension_vs { struct field_extension base; const value_string *vs; }
#define FIELD_INITIALIZE_VS(vs) { { FIELD_EXT_VS }, vs }

  struct field_extension_vse { struct field_extension base; value_string_ext *vse; }
#define FIELD_INITIALIZE_VSE(vse) { { FIELD_EXT_VSE }, vse }


Q: Can we do b/ easier if we go to C++?

Q: Is a/ union initializer supported by all new modern C++ compilers?
   For MSVC (AFAIR only when compiling C++ code) I see it was supported in MSVC2005: http://msdn.microsoft.com/en-us/library/81k8cwsz%28v=vs.80%29.aspx
   

Cheers,
Jakub.