Sign in to follow this  
Quat

Flood of "Change" Events

Recommended Posts

I am building an editor of sorts, which means my mesh geometry can change.  Certain parts of my application will want to be notified when a mesh's geometry changes.  So I added a "GeometryChanged" event to my mesh class.  The problem is that several of my Mesh API functions can raise the GeometryChanged event, and often these calls will be made sequentially.  Here is a simple example:

 

mesh.AddVertices(...);

mesh.AddIndices(...);

mesh.Indices[3] = 2;

 

Each of these changes the geometry of the mesh, and would trigger 3 "GeometryChanged" events.  If the event handlers were heavy duty this would be pretty wasteful.  One solution I am considering is to wrap changes between a BeginGeometryChange()/EndGeometryChange pair, and only Begin/EndGeometryChange would raise events. 

 

This would work and is relatively easy to integrate, but I am wondering if there is a more elegant solution.  I find this somewhat inelegant because of the extra API calls to Begin/EndGeometryChange that the client must manually remember to call.  I can of course add debug asserts to make sure they are properly called, but still.

 

 

Share this post


Link to post
Share on other sites

There is nothing wrong or inelegant with your idea to begin/end a series of events.  This is called “batching” and is the normal way to go about this.  It is also heavily used in undo/redo systems, especially related to typing events.

 

 

 

L. Spiro

Share this post


Link to post
Share on other sites
Another approach is to wrap the begin/end calls in a scoped handle class, which calls begin when it is created, and end when it is destructed (i.e. falls out of scope)
 
class Mesh
{
	 friend class MeshModifier;
private:
	 void begin_modifications();
	 void end_modifications();

	 void add_vertices_impl();
	 void add_indices_impl();
};

class MeshModifier
{
	 Mesh &mMesh;
public:
	 MeshModifier(Mesh &mesh) : mMesh(mesh) {mMesh.begin_modifications();}
	 ~MeshModifier() {mMesh.end_modifications();}

	 void add_vertices();
	 void add_indices();
};

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this