1 /* 2 * 3 * Copyright (C) 1996-2001, OFFIS 4 * 5 * This software and supporting documentation were developed by 6 * 7 * Kuratorium OFFIS e.V. 8 * Healthcare Information and Communication Systems 9 * Escherweg 2 10 * D-26121 Oldenburg, Germany 11 * 12 * THIS SOFTWARE IS MADE AVAILABLE, AS IS, AND OFFIS MAKES NO WARRANTY 13 * REGARDING THE SOFTWARE, ITS PERFORMANCE, ITS MERCHANTABILITY OR 14 * FITNESS FOR ANY PARTICULAR USE, FREEDOM FROM ANY COMPUTER DISEASES OR 15 * ITS CONFORMITY TO ANY SPECIFICATION. THE ENTIRE RISK AS TO QUALITY AND 16 * PERFORMANCE OF THE SOFTWARE IS WITH THE USER. 17 * 18 * Module: dcmimgle 19 * 20 * Author: Joerg Riesmeier 21 * 22 * Purpose: DicomObjectCounter (Header) 23 * 24 */ 25 26 27 #ifndef __DIOBJCOU_H 28 #define __DIOBJCOU_H 29 30 #include "osconfig.h" 31 32 #include "ofthread.h" 33 34 35 /*---------------------* 36 * class declaration * 37 *---------------------*/ 38 39 /** Class to count number of instances (objects created from a certain class). 40 * used to manage more than one reference to an object in a secure way. 41 */ 42 class DiObjectCounter 43 { 44 public: 45 46 /** add a reference. 47 * Increase the internal counter by 1. 48 */ addReference()49 inline void addReference() 50 { 51 #ifdef _REENTRANT 52 theMutex.lock(); 53 #endif 54 Counter++; 55 #ifdef _REENTRANT 56 theMutex.unlock(); 57 #endif 58 } 59 60 /** remove a reference. 61 * Decrease the internal counter by 1 and delete the object only if the counter is zero. 62 */ removeReference()63 inline void removeReference() 64 { 65 #ifdef _REENTRANT 66 theMutex.lock(); 67 #endif 68 if (--Counter == 0) 69 { 70 #ifdef _REENTRANT 71 theMutex.unlock(); 72 #endif 73 delete this; 74 #ifdef _REENTRANT 75 } else { 76 theMutex.unlock(); 77 #endif 78 } 79 } 80 81 82 protected: 83 84 /** constructor. 85 * Internal counter is initialized with 1. 86 */ DiObjectCounter()87 DiObjectCounter() 88 : Counter(1) 89 #ifdef _REENTRANT 90 ,theMutex() 91 #endif 92 { 93 } 94 95 /** destructor 96 */ ~DiObjectCounter()97 virtual ~DiObjectCounter() 98 { 99 } 100 101 102 private: 103 104 /// internal counter 105 unsigned long Counter; 106 107 #ifdef _REENTRANT 108 /** if compiled for multi-thread operation, the Mutex protecting 109 * access to the value of this object. 110 */ 111 OFMutex theMutex; 112 #endif 113 }; 114 115 116 #endif 117