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

Array.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 // This file is a part of the UPPAAL toolkit.
00005 // Copyright (c) 1995 - 2006, Uppsala University and Aalborg University.
00006 // All right reserved.
00007 //
00008 ///////////////////////////////////////////////////////////////////
00009 
00010 #ifndef INCLUDE_BASE_ARRAY_H
00011 #define INCLUDE_BASE_ARRAY_H
00012 
00013 #include <new>
00014 #include <string>
00015 #include "base/Object.h"
00016 
00017 namespace base
00018 {
00019     /** Array of X where X is a scalar type (or struct of scalars
00020      * without constructor or destructor. Constructors and
00021      * destructors of X are not called.
00022      */
00023     template <class X>
00024     class Array : public Object
00025     {
00026     public:
00027         virtual ~Array() {}
00028 
00029         /** Creation & destruction. */
00030         static Array<X>* create(size_t size);
00031         virtual void destroy();
00032 
00033         /** Copy (also trunk and reallocate more). @post New elements = 0. */
00034         Array<X>* copy()            const { return copy(size()); }
00035         Array<X>* copy(size_t size) const;
00036 
00037         /** Simplified iterators. */
00038         const X* begin() const { return first; }
00039         const X* end()   const { return last; }
00040         X* begin()             { assert(isMutable()); return first; }
00041         X* end()               { assert(isMutable()); return last; }
00042         
00043         /** Standard operators. */
00044         const X& operator [] (size_t index) const;
00045         X& operator [] (size_t index);
00046 
00047         /** @return the size of the array. */
00048         size_t size() const { return last - first; }
00049 
00050         /** Set the array to zero. */
00051         Array<X>* zero();
00052 
00053     private:
00054         Array(size_t size)
00055             : last(first + size) {}
00056 
00057         X* last;
00058         X first[];
00059     };
00060 
00061 
00062     /****************************
00063      * Inlined implementations. *
00064      ****************************/
00065 
00066     template <class X> inline
00067     const X& Array<X>::operator [] (size_t index) const
00068     {
00069         assert(&first[index] < last);
00070         return first[index];
00071     }
00072 
00073     template <class X> inline
00074     X& Array<X>::operator [] (size_t index)
00075     {
00076         assert(isMutable());
00077         assert(&first[index] < last);
00078         return first[index];
00079     }
00080 
00081     template <class X> inline
00082     Array<X>* Array<X>::create(size_t size)
00083     {
00084         return new (new char[sizeof(Array<X>) + size*sizeof(X)]) Array<X>(size);
00085     }
00086 
00087     template <class X> inline
00088     void Array<X>::destroy()
00089     {
00090         delete [] (char*) this;
00091     }
00092 
00093     template <class X> inline
00094     Array<X>* Array<X>::copy(size_t copySize) const
00095     {
00096         Array<X>* a = create(copySize);
00097         size_t mySize = size();
00098         memcpy(a->first, first,
00099                copySize >= mySize
00100                ?(char*)last - (char*)first
00101                : copySize*sizeof(X));
00102         if (copySize > mySize)
00103         {
00104             memset(&a->first[mySize], 0,
00105                    (char*)a->last - (char*)&a->first[mySize]);
00106         }
00107         return a;
00108     }
00109 
00110     template <class X> inline
00111     Array<X>* Array<X>::zero()
00112     {
00113         memset(first, 0, (char*)last - (char*)first);
00114         return this;
00115     }
00116 }
00117 
00118 #endif // INCLUDE_BASE_ARRAY_H

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