replicode
array.h
Go to the documentation of this file.
1 /*
2 * HUMANOBS - mBrane
3 *
4 * Eric Nivel
5 * Center for Analysis and Design of Intelligent Agents
6 * Reykjavik University, Menntavegur 1, 101 Reykjavik, Iceland
7 * http://cadia.ru.is
8 * Copyright(c)2012
9 *
10 * This software was developed by the above copyright holder as part of
11 * the HUMANOBS EU research project, in collaboration with the
12 * following parties:
13 *
14 * Autonomous Systems Laboratory
15 * Technical University of Madrid, Spain
16 * http://www.aslab.org/
17 *
18 * Communicative Machines
19 * Edinburgh, United Kingdom
20 * http://www.cmlabs.com/
21 *
22 * Istituto Dalle Molle di Studi sull'Intelligenza Artificiale
23 * University of Lugano and SUPSI, Switzerland
24 * http://www.idsia.ch/
25 *
26 * Institute of Cognitive Sciences and Technologies
27 * Consiglio Nazionale delle Ricerche, Italy
28 * http://www.istc.cnr.it/
29 *
30 * Dipartimento di Ingegneria Informatica
31 * University of Palermo, Italy
32 * http://roboticslab.dinfo.unipa.it/index.php/Main/HomePage
33 *
34 *
35 * --- HUMANOBS Open-Source BSD License, with CADIA Clause v 1.0 ---
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, is permitted provided that the following conditions
39 * are met:
40 *
41 * - Redistributions of source code must retain the above copyright
42 * and collaboration notice, this list of conditions and the
43 * following disclaimer.
44 *
45 * - Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following
47 * disclaimer in the documentation and/or other materials provided
48 * with the distribution.
49 *
50 * - Neither the name of its copyright holders nor the names of its
51 * contributors may be used to endorse or promote products
52 * derived from this software without specific prior written permission.
53 *
54 * - CADIA Clause: The license granted in and to the software under this
55 * agreement is a limited-use license. The software may not be used in
56 * furtherance of:
57 * (i) intentionally causing bodily injury or severe emotional distress
58 * to any person;
59 * (ii) invading the personal privacy or violating the human rights of
60 * any person; or
61 * (iii) committing or preparing for any act of war.
62 *
63 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
64 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
65 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
66 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
67 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
68 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
69 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
70 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
71 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
72 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
73 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
74 */
75 
76 #ifndef mBrane_sdk_array_h
77 #define mBrane_sdk_array_h
78 
79 #include "../CoreLibrary/types.h"
80 #include <string.h>
81 
82 using namespace core;
83 
84 namespace mBrane
85 {
86 namespace sdk
87 {
88 
89 template<typename T> class StaticArray
90 {
91 private:
93  T *_array;
94  bool _once;
95 public:
96  StaticArray();
97  ~StaticArray();
98  void alloc(uint32_t count); // to be called only once
99  uint32_t count() const;
100  T &operator [](uint32_t i); // unprotected, i.e. do not let i>=count()
101  T *data() const;
102 };
103 
104 class ArrayManaged {};
105 class ArrayUnmanaged {};
106 
107 template<typename T, uint16_t Size, class Managed> class Block
108 {
109 public:
110  Block(Block *prev = NULL): next(NULL), prev(prev) {}
112  {
113  if (next) {
114  delete next;
115  }
116  }
117  T data[Size];
120 };
121 
122 // Specialization of Block<T,Size> holding pointers and managing objects.
123 template<typename T, uint16_t Size> class Block<T *, Size, ArrayManaged>
124 {
125 public:
126  Block(Block *prev = NULL): next(NULL), prev(prev)
127  {
128  memset(data, 0, Size * sizeof(T));
129  }
131  {
132  for (uint32_t i = 0; i < Size; i++)if (data[i]) {
133  delete data[i];
134  }
135 
136  if (next) {
137  delete next;
138  }
139  }
140  T *data[Size];
143 };
144 
145 template<typename T, uint16_t Size, class Managed> class _Array
146 {
147 protected:
151  // navigation optimization
155  _Array();
156 public:
157  virtual ~_Array();
158  uint32_t count() const;
159 };
160 
161 // Dynamic array, i.e. a linked list of Block<T,Size>.
162 template<typename T, uint16_t Size, class Managed = ArrayUnmanaged> class Array:
163  public _Array<T, Size, Managed>
164 {
165 public:
166  Array();
167  ~Array();
168  T *get(uint32_t i); // no allocation performed
169  T &operator [](uint32_t i); // changes counts, i.e. makes sure that there's room. Doesn't mean any object has been assigned to the ith position
170 };
171 
172 // Specialization of _Array<T,Size> holding pointers and managing objects.
173 // Array<T*,Size> instantiates pointed objects (calling T()) whenever needed upon calls to operator [].
174 // Array<T*,Size> deletes pointed objects.
175 template<typename T, uint16_t Size> class Array<T *, Size, ArrayManaged>:
177 {
178 public:
179  Array();
180  ~Array();
181  T **get(uint32_t i); // no allocation performed
182  T *&operator [](uint32_t i); // changes counts, i.e. makes sure that there's room. Doesn't mean any object has been assigned to the ith position
183 };
184 }
185 }
186 
187 
188 #include "array.tpl.cpp"
189 
190 
191 #endif
uint32_t minIndex
Definition: array.h:153
bool _once
Definition: array.h:94
Block< T, Size, Managed > block
Definition: array.h:149
Definition: base.cpp:81
Block(Block *prev=NULL)
Definition: array.h:126
Definition: array.h:84
Definition: array.h:105
~Block()
Definition: array.h:111
Block(Block *prev=NULL)
Definition: array.h:110
Definition: array.h:107
Block * prev
Definition: array.h:119
Block * prev
Definition: array.h:142
Block< T, Size, Managed > * current
Definition: array.h:152
Definition: array.h:145
uint32_t _count
Definition: array.h:92
Block * next
Definition: array.h:141
T * _array
Definition: array.h:93
Block * next
Definition: array.h:118
Definition: array.h:162
uint32_t maxIndex
Definition: array.h:154
Definition: array.h:104
uint32_t _count
Definition: array.h:148
Definition: array.h:89
Block< T, Size, Managed > * last
Definition: array.h:150