|
Boost : |
From: Larry Evans (cppljevans_at_[hidden])
Date: 2006-01-08 10:19:25
On 01/08/2006 04:37 AM, Ion Gaztañaga wrote:
[snip]
> types to call that constructor. What I would propose is the possibility
> of selecting via integer value which member of boost::variant will be
> constructed, just like which() returns the index of constructed member.
> This way, we can efficiently compact members of classes.
>
> class MyRWLock
> {
> public:
> enum { r_prioriy, w_prioriy, a_prioriy };
>
> MyRWLock(int run_time_policy);
>
> private:
> struct r_prioriry_members
> {
> //...
> };
> struct w_prioriry_members
> {
> //...
> };
> struct a_prioriry_members
> {
> //...
> };
>
> //Union of needed members
> boost::variant<r_prioriry_members,
> w_prioriry_members,
> a_prioriry_members> m_data;
> };
>
> inline MyRWLock::MyRWLock(int run_time_policy)
> : m_data(/*what do I have to do to select a constructor?*/)
> {
>
> }
>
[snip]
Wizofaus had a somewhat similar problem which he posted here:
In summary, he wanted a run time integer index to select a
constructor to create some element of a variant. A solution
posted in http://boost-consulting.com/vault/ under:
Template Metaprogramming/little.funvec_.zip(little.funvec_foreach.cpp)
uses a static constant vector of function pointers:
setter_rec<RecId>::prp_vec_ctor const
setter_rec<RecId>::our_prp_vec;
which was used in:
setter_rec<RecId>::set(void*in,int prop,int value)
{ our_prp_vec[prop](in,value); }
The above set corresponds to your runtime selection of
the ctor from a vector of "pseudo" ctor's stored in
our_prp_vec. The void*in corresponds to your &m_data.
The above value argument would not be needed since the
"pseudo" ctor's in our_prp_vec would supply the
required arguments while invoking a placement new
on &m_data.
I've got something almost like this on my local machine.
It's a successor to the boost sandbox boost/indexed_types.
Let me know if you're interested.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk