Boost logo

Boost Users :

Subject: Re: [Boost-users] Needs advices on design ( mpl or processor )
From: Larry Evans (cppljevans_at_[hidden])
Date: 2012-01-10 14:24:44


On 01/10/12 11:43, Allan Nielsen wrote:
> Hi
>
> Just for the record, here is the souce code I ended up using.
>
> It is quite different from the compressed-tuple source, and the source
> code Larry suggested. It is most likely not as generric as these
> alternativies and it will only work with simple types as
> storege-buffer.
>
>
> But it has some features which is important for me:
>
> - Struct like groups
> EXAMPLE:
> CompressedEnums<
> unsigned,
> CompressedEnum<e1>,
> EnumStruct<e2,
> CompressedEnum<e1>,
> CompressedEnum<e2>,
> CompressedEnum<e3>
> >,
> CompressedEnum<e3>,
> CompressedEnum<e4>
> > ce2;
>
> ce2.set<e1>(e1::E1);
> std::cout << ce2.get<e1>() << std::endl;
>
> ce2.set<e2, e1>(e1::E1);
> std::cout << ce2.get<e2, e1>() << std::endl;
>
> - Unions like groups
> CompressedEnums<
> unsigned,
> CompressedEnum<e1>,
>
> EnumUnion<e2,
> CompressedEnum<e1>,
> CompressedEnum<Abc>,
> CompressedEnum<YesNoMaby>
> >,
> CompressedEnum<e3>
> > ce7;
>
> ce7.set<e2, Abc>(Abc::B);
> cout << ce7.get<e2, e1>() << " " <<
> ce7.get<e2, Abc>() << " " <<
> ce7.get<e2, YesNoMaby>() << endl;
> ce7.set<e2, Abc>(Abc::A);
> cout << ce7.get<e2, e1>() << " " <<
> ce7.get<e2, Abc>() << " " <<
> ce7.get<e2, YesNoMaby>() << endl;
> ce7.set<e2, YesNoMaby>(YesNoMaby::Maby);
> cout << ce7.get<e2, e1>() << " " <<
> ce7.get<e2, Abc>() << " " <<
> ce7.get<e2, YesNoMaby>() << endl;
>
> - Arrays (of leafs only)
> CompressedEnums<
> unsigned,
> CompressedEnum<e1>,
> CompressedEnumArray<Abc, 5>,
> CompressedEnum<e3>
> > ce6;
>
> ce6.data = 0;
> for (int i = 0; i < ce6.get_size<Abc>(); i++) {
> std::cout << ce6.get<4, Abc>() <<
> ce6.get<3, Abc>() <<
> ce6.get<2, Abc>() <<
> ce6.get<1, Abc>() <<
> ce6.get<0, Abc>() <<
> std::endl;
> ce6.next<0, Abc>();
> }
>
> Comments are always appreciated, but before you dive into the code, I
> should warn you that this is the first time I have tried to sue the
> boost::mpl for something ( other than playing around ).
>
> Thanks for all the help and inputs.
>
You're most welcome. I'm interested because I had a similar
problem with tuples and tagged unions which was implemented
here:

http://svn.boost.org/svn/boost/sandbox/variadic_templates/boost/composite_storage/pack/

The tuple was implemented in:

  container_all_of_aligned.hpp

and the tagged union in:

  container_one_of_maybe.hpp

However, that implementation was complicated by having to calculate
the correct alignment, a problem absent in your CompressedEnums since
you want to pack everything in as small a space as possible and only
unpack when needed (via the get functions).

IIUC, the EnumUnion has no tag, which means you could store an Enum1
and retrieve an Enum2 without any warning. Is that correct?

In UnionModel, there's:

    static const size_t size
      = head::size > _tail::size ? head::size : _tail::size;

which makes sense because a union just has to be large enough to store
the largest enumeration. However, in the case of StructModel_r, I
don't understand:

    static const size_t size = head::size * _tail::size;

around line 200. I would think that there would just be additions
since you want to store one value after another. That's why, in my
previous post, in template sum_bits, there's:

      , integral_c<unsigned,Bits+enum_bits<Enum>::size>

Could you explain why multiplication instead of addition is used to
calculate the size in StructModel_r?

Also, is there any check that the storage_type is large enough
to store all the bits?

-regards,
Larry


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