Arduino Libraries
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
array.hpp
Go to the documentation of this file.
1 // Author: Mario S. Könz <mskoenz@gmx.net>
2 // Date: 15.06.2013 23:45:24 EDT
3 // File: array.hpp
4 
5 /* This program is free software. It comes without any warranty, to
6  * the extent permitted by applicable law. You can redistribute it
7  * and/or modify it under the terms of the Do What The Fuck You Want
8  * To Public License, Version 2, as published by Sam Hocevar. See
9  * http://www.wtfpl.net/ or COPYING for more details. */
10 
11 #ifndef __ARRAY_HEADER
12 #define __ARRAY_HEADER
13 
14 #include "ard_assert.hpp"
15 
16 namespace ustd {
21  template<typename T, uint16_t N>
22  class array {
23  public:
24  typedef T value_type;
25  typedef uint16_t size_type;
26  //------------------- ctors -------------------
29  array() {
30  for(size_type i = 0; i < N; ++i) {
31  array_[i] = T();
32  }
33  }
37  array(T const & init) {
38  for(size_type i = 0; i < N; ++i) {
39  array_[i] = init;
40  }
41  }
44  array(array const & arg) {
45  for(size_type i = 0; i < N; ++i) {
46  array_[i] = arg.array_[i];
47  }
48  }
51  array & operator=(array const & rhs) {
52  for(size_type i = 0; i < N; ++i)
53  array_[i] = rhs.array_[i];
54  return (*this);
55  }
56  //------------------- getter -------------------
62  T & operator[](size_type const & pos) {
63  ASSERT(pos >= 0)
64  ASSERT(pos < N)
65  return array_[pos];
66  }
72  T const & operator[](size_type const & pos) const {
73  ASSERT(pos >= 0)
74  ASSERT(pos < N)
75  return array_[pos];
76  }
79  T * data() {
80  return array_;
81  }
82  //------------------- size/capacity -------------------
85  constexpr size_type size() {
86  return N;
87  }
88  //------------------- iterator -------------------
89  //~ struct iterator {
90  //~ iterator(array & arr, size_type const & pos): arr_(arr), pos_(pos) {
91  //~ }
92  //~ iterator & operator++() {
93  //~ ++pos_;
94  //~ return (*this);
95  //~ }
96  //~ bool operator!=(iterator const & rhs) const {
97  //~ return pos_ != rhs.pos_;
98  //~ }
99  //~ value_type & operator*() {
100  //~ return arr_[pos_];
101  //~ }
102  //~ array & arr_;
103  //~ size_type pos_;
104  //~ };
105  //~ iterator begin() {
106  //~ return iterator((*this), 0);
107  //~ }
108  //~ iterator end() {
109  //~ return iterator((*this), N);
110  //~ }
111  //------------------- print & serialize-------------------
116  template<typename S>
117  void print(S & os) const {
118  os << F("[");
119  for(size_type i = 0; i < N; ++i) {
120  os << array_[i];
121  if(i != N - 1)
122  os << F(", ");
123  }
124  os << F("]");
125  }
130  template<typename Archive>
131  void serialize(Archive & ar) {
132  for(size_type i = 0; i< N; ++i)
133  ar & array_[i];
134  }
135  //------------------- custom operations / not in std -------------------
138  void clear() {
139  for(size_type i = 0; i< N; ++i)
140  array_[i] = T();
141  }
142  private:
143  T array_[N];
144  };
145 }//end namespace ustd
146 
147 
148 #endif //__ARRAY_HEADER
T * data()
just return the internal cpp-array
Definition: array.hpp:79
constexpr size_type size()
retuns the size (equal to N)
Definition: array.hpp:85
void print(S &os) const
print to any stream-concept
Definition: array.hpp:117
array()
just initialises all with "zeros"
Definition: array.hpp:29
T & operator[](size_type const &pos)
returns element at a position
Definition: array.hpp:62
array(array const &arg)
copy constructor
Definition: array.hpp:44
uint16_t size_type
Definition: array.hpp:25
basically just a wrap for an cpp-array
Definition: array.hpp:22
array(T const &init)
initialises all with init
Definition: array.hpp:37
T const & operator[](size_type const &pos) const
returns element at a position (const version)
Definition: array.hpp:72
void serialize(Archive &ar)
serializes to or from archive
Definition: array.hpp:131
#define F(x)
Definition: ustd_generic.hpp:15
void clear()
set all elements to "zero"
Definition: array.hpp:138
#define ASSERT(exp)
Definition: ard_assert.hpp:33
array & operator=(array const &rhs)
asignment operator
Definition: array.hpp:51
T value_type
Definition: array.hpp:24