Am I abusing Policies?

Posted by pmr on Stack Overflow See other posts from Stack Overflow or by pmr
Published on 2010-04-01T14:33:07Z Indexed on 2010/04/01 14:43 UTC
Read the original article Hit count: 318

Filed under:
|

I find myself using policies a lot in my code and usually I'm very happy with that. But from time to time I find myself confronted with using that pattern in situations where the Policies are selected and runtime and I have developed habbits to work around such situations. Usually I start with something like that:

class DrawArrays {
protected:
  void sendDraw() const;
};

class DrawElements {
protected:
  void sendDraw() const;
};

template<class Policy>
class Vertices : public Policy {
  using Policy::sendDraw();
public:
  void render() const;
};

When the policy is picked at runtime I have different choices of working around the situation.

Different code paths:

if(drawElements) {  
     Vertices<DrawElements> vertices;  
} else {   
     Vertices<DrawArrays> vertices;   
}

Inheritance and virtual calls:

class PureVertices {
public:
  void render()=0;
};

template<class Policy>
class Vertices : public PureVertices, public Policy {
  //..
};

Both solutions feel wrong to me. The first creates an umaintainable mess and the second introduces the overhead of virtual calls that I tried to avoid by using policies in the first place.

Am I missing the proper solutions or do I use the wrong pattern to solve the problem?

© Stack Overflow or respective owner

Related posts about c++

Related posts about design-patterns