Boost logo

Boost Users :

From: Jens Müller (jens.mueller_at_[hidden])
Date: 2006-11-23 05:05:43


Hi,

I have the following task: There is an algorithm where you can set
different behaviors for creating a BFS tree. Currently this is done by
setting a policy object (dynamic), a function of which is called to
perform the search.

I'd like to use static policies here (as is done in generic Boost
algorithms), but still keep the possibility of setting the behavior
manually. The reason for this is: You can set the behavior using a
command line parameter, and it would be impractical to instantiate all
templates and then use a huge switch (think of combinations of several
policies) to choose the appropriate instantiation. Here a wrapper
(dynamic polymorphism) would be needed anyway, or any code using the
algorithm would need to be duplicated as well.

So my idea is as follows: There is a template parameter for a policy
class. Static policy exist for each possible behavior, as well for a
dynamic behavior. The "dynamic" policy class has virtual member
functions and there exists an inheriting dynamic class for each behavior.[1]

The algorithm then has a named param for the policy class. Default value
if to default-construct an object of the policy class. (If useful, the
template might have a default value for the policy template parameter as
well, so the user needn't do anything).

Has someone already taken this or a similiar approach? If so, could you
point me to a reference? What do you think of the approach?

Greeting,

Jens

[1] Hmmm ... Could this inheriting class be used as template parameter
without overhead? Other solution would be:

class dyn_behavior{

public:
   virtual void perform_search(something ...) = 0;
}

class specific_dyn_behavior<class T> : dyn_behavior {
   T behavior;

public:
   void perform_search(something ...){
     behavior.perform_search(something ...);
   }
}


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