ObjectClass.cpp
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2012-2016, High Performance Humanoid Technologies (H2T),
5 * Karlsruhe Institute of Technology (KIT), all rights reserved.
6 *
7 * ArmarX is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * ArmarX is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * @author Fabian Peller-Konrad (fabian dot peller-konrad at kit dot edu)
20 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
21 * GNU General Public License
22 */
23
24// Header
25#include "ObjectClass.h"
26
27#include <SimoxUtility/algorithm/vector.hpp>
28#include <SimoxUtility/meta/type_name.h>
29
31{
32 // constructors
35 e.getObjectNameWithTemplateInstantiations(), // should be similar to the object name
36 e.getObjectNameWithTemplates(),
37 simox::meta::get_type_name<data::dto::Dict>(),
38 simox::meta::get_type_name<type::dto::AronObject>(),
39 e)
40 {
41 if (type.getMaybe() != type::Maybe::NONE)
42 {
43 throw error::ValueNotValidException(
44 __PRETTY_FUNCTION__,
45 "Somehow the maybe flag of a top level object declaration is set. This is not "
46 "valid!",
47 std::to_string((int)type.getMaybe()) + " aka " +
48 type::defaultconversion::string::Maybe2String.at(type.getMaybe()),
49 type.getPath());
50 }
51 }
52
53 std::pair<std::vector<std::pair<std::string, std::string>>, bool>
54 ObjectClass::getCtorInitializers(const std::string& name) const
55 {
56 std::vector<std::pair<std::string, std::string>> ret;
57 bool any = false;
58 for (const auto& [key, child] : type.getDirectMemberTypes())
59 {
60 auto child_s = FromAronType(*child);
61 auto p = child_s->getCtorInitializers(key);
62 for (const auto& el : p.first)
63 {
64 ret.push_back(el);
65 }
66 any = any or p.second;
67 }
68 return {ret, any};
69 }
70
71 std::vector<std::string>
73 {
74 std::vector<std::string> ret;
75 for (const auto& [key, child] : type.getDirectMemberTypes())
76 {
77 (void)key;
78 auto child_s = FromAronType(*child);
79 ret = simox::alg::appended(ret, child_s->getRequiredIncludes());
80 }
81 return ret;
82 }
83
84 std::vector<CppFieldPtr>
86 {
87 std::vector<CppFieldPtr> fields;
88 for (const auto& [key, member] : type.getDirectMemberTypes())
89 {
90 auto member_s = FromAronType(*member);
91 std::vector<CppFieldPtr> member_fields = member_s->getPublicVariableDeclarations(key);
92 fields.insert(fields.end(), member_fields.begin(), member_fields.end());
93 }
94 return fields;
95 }
96
98 ObjectClass::getResetSoftBlock(const std::string& accessor) const
99 {
100 CppBlockPtr block_if_data = std::make_shared<CppBlock>();
101 if (type.getExtends() != nullptr)
102 {
103 const auto extends_s = FromAronType(*type.getExtends());
104 block_if_data->addLine(extends_s->getFullInstantiatedCppTypename() + "::resetSoft();");
105 }
106
107 for (const auto& [key, child] : type.getDirectMemberTypes())
108 {
109 auto child_s = FromAronType(*child);
110 CppBlockPtr b2 = child_s->getResetSoftBlock(key);
111 block_if_data->appendBlock(b2);
112 }
113 return block_if_data;
114 }
115
117 ObjectClass::getResetHardBlock(const std::string& accessor) const
118 {
119 CppBlockPtr block_if_data = std::make_shared<CppBlock>();
120 if (type.getExtends() != nullptr)
121 {
122 const auto extends_s = FromAronType(*type.getExtends());
123 block_if_data->addLine(extends_s->getFullInstantiatedCppTypename() + "::resetHard();");
124 }
125
126 for (const auto& [key, child] : type.getDirectMemberTypes())
127 {
128 const auto child_s = FromAronType(*child);
129 CppBlockPtr b2 = child_s->getResetHardBlock(key);
130 block_if_data->appendBlock(b2);
131 }
132 return block_if_data;
133 }
134
137 const std::string&,
138 const Path& p,
139 std::string&) const
140 {
141 static const std::string OBJECT_MEMBERS_ACCESSOR = ARON_VARIABLE_PREFIX + "_objectMembers";
142 static const std::string OBJECT_EXTENDS_ACCESSOR = ARON_VARIABLE_PREFIX + "_objectExtends";
143
144 CppBlockPtr b = std::make_shared<CppBlock>();
145 b->addLine("std::map<std::string, _Aron_T> " + OBJECT_MEMBERS_ACCESSOR + ";");
146
147 if (type.getExtends() != nullptr)
148 {
149 const auto extends_s = FromAronType(*type.getExtends());
150 b->addLine("// get base class of " + this->getFullInstantiatedCppTypename());
151 b->addLine("auto " + OBJECT_EXTENDS_ACCESSOR + " = " +
152 extends_s->getFullInstantiatedCppTypename() + "::writeType(" +
153 ARON_WRITER_ACCESSOR + ", " + "{" +
154 simox::alg::join(type.getExtends()->getTemplateInstantiations(), ", ") +
155 "}, " + "::armarx::aron::type::Maybe::NONE);");
156 }
157 else
158 {
159 b->addLine("auto " + OBJECT_EXTENDS_ACCESSOR + " = std::nullopt;");
160 }
161
162 b->addLine("// members of " + this->getFullInstantiatedCppTypename());
163 for (const auto& [key, child] : type.getDirectMemberTypes())
164 {
165 const auto child_s = FromAronType(*child);
166 std::string child_return_variant;
167 Path nextPath = p.withElement(key, true);
168 CppBlockPtr child_b = child_s->getWriteTypeBlock(
169 child_s->getFullInstantiatedCppTypename(), key, nextPath, child_return_variant);
170 b->appendBlock(child_b);
171 if (child->getImportance().has_value())
172 {
173 b->addLine(child_return_variant + "->setImportance(" +
174 OptionalFloatLiteral(child->getImportance()) + ");");
175 }
176 b->addLine(OBJECT_MEMBERS_ACCESSOR + ".emplace(\"" + key + "\", " +
177 child_return_variant + ");");
178 }
179 std::vector<std::string> templatesQuoted;
180 std::vector<std::string> templateIntantiationsQuoted;
181 for (const auto& t : type.getTemplates())
182 {
183 templatesQuoted.push_back("\"" + t + "\"");
184 }
185 for (const auto& t : type.getTemplateInstantiations())
186 {
187 templateIntantiationsQuoted.push_back("\"" + t + "\"");
188 }
189
190 b->addLine("return " + ARON_WRITER_ACCESSOR + ".writeObject(\"" + type.getObjectName() +
191 "\", " + "{" + simox::alg::join(templatesQuoted, ", ") + "}, " +
192 ARON_TEMPLATE_INSTANTIATIONS_ACCESSOR + ", " + OBJECT_MEMBERS_ACCESSOR + ", " +
193 OBJECT_EXTENDS_ACCESSOR + ", " + ARON_MAYBE_TYPE_ACCESSOR + ", " +
194 ARON_PATH_ACCESSOR + "); // of top level object " +
196 ;
197
198 return b;
199 }
200
202 ObjectClass::getWriteBlock(const std::string&, const Path& p, std::string&) const
203 {
204 static const std::string OBJECT_MEMBERS_ACCESSOR = ARON_VARIABLE_PREFIX + "_objectMembers";
205 static const std::string OBJECT_EXTENDS_ACCESSOR = ARON_VARIABLE_PREFIX + "_objectExtends";
206
207 CppBlockPtr block_if_data = std::make_shared<CppBlock>();
208
209 block_if_data->addLine("std::map<std::string, _Aron_T> " + OBJECT_MEMBERS_ACCESSOR + ";");
210
211 block_if_data->addLine("std::optional<_Aron_T> " + OBJECT_EXTENDS_ACCESSOR + ";");
212 if (type.getExtends() != nullptr)
213 {
214 const auto extends_s = FromAronType(*type.getExtends());
215 block_if_data->addLine("// write base class of " +
217 block_if_data->addLine(OBJECT_EXTENDS_ACCESSOR + " = " +
218 extends_s->getFullInstantiatedCppTypename() + "::write(" +
219 ARON_WRITER_ACCESSOR + ");");
220 }
221
222 block_if_data->addLine("// members of " + this->getFullInstantiatedCppTypename());
223 for (const auto& [key, child] : type.getDirectMemberTypes())
224 {
225 const auto child_s = FromAronType(*child);
226 std::string child_return_variant;
227
228 Path nextPath = p.withElement(key, true);
229 CppBlockPtr child_b = child_s->getWriteBlock(key, nextPath, child_return_variant);
230 block_if_data->addLine("auto " + child_return_variant + " = " + ARON_WRITER_ACCESSOR +
231 ".writeNull();");
232 block_if_data->appendBlock(child_b);
233 if (child->getImportance().has_value())
234 {
235 block_if_data->addLine("if (" + child_return_variant + ")");
236 block_if_data->addLineAsBlock(child_return_variant + "->setImportance(" +
237 OptionalFloatLiteral(child->getImportance()) + ");");
238 }
239 block_if_data->addLine(OBJECT_MEMBERS_ACCESSOR + ".emplace(\"" + key + "\", " +
240 child_return_variant + ");");
241 }
242
243 block_if_data->addLine("return " + ARON_WRITER_ACCESSOR + ".writeDict(" +
244 OBJECT_MEMBERS_ACCESSOR + ", " + OBJECT_EXTENDS_ACCESSOR + ", " +
245 ARON_PATH_ACCESSOR + "); // of top level object " +
247 return block_if_data;
248 }
249
251 ObjectClass::getReadBlock(const std::string&, const std::string& variantAccessor) const
252 {
253 static const std::string OBJECT_MEMBERS_ACCESSOR = ARON_VARIABLE_PREFIX + "_objectMembers";
254 static const std::string OBJECT_EXTENDS_ACCESSOR = ARON_VARIABLE_PREFIX + "_objectExtends";
255
256 CppBlockPtr block_if_data = std::make_shared<CppBlock>();
257 block_if_data->addLine("std::map<std::string, _Aron_TNonConst> " + OBJECT_MEMBERS_ACCESSOR +
258 ";");
259
260 if (type.getExtends() != nullptr)
261 {
262 const auto extends_s = FromAronType(*type.getExtends());
263 block_if_data->addLine(extends_s->getFullInstantiatedCppTypename() + "::read(" +
264 ARON_READER_ACCESSOR + ", " + variantAccessor + ");");
265 }
266
267 block_if_data->addLine("" + ARON_READER_ACCESSOR + ".readDict(" + variantAccessor + ", " +
268 OBJECT_MEMBERS_ACCESSOR + "); // of top level object " +
270
271 for (const auto& [key, child] : type.getDirectMemberTypes())
272 {
273 const auto child_s = FromAronType(*child);
274 std::string child_accessor = OBJECT_MEMBERS_ACCESSOR + "_" + key + "_iterator";
275 block_if_data->addLine("auto " + child_accessor + " = " + OBJECT_MEMBERS_ACCESSOR +
276 ".find(\"" + key + "\");");
277 block_if_data->addLine(
278 "ARMARX_CHECK_AND_THROW(" + child_accessor + " != " + OBJECT_MEMBERS_ACCESSOR +
279 ".end(), ::armarx::aron::error::AronException(__PRETTY_FUNCTION__, \"Missing "
280 "member '" +
281 key + "' in aron object '" + getFullClassCppTypename() + "'.\"));");
282 block_if_data->appendBlock(child_s->getReadBlock(key, child_accessor + "->second"));
283 }
284 return block_if_data;
285 }
286
288 ObjectClass::getEqualsBlock(const std::string& accessor,
289 const std::string& otherInstanceAccessor) const
290 {
291 CppBlockPtr block_if_data = std::make_shared<CppBlock>();
292 if (type.getExtends() != nullptr)
293 {
294 const auto extends_s = FromAronType(*type.getExtends());
295 block_if_data->addLine("if (not (" + extends_s->getFullInstantiatedCppTypename() +
296 "::operator== (" + otherInstanceAccessor + ")))");
297 block_if_data->addLineAsBlock("return false;");
298 }
299 for (const auto& [key, child] : type.getDirectMemberTypes())
300 {
301 auto child_s = FromAronType(*child);
302 CppBlockPtr b2 = child_s->getEqualsBlock(key, otherInstanceAccessor + "." + key);
303 block_if_data->appendBlock(b2);
304 }
305 return block_if_data;
306 }
307
308 std::pair<std::vector<std::pair<std::string, std::string>>, bool>
309 ObjectClass::getCopyCtorInitializers(const std::string&) const
310 {
311 std::vector<std::pair<std::string, std::string>> ret;
312
313 if (type.getExtends() != nullptr)
314 {
315 const auto extends_s = FromAronType(*type.getExtends());
316 ret.push_back({extends_s->getFullInstantiatedCppTypename(), ARON_OTHER_ACCESSOR});
317 }
318
319 bool anyComplex = false;
320 for (const auto& [key, child] : type.getDirectMemberTypes())
321 {
322 auto child_s = FromAronType(*child);
323 auto initList = child_s->getCopyCtorInitializers(key);
324 simox::alg::append(ret, initList.first);
325 anyComplex = anyComplex || initList.second;
326 }
327 return {ret, anyComplex};
328 }
329} // namespace armarx::aron::codegenerator::cpp::generator
The Path class.
Definition Path.h:36
Path withElement(const std::string &, bool escape=false) const
Definition Path.cpp:163
static const std::string ARON_OTHER_ACCESSOR
Definition Generator.h:170
static std::string OptionalFloatLiteral(const std::optional< float > &)
Definition Generator.cpp:74
static const std::string ARON_READER_ACCESSOR
Definition Generator.h:166
static const std::string ARON_PATH_ACCESSOR
Definition Generator.h:165
static const std::string ARON_VARIABLE_PREFIX
Definition Generator.h:162
static const std::string ARON_WRITER_ACCESSOR
Definition Generator.h:167
static std::unique_ptr< Generator > FromAronType(const type::Variant &)
static const std::string ARON_TEMPLATE_INSTANTIATIONS_ACCESSOR
Definition Generator.h:168
static const std::string ARON_MAYBE_TYPE_ACCESSOR
Definition Generator.h:164
std::pair< std::vector< std::pair< std::string, std::string > >, bool > getCopyCtorInitializers(const std::string &) const final
std::vector< std::string > getRequiredIncludes() const final
CppBlockPtr getEqualsBlock(const std::string &, const std::string &) const final
CppBlockPtr getWriteBlock(const std::string &cppAccessor, const Path &, std::string &) const final
CppBlockPtr getResetHardBlock(const std::string &cppAccessor) const final
CppBlockPtr getReadBlock(const std::string &cppAccessor, const std::string &) const final
CppBlockPtr getWriteTypeBlock(const std::string &typeAccessor, const std::string &cppAccessor, const Path &, std::string &variantAccessor) const final
std::vector< CppFieldPtr > getPublicVariableDeclarations(const std::string &) const final
std::pair< std::vector< std::pair< std::string, std::string > >, bool > getCtorInitializers(const std::string &) const final
CppBlockPtr getResetSoftBlock(const std::string &cppAccessor) const final
SpecializedGeneratorBase(const std::string &instantiatedCppTypename, const std::string &classCppTypename, const std::string &aronDataTypename, const std::string &aronTypeTypename, const type::Object &t)
The Object class.
Definition Object.h:43
A convenience header to include all aron files (full include, not forward declared)
std::shared_ptr< CppBlock > CppBlockPtr
Definition CppBlock.h:37
Definition Impl.cpp:41