C++ Templates Question

Started by
12 comments, last by small_duck 16 years ago
You could refer to the 'command' design pattern here: http://en.wikipedia.org/wiki/Command_pattern.

The reason I recommend it is because it was designed by experts and is pretty much the de facto method of implementing undo/redo in application software.
Advertisement
"Other than the iParam could you could also have separate Event.IDs and handlers for polygons and vertices."

Yeah, but this is exactly what I try to avoid, I need as few seperate events possible to maintain the bunch. It get tricky guaruateeing stuff when there are al lot of event etc.

"Essentially what you're doing here is ad-hoc runtime polymorphism using a void pointer rather than a reference to a base-class for undo/redo-able objects"

Yes. In my view seeing events as objects with undo-redoable functions etc leads to way too much concepts and coding....
Quote:Original post by Kincaid
Yes. In my view seeing events as objects with undo-redoable functions etc leads to way too much concepts and coding....


The concepts are already there, you only need to find a way to implement them. You are basically looking for a way to perform your undo-redo based on the type of the event. There are two ways of doing this:

  1. You can use the language features which will achieve this binding with four lines of base class definition (define the class, two virtual functions and a virtual destructor) and an additional four lines per event type (define the function body for each function) in addition to the type-specific code.

    class Event {  virtual void Undo(State &) = 0;  virtual void Redo(State &) = 0;  virtual ~Event();};


  2. You can implement dynamic dispatch yourself, which will involve at least a dozen lines of handling (manage RTTI, determine object type, cast to correct type, call the function) for each event in addition to the type-specific code and spread all over the place.

    I fail to see how one could even hope that the second method would require less work than the first method, let alone the debugging time for your in-house dynamic dispatch system.


Besides, the brittleness of a void-based solution prevents you from making any guarantees on even reasonably complex event systems, which is very bad news if you want your program to do any serious processing. By comparison, the entire dispatch system in the first method is guaranteed by the compiler, meaning you can concentrate on making your individual undo-redo functions respect their intended invariants.
As ToohrVyk is saying, what about using inheritance?

template <class X>
class xclass : public container
{
X object;

void disconnect();
}

This way, after creating your different xclass objects, you can abstract them as a "container" class:

std::vector<container *> myStuff;
myStuff.push_back(new xclass<vertex *>());
myStuff.push_back(new xclass<polygon *>());

myStuff.at(0)->disconnect();
myStuff.at(1)->disconnect();

This topic is closed to new replies.

Advertisement