Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1.  
  2. /** \file lispcleanupstack.h
  3.  * Implementation of a cleanup stack for exception handling on platforms
  4.  *  that don't clean up the stack automatically after an exception
  5.  *  occurs. The macro's SAFEPUSH and SAFEPOP as defined in
  6.  *  plat/<plat>/lisptype.h define which cleanup handler
  7.  *  to use, so it can be configured differently for different platforms.
  8.  *
  9.  */
  10.  
  11. #ifndef __lispcleanupstack_h__
  12. #define __lispcleanupstack_h__
  13.  
  14. #include "yacasbase.h"
  15. #include "grower.h"
  16.  
  17. /** Base class that specifies one pure abstract method Delete.
  18.  *  Only classes derived from this one can be pushed onto
  19.  *  a cleanup stack. This in order to assure the cleanup code
  20.  *  knows where to find the destructor.
  21.  *
  22.  *  Typical candidates for cleanup include places in the code that
  23.  *  have temporal global side effects that need to be finalized on,
  24.  *  Like opening a file for reading, reading and then closing. If
  25.  *  reading prematurely finishes through an exception, the file
  26.  *  should be closed.
  27.  */
  28. class LispBase : public YacasBase
  29. {
  30. public:
  31.     virtual void Delete()=0;
  32.     virtual ~LispBase(){};
  33. };
  34.  
  35. /** Clean up stack that doesn't actually delete objects itself.
  36.  *  Use this clean up stack if the compiler generates
  37.  *  cleanup code itself (particularly for newer c++ compilers).
  38.  *  Alternatively SAFEPUSH and SAFEPOP can then be defined to do nothing.
  39.  */
  40. class LispCleanup : public YacasBase
  41. {
  42. public:
  43.    inline LispCleanup() : iObjects() {}
  44.     virtual ~LispCleanup();
  45.     /// Push an object onto the cleanup stack for guarding
  46.     virtual void Push(LispBase& aObject);
  47.     /// Pop an object from the cleanup stack (the system is finished using it)
  48.     virtual void Pop();
  49.     /// Exception occurred: delete all objects on the stack, back to front.
  50.     virtual void Delete();
  51.     /** For testing purposes, verify that all places that pushed an object
  52.      *  popped it too.
  53.      */
  54.     void CheckStackEmpty();
  55.  
  56. protected:
  57.     CArrayGrower<LispBase*, ArrOpsCustomPtr<LispBase> > iObjects;
  58. };
  59.  
  60. /** Clean up stack that deletes objects itself when needed.
  61.  *  Use this clean up stack if the compiler doesn't generate
  62.  *  cleanup code itself (particularly for older c++ compilers).
  63.  */
  64. class DeletingLispCleanup : public LispCleanup
  65. {
  66. public:
  67.     virtual ~DeletingLispCleanup();
  68.     virtual void Push(LispBase& aObject);
  69.     virtual void Pop();
  70.     virtual void Delete();
  71. };
  72.  
  73.  
  74. #endif
  75.  
  76.  
  77.