AERA
structure_member.cpp
1 //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
2 //_/_/
3 //_/_/ AERA
4 //_/_/ Autocatalytic Endogenous Reflective Architecture
5 //_/_/
6 //_/_/ Copyright (c) 2018-2023 Jeff Thompson
7 //_/_/ Copyright (c) 2018-2023 Kristinn R. Thorisson
8 //_/_/ Copyright (c) 2018-2023 Icelandic Institute for Intelligent Machines
9 //_/_/ http://www.iiim.is
10 //_/_/
11 //_/_/ Copyright (c) 2010-2012 Eric Nivel
12 //_/_/ Center for Analysis and Design of Intelligent Agents
13 //_/_/ Reykjavik University, Menntavegur 1, 102 Reykjavik, Iceland
14 //_/_/ http://cadia.ru.is
15 //_/_/
16 //_/_/ Part of this software was developed by Eric Nivel
17 //_/_/ in the HUMANOBS EU research project, which included
18 //_/_/ the following parties:
19 //_/_/
20 //_/_/ Autonomous Systems Laboratory
21 //_/_/ Technical University of Madrid, Spain
22 //_/_/ http://www.aslab.org/
23 //_/_/
24 //_/_/ Communicative Machines
25 //_/_/ Edinburgh, United Kingdom
26 //_/_/ http://www.cmlabs.com/
27 //_/_/
28 //_/_/ Istituto Dalle Molle di Studi sull'Intelligenza Artificiale
29 //_/_/ University of Lugano and SUPSI, Switzerland
30 //_/_/ http://www.idsia.ch/
31 //_/_/
32 //_/_/ Institute of Cognitive Sciences and Technologies
33 //_/_/ Consiglio Nazionale delle Ricerche, Italy
34 //_/_/ http://www.istc.cnr.it/
35 //_/_/
36 //_/_/ Dipartimento di Ingegneria Informatica
37 //_/_/ University of Palermo, Italy
38 //_/_/ http://diid.unipa.it/roboticslab/
39 //_/_/
40 //_/_/
41 //_/_/ --- HUMANOBS Open-Source BSD License, with CADIA Clause v 1.0 ---
42 //_/_/
43 //_/_/ Redistribution and use in source and binary forms, with or without
44 //_/_/ modification, is permitted provided that the following conditions
45 //_/_/ are met:
46 //_/_/ - Redistributions of source code must retain the above copyright
47 //_/_/ and collaboration notice, this list of conditions and the
48 //_/_/ following disclaimer.
49 //_/_/ - Redistributions in binary form must reproduce the above copyright
50 //_/_/ notice, this list of conditions and the following disclaimer
51 //_/_/ in the documentation and/or other materials provided with
52 //_/_/ the distribution.
53 //_/_/
54 //_/_/ - Neither the name of its copyright holders nor the names of its
55 //_/_/ contributors may be used to endorse or promote products
56 //_/_/ derived from this software without specific prior
57 //_/_/ written permission.
58 //_/_/
59 //_/_/ - CADIA Clause: The license granted in and to the software
60 //_/_/ under this agreement is a limited-use license.
61 //_/_/ The software may not be used in furtherance of:
62 //_/_/ (i) intentionally causing bodily injury or severe emotional
63 //_/_/ distress to any person;
64 //_/_/ (ii) invading the personal privacy or violating the human
65 //_/_/ rights of any person; or
66 //_/_/ (iii) committing or preparing for any act of war.
67 //_/_/
68 //_/_/ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
69 //_/_/ CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
70 //_/_/ INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
71 //_/_/ MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
72 //_/_/ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
73 //_/_/ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
74 //_/_/ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
75 //_/_/ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
76 //_/_/ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
77 //_/_/ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
78 //_/_/ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
79 //_/_/ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
80 //_/_/ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
81 //_/_/ OF SUCH DAMAGE.
82 //_/_/
83 //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
84 
85 #include "structure_member.h"
86 #include "compiler.h"
87 
88 
89 namespace r_comp {
90 
91 StructureMember::StructureMember() {
92 }
93 
94 StructureMember::StructureMember(_Read r,
95  std::string m,
96  std::string p,
97  Iteration i) : read_(r),
98  name_(m),
99  class_(p),
100  iteration_(i) {
101 
102  if (read_ == &Compiler::read_any) type_ = ANY;
103  else if (read_ == &Compiler::read_number) type_ = NUMBER;
104  else if (read_ == &Compiler::read_timestamp) type_ = TIMESTAMP;
105  else if (read_ == &Compiler::read_duration) type_ = DURATION;
106  else if (read_ == &Compiler::read_boolean) type_ = BOOLEAN;
107  else if (read_ == &Compiler::read_string) type_ = STRING;
108  else if (read_ == &Compiler::read_node) type_ = NODE_ID;
109  else if (read_ == &Compiler::read_device) type_ = DEVICE_ID;
110  else if (read_ == &Compiler::read_function) type_ = FUNCTION_ID;
111  else if (read_ == &Compiler::read_expression) type_ = ANY;
112  else if (read_ == &Compiler::read_set) type_ = ReturnType::SET;
113  else if (read_ == &Compiler::read_class) type_ = ReturnType::CLASS;
114 }
115 
116 Class *StructureMember::get_class(Metadata *metadata) const {
117 
118  return class_ == "" ? NULL : &metadata->classes_.find(class_)->second;
119 }
120 
121 ReturnType StructureMember::get_return_type() const {
122 
123  return type_;
124 }
125 
126 bool StructureMember::used_as_expression() const {
127 
128  return iteration_ == I_EXPRESSION;
129 }
130 
131 StructureMember::Iteration StructureMember::getIteration() const {
132 
133  return iteration_;
134 }
135 
136 _Read StructureMember::read() const {
137 
138  return read_;
139 }
140 
141 void StructureMember::write(word32 *storage) const {
142 
143  if (read_ == &Compiler::read_any)
144  storage[0] = R_ANY;
145  else if (read_ == &Compiler::read_number)
146  storage[0] = R_NUMBER;
147  else if (read_ == &Compiler::read_timestamp)
148  storage[0] = R_TIMESTAMP;
149  else if (read_ == &Compiler::read_duration)
150  storage[0] = R_DURATION;
151  else if (read_ == &Compiler::read_boolean)
152  storage[0] = R_BOOLEAN;
153  else if (read_ == &Compiler::read_string)
154  storage[0] = R_STRING;
155  else if (read_ == &Compiler::read_node)
156  storage[0] = R_NODE;
157  else if (read_ == &Compiler::read_device)
158  storage[0] = R_DEVICE;
159  else if (read_ == &Compiler::read_function)
160  storage[0] = R_FUNCTION;
161  else if (read_ == &Compiler::read_expression)
162  storage[0] = R_EXPRESSION;
163  else if (read_ == &Compiler::read_set)
164  storage[0] = R_SET;
165  else if (read_ == &Compiler::read_class)
166  storage[0] = R_CLASS;
167  uint32 offset = 1;
168  storage[offset++] = type_;
169  r_code::Write(storage + offset, class_);
170  offset += r_code::GetSize(class_);
171  storage[offset++] = iteration_;
172  r_code::Write(storage + offset, name_);
173 }
174 
175 void StructureMember::read(word32 *storage) {
176 
177  switch (storage[0]) {
178  case R_ANY: read_ = &Compiler::read_any; break;
179  case R_NUMBER: read_ = &Compiler::read_number; break;
180  case R_TIMESTAMP: read_ = &Compiler::read_timestamp; break;
181  case R_DURATION: read_ = &Compiler::read_duration; break;
182  case R_BOOLEAN: read_ = &Compiler::read_boolean; break;
183  case R_STRING: read_ = &Compiler::read_string; break;
184  case R_NODE: read_ = &Compiler::read_node; break;
185  case R_DEVICE: read_ = &Compiler::read_device; break;
186  case R_FUNCTION: read_ = &Compiler::read_function; break;
187  case R_EXPRESSION: read_ = &Compiler::read_expression; break;
188  case R_SET: read_ = &Compiler::read_set; break;
189  case R_CLASS: read_ = &Compiler::read_class; break;
190  }
191  uint32 offset = 1;
192  type_ = (ReturnType)storage[offset++];
193  r_code::Read(storage + offset, class_);
194  offset += r_code::GetSize(class_);
195  iteration_ = (Iteration)storage[offset++];
196  r_code::Read(storage + offset, name_);
197 }
198 
199 uint32 StructureMember::get_size() { // see segments.cpp for the RAM layout
200 
201  uint32 size = 3; // read ID, return type, iteration
202  size += r_code::GetSize(class_);
203  size += r_code::GetSize(name_);
204  return size;
205 }
206 }