ofstd/include/dcmtk/ofstd/ofcond.h

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: class OFCondition and helper classes
00019  *
00020  *  Last Update:      $Author: joergr $
00021  *  Update Date:      $Date: 2010-10-14 13:15:50 $
00022  *  CVS/RCS Revision: $Revision: 1.10 $
00023  *  Status:           $State: Exp $
00024  *
00025  *  CVS/RCS Log at end of file
00026  *
00027  */
00028 
00029 
00030 #ifndef OFCOND_H
00031 #define OFCOND_H
00032 
00033 #include "dcmtk/config/osconfig.h"
00034 #include "dcmtk/ofstd/oftypes.h"   /* for class OFBool */
00035 #include "dcmtk/ofstd/ofstring.h"  /* for class OFString */
00036 #include "dcmtk/ofstd/ofcast.h"
00037 
00038 #define INCLUDE_CASSERT
00039 #include "dcmtk/ofstd/ofstdinc.h"
00040 
00043 enum OFStatus
00044 {
00046   OF_ok,
00047 
00049   OF_error,
00050 
00052   OF_failure
00053 };
00054 
00055 
00058 class OFConditionBase
00059 {
00060 public:
00061 
00063   OFConditionBase()
00064   {
00065   }
00066 
00068   OFConditionBase(const OFConditionBase& /* arg */)
00069   {
00070   }
00071 
00073   virtual ~OFConditionBase()
00074   {
00075   }
00076 
00082   virtual const OFConditionBase *clone() const = 0;
00083 
00087   virtual unsigned long codeAndModule() const = 0;
00088 
00090   virtual OFStatus status() const = 0;
00091 
00093   virtual const char *text() const = 0;
00094 
00099   virtual OFBool deletable() const = 0;
00100 
00102   unsigned short module() const
00103   {
00104     return OFstatic_cast(unsigned short,((codeAndModule() >> 16) & 0xFFFF));
00105   }
00106 
00108   unsigned short code() const
00109   {
00110     return OFstatic_cast(unsigned short,(codeAndModule() & 0xFFFF));
00111   }
00112 
00118   OFBool operator==(const OFConditionBase& arg) const
00119   {
00120     return ((status() == arg.status()) && (codeAndModule() == arg.codeAndModule()));
00121   }
00122 
00128   OFBool operator!=(const OFConditionBase& arg) const
00129   {
00130     return ((status() != arg.status()) || (code() != arg.code()) || (module() != arg.module()));
00131   }
00132 
00133 private:
00134 
00136   OFConditionBase& operator=(const OFConditionBase& arg);
00137 
00138 };
00139 
00140 
00141 
00146 class OFConditionConst: public OFConditionBase
00147 {
00148 public:
00149 
00158   OFConditionConst(unsigned short aModule, unsigned short aCode, OFStatus aStatus, const char *aText)
00159   : OFConditionBase()
00160   , theCodeAndModule(OFstatic_cast(unsigned long, aCode) | OFstatic_cast(unsigned long, aModule << 16))
00161   , theStatus(aStatus)
00162   , theText(aText)
00163   {
00164   }
00165 
00167   OFConditionConst(const OFConditionConst& arg)
00168   : OFConditionBase(arg)
00169   , theCodeAndModule(arg.theCodeAndModule)
00170   , theStatus(arg.theStatus)
00171   , theText(arg.theText)
00172   {
00173   }
00174 
00176   virtual ~OFConditionConst()
00177   {
00178   }
00179 
00184   virtual const OFConditionBase *clone() const;
00185 
00189   virtual unsigned long codeAndModule() const;
00190 
00192   virtual OFStatus status() const;
00193 
00195   virtual const char *text() const;
00196 
00201   virtual OFBool deletable() const;
00202 
00203 private:
00204 
00206   OFConditionConst& operator=(const OFConditionConst& arg);
00207 
00209   unsigned long theCodeAndModule;
00210 
00212   OFStatus theStatus;
00213 
00215   const char *theText;
00216 
00217 };
00218 
00219 
00220 
00224 class OFConditionString: public OFConditionBase
00225 {
00226 public:
00227 
00235   OFConditionString(unsigned short aModule, unsigned short aCode, OFStatus aStatus, const char *aText)
00236   : OFConditionBase()
00237   , theCodeAndModule(OFstatic_cast(unsigned long, aCode) | OFstatic_cast(unsigned long, aModule << 16))
00238   , theStatus(aStatus)
00239   , theText()
00240   {
00241     if (aText) theText = aText;
00242   }
00243 
00245   OFConditionString(const OFConditionString& arg)
00246   : OFConditionBase(arg)
00247   , theCodeAndModule(arg.theCodeAndModule)
00248   , theStatus(arg.theStatus)
00249   , theText(arg.theText)
00250   {
00251   }
00252 
00254   virtual ~OFConditionString()
00255   {
00256   }
00257 
00262   virtual const OFConditionBase *clone() const;
00263 
00267   virtual unsigned long codeAndModule() const;
00268 
00270   virtual OFStatus status() const;
00271 
00273   virtual const char *text() const;
00274 
00279   virtual OFBool deletable() const;
00280 
00281 private:
00283   OFConditionString& operator=(const OFConditionString& arg);
00284 
00286   unsigned long theCodeAndModule;
00287 
00289   OFStatus theStatus;
00290 
00292   OFString theText;
00293 };
00294 
00295 
00296 // global constant used by OFCondition default constructor.
00297 extern const OFConditionConst ECC_Normal;
00298 
00299 
00305 class OFCondition
00306 {
00307 public:
00308 
00314   OFCondition(OFConditionString *base)
00315   : theCondition(base)
00316   {
00317     assert(theCondition);
00318   }
00319 
00325 #ifdef OFCONDITION_STRICT_MODE
00326   // in strict mode OFCondition has no default constructor.
00327   OFCondition(const OFConditionConst& base)
00328 #else
00329   OFCondition(const OFConditionConst& base = ECC_Normal)
00330 #endif
00331   : theCondition(&base)
00332   {
00333     assert(theCondition);
00334   }
00335 
00337   OFCondition(const OFCondition& arg)
00338   : theCondition(arg.theCondition->clone())
00339   {
00340     assert(theCondition);
00341   }
00342 
00344   ~OFCondition()
00345   {
00346     if (theCondition->deletable())
00347     {
00348       delete OFconst_cast(OFConditionBase *, theCondition); // cast away const
00349     }
00350   }
00351 
00353   OFCondition& operator=(const OFCondition& arg)
00354   {
00355     if (&arg != this)
00356     {
00357       if (theCondition->deletable())
00358       {
00359         delete OFconst_cast(OFConditionBase *, theCondition); // cast away const
00360       }
00361       theCondition = arg.theCondition->clone();
00362       assert(theCondition);
00363     }
00364     return *this;
00365   }
00366 
00368   inline unsigned short module() const
00369   {
00370     return theCondition->module();
00371   }
00372 
00374   inline unsigned short code() const
00375   {
00376     return theCondition->code();
00377   }
00378 
00380   inline OFStatus status() const
00381   {
00382     return theCondition->status();
00383   }
00384 
00386   inline const char *text() const
00387   {
00388     return theCondition->text();
00389   }
00390 
00392   inline OFBool good() const
00393   {
00394     OFStatus s = theCondition->status();
00395     return (s == OF_ok);
00396   }
00397 
00399   inline OFBool bad() const
00400   {
00401     OFStatus s = theCondition->status();
00402     return (s != OF_ok);
00403   }
00404 
00405 #ifdef OFCONDITION_IMPLICIT_BOOL_CONVERSION
00406   /* Implicit conversion from OFCondition to bool might
00407    * not always be a good idea since it can hide unwanted constructs.
00408    * Therefore, we disable this operator by default.
00409    */
00410 
00414   inline operator OFBool() const
00415   {
00416     return good();
00417   }
00418 #endif
00419 
00425   inline OFBool operator==(const OFCondition& arg) const
00426   {
00427     return (*theCondition == *arg.theCondition);
00428   }
00429 
00435   inline OFBool operator!=(const OFCondition& arg) const
00436   {
00437     return (*theCondition != *arg.theCondition);
00438   }
00439 
00440 private:
00441 
00443   const OFConditionBase *theCondition;
00444 
00445 };
00446 
00447 
00448 /*  global condition constants.
00449  *  All constants defined here use module number 0 which is reserved for
00450  *  global definitions. Other constants are defined elsewhere.
00451  */
00452 
00454 extern const OFCondition EC_Normal;
00455 
00457 extern const OFCondition EC_IllegalParameter;
00458 
00460 extern const OFCondition EC_MemoryExhausted;
00461 
00462 
00465 #define makeOFCondition(A, B, C, D) OFCondition(new OFConditionString((A), (B), (C), (D)))
00466 
00467 
00468 #endif
00469 
00470 /*
00471  * CVS/RCS Log:
00472  * $Log: ofcond.h,v $
00473  * Revision 1.10  2010-10-14 13:15:50  joergr
00474  * Updated copyright header. Added reference to COPYRIGHT file.
00475  *
00476  * Revision 1.9  2005/12/08 16:05:50  meichel
00477  * Changed include path schema for all DCMTK header files
00478  *
00479  * Revision 1.8  2003/12/05 10:37:41  joergr
00480  * Removed leading underscore characters from preprocessor symbols (reserved
00481  * symbols). Updated copyright date where appropriate.
00482  *
00483  * Revision 1.7  2003/07/09 13:57:43  meichel
00484  * Adapted type casts to new-style typecast operators defined in ofcast.h
00485  *
00486  * Revision 1.6  2003/07/04 13:31:51  meichel
00487  * Fixed issues with compiling with HAVE_STD_STRING
00488  *
00489  * Revision 1.5  2003/06/12 13:15:59  joergr
00490  * Fixed inconsistent API documentation reported by Doxygen.
00491  *
00492  * Revision 1.4  2001/11/09 15:44:39  joergr
00493  * Removed ";" from macro definition to avoid compiler warnings reported by
00494  * Sun CC 2.0.1.
00495  *
00496  * Revision 1.3  2001/10/12 10:42:26  meichel
00497  * Introduced conditional define OFCONDITION_STRICT_MODE in which the
00498  *   compatibility options related to the transition to OFCondition are disabled:
00499  *   No OFCondition default constructor, no typedefs for E_Condition, CONDITION,
00500  *   no macros for SUCCESS and condition aliases.
00501  *
00502  * Revision 1.2  2001/09/25 17:07:24  meichel
00503  * Disabled implicit conversion to bool, added default constructor
00504  *   to class OFCondition.
00505  *
00506  * Revision 1.1  2001/08/23 16:08:37  meichel
00507  * Initial release of class OFCondition, a generic approach for condition codes
00508  *
00509  *
00510  */


Generated on 6 Jan 2011 for OFFIS DCMTK Version 3.6.0 by Doxygen 1.5.1