Click here to Skip to main content
15,894,405 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.5K   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.
#ifndef ALLOCATOR_H_INCLUDED
#define ALLOCATOR_H_INCLUDED

#include "MemManager7.h"
#include <memory>

namespace Mm {
	template <class T>
	class Allocator 
	{
	public:
		typedef T value_type;
		typedef size_t size_type;
		typedef ptrdiff_t difference_type;
		
		typedef T* pointer;
		typedef const T* const_pointer;
		
		typedef T& reference;
		typedef const T& const_reference;
		
		pointer address(reference r) const { return &r; }
		const_pointer address(const_reference r) const { return &r; }
		
		Allocator() throw() {}
//		Allocator(const Allocator&) throw() {}
	
		template <class U> 
		Allocator(const Allocator<U>&) throw() {}
		
		~Allocator() throw() {}
		
		pointer allocate(size_type n, std::allocator<void>::const_pointer hint = 0)
		{
			return static_cast<pointer>(Mm::Allocate(this, n * sizeof(value_type)));
		}
		
		pointer _Charalloc(size_type bytes, std::allocator<void>::const_pointer hint = 0)
		{
			return static_cast<pointer>(Mm::Allocate(this, bytes));
		}
		
		void deallocate(void* p, size_type n)
		{
			Mm::Deallocate(p, n*sizeof(T));
		}
		
		void construct(pointer p, const T& val) { new(p) T(val); }
		void destroy(pointer p) { p->~T(); }
		
		size_type max_size() const throw() { return (size_type)(-1); }
		
		template <class U>
		bool operator==(const Allocator<U>& other) const
		{ return (true); }

		template <class U>
		bool operator!=(const Allocator<U>& other)  const
		{ return (false); }

		template <class U>
		struct rebind { typedef Allocator<U> other; };
	};

	typedef std::basic_string<char, std::char_traits<char>, Allocator<char> > string;
};

#endif // ALLOCATOR_H_INCLUDED

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