Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

Enumerator.h

Go to the documentation of this file.
00001 // -*- mode: C++; c-file-style: "stroustrup"; c-basic-offset: 4; indent-tabs-mode: nil; -*-
00002 ////////////////////////////////////////////////////////////////////
00003 //
00004 // Filename : Enumerator.h (base)
00005 //
00006 // LinkableEnumerator
00007 //  |
00008 //  +-Enumerator<T>
00009 //
00010 // This file is a part of the UPPAAL toolkit.
00011 // Copyright (c) 1995 - 2003, Uppsala University and Aalborg University.
00012 // All right reserved.
00013 //
00014 // $Id: Enumerator.h,v 1.5 2005/01/25 18:30:22 adavid Exp $
00015 //
00016 ///////////////////////////////////////////////////////////////////
00017 
00018 #ifndef INCLUDE_BASE_ENUMERATOR_H
00019 #define INCLUDE_BASE_ENUMERATOR_H
00020 
00021 #include <cstddef>
00022 #include "base/inttypes.h"
00023 #include "base/linkable.h"
00024 
00025 namespace base
00026 {
00027     /** Generic enumerator for linkables: enumerate on a
00028      * table of linkables. Typically, this is used in
00029      * hash tables.
00030      */
00031     class LinkableEnumerator
00032     {
00033     public:
00034 
00035         /** Constructor:
00036          * @param sizeOfTable: size of the table
00037          * of linkables
00038          * @param theTable: table of linkables
00039          * @pre theTable is a Linkable_t*[sizeOfTable]
00040          */
00041         LinkableEnumerator(size_t sizeOfTable,
00042                            SingleLinkable_t **theTable)
00043             : size(sizeOfTable), table(theTable), current(*theTable)
00044             {}
00045         
00046         /** Enumerate all the nodes in the table. The
00047          * order is not specified. In practice we start
00048          * from the beginning of the table and we enumerate
00049          * all nodes in natural order.
00050          * @return next linkable, or NULL if there are no
00051          * linkable left.
00052          * @post internal state is changed to return the
00053          * next linkable everytime the method is called.
00054          */
00055         SingleLinkable_t *getNextLinkable();
00056         
00057     private:
00058         size_t size;          /**< size of the table           */
00059         SingleLinkable_t **table;  /**< table of linkables          */
00060         SingleLinkable_t *current; /**< current node in enumeration */
00061     };
00062 
00063 
00064     /** Template for typed enumerators:
00065      * assume T is a kind of SingleLinkable_t.
00066      * This is basically a wrapper for typed
00067      * linkables.
00068      */
00069     template<class T>
00070     class Enumerator : public LinkableEnumerator
00071     {
00072     public:
00073         
00074         /** Constructor:
00075          * @see LinkableEnumerator
00076          */
00077         Enumerator(size_t sizeOfTable, T **theTable)
00078             : LinkableEnumerator(sizeOfTable,
00079                                  reinterpret_cast<SingleLinkable_t**>(theTable))
00080             {}
00081 
00082         /** Enumeration:
00083          * Enumerates all nodes in the table in an unspecified order.
00084          * @return next node or NULL if there are no nodes left.
00085          * @see LinkableEnumerator
00086          */
00087         T* getNext()
00088         {
00089             return static_cast<T*>(getNextLinkable());
00090         }
00091     };
00092 }
00093 
00094 
00095 #endif // INCLUDE_BASE_ENUMERATOR_H

Generated on Fri Jun 30 00:02:30 2006 for Module base by  doxygen 1.4.2