| 1 | // **********************************************************************
 | 
|---|
| 2 | //
 | 
|---|
| 3 | // Copyright (c) 2000
 | 
|---|
| 4 | // Object Oriented Concepts, Inc.
 | 
|---|
| 5 | // Billerica, MA, USA
 | 
|---|
| 6 | //
 | 
|---|
| 7 | // All Rights Reserved
 | 
|---|
| 8 | //
 | 
|---|
| 9 | // **********************************************************************
 | 
|---|
| 10 | 
 | 
|---|
| 11 | #ifndef JTC_HANDLE_H
 | 
|---|
| 12 | #define JTC_HANDLE_H
 | 
|---|
| 13 | 
 | 
|---|
| 14 | //
 | 
|---|
| 15 | // Provide some handy typedefs.
 | 
|---|
| 16 | //
 | 
|---|
| 17 | template <class T> class JTCHandleT;
 | 
|---|
| 18 | 
 | 
|---|
| 19 | class JTCThread;
 | 
|---|
| 20 | typedef JTCHandleT<JTCThread> JTCThreadHandle;
 | 
|---|
| 21 | 
 | 
|---|
| 22 | class JTCThreadGroup;
 | 
|---|
| 23 | typedef JTCHandleT<JTCThreadGroup> JTCThreadGroupHandle;
 | 
|---|
| 24 | 
 | 
|---|
| 25 | class JTCRunnable;
 | 
|---|
| 26 | typedef JTCHandleT<JTCRunnable> JTCRunnableHandle;
 | 
|---|
| 27 | 
 | 
|---|
| 28 | //
 | 
|---|
| 29 | // JTCRefCount class. This is a base class to be used in conjunction
 | 
|---|
| 30 | // with the JTCHandleT class.
 | 
|---|
| 31 | //
 | 
|---|
| 32 | class JTCRefCount
 | 
|---|
| 33 | {
 | 
|---|
| 34 | public:
 | 
|---|
| 35 |     int _jtc_refCount_;
 | 
|---|
| 36 |     JTCMutex refMutex_;
 | 
|---|
| 37 |     
 | 
|---|
| 38 |     JTCRefCount()
 | 
|---|
| 39 |         : _jtc_refCount_(0)
 | 
|---|
| 40 |     {
 | 
|---|
| 41 |     }
 | 
|---|
| 42 | 
 | 
|---|
| 43 |     virtual ~JTCRefCount()
 | 
|---|
| 44 |     {
 | 
|---|
| 45 |     }
 | 
|---|
| 46 | };
 | 
|---|
| 47 | 
 | 
|---|
| 48 | //
 | 
|---|
| 49 | // This class is a handle to a reference counted object. This ensures
 | 
|---|
| 50 | // that the object is deleted when it is no longer referenced.
 | 
|---|
| 51 | //
 | 
|---|
| 52 | template <class T>
 | 
|---|
| 53 | class JTCHandleT
 | 
|---|
| 54 | {
 | 
|---|
| 55 |     T* tg_; // Referenced object
 | 
|---|
| 56 | 
 | 
|---|
| 57 | public:
 | 
|---|
| 58 | 
 | 
|---|
| 59 |     //
 | 
|---|
| 60 |     // Constructors.
 | 
|---|
| 61 |     //
 | 
|---|
| 62 |     JTCHandleT(T* tg = 0);
 | 
|---|
| 63 |     JTCHandleT(const JTCHandleT<T>& rhs);
 | 
|---|
| 64 | 
 | 
|---|
| 65 |     //
 | 
|---|
| 66 |     // Destructor. Deletes object when reference count drops to
 | 
|---|
| 67 |     // zero.
 | 
|---|
| 68 |     //
 | 
|---|
| 69 |     ~JTCHandleT();
 | 
|---|
| 70 | 
 | 
|---|
| 71 |     //
 | 
|---|
| 72 |     // Assignment operator.
 | 
|---|
| 73 |     //
 | 
|---|
| 74 |     JTCHandleT<T>& operator=(const JTCHandleT<T>& rhs);
 | 
|---|
| 75 | 
 | 
|---|
| 76 |     //
 | 
|---|
| 77 |     // Equality and inequality.
 | 
|---|
| 78 |     //
 | 
|---|
| 79 |     bool operator==(const JTCHandleT<T>& rhs) const
 | 
|---|
| 80 |     { return tg_ == rhs.tg_; }
 | 
|---|
| 81 |     bool operator!=(const JTCHandleT<T>& rhs) const
 | 
|---|
| 82 |     { return tg_ != rhs.tg_; }
 | 
|---|
| 83 | 
 | 
|---|
| 84 |     //
 | 
|---|
| 85 |     // Logical not operator. Are we referring to a valid Thread?
 | 
|---|
| 86 |     //
 | 
|---|
| 87 |     bool operator!() const { return tg_ == 0; }
 | 
|---|
| 88 |     operator bool() const { return tg_ != 0; }
 | 
|---|
| 89 |     
 | 
|---|
| 90 |     //
 | 
|---|
| 91 |     // Make access to the Thread intuitive.
 | 
|---|
| 92 |     //
 | 
|---|
| 93 |     T* operator->() const { return tg_; }
 | 
|---|
| 94 |     T* get() const { return tg_; }
 | 
|---|
| 95 |     T& operator*() { return *tg_; }
 | 
|---|
| 96 | 
 | 
|---|
| 97 |     void _jtc_deref();
 | 
|---|
| 98 | };
 | 
|---|
| 99 | 
 | 
|---|
| 100 | #if defined(HAVE_NO_EXPLICIT_TEMPLATES) && !defined(HAVE_PRAGMA_DEFINE)
 | 
|---|
| 101 | #   include "JTC/HandleI.h"
 | 
|---|
| 102 | #endif
 | 
|---|
| 103 | 
 | 
|---|
| 104 | #endif
 | 
|---|