torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Array.hpp
Go to the documentation of this file.
1 // Torc - Copyright 2011-2013 University of Southern California. All Rights Reserved.
2 // $HeadURL$
3 // $Id$
4 
5 // This program is free software: you can redistribute it and/or modify it under the terms of the
6 // GNU General Public License as published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
11 // the GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License along with this program. If
14 // not, see <http://www.gnu.org/licenses/>.
15 
16 /// \file
17 /// \brief Header for the Array class.
18 
19 #ifndef TORC_ARCHITECTURE_ARRAY_HPP
20 #define TORC_ARCHITECTURE_ARRAY_HPP
21 
22 #include <boost/cstdint.hpp>
23 #include <boost/type_traits.hpp>
24 #include <boost/noncopyable.hpp>
25 #include <stdexcept>
26 #include <cstdio>
27 
28 namespace torc {
29 namespace architecture {
30 
31  /// \brief Encapsulation of a static array.
32  /// \details Arrays are intended for fixed-size operation without frills, and are used
33  /// extensively to represent device wiring data. Although this class could be used more
34  /// broadly, it is likely that most code would be better served by raw arrays or by STL
35  /// containers.
36  /// <p>
37  /// Arrays can be resized, but their contents are not retained when resized, nor are the
38  /// contents cleared upon initialization.
39  template <class T> class Array : private boost::noncopyable {
40  protected:
41  // types
42  /// \brief Imported type name.
43  typedef boost::uint32_t uint32_t;
44  /// \brief A type identical to template parameter T, but with any 'const' trait removed.
45  typedef typename boost::remove_const<T>::type T_non_const;
46  // members
47  /// \brief The internal array.
48  T* mArray;
49  /// \brief The logical and actual size of the array.
51  private:
52  /// \brief Allocate an array of the specified size.
53  void allocate(uint32_t inSize) {
54  if(mArray != 0) deallocate();
55  if(inSize > 0) {
56  // T might be a const type, so we get Boost to remove the const trait
57  mArray = new T_non_const[inSize];
58  mSize = inSize;
59  }
60  }
61  /// \brief Deallocate the array.
62  void deallocate(void) {
63  if(mArray != 0) delete[] mArray;
64  mArray = 0;
65  mSize = 0;
66  }
67 #ifdef DEBUG
68  /// \brief Enforce array bounds if so requested.
69  #define enforceBounds(index) \
70  if(index >= mSize || index < 0) { \
71  char scratch[1 << 10]; \
72  snprintf(scratch, sizeof(scratch), "Index %d not in valid range [0,%d).", index, \
73  mSize); \
74  throw std::out_of_range(scratch); \
75  }
76 #else
77  /// \brief Enforce array bounds if so requested.
78  #define enforceBounds(index) ;
79 #endif
80  public:
81  // types
82  /// \brief Constant T iterator type.
83  typedef const T* const_iterator;
84  /// \brief Non-constant T iterator type.
85  typedef T* iterator;
86  // constructors
87  /// \brief Null constructor.
88  Array<T>(void) : mArray(0), mSize(0) {}
89  /// \brief Public constructor.
90  Array<T>(uint32_t inSize) : mArray(0), mSize(0) { allocate(inSize); }
91  /// \brief Non-virtual destructor.
92  ~Array<T>(void) { deallocate(); }
93  // iterators
94  /// \brief Returns the non-constant begin iterator.
95  T* begin(void) { return mArray; }
96  /// \brief Returns the non-constant end iterator.
97  T* end(void) { return mArray + mSize; }
98  /// \brief Returns the constant begin iterator.
99  const T* begin(void) const { return mArray; }
100  /// \brief Returns the constant end iterator.
101  const T* end(void) const { return mArray + mSize; }
102  // accessors
103  /// \brief Returns the array size.
104  inline uint32_t getSize(void) const { return mSize; }
105  // functions
106  /// \brief Discards all contents and resizes the array.
107  inline void setSize(uint32_t inSize) { allocate(inSize); }
108  // operators
109  /// \brief Non-constant subscript operator.
110  T& operator [](uint32_t inIndex) {
111  enforceBounds(inIndex);
112  return mArray[inIndex];
113  }
114  /// \brief Constant subscript operator.
115  const T& operator [](uint32_t inIndex) const {
116  enforceBounds(inIndex);
117  return mArray[inIndex];
118  }
119  };
120 
121  /// \brief Encapsulation of a 2D static array.
122  /// \details 2D arrays are used to represent parts of the device wiring data. In general, the
123  /// nested arrays will not be identically sized, so we provide no deep constructor here.
124  /// See Array for more details on the superclass.
125  template <class T> class Array2D : public Array< Array<T> > {
126  protected:
127  // types
128  /// \brief Typedef for the base class.
130  public:
131  // constructors
132  /// \brief Null constructor.
133  Array2D<T>(void) : super() {}
134  /// \brief Public constructor.
135  /// \param inSize The size of the outer array. The inner arrays will be constructed with
136  /// their unsized default constructor.
137  Array2D<T>(uint32_t inSize) : super(inSize) {}
138  };
139 
140 } // namespace architecture
141 } // namespace torc
142 
143 #endif // TORC_ARCHITECTURE_ARRAY_HPP
const T * const_iterator
Constant T iterator type.
Definition: Array.hpp:83
#define enforceBounds(index)
Enforce array bounds if so requested.
Definition: Array.hpp:78
const T * end(void) const
Returns the constant end iterator.
Definition: Array.hpp:101
T * end(void)
Returns the non-constant end iterator.
Definition: Array.hpp:97
T * mArray
The internal array.
Definition: Array.hpp:48
const T * begin(void) const
Returns the constant begin iterator.
Definition: Array.hpp:99
Array< Array< T > > super
Typedef for the base class.
Definition: Array.hpp:129
void allocate(uint32_t inSize)
Allocate an array of the specified size.
Definition: Array.hpp:53
Encapsulation of a 2D static array.
Definition: Array.hpp:125
boost::remove_const< T >::type T_non_const
A type identical to template parameter T, but with any 'const' trait removed.
Definition: Array.hpp:45
uint32_t mSize
The logical and actual size of the array.
Definition: Array.hpp:50
boost::uint32_t uint32_t
Imported type name.
Definition: Array.hpp:43
T * begin(void)
Returns the non-constant begin iterator.
Definition: Array.hpp:95
void deallocate(void)
Deallocate the array.
Definition: Array.hpp:62
T * iterator
Non-constant T iterator type.
Definition: Array.hpp:85
void setSize(uint32_t inSize)
Discards all contents and resizes the array.
Definition: Array.hpp:107
T & operator[](uint32_t inIndex)
Non-constant subscript operator.
Definition: Array.hpp:110
Encapsulation of a static array.
Definition: Array.hpp:39
uint32_t getSize(void) const
Returns the array size.
Definition: Array.hpp:104