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

  1. #ifndef OBJECT_H
  2. #define OBJECT_H
  3. #ifndef DEBUG
  4. #define NDEBUG
  5. #endif /* !DEBUG */
  6. extern "C"
  7. {
  8. #include <assert.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <malloc.h>
  12. #ifdef __EMX__
  13. #include <alloca.h>
  14. #endif
  15.  
  16.  
  17. #define INCL_DOSSEMAPHORES
  18. #define INCL_DOSPROCESS
  19. #include <os2.h>
  20. };
  21.  
  22.  
  23. typedef unsigned int Boolean;
  24.  
  25.  
  26. #ifdef DEBUG
  27. #define printFileLine() fprintf(stderr, "%s:%u\n", __FILE__, __LINE__)
  28. #else
  29. #define printFileLine() (void)1
  30. #endif
  31.  
  32.  
  33. /*
  34. This is the basic class, all other classes should be derived from this one
  35. */
  36. class object
  37. {       // Should be set to FALSE by using setNoSuccess()
  38.     // if something goes wrong in a constructor
  39.     Boolean bSuccessfullConstructed;
  40.     public:
  41.     inline Boolean isSuccessfull(void)
  42.     {    return bSuccessfullConstructed;
  43.     };
  44.     inline void setNoSuccess(void)
  45.     {    bSuccessfullConstructed = FALSE;
  46.     };
  47.     object(void);
  48.     virtual ~object(void);
  49.     void *operator new(unsigned int iSize);
  50.     void operator delete(void *pObject);
  51. };
  52.  
  53.  
  54. /*
  55. This class should be used to avoid access of more then one thread to a class.
  56. The thread which owns the lock can call getMutualExclusiveAccess()
  57. more then once.
  58. I DO NOT KNOW WETHER THIS IS DONE WITHOUT BUGS!
  59. */
  60. class mtxObject:virtual public object
  61. {       HMTX hMtxAccess;
  62.     public:
  63.     mtxObject(void):object()
  64.     {       if (DosCreateMutexSem((unsigned char*)0, &hMtxAccess, 0, FALSE))
  65.             setNoSuccess();
  66.     }
  67.     virtual ~mtxObject(void)
  68.     {       DosEnterCritSec();
  69.         if (hMtxAccess)
  70.             DosCloseMutexSem(hMtxAccess);
  71.         DosExitCritSec();
  72.     }
  73.     virtual void getMutualExclusiveAccess(void)
  74.     {       DosRequestMutexSem(hMtxAccess,
  75.             (unsigned int)SEM_INDEFINITE_WAIT);
  76.     }
  77.     virtual void freeMutualExclusiveAccess(void)
  78.     {       DosReleaseMutexSem(hMtxAccess);
  79.     }
  80. };
  81.  
  82.  
  83. /*
  84. This class should be used for windows, threads and processes.
  85. See object.cpp and the implementation of process/threads/windows.
  86. If something goes wrong in the init function FALSE should be returned.
  87. More in object.cpp...
  88. */
  89. class construct:virtual public object
  90. {    public:
  91.     construct(void):object()
  92.     {
  93.     }
  94.     virtual void run(void);
  95.     virtual void doSomething(void);
  96.     virtual Boolean init(void);
  97.     virtual Boolean initPre(void);
  98.     virtual Boolean create(void);
  99.     virtual Boolean initPost(void);
  100.     virtual void destructPost(void);
  101.     virtual void destructPre(void);
  102.     virtual void destruct(void);
  103.     virtual void destructAll(void);
  104. };
  105.  
  106.  
  107. #endif /* !OBJECT_H */
  108.