[xsde-users] XSDE : choice_arm function code generation question
Boris Kolpackov
boris at codesynthesis.com
Wed Feb 9 08:36:26 EST 2022
Yegnaram, Shrikant <SYegnaram at cls-bank.com> writes:
> union
> {
> union
> {
> size_t align_;
> char data_[sizeof (::xmlevents::XYZ_CONTROL_CLASS)];
> } control_;
> ::xmlevents::XYZ_COM_INPUT_CLASS* comInput_;
> ::xmlevents::ABC_INPUT_CLASS* abcInputClass_;
> ::xmlevents::XYZ_NOTIFICATION_CLASS* notification_;
> }
>
> [...]
>
> void XYZMessageClass::
> choice_arm (choice_arm_tag x)
> {
> if (this->choice_arm_ == x)
> return;
>
> switch (this->choice_arm_)
> {
> case control_tag:
> {
> typedef ::xmlevents::XYZ_CONTROL_CLASS _dtor;
> reinterpret_cast< ::xmlevents::XYZ_CONTROL_CLASS& > (this->choice_.control_).~_dtor ();
> break;
> }
> case comInput_tag:
> {
> delete this->choice_.comInput_;
> break;
> }
> case abcInputClass_tag:
> {
> delete this->choice_.abcInputClass_;
> break;
> }
>
> ...
>
> this->choice_arm_ = choice_arm_tag (57);
>
> switch (x)
> {
> case control_tag:
> {
> new (&this->choice_.control_) ::xmlevents::XYZ_CONTROL_CLASS;
> break;
> }
> case comInput_tag:
> {
> this->choice_.comInput_ = 0;
> break;
> }
> case abcInputClass_tag:
> {
> this->choice_.abcInputClass_ = 0;
> break;
> }
>
> ...
>
> this->choice_arm_ = x;
> }
>
>
> The question here is regarding elements like XYZMessage.comInput are
> not allocated / deallocated memory in the choice_arm function like
> XYZMessage.control field.
The difference is due to whether the type is fixed or variable-length.
This is covered in Section 4.2, "Memory Management":
https://codesynthesis.com/projects/xsde/documentation/cxx/hybrid/guide/#4.2
> 1. Why is XSDE creating union {align,data_} for some of the classes
> in the xs:choice and not for others? We would like to understand
> this better.
This is due to the fixed vs variable-length distinction.
> 2. If we can change the default behaviour to generate
> allocate/deallocate memory code for all such enums using any
> XSDE code generation options in choice_arm function
No, this is not going to be easy (unless you can change all your
types to be fixed-length). But perhaps, after reading Section 4.2,
you will see the reasoning behind doing it this way?
> 3. What is the ideal way to create these objects for serialization
For a fixed-length choice arm you can just assing/copy by-value. For
a variable-length arm, you would allocate the arm object with new
and then pass the pointer to it. Section 4.5, "Compositors" has some
examples:
https://codesynthesis.com/projects/xsde/documentation/cxx/hybrid/guide/#4.5
More information about the xsde-users
mailing list