Boost logo

Boost Users :

From: Ovanes Markarian (om_boost_at_[hidden])
Date: 2006-06-28 12:39:18


Dear Users and Developers,

I am currently working on a kind of discriminated union type, e.g. XDR specific union type
implementation (please refer to http://www.faqs.org/rfcs/rfc1832.html section) which maps
enumeration values to types. In this case one wants to provide users as much comfort as possible
for accesing this union. I have written a template class, which is defined by mpl::vector pairs of
discriminant and related type. This class contains a boost::variant definition from this sequence.

The problem is, that accessing the values etc. of this variant is a little bit clumsy:

discriminated_union_inst.get<some const int for discrminant>();
discriminated_union_inst.set<some const int for discrminant>(new value);

So I defined some accessor template. If initialized the accessor is responsible to access only one
particular discriminant related type

for example if discriminated_union is defined by sequence of:
mpl_derived_pair<0, int>, mpl_derived_pair<1, std::string> ... mpl_derived_pair<N, MyClass>

I define an accessor object:
accessor<0> accessor_for_0(union_instance);

later on this accessor allows the access if valid data is contained in the union.

if union contains second type (mpl_derived_pair<1, string>)

accessor_for_0->some_value //throws exception

or

if(accessor_for_0) // is true: means unions contains the correct type
  //do some access if true

Now I must say: It was a pleasure to implement this using many boost libraries!

But as some of you might have seen I have to pass to all accessors the reference or pointer to the
accessed discrminated union type. So every instance requires on 32bit OS at least 4 bytes. The
banal thing is that a group of accessors accesses the same type. The question/idea here would be
to use or to make these objects share the reference, so that all related accessor references
require 4 bytes. What do you think of this? Is it possible at all?

With Kind Regards,

Ovanes Markarian


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net