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

pointer.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 : pointer.h (base)
00005 //
00006 // This file is a part of the UPPAAL toolkit.
00007 // Copyright (c) 1995 - 2000, Uppsala University and Aalborg University.
00008 // All right reserved.
00009 //
00010 // $Id: pointer.h,v 1.7 2005/04/22 15:20:10 adavid Exp $
00011 //
00012 ///////////////////////////////////////////////////////////////////
00013 
00014 #ifndef INCLUDE_BASE_POINTER_H
00015 #define INCLUDE_BASE_POINTER_H
00016 
00017 #include <assert.h>
00018 #include "base/intutils.h"
00019 #include "hash/compute.h"
00020 
00021 namespace base
00022 {
00023     /** A simple reference with maximal capacity for access checks.
00024      * This is a pointer to some bounded memory. array_t is defined
00025      * too, but pointer_t has no memory management, it is only a reference.
00026      * Accesses ptr[i] are checked. Main purpose is debugging + simple
00027      * iteration. std::vector does not provide these debugging capabilities.
00028      * Everything is assumed to be 32 bits aligned
00029      * so this wrapper is obviously not designed
00030      * for int8 or int16 types.
00031      */
00032     template<class T>
00033     class pointer_t
00034     {
00035     public:
00036 
00037         /** Default constructor
00038          */
00039         pointer_t()
00040             : data(NULL), capa(0)
00041         {
00042             // check assumption on valid data types
00043             assert((sizeof(T) & 3) == 0 && sizeof(T) > 0);
00044         }
00045         
00046         /** Constructor:
00047          * @param ptr: pointer to wrap.
00048          * @param max: number of elements
00049          * pointed by ptr.
00050          */
00051         pointer_t(T *ptr, size_t max)
00052             : data(ptr), capa(max)
00053         {
00054             // check assumption on valid data types
00055             assert((sizeof(T) & 3) == 0 && sizeof(T) > 0);
00056         }
00057         
00058         /** Pointer equality testing.
00059          * It is ambiguous to define a == operator since
00060          * it could be interpreted as pointer or content testing.
00061          */
00062         bool isSamePointerAs(const pointer_t<T> &other) const
00063         {
00064             return begin() == other.begin() && size() == other.size();
00065         }
00066 
00067         /** wrap and check ptr[at]
00068          */
00069         T& operator [] (size_t at)
00070         {
00071             assert(at < capa);
00072             assert(data);
00073             return data[at];
00074         }
00075 
00076         /** wrap and check read only ptr[at]
00077          */
00078         const T operator [] (size_t at) const
00079         {
00080             assert(at < capa);
00081             assert(data);
00082             return data[at];
00083         }
00084         
00085         /** wrap and check ptr->something
00086          */
00087         T* operator ->()
00088         {
00089             assert(data);
00090             return data;
00091         }
00092 
00093         /** wrap and check *ptr
00094          */
00095         T& operator *()
00096         {
00097             assert(data);
00098             return *data;
00099         }
00100 
00101         /** size of pointed area in T nb of elements
00102          */
00103         size_t size() const { return capa; }
00104         
00105         /** reset all pointed elements
00106          */
00107         void reset()
00108         {
00109             assert(capa == 0 || data);
00110             base_resetSmall(data, capa*intsizeof(T));
00111         }
00112 
00113         /** @return a hash value.
00114          */
00115         uint32_t hash() const
00116         {
00117             return hash_computeU32((uint32_t*)data, intsizeof(T[capa]), capa);
00118         }
00119 
00120         /** copy all pointed elements from a pointer.
00121          * @param src: compatible pointer
00122          * @pre sizes are the same.
00123          */
00124         void copyFrom(const pointer_t<T> &src)
00125         {
00126             assert(capa == src.capa);
00127             assert(capa == 0 || (data && src.data));
00128             base_copySmall(data, src.data, capa*intsizeof(T));
00129         }
00130 
00131         /** Simple iteration.
00132          */
00133         const T* begin() const { assert(capa == 0 || data); return data; }
00134         const T* end() const { assert(capa == 0 || data); return data + capa; }
00135         T* begin() { assert(capa == 0 || data); return data; }
00136         T* end() { assert(capa == 0 || data); return data + capa; }
00137 
00138         /** Reading data.
00139          */
00140         const T* operator () () const { return data; }
00141 
00142     protected:
00143         T *data;     /**< data pointed                       */
00144         size_t capa; /**< size of data, important for checks */
00145     };
00146 
00147 } // namespace base
00148 
00149 #endif

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