Torque3D Documentation / _generateds / tUnmanagedVector.h

tUnmanagedVector.h

Engine/source/core/util/tUnmanagedVector.h

More...

Classes:

class

An array that does not manage the memory it gets passed.

Detailed Description

 1
 2//-----------------------------------------------------------------------------
 3// Copyright (c) 2012 GarageGames, LLC
 4//
 5// Permission is hereby granted, free of charge, to any person obtaining a copy
 6// of this software and associated documentation files (the "Software"), to
 7// deal in the Software without restriction, including without limitation the
 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 9// sell copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included in
13// all copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21// IN THE SOFTWARE.
22//-----------------------------------------------------------------------------
23
24#ifndef _TUNMANAGEDVECTOR_H_
25#define _TUNMANAGEDVECTOR_H_
26
27
28/// An array that does not manage the memory it gets passed.  Conversely, the
29/// array cannot be enlarged.
30///
31/// @note As the array does not manage the instances it uses, it will also not
32///   destruct them when it is itself destructed.
33template< typename T >
34class UnmanagedVector
35{
36   protected:
37
38      U32 mCount;
39      T* mArray;
40
41   public:
42
43      typedef T* iterator;
44      typedef const T* const_iterator;
45
46      UnmanagedVector()
47         : mCount( 0 ), mArray( NULL ) {}
48      UnmanagedVector( T* array, U32 count )
49         : mCount( count ), mArray( array ) {}
50
51      U32 size() const { return mCount; }
52      bool empty() const { return ( mCount == 0 ); }
53      const T* address() const { return mArray; }
54      T* address() { return mArray; }
55
56      void clear() { mCount = 0; mArray = NULL; }
57
58      iterator begin() { return &mArray[ 0 ]; }
59      iterator end() { return &mArray[ mCount ]; }
60      const_iterator begin() const { return &mArray[ 0 ]; }
61      const_iterator end() const { return &mArray[ mCount ]; }
62
63      const T& first() const
64      {
65         AssertFatal( !empty(), "UnmanagedVector::first - Vector is empty" );
66         return mArray[ 0 ];
67      }
68      T& first()
69      {
70         AssertFatal( !empty(), "UnmanagedVector::first - Vector is empty" );
71         return mArray[ 0 ];
72      }
73      const T& last() const
74      {
75         AssertFatal( !empty(), "UnmanagedVector::last - Vector is empty" );
76         return mArray[ mCount - 1 ];
77      }
78      T& last()
79      {
80         AssertFatal( !empty(), "UnmanagedVector::last - Vector is empty" );
81         return mArray[ mCount - 1 ];
82      }
83
84      const T& operator []( U32 index ) const
85      {
86         AssertFatal( index <= size(), "UnmanagedVector::operator[] - Index out of range" );
87         return mArray[ index ];
88      }
89      T& operator []( U32 index )
90      {
91         AssertFatal( index <= size(), "UnmanagedVector::operator[] - Index out of range" );
92         return mArray[ index ];
93      }
94};
95
96#endif // !_TUNMANAGEDVECTOR_H_
97