FreeNOS
Array.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Niek Linnenbank
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef __LIBSTD_ARRAY_H
19 #define __LIBSTD_ARRAY_H
20 
21 #include "Assert.h"
22 #include "Sequence.h"
23 #include "Types.h"
24 #include "Macros.h"
25 
39 template <class T, Size N> class Array : public Sequence<T>
40 {
41  public:
42 
47  {
48  }
49 
55  Array(const Array<T,N> & a)
56  {
57  for (Size i = 0; i < N; i++)
58  {
59  m_array[i] = a->m_array[i];
60  }
61  }
62 
74  virtual bool insert(Size position, const T & item)
75  {
76  if (position >= N)
77  {
78  return false;
79  }
80  m_array[position] = item;
81  return true;
82  }
83 
91  virtual const T * get(Size position) const
92  {
93  if (position >= N)
94  {
95  return ZERO;
96  }
97  return &m_array[position];
98  }
99 
110  virtual const T & at(Size position) const
111  {
112  return m_array[position];
113  }
114 
125  virtual const T value(Size position) const
126  {
127  if (position >= N)
128  return T();
129  else
130  return m_array[position];
131  }
132 
138  virtual Size size() const
139  {
140  return N;
141  }
142 
148  virtual Size count() const
149  {
150  return N;
151  }
152 
153  private:
154 
156  T m_array[N];
157 };
158 
164 #endif /* __LIBSTD_ARRAY_H */
Array::Array
Array(const Array< T, N > &a)
Copy constructor.
Definition: Array.h:55
Macros.h
Types.h
Sequence.h
Assert.h
Array::m_array
T m_array[N]
The actual array where the data is stored.
Definition: Array.h:156
Array
This is a wrapper class for a fixed size array.
Definition: Array.h:39
Array::get
virtual const T * get(Size position) const
Returns the item at the given position.
Definition: Array.h:91
Sequence
Sequences are containers that provide indexed based storage of items.
Definition: Sequence.h:37
Size
unsigned int Size
Any sane size indicator cannot go negative.
Definition: Types.h:128
Array::Array
Array()
Default constructor.
Definition: Array.h:46
ZERO
#define ZERO
Zero value.
Definition: Macros.h:43
Array::at
virtual const T & at(Size position) const
Returns a reference to the item at the given position.
Definition: Array.h:110
Array::size
virtual Size size() const
Returns the maximum size of this Array.
Definition: Array.h:138
Array::value
virtual const T value(Size position) const
Return value at the given position.
Definition: Array.h:125
Array::count
virtual Size count() const
Returns the number of items in the Array.
Definition: Array.h:148
Array::insert
virtual bool insert(Size position, const T &item)
Puts the given item at the given position.
Definition: Array.h:74