00001 /* 00002 * 00003 * Copyright (C) 1997-2005, OFFIS 00004 * 00005 * This software and supporting documentation were developed by 00006 * 00007 * Kuratorium OFFIS e.V. 00008 * Healthcare Information and Communication Systems 00009 * Escherweg 2 00010 * D-26121 Oldenburg, Germany 00011 * 00012 * THIS SOFTWARE IS MADE AVAILABLE, AS IS, AND OFFIS MAKES NO WARRANTY 00013 * REGARDING THE SOFTWARE, ITS PERFORMANCE, ITS MERCHANTABILITY OR 00014 * FITNESS FOR ANY PARTICULAR USE, FREEDOM FROM ANY COMPUTER DISEASES OR 00015 * ITS CONFORMITY TO ANY SPECIFICATION. THE ENTIRE RISK AS TO QUALITY AND 00016 * PERFORMANCE OF THE SOFTWARE IS WITH THE USER. 00017 * 00018 * Module: ofstd 00019 * 00020 * Author: Marco Eichelberg 00021 * 00022 * Purpose: class OFCondition and helper classes 00023 * 00024 * Last Update: $Author: meichel $ 00025 * Update Date: $Date: 2005/12/08 16:05:50 $ 00026 * CVS/RCS Revision: $Revision: 1.9 $ 00027 * Status: $State: Exp $ 00028 * 00029 * CVS/RCS Log at end of file 00030 * 00031 */ 00032 00033 00034 #ifndef OFCOND_H 00035 #define OFCOND_H 00036 00037 #include "dcmtk/config/osconfig.h" 00038 #include "dcmtk/ofstd/oftypes.h" /* for class OFBool */ 00039 #include "dcmtk/ofstd/ofstring.h" /* for class OFString */ 00040 #include "dcmtk/ofstd/ofcast.h" 00041 00042 #define INCLUDE_CASSERT 00043 #include "dcmtk/ofstd/ofstdinc.h" 00044 00047 enum OFStatus 00048 { 00050 OF_ok, 00051 00053 OF_error, 00054 00056 OF_failure 00057 }; 00058 00059 00062 class OFConditionBase 00063 { 00064 public: 00065 00067 OFConditionBase() 00068 { 00069 } 00070 00072 OFConditionBase(const OFConditionBase& /* arg */) 00073 { 00074 } 00075 00077 virtual ~OFConditionBase() 00078 { 00079 } 00080 00086 virtual const OFConditionBase *clone() const = 0; 00087 00091 virtual unsigned long codeAndModule() const = 0; 00092 00094 virtual OFStatus status() const = 0; 00095 00097 virtual const char *text() const = 0; 00098 00103 virtual OFBool deletable() const = 0; 00104 00106 unsigned short module() const 00107 { 00108 return OFstatic_cast(unsigned short,((codeAndModule() >> 16) & 0xFFFF)); 00109 } 00110 00112 unsigned short code() const 00113 { 00114 return OFstatic_cast(unsigned short,(codeAndModule() & 0xFFFF)); 00115 } 00116 00122 OFBool operator==(const OFConditionBase& arg) const 00123 { 00124 return ((status() == arg.status()) && (codeAndModule() == arg.codeAndModule())); 00125 } 00126 00132 OFBool operator!=(const OFConditionBase& arg) const 00133 { 00134 return ((status() != arg.status()) || (code() != arg.code()) || (module() != arg.module())); 00135 } 00136 00137 private: 00138 00140 OFConditionBase& operator=(const OFConditionBase& arg); 00141 00142 }; 00143 00144 00145 00150 class OFConditionConst: public OFConditionBase 00151 { 00152 public: 00153 00162 OFConditionConst(unsigned short aModule, unsigned short aCode, OFStatus aStatus, const char *aText) 00163 : OFConditionBase() 00164 , theCodeAndModule(OFstatic_cast(unsigned long, aCode) | OFstatic_cast(unsigned long, aModule << 16)) 00165 , theStatus(aStatus) 00166 , theText(aText) 00167 { 00168 } 00169 00171 OFConditionConst(const OFConditionConst& arg) 00172 : OFConditionBase(arg) 00173 , theCodeAndModule(arg.theCodeAndModule) 00174 , theStatus(arg.theStatus) 00175 , theText(arg.theText) 00176 { 00177 } 00178 00180 virtual ~OFConditionConst() 00181 { 00182 } 00183 00188 virtual const OFConditionBase *clone() const; 00189 00193 virtual unsigned long codeAndModule() const; 00194 00196 virtual OFStatus status() const; 00197 00199 virtual const char *text() const; 00200 00205 virtual OFBool deletable() const; 00206 00207 private: 00208 00210 OFConditionConst& operator=(const OFConditionConst& arg); 00211 00213 unsigned long theCodeAndModule; 00214 00216 OFStatus theStatus; 00217 00219 const char *theText; 00220 00221 }; 00222 00223 00224 00228 class OFConditionString: public OFConditionBase 00229 { 00230 public: 00231 00239 OFConditionString(unsigned short aModule, unsigned short aCode, OFStatus aStatus, const char *aText) 00240 : OFConditionBase() 00241 , theCodeAndModule(OFstatic_cast(unsigned long, aCode) | OFstatic_cast(unsigned long, aModule << 16)) 00242 , theStatus(aStatus) 00243 , theText() 00244 { 00245 if (aText) theText = aText; 00246 } 00247 00249 OFConditionString(const OFConditionString& arg) 00250 : OFConditionBase(arg) 00251 , theCodeAndModule(arg.theCodeAndModule) 00252 , theStatus(arg.theStatus) 00253 , theText(arg.theText) 00254 { 00255 } 00256 00258 virtual ~OFConditionString() 00259 { 00260 } 00261 00266 virtual const OFConditionBase *clone() const; 00267 00271 virtual unsigned long codeAndModule() const; 00272 00274 virtual OFStatus status() const; 00275 00277 virtual const char *text() const; 00278 00283 virtual OFBool deletable() const; 00284 00285 private: 00287 OFConditionString& operator=(const OFConditionString& arg); 00288 00290 unsigned long theCodeAndModule; 00291 00293 OFStatus theStatus; 00294 00296 OFString theText; 00297 }; 00298 00299 00300 // global constant used by OFCondition default constructor. 00301 extern const OFConditionConst ECC_Normal; 00302 00303 00309 class OFCondition 00310 { 00311 public: 00312 00318 OFCondition(OFConditionString *base) 00319 : theCondition(base) 00320 { 00321 assert(theCondition); 00322 } 00323 00329 #ifdef OFCONDITION_STRICT_MODE 00330 // in strict mode OFCondition has no default constructor. 00331 OFCondition(const OFConditionConst& base) 00332 #else 00333 OFCondition(const OFConditionConst& base = ECC_Normal) 00334 #endif 00335 : theCondition(&base) 00336 { 00337 assert(theCondition); 00338 } 00339 00341 OFCondition(const OFCondition& arg) 00342 : theCondition(arg.theCondition->clone()) 00343 { 00344 assert(theCondition); 00345 } 00346 00348 ~OFCondition() 00349 { 00350 if (theCondition->deletable()) 00351 { 00352 delete OFconst_cast(OFConditionBase *, theCondition); // cast away const 00353 } 00354 } 00355 00357 OFCondition& operator=(const OFCondition& arg) 00358 { 00359 if (&arg != this) 00360 { 00361 if (theCondition->deletable()) 00362 { 00363 delete OFconst_cast(OFConditionBase *, theCondition); // cast away const 00364 } 00365 theCondition = arg.theCondition->clone(); 00366 assert(theCondition); 00367 } 00368 return *this; 00369 } 00370 00372 inline unsigned short module() const 00373 { 00374 return theCondition->module(); 00375 } 00376 00378 inline unsigned short code() const 00379 { 00380 return theCondition->code(); 00381 } 00382 00384 inline OFStatus status() const 00385 { 00386 return theCondition->status(); 00387 } 00388 00390 inline const char *text() const 00391 { 00392 return theCondition->text(); 00393 } 00394 00396 inline OFBool good() const 00397 { 00398 OFStatus s = theCondition->status(); 00399 return (s == OF_ok); 00400 } 00401 00403 inline OFBool bad() const 00404 { 00405 OFStatus s = theCondition->status(); 00406 return (s != OF_ok); 00407 } 00408 00409 #ifdef OFCONDITION_IMPLICIT_BOOL_CONVERSION 00410 /* Implicit conversion from OFCondition to bool might 00411 * not always be a good idea since it can hide unwanted constructs. 00412 * Therefore, we disable this operator by default. 00413 */ 00414 00418 inline operator OFBool() const 00419 { 00420 return good(); 00421 } 00422 #endif 00423 00429 inline OFBool operator==(const OFCondition& arg) const 00430 { 00431 return (*theCondition == *arg.theCondition); 00432 } 00433 00439 inline OFBool operator!=(const OFCondition& arg) const 00440 { 00441 return (*theCondition != *arg.theCondition); 00442 } 00443 00444 private: 00445 00447 const OFConditionBase *theCondition; 00448 00449 }; 00450 00451 00452 /* global condition constants. 00453 * All constants defined here use module number 0 which is reserved for 00454 * global definitions. Other constants are defined elsewhere. 00455 */ 00456 00458 extern const OFCondition EC_Normal; 00459 00461 extern const OFCondition EC_IllegalParameter; 00462 00464 extern const OFCondition EC_MemoryExhausted; 00465 00466 00469 #define makeOFCondition(A, B, C, D) OFCondition(new OFConditionString((A), (B), (C), (D))) 00470 00471 00472 #endif 00473 00474 /* 00475 * CVS/RCS Log: 00476 * $Log: ofcond.h,v $ 00477 * Revision 1.9 2005/12/08 16:05:50 meichel 00478 * Changed include path schema for all DCMTK header files 00479 * 00480 * Revision 1.8 2003/12/05 10:37:41 joergr 00481 * Removed leading underscore characters from preprocessor symbols (reserved 00482 * symbols). Updated copyright date where appropriate. 00483 * 00484 * Revision 1.7 2003/07/09 13:57:43 meichel 00485 * Adapted type casts to new-style typecast operators defined in ofcast.h 00486 * 00487 * Revision 1.6 2003/07/04 13:31:51 meichel 00488 * Fixed issues with compiling with HAVE_STD_STRING 00489 * 00490 * Revision 1.5 2003/06/12 13:15:59 joergr 00491 * Fixed inconsistent API documentation reported by Doxygen. 00492 * 00493 * Revision 1.4 2001/11/09 15:44:39 joergr 00494 * Removed ";" from macro definition to avoid compiler warnings reported by 00495 * Sun CC 2.0.1. 00496 * 00497 * Revision 1.3 2001/10/12 10:42:26 meichel 00498 * Introduced conditional define OFCONDITION_STRICT_MODE in which the 00499 * compatibility options related to the transition to OFCondition are disabled: 00500 * No OFCondition default constructor, no typedefs for E_Condition, CONDITION, 00501 * no macros for SUCCESS and condition aliases. 00502 * 00503 * Revision 1.2 2001/09/25 17:07:24 meichel 00504 * Disabled implicit conversion to bool, added default constructor 00505 * to class OFCondition. 00506 * 00507 * Revision 1.1 2001/08/23 16:08:37 meichel 00508 * Initial release of class OFCondition, a generic approach for condition codes 00509 * 00510 * 00511 */