home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mandelc.zip / object.cpp < prev    next >
C/C++ Source or Header  |  1993-06-24  |  2KB  |  122 lines

  1. #include "object.h"
  2. extern "C"
  3. {
  4. #include <memory.h>
  5. };
  6.  
  7.  
  8. object::object(void)
  9. {    bSuccessfullConstructed = TRUE;
  10. }
  11.  
  12.  
  13. object::~object(void)
  14. {
  15. }
  16.  
  17.  
  18. /*
  19. Remembers the size of the object to memset it to zero when deleting.
  20. This helps finding bugs caused of destructing objects more then once.
  21. */
  22. void *object::operator new(unsigned int iSize)
  23. {    unsigned int *pObject;
  24.  
  25.     if (!(pObject = (unsigned int*)::new char[iSize + 8]))
  26.         return (void *)0;
  27.     *pObject = iSize;
  28.     memset((char *)pObject += 8, 0, iSize);
  29.     return (void*)pObject;
  30. }
  31.  
  32.  
  33. void object::operator delete(void *pObject)
  34. {    unsigned char *pSize;
  35.  
  36.     memset(pObject, 0, *(pSize = ((unsigned char*)pObject - 8)));
  37.     ::delete pSize;
  38. }
  39.  
  40.  
  41. void construct::doSomething(void)
  42. {
  43. }
  44.  
  45.  
  46. //     Call it direct only if init() is called direct
  47. //     if run() is not used caused of that no dosSomething exists
  48. //     - e.g. windows
  49. void construct::destructAll()
  50. {    if (isSuccessfull())
  51.     {    destructPre();
  52.         destruct();
  53.         destructPost();
  54.     }
  55. }
  56.  
  57.  
  58. void construct::run(void)
  59. {    if (init())
  60.     {    doSomething();
  61.         destructAll();
  62.     }
  63. }
  64.  
  65.  
  66. // You can see that there is a matching counterpart for every init function.
  67. //    initPre() - destructPost()
  68. //    create() - destruct()
  69. //    initPost() - destructPre()
  70. Boolean construct::init(void)
  71. {    if (isSuccessfull())
  72.     {    if (initPre())
  73.             if (create())
  74.             {    if (initPost())
  75.                     return TRUE;
  76.                 else
  77.                 {    destruct();
  78.                     destructPost();
  79.                     return FALSE;
  80.                 }
  81.             }
  82.             else
  83.             {    destructPost();
  84.                 return FALSE;
  85.             }
  86.         else
  87.             return FALSE;
  88.     }
  89.     else
  90.         return FALSE;
  91. }
  92.  
  93.  
  94. Boolean construct::initPre(void)
  95. {    return TRUE;
  96. }
  97.  
  98.  
  99. Boolean construct::create(void)
  100. {    return TRUE;
  101. }
  102.  
  103.  
  104. Boolean construct::initPost(void)
  105. {    return TRUE;
  106. }
  107.  
  108.  
  109. void construct::destructPost(void)
  110. {
  111. }
  112.  
  113.  
  114. void construct::destructPre(void)
  115. {
  116. }
  117.  
  118.  
  119. void construct::destruct(void)
  120. {
  121. }
  122.