00001 /* 00002 * 00003 * Copyright (C) 1997-2010, OFFIS e.V. 00004 * All rights reserved. See COPYRIGHT file for details. 00005 * 00006 * This software and supporting documentation were developed by 00007 * 00008 * OFFIS e.V. 00009 * R&D Division Health 00010 * Escherweg 2 00011 * D-26121 Oldenburg, Germany 00012 * 00013 * 00014 * Module: ofstd 00015 * 00016 * Author: Marco Eichelberg 00017 * 00018 * Purpose: OFGlobal is a template class intended for the declaration 00019 * of global objects, access to which is protected by a Mutex 00020 * for multi-thread applications. 00021 * class T must have copy constructor and assignment operator. 00022 * 00023 * 00024 * Last Update: $Author: joergr $ 00025 * Update Date: $Date: 2010-10-14 13:15:50 $ 00026 * CVS/RCS Revision: $Revision: 1.8 $ 00027 * Status: $State: Exp $ 00028 * 00029 * CVS/RCS Log at end of file 00030 * 00031 */ 00032 00033 00034 #ifndef OFGLOBAL_H 00035 #define OFGLOBAL_H 00036 00037 #include "dcmtk/config/osconfig.h" 00038 #include "dcmtk/ofstd/ofthread.h" /* for class OFBool */ 00039 00045 template <class T> class OFGlobal 00046 { 00047 public: 00048 00052 OFGlobal(const T &arg) 00053 : val(arg) 00054 #ifdef WITH_THREADS 00055 , theMutex() 00056 #endif 00057 { 00058 } 00059 00062 virtual ~OFGlobal() { } 00063 00068 void set(const T& arg) 00069 { 00070 #ifdef WITH_THREADS 00071 theMutex.lock(); 00072 #endif 00073 val = arg; 00074 #ifdef WITH_THREADS 00075 theMutex.unlock(); 00076 #endif 00077 } 00078 00083 void xget(T& arg) 00084 { 00085 #ifdef WITH_THREADS 00086 theMutex.lock(); 00087 #endif 00088 arg = val; 00089 #ifdef WITH_THREADS 00090 theMutex.unlock(); 00091 #endif 00092 } 00093 00099 T get() 00100 { 00101 #ifdef WITH_THREADS 00102 theMutex.lock(); 00103 #endif 00104 T result(val); 00105 #ifdef WITH_THREADS 00106 theMutex.unlock(); 00107 #endif 00108 return result; 00109 } 00110 00111 private: 00112 00115 T val; 00116 00117 #ifdef WITH_THREADS 00118 00121 OFMutex theMutex; 00122 #endif 00123 00125 OFGlobal(); 00126 00128 OFGlobal(const OFGlobal<T>& arg); 00129 00131 const OFGlobal<T>& operator=(const OFGlobal<T>& arg); 00132 00133 }; 00134 00135 00136 #endif 00137 00138 /* 00139 * 00140 * CVS/RCS Log: 00141 * $Log: ofglobal.h,v $ 00142 * Revision 1.8 2010-10-14 13:15:50 joergr 00143 * Updated copyright header. Added reference to COPYRIGHT file. 00144 * 00145 * Revision 1.7 2010-10-04 14:44:47 joergr 00146 * Replaced "#ifdef _REENTRANT" by "#ifdef WITH_THREADS" where appropriate (i.e. 00147 * in all cases where OFMutex, OFReadWriteLock, etc. are used). 00148 * 00149 * Revision 1.6 2005/12/08 16:05:57 meichel 00150 * Changed include path schema for all DCMTK header files 00151 * 00152 * Revision 1.5 2003/12/05 10:37:41 joergr 00153 * Removed leading underscore characters from preprocessor symbols (reserved 00154 * symbols). Updated copyright date where appropriate. 00155 * 00156 * Revision 1.4 2001/06/01 15:51:34 meichel 00157 * Updated copyright header 00158 * 00159 * Revision 1.3 2000/10/10 12:01:21 meichel 00160 * Created/updated doc++ comments 00161 * 00162 * Revision 1.2 2000/05/30 17:03:38 meichel 00163 * Added default constructor for Mutex to initializer list in OFGlobal. 00164 * 00165 * Revision 1.1 2000/04/14 15:17:48 meichel 00166 * Created new templace class OFGlobal which allows to easily implement 00167 * mutex protected global flags. 00168 * 00169 * 00170 */