replicode
payload_utils.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_payload_utils_h
77 #define mBrane_sdk_payload_utils_h
78 
79 #include "message.h"
80 
81 
82 namespace mBrane
83 {
84 namespace sdk
85 {
86 namespace payloads
87 {
88 /*
89  template<typename T> class Array;
90  // Leads a contiguous raw data storage; the allocated size is not sizeof(Storage<T>), but size; size <=2^30-1
91  // _metaData is [size(30)|allocation scheme(2)]
92  // CoreSize is sizeof(Storage<T>); Object<>::CoreSize() is used
93  // Storage<T> is registered in the ClassRegister (allocator=Storage<T>::New)
94  // Specialize for any particular T holding pointers: redefine ptrCount, getPtr and setPtr
95  template<typename T> class Storage:
96  public RPayload<Storage<T>,Memory>{
97  template<typename TA> friend class Array;
98  private:
99  uint32_t normalizedSize; // currently allocated size, in bytes; normalized, i.e. _size = 64*s where s is a power of 2
100  uint32_t count; // current max index; maintained by Array<T>
101  T *data; // points to data+sizeof(T*)
102  void setNormalizedSize(uint32_t size);
103  public:
104  static void *New(uint32_t size); // to initialize the _vftable on recv()
105  Storage();
106  ~Storage();
107  void *operator new(size_t s,uint32_t size,uint32_t &normalizedSize);
108  void operator delete(void *storage);
109  uint32_t getNormalizedSize();
110  T &operator [](uint32_t i);
111  size_t size() const;
112  };
113 
114  // Specialization for pointer storage
115  // T must be a subclass of __Payload
116  template<typename T> class Storage<P<T> >:
117  public RPayload<Storage<T>,Memory>{
118  template<typename TA> friend class Array;
119  private:
120  uint32_t normalizedSize;
121  uint32_t count;
122  P<T> *data;
123  void setNormalizedSize(uint32_t size);
124  public:
125  static void *New(uint32_t size);
126  Storage();
127  ~Storage();
128  void *operator new(size_t s,uint32_t size,uint32_t &normalizedSize);
129  void operator delete(void *storage);
130  uint32_t getNormalizedSize();
131  P<T> &operator [](uint32_t i);
132  size_t size() const;
133  uint16_t ptrCount() const;
134  __Payload *getPtr(uint16_t i) const;
135  void setPtr(uint16_t i,__Payload *p);
136  };
137 
138  // To embed in a payload
139  template<typename T> class Array{
140  protected:
141  uint32_t _maxCount; // capacity as a number of objects
142  P<Storage<T> > _data; // contiguous data storage allocated via Memory::GetDynamic(needed_size)->alloc()
143  public:
144  Array();
145  ~Array();
146  void ensure(uint32_t count); // number of objects
147  uint32_t count() const; // number of objects
148  T &operator [](uint32_t i); // reallocates if necessary (using ensure())
149  uint8_t *asBytes(uint32_t i); // returns the raw data from (and including) i (an index on Ts, not on bytes); no capacity check
150  // functions to be invoked by the embedding payload class
151  uint16_t ptrCount() const;
152  __Payload *getPtr(uint16_t i) const;
153  void setPtr(uint16_t i,__Payload *p);
154  };*/
155 }
156 }
157 }
158 
159 
160 #include "payload_utils.tpl.cpp"
161 
162 
163 #endif
Definition: array.h:84