I often use policies in my code, and as a rule, I am very pleased with this. But from time to time I come across the fact that I use this template in situations where policies and runtimes are selected, and I developed habbits to work on such situations. Usually I start with something like this:
class DrawArrays { protected: void sendDraw() const; }; class DrawElements { public: void setIndices( GLenum mode, GLsizei count, GLenum type, const GLvoid *indices); protected: void sendDraw() const; }; template<class Policy> class Vertices : public Policy { using Policy::sendDraw(); public: void render() const; };
When a policy is selected at runtime, I have different options for dealing with 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 decisions consider me wrong. The first one creates an unacceptable mess, and the second one introduces overhead for virtual calls, which I tried to avoid using policies first.
Are there enough correct solutions or am I using the wrong template to solve the problem?
source share