Wireshark-dev: Re: [Wireshark-dev] const'ness of value_string_ext
From: Kevin Cox <kevincox@xxxxxxxxxxx>
Date: Fri, 25 Jul 2014 10:40:37 -0400
On 25/07/14 03:01, darkjames-ws@xxxxxxxxxxxx wrote:
> 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.

This isn't bad but I don't think we want to require C99.  Even msvcc
isn't too good at C99.

> 
> 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 }
> 

I like this quite a lot.  Traditional C inheritance.  It might be best
to do a true vtable though.

struct field_ext_type {
	const char *(*to_str)(field_ext *self, int val);
	...
};

struct field_ext {
	struct_field_ext_type *type;
};

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

Yes, because C++ does all of the type-tracking and virtual functions for us.

> 
> 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
>

No clue.

Attachment: signature.asc
Description: OpenPGP digital signature