f



policy container?

Everyone,


How can I design a container of policies?

So that it can be passed as to a class ctor?

Or is this even a good idea if I have 6 or more policies?

Thanks,
Jeff



      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Jeffrey
6/26/2003 7:36:47 PM
comp.lang.c++.moderated 10738 articles. 1 followers. allnor (8510) is leader. Post Follow

1 Replies
500 Views

Similar Articles

[PageSpeed] 7

Jeffrey Graham wrote:
> Everyone,
> 
> 
> How can I design a container of policies?
> 
I suppose that you could use an empty PolicyBase class like
class PolicyBase { };
class MyPolicy : PolicyBase { // implementation };
etc, . . . but I don't think it's a good idea.

> So that it can be passed as to a class ctor?
> 
> Or is this even a good idea if I have 6 or more policies?
>
Policy classes are orthogonal. So having a container of policies means 
that the policies support the ISA relationship which in turn implies 
that the policies are not orthogonal. Well, some policies aren't 
completely orthogonal as they support some logical concepts. 
Multithreaded classes for example support a lock(), unlock() so they
could derive from a LockBase ADT class just as Creators could derive 
from a CreateBase ADT class that has a create(); but using a base class 
is probably unnecessilary complex for small simple policy classes.

The whole point of policies is to create more complex types by combining

simpler types using multiple inheritance and templates. for example,

class SingleThreaded { ... };
class Atomic { ... };
class Semaphore { ... };
template <class T> NewCreator { ... };
template <class T> MallocCreator { ... };

template <
    template<class T> class CreatorPolicy,
    class ThreadPolicy
 >
class MyMgr : public CreatorPolicy, ThreadPolicy { ... };

class Widget { ... };

// Single Threaded class that uses new to create objects of type widget
MyMgr<NewCreator<Widget>, SingleThreaded> one;
// Semaphore controlled acceess that uses new to create objects of type 
// widget.
MyMgr<NewCreator<Widget>, Semaphore> two;

Having policies in a container class won't allow you to inherit the 
policies. You'll have to add a lot of plumbing to connect the class you 
want to use to the different policies that your using. Kind of defeats 
the point of policies. Hope that helps, the book Modern C++ Design is 
filled with more information on policies and templates. It's a must read

book. If you have 6 or more policies it doesn't mean that you'll have 6 
template parameters, maybe you only need 2. If you do need all 6 
template parameters then I would expect more than 6 policies, otherwise 
maybe that class doesn't really need to be designed by policy or not all

the parts of the class should be a policy. Just my thoughts.

P.S. sorry if I misspelled stuff, my brain doesn't seem to be working as

I can't find my spell check in mozilla . . . humph, weird. Probably need

more sleep.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Juan
6/29/2003 10:15:47 PM
Reply: