[xsde-users] variable length member : suggestion for "_present (bool x)" methods

Boris Kolpackov boris at codesynthesis.com
Fri Sep 10 09:10:54 EDT 2010

Hi Ivan,

Ivan Le Lann <ivan.lelann at free.fr> writes:

> Editing an element schema, with C++ code going from fix to variable
> length, you might silently break some code. Typically.
> //
> foo f;
> f.bar_present(true);
> f.bar().baz();
> //
> If "bar" becomes variable length some day, you're dead.
> I propose to add an option to change generated code in "foo":
> inline
> void foo::
> bar_present (bool x)
> {
>   if (!x)
>   {
>     delete this->bar_;
>     this->bar_ = 0;
>   }
>   // patch suggestion : 
>   else if (!bar_present())
>   {
>     this->bar_ = new bar;
>   }
> }

Two problems with this suggestion:

1. When exceptions are disabled the memory allocation failure should be 
   propagated to the caller somehow. Right now the generated classes
   carefully avoid making any (implicit) memory allocations. All var-
   length objects are allocated explicitly by the user.

2. The more serious problem is the fact that the newly created object
   (bar in the code above) is itself uninitialized. Since it is var-
   length, it will most likely contain members that need initialization
   (e.g., a string member when STL is disabled). So the patch only
   postpones the problem for a little while:

   f.bar ().baz ();       // ok
   f.bar ().baz ().str () // segfault

I agree it would be nice to help the user here somehow. I just don't
see any good way to do it. Maybe not generate _present(bool) for var-
length members at all? The reset behavior can be achieved by simply 
passing the NULL pointer to the modifier function. What do you think?


More information about the xsde-users mailing list