ofstd/include/dcmtk/ofstd/ofstack.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:  Andreas Barth
00017  *              
00018  *  Purpose:
00019  *      Defines a template stack class with interfaces similar to the C++ Standard
00020  *
00021  *  Last Update:      $Author: joergr $
00022  *  Update Date:      $Date: 2010-10-14 13:15:50 $
00023  *  CVS/RCS Revision: $Revision: 1.18 $
00024  *  Status:           $State: Exp $
00025  *
00026  *  CVS/RCS Log at end of file
00027  *
00028  */
00029 
00030 
00031 #ifndef OFSTACK_H
00032 #define OFSTACK_H
00033 
00034 #include "dcmtk/config/osconfig.h"    /* make sure OS specific configuration is included first */
00035 #include "dcmtk/ofstd/oftypes.h"
00036 #include "dcmtk/ofstd/ofcast.h"
00037 
00038 #if defined(HAVE_STL) || defined(HAVE_STL_STACK)
00039 // It is possible to use the standard template library list class since the 
00040 // interface is nearly identical.
00041 // Important: If you want to use the standard template library, no variable
00042 // in a namespace with using a list shall have the name stack 
00043 #include <stack>
00044 #define OFStack std::stack
00045 #else
00046 
00047 #define INCLUDE_CASSERT
00048 #define INCLUDE_CSTDDEF
00049 #include "dcmtk/ofstd/ofstdinc.h"
00050 
00051 #ifndef HAVE_CLASS_TEMPLATE
00052 #error Your C++ compiler cannot handle class templates:
00053 #endif
00054 
00055 
00059 struct OFStackLinkBase
00060 {
00062     OFStackLinkBase * next;
00063 
00065     OFStackLinkBase()
00066       : next(NULL)
00067     {
00068     }
00069 
00071     virtual ~OFStackLinkBase()
00072     {
00073     }
00074 private:
00075 
00077     OFStackLinkBase(const OFStackLinkBase &);
00078 
00080     OFStackLinkBase &operator=(const OFStackLinkBase &);
00081 };
00082 
00086 class OFStackBase
00087 {
00088 public:
00090     OFStackBase()
00091       : head(NULL),
00092         stackSize(0)
00093     {
00094     }
00095 
00097     virtual ~OFStackBase()
00098     {
00099       while(!base_empty())
00100         base_pop();
00101     }
00102 
00106     OFBool base_empty() const { return head == NULL; }
00107 
00111     size_t base_size() const { return stackSize; }
00112 
00116     OFStackLinkBase * base_top() 
00117     {
00118     assert(head!=NULL);
00119     return head;
00120     }
00121 
00125     void base_push(OFStackLinkBase * element)
00126     {
00127       element->next = head;
00128       head = element;
00129       stackSize++;
00130     }
00131     
00135     void base_pop()
00136     {
00137       assert(head!=NULL);
00138       OFStackLinkBase * delObj = head;
00139       head = head->next;
00140       delete delObj;
00141       stackSize--;
00142     }
00143 
00144 protected:
00145 
00147     OFStackLinkBase * head;
00148     
00150     size_t stackSize;
00151 
00152 private:
00153  
00155     OFStackBase(const OFStackBase &);
00156 
00158     OFStackBase &operator=(const OFStackBase &);
00159 
00160 };
00161 
00165 template <class T>
00166 struct OFStackLink : public OFStackLinkBase
00167 {
00169     T info;
00170 
00174     OFStackLink(const T & i) : OFStackLinkBase(), info(i)  { }
00175 
00177     virtual ~OFStackLink()
00178     {
00179     }
00180 
00181 private:
00182  
00184     OFStackLink(const OFStackLink<T> &);
00185 
00187     OFStackLink<T> &operator=(const OFStackLink<T> &);
00188 };
00189 
00190 
00191 
00195 template <class T>
00196 class OFStack : private OFStackBase
00197 {
00198 
00199 public:
00200 
00202     OFStack() {};
00203 
00205     OFStack(const OFStack<T> & x) : OFStackBase()
00206     {
00207         copy(x);
00208     }
00209 
00211     OFStack<T> &operator=(const OFStack<T> &x)
00212     {
00213         copy(x);
00214         return *this;
00215     }
00216 
00220     OFBool empty() const { return OFStackBase::base_empty(); }
00221 
00225     size_t size() const { return OFStackBase::base_size(); }
00226 
00231     T & top() 
00232     { 
00233         return (OFstatic_cast(OFStackLink<T> *, OFStackBase::base_top()))->info; 
00234     }
00235 
00240     void push(const T & x) 
00241     { 
00242         OFStackBase::base_push(new OFStackLink<T>(x));
00243     }
00244 
00248     void pop() { OFStackBase::base_pop(); }
00249 
00250 private:
00251 
00256     int copy(const OFStack<T> & x)
00257     {
00258         stackSize = x.size();
00259         if (stackSize)
00260         {
00261             head = new OFStackLink<T>((OFstatic_cast(OFStackLink<T>*, x.head))->info);
00262             OFStackLinkBase * newPtr = head;
00263             OFStackLinkBase * oldPtr = x.head->next;
00264             while (oldPtr)
00265             {
00266             newPtr->next = 
00267                 new OFStackLink<T>((OFstatic_cast(OFStackLink<T>*, oldPtr))->info);
00268             oldPtr = oldPtr->next;
00269             newPtr = newPtr->next;
00270             }
00271         }
00272         return 0;
00273     }
00274 
00275 };
00276 
00277 #endif
00278 #endif
00279 
00280 /*
00281 ** CVS/RCS Log:
00282 ** $Log: ofstack.h,v $
00283 ** Revision 1.18  2010-10-14 13:15:50  joergr
00284 ** Updated copyright header. Added reference to COPYRIGHT file.
00285 **
00286 ** Revision 1.17  2005/12/08 16:06:03  meichel
00287 ** Changed include path schema for all DCMTK header files
00288 **
00289 ** Revision 1.16  2003/08/14 14:41:39  meichel
00290 ** OFStack now explicitly defined as std::stack if compiling with HAVE_STL
00291 **
00292 ** Revision 1.15  2003/07/09 13:57:43  meichel
00293 ** Adapted type casts to new-style typecast operators defined in ofcast.h
00294 **
00295 ** Revision 1.14  2002/11/27 11:23:06  meichel
00296 ** Adapted module ofstd to use of new header file ofstdinc.h
00297 **
00298 ** Revision 1.13  2002/07/10 11:50:40  meichel
00299 ** Added vitual destructor to class OFStackLink
00300 **
00301 ** Revision 1.12  2001/12/03 15:09:09  meichel
00302 ** Completed doc++ documentation
00303 **
00304 ** Revision 1.11  2001/07/03 14:35:01  meichel
00305 ** Fixed memory leak in ofstack.h
00306 **
00307 ** Revision 1.10  2001/06/01 15:51:35  meichel
00308 ** Updated copyright header
00309 **
00310 ** Revision 1.9  2000/10/12 08:11:35  joergr
00311 ** Added assignment operator to class OFStack.
00312 ** Declared (unimplemented) copy constructor and assignment operator in class
00313 ** OFStackLink to avoid compiler warnings (e.g. on Sun CC 2.0.1).
00314 **
00315 ** Revision 1.8  2000/10/10 12:01:21  meichel
00316 ** Created/updated doc++ comments
00317 **
00318 ** Revision 1.7  2000/03/08 16:36:02  meichel
00319 ** Updated copyright header.
00320 **
00321 ** Revision 1.6  1998/11/27 12:42:52  joergr
00322 ** Added copyright message to source files and changed CVS header.
00323 **
00324 ** Revision 1.5  1998/07/02 10:11:31  joergr
00325 ** Minor changes to avoid compiler warnings (gcc 2.8.1 with additional
00326 ** options), e.g. add copy constructors.
00327 **
00328 ** Revision 1.4  1998/02/06 15:07:40  meichel
00329 ** Removed many minor problems (name clashes, unreached code)
00330 **   reported by Sun CC4 with "+w" or Sun CC2.
00331 **
00332 ** Revision 1.3  1997/09/11 15:43:16  hewett
00333 ** Minor changes to eliminate warnings when compiled under the
00334 ** Signus GnuWin32 envionment.  Changed order of initialisers
00335 ** for OFListLink and OFStackLink.  Make ~OFLisBase and ~OFStackBase
00336 ** virtual destructors.
00337 **
00338 ** Revision 1.2  1997/07/21 09:02:24  andreas
00339 ** - New copy constructor for class OFStack
00340 **
00341 ** Revision 1.1  1997/07/02 11:51:15  andreas
00342 ** - Preliminary release of the OFFIS Standard Library.
00343 **   In the future this library shall contain a subset of the
00344 **   ANSI C++ Library (Version 3) that works on a lot of different
00345 **   compilers. Additionally this library shall include classes and
00346 **   functions that are often used. All classes and functions begin
00347 **   with OF... This library is independent of the DICOM development and
00348 **   shall contain no DICOM specific stuff.
00349 **
00350 **
00351 */


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