Click here to Skip to main content
15,892,927 members
Articles / Programming Languages / C++

Undo and Redo the "Easy" Way

Rate me:
Please Sign up or sign in to vote.
4.95/5 (42 votes)
20 Jun 2004CPOL22 min read 290.4K   8.6K   114  
This article introduces a simple approach to in-memory transactions that can be used to implement Undo and Redo. The technique uses SEH and Virtual Memory and requires only STL and Win32.
#pragma once

#include <vector>
#include <map>

// Client API: Dlls are dynamically loaded and searched for a function with 
// the name indicated in 'lpzDrawDLLInit'.  That function must be implemented
// with the signature PFNDLLINIT: 
//
// bool DrawDllInit(PFNADDDRAWCALLBACKS fnAddCB);
//
// The client dll should call fnAddCB once for each draw code it supports and pass
// pointers to the functions that implement create/draw for the code.  The returned
// draw code should be stored to later identify how to draw specific objects
//

class DrawItAPI {
public:
	virtual unsigned long AddPoint(const POINT* where) = 0;  // creates a point, returns index
	virtual void SetPoint(unsigned long id, const POINT* where) = 0;
	virtual const POINT* GetPoint(unsigned long id) = 0;
	virtual void RemovePoint(unsigned long id) = 0;
	
	virtual unsigned long AddData(unsigned long data) = 0; // adds data to object, returns index
	virtual void SetData(unsigned long id, unsigned long data) = 0;
	virtual void ResizeData(unsigned int count) = 0;
};

typedef struct DRAWDATA {
	unsigned long*	data;
	unsigned long	count_data;
} _DRAWDATA; 

typedef void (*PFNCREATE)(DrawItAPI* api, const POINT* mouse, const DRAWDATA* data);
typedef void (*PFNCOMPLETE)(DrawItAPI* api, const DRAWDATA* data);
typedef void (*PFNDRAW)(DrawItAPI* api, HDC dc, const DRAWDATA* data);

typedef struct CLIENTCALLBACKS {
	PFNCREATE	create;
	PFNCOMPLETE	complete;
	PFNDRAW		draw;
} _CLIENTCALLBACKS;

// DrawIt host API
typedef unsigned int (*PFNADDCLIENTCALLBACKS)(const CLIENTCALLBACKS* cb);

static const char* lpzDrawDLLInit = "DrawDllInit";
typedef bool (*PFNDLLINIT)(PFNADDCLIENTCALLBACKS fnAddCB);

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
A compiler warns of bogasity, ignore it at your peril. Unless you've done the compiler's job yourself, don't criticize it.

Comments and Discussions