Click here to Skip to main content
15,885,216 members
Articles / Programming Languages / C++

A Simple, Action Based, Undo/Redo Framework

Rate me:
Please Sign up or sign in to vote.
4.51/5 (38 votes)
16 Feb 2013CPOL5 min read 97.3K   1.9K   104  
How to use a simple, action based, undo/redo framework
/*
***************************************************************************

kis v 1 r 1

(c) 2002-2009, Florin DUMITRESCU

mailto: fdproxy@gmail.com

$Workfile: kis_SharedPtr.h $ 

***************************************************************************
*/


#ifndef _KIS_SHARED_PTR_H_
#define _KIS_SHARED_PTR_H_


KIS_BEG


//-------------------------------------------------------------------------
/** [To be supplied.] */
template < class T >
class C_SharedPtr
{

public:

  explicit C_SharedPtr()
  :
  m_p( 0 )
  {
    // nop
  }

  explicit C_SharedPtr( T* a_p ) 
  :
  m_p( a_p )
  {
    _AddRef();
  }

  C_SharedPtr( const C_SharedPtr<T>& a_s )
  :
  m_p( a_s.m_p )
  {
    _AddRef();
  }

  ~C_SharedPtr()
  {
    _Release();
  }

  T* operator->()
  {
    return m_p;
  }

  const T* operator->() const
  {
    return m_p;
  }

  C_SharedPtr<T>& operator=( const C_SharedPtr<T>& a_r )
  {
    if ( m_p != a_r.m_p )
    {
      _Release();
      m_p = a_r.m_p;
      _AddRef();
    }
    return *this;
  }

  operator bool() const
  {
    return m_p != 0;
  }

protected:

  T* m_p;

  void _AddRef()
  {
    if ( m_p )
      m_p->AddRef();
  }

  void _Release()
  {
    if ( m_p )
      m_p->Release();
    m_p = 0;
  }

};


KIS_END


#endif  _KIS_SHARED_PTR_H_

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
zdf
Romania Romania
Just a humble programmer.

Comments and Discussions