C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

use policies to "toggle" members present/not-present in clas

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
isaacyho@gmail.com
Guest





PostPosted: Sat Aug 21, 2004 4:06 am    Post subject: use policies to "toggle" members present/not-present in clas Reply with quote



I seem to have trouble using template policies to achieve the
following, and was wondering if this was a "misuse" of templates ( to
the extent that there are no good ways of doing this ):

I have a class for which it would be great to be able to toggle
various members present / not present in the class. An example would
be a Car:

class Car
{
Engine engine;
Spoiler spoiler;
Radio radio;
};

The engine should always be there, but the spoiler and radio may/may
not. An idea is to use templates:

class HasSpoilerPolicy
{
Spoiler spoiler;
};

class HasAmFmStereoPolicy
{
AmFmStereo stereo; // can be am/fm, or cd player, or none
};
class HasCDStereoPolicy
{
CDStereo stereo; // can be am/fm, or cd player, or none
};

template
< SPOILER_POLICY, RADIO_POLICY >
class Car : public SPOILER_POLICY, RADIO_POLICY
{
Engine engine;
};

The motivation is to only include the data members needed by the
client: if you don't have a radio in the car, don't include even a
pointer for it.

These design decisions do seem orthogonal: the presence of a spoiler
does not affect the engine or radio, for instance. But the problem
seems to be that of coordinating the various classes, esp. during
construction. How do we get a clean construction syntax for such an
object? The best I can come up with is

template
<SPOILER_POLICY, SPOILER_CTOR_T, RADIO_POLICY, RADIO_CTOR_T>
class Car : public SPOILER_POLICY, RADIO_POLICY
{
public:
Car( const Engine & engine_, SPOILER_CTOR_T spoiler_, RADIO_CTOR_T
radio_ ) : SPOILER_POILCY( spoiler_ ), RADIO_POLICY( radio_ ) {}
};

class NullPolicy
{
template <T> NullPolicy( T const & ) {}
};

with client syntax

main()
{
Engine engine;
AmFmRadio radio;

Car< NullPoilcy, HasRadioPoilcy > car( engine, null, radio );
}

This forces the client to include a dummy value in the Car constructor
for the Null fields.

Any better way? Or should I just give up using templates for this
purpose ( toggling central data values )? This solution does seem
very similar to the policies presented by Andresceau ( smart_ptr ),
except that the toggled data structures have to be constructed. It's
nice because it could potentially solve the combinatorial explosion
resulting from 2^n possible toggles, so I'd like to use it if I can do
it in a cleaner way.

Thanks,

Isaac

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Michiel Salters
Guest





PostPosted: Tue Aug 24, 2004 10:40 pm    Post subject: Re: use policies to "toggle" members present/not-present in Reply with quote



[email]isaacyho (AT) gmail (DOT) com[/email] wrote in message news:<ddcb36f8.0408201551.3355cbd6 (AT) posting (DOT) google.com>...
Quote:
I seem to have trouble using template policies to achieve the
following, and was wondering if this was a "misuse" of templates ( to
the extent that there are no good ways of doing this ):

I have a class for which it would be great to be able to toggle
various members present / not present in the class. An example would
be a Car:

class Car
{
Engine engine;
Spoiler spoiler;
Radio radio;
};

The engine should always be there, but the spoiler and radio may/may
not. An idea is to use templates:

Another is to use boost::mpl ([url]www.boost.org)[/url]. No need to reinvent
the wheel.

Quote:
The motivation is to only include the data members needed by the
client: if you don't have a radio in the car, don't include even a
pointer for it.

These design decisions do seem orthogonal: the presence of a spoiler
does not affect the engine or radio, for instance. But the problem
seems to be that of coordinating the various classes, esp. during
construction. How do we get a clean construction syntax for such an
object?

Again, using mpl. The mpl typelist allows you to pass in the types
you want in your class. Use the mpl enable_if on the length of the
typelist, and you can create a ctor that takes one argument for each
type in the typelist. Altrenatively, pass in one typelist.

Regards,
Michiel Salters

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Nicola Musatti
Guest





PostPosted: Wed Aug 25, 2004 9:45 pm    Post subject: Re: use policies to "toggle" members present/not-present in Reply with quote



[email]Michiel.Salters (AT) logicacmg (DOT) com[/email] (Michiel Salters) wrote in message news:<fcaee77e.0408240716.39bfd194 (AT) posting (DOT) google.com>...
[...]
Quote:
Again, using mpl. The mpl typelist allows you to pass in the types
you want in your class. Use the mpl enable_if on the length of the
typelist, and you can create a ctor that takes one argument for each
type in the typelist. Altrenatively, pass in one typelist.

Could you provide an example, please? I was about to post almost the
same question the OP asked...

Cheers,
Nicola Musatti

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Craig Henderson
Guest





PostPosted: Tue Sep 07, 2004 10:25 pm    Post subject: Re: use policies to "toggle" members present/not-present in Reply with quote

<isaacyho (AT) gmail (DOT) com> wrote

Quote:
I seem to have trouble using template policies to achieve the
following, and was wondering if this was a "misuse" of templates ( to
the extent that there are no good ways of doing this ):

I have a class for which it would be great to be able to toggle
various members present / not present in the class. An example would
be a Car:

class Car
{
Engine engine;
Spoiler spoiler;
Radio radio;
};

The engine should always be there, but the spoiler and radio may/may
not.

See my article "Optional Storage" in CUJ 22(5) May 2004 for a discussion and
code to do exactly this. If you don't subscribe to CUJ - and why wouldn't
you Smile - email me private. See
http://freespace.virgin.net/craig.henderson/site/email.htm

Regards
-- Craig



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.