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
34  detail::SpecializedGeneratorBase<type::Object, ObjectClass>(
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  {
42  {
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 " +
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 
136  ObjectClass::getWriteTypeBlock(const std::string&,
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  b->addLine(OBJECT_MEMBERS_ACCESSOR + ".emplace(\"" + key + "\", " +
172  child_return_variant + ");");
173  }
174  std::vector<std::string> templatesQuoted;
175  std::vector<std::string> templateIntantiationsQuoted;
176  for (const auto& t : type.getTemplates())
177  {
178  templatesQuoted.push_back("\"" + t + "\"");
179  }
180  for (const auto& t : type.getTemplateInstantiations())
181  {
182  templateIntantiationsQuoted.push_back("\"" + t + "\"");
183  }
184 
185  b->addLine("return " + ARON_WRITER_ACCESSOR + ".writeObject(\"" + type.getObjectName() +
186  "\", " + "{" + simox::alg::join(templatesQuoted, ", ") + "}, " +
187  ARON_TEMPLATE_INSTANTIATIONS_ACCESSOR + ", " + OBJECT_MEMBERS_ACCESSOR + ", " +
188  OBJECT_EXTENDS_ACCESSOR + ", " + ARON_MAYBE_TYPE_ACCESSOR + ", " +
189  ARON_PATH_ACCESSOR + "); // of top level object " +
191  ;
192 
193  return b;
194  }
195 
197  ObjectClass::getWriteBlock(const std::string&, const Path& p, std::string&) const
198  {
199  static const std::string OBJECT_MEMBERS_ACCESSOR = ARON_VARIABLE_PREFIX + "_objectMembers";
200  static const std::string OBJECT_EXTENDS_ACCESSOR = ARON_VARIABLE_PREFIX + "_objectExtends";
201 
202  CppBlockPtr block_if_data = std::make_shared<CppBlock>();
203 
204  block_if_data->addLine("std::map<std::string, _Aron_T> " + OBJECT_MEMBERS_ACCESSOR + ";");
205 
206  block_if_data->addLine("std::optional<_Aron_T> " + OBJECT_EXTENDS_ACCESSOR + ";");
207  if (type.getExtends() != nullptr)
208  {
209  const auto extends_s = FromAronType(*type.getExtends());
210  block_if_data->addLine("// write base class of " +
212  block_if_data->addLine(OBJECT_EXTENDS_ACCESSOR + " = " +
213  extends_s->getFullInstantiatedCppTypename() + "::write(" +
214  ARON_WRITER_ACCESSOR + ");");
215  }
216 
217  block_if_data->addLine("// members of " + this->getFullInstantiatedCppTypename());
218  for (const auto& [key, child] : type.getDirectMemberTypes())
219  {
220  const auto child_s = FromAronType(*child);
221  std::string child_return_variant;
222 
223  Path nextPath = p.withElement(key, true);
224  CppBlockPtr child_b = child_s->getWriteBlock(key, nextPath, child_return_variant);
225  block_if_data->addLine("auto " + child_return_variant + " = " + ARON_WRITER_ACCESSOR +
226  ".writeNull();");
227  block_if_data->appendBlock(child_b);
228  block_if_data->addLine(OBJECT_MEMBERS_ACCESSOR + ".emplace(\"" + key + "\", " +
229  child_return_variant + ");");
230  }
231 
232  block_if_data->addLine("return " + ARON_WRITER_ACCESSOR + ".writeDict(" +
233  OBJECT_MEMBERS_ACCESSOR + ", " + OBJECT_EXTENDS_ACCESSOR + ", " +
234  ARON_PATH_ACCESSOR + "); // of top level object " +
236  return block_if_data;
237  }
238 
240  ObjectClass::getReadBlock(const std::string&, const std::string& variantAccessor) const
241  {
242  static const std::string OBJECT_MEMBERS_ACCESSOR = ARON_VARIABLE_PREFIX + "_objectMembers";
243  static const std::string OBJECT_EXTENDS_ACCESSOR = ARON_VARIABLE_PREFIX + "_objectExtends";
244 
245  CppBlockPtr block_if_data = std::make_shared<CppBlock>();
246  block_if_data->addLine("std::map<std::string, _Aron_TNonConst> " + OBJECT_MEMBERS_ACCESSOR +
247  ";");
248 
249  if (type.getExtends() != nullptr)
250  {
251  const auto extends_s = FromAronType(*type.getExtends());
252  block_if_data->addLine(extends_s->getFullInstantiatedCppTypename() + "::read(" +
253  ARON_READER_ACCESSOR + ", " + variantAccessor + ");");
254  }
255 
256  block_if_data->addLine("" + ARON_READER_ACCESSOR + ".readDict(" + variantAccessor + ", " +
257  OBJECT_MEMBERS_ACCESSOR + "); // of top level object " +
259 
260  for (const auto& [key, child] : type.getDirectMemberTypes())
261  {
262  const auto child_s = FromAronType(*child);
263  std::string child_accessor = OBJECT_MEMBERS_ACCESSOR + "_" + key + "_iterator";
264  block_if_data->addLine("auto " + child_accessor + " = " + OBJECT_MEMBERS_ACCESSOR +
265  ".find(\"" + key + "\");");
266  block_if_data->addLine(
267  "ARMARX_CHECK_AND_THROW(" + child_accessor + " != " + OBJECT_MEMBERS_ACCESSOR +
268  ".end(), ::armarx::aron::error::AronException(__PRETTY_FUNCTION__, \"Missing "
269  "member '" +
270  key + "' in aron object '" + getFullClassCppTypename() + "'.\"));");
271  block_if_data->appendBlock(child_s->getReadBlock(key, child_accessor + "->second"));
272  }
273  return block_if_data;
274  }
275 
277  ObjectClass::getEqualsBlock(const std::string& accessor,
278  const std::string& otherInstanceAccessor) const
279  {
280  CppBlockPtr block_if_data = std::make_shared<CppBlock>();
281  if (type.getExtends() != nullptr)
282  {
283  const auto extends_s = FromAronType(*type.getExtends());
284  block_if_data->addLine("if (not (" + extends_s->getFullInstantiatedCppTypename() +
285  "::operator== (" + otherInstanceAccessor + ")))");
286  block_if_data->addLineAsBlock("return false;");
287  }
288  for (const auto& [key, child] : type.getDirectMemberTypes())
289  {
290  auto child_s = FromAronType(*child);
291  CppBlockPtr b2 = child_s->getEqualsBlock(key, otherInstanceAccessor + "." + key);
292  block_if_data->appendBlock(b2);
293  }
294  return block_if_data;
295  }
296 
297  std::pair<std::vector<std::pair<std::string, std::string>>, bool>
298  ObjectClass::getCopyCtorInitializers(const std::string&) const
299  {
300  std::vector<std::pair<std::string, std::string>> ret;
301 
302  if (type.getExtends() != nullptr)
303  {
304  const auto extends_s = FromAronType(*type.getExtends());
305  ret.push_back({extends_s->getFullInstantiatedCppTypename(), ARON_OTHER_ACCESSOR});
306  }
307 
308  bool anyComplex = false;
309  for (const auto& [key, child] : type.getDirectMemberTypes())
310  {
311  auto child_s = FromAronType(*child);
312  auto initList = child_s->getCopyCtorInitializers(key);
313  simox::alg::append(ret, initList.first);
314  anyComplex = anyComplex || initList.second;
315  }
316  return {ret, anyComplex};
317  }
318 } // namespace armarx::aron::codegenerator::cpp::generator
armarx::aron::codegenerator::cpp::Generator::getFullInstantiatedCppTypename
std::string getFullInstantiatedCppTypename() const
Definition: Generator.cpp:116
armarx::aron::codegenerator::cpp::generator::ObjectClass::getCtorInitializers
std::pair< std::vector< std::pair< std::string, std::string > >, bool > getCtorInitializers(const std::string &) const final
Definition: ObjectClass.cpp:54
armarx::aron::Path::withElement
Path withElement(const std::string &, bool escape=false) const
Definition: Path.cpp:161
armarx::aron::codegenerator::cpp::Generator::ARON_MAYBE_TYPE_ACCESSOR
static const std::string ARON_MAYBE_TYPE_ACCESSOR
Definition: Generator.h:149
armarx::aron::codegenerator::cpp::Generator::getFullClassCppTypename
std::string getFullClassCppTypename() const
Definition: Generator.cpp:168
armarx::aron::ret
ReaderT::InputType T & ret
Definition: rw.h:21
armarx::aron::codegenerator::cpp::generator::ObjectClass::getRequiredIncludes
std::vector< std::string > getRequiredIncludes() const final
Definition: ObjectClass.cpp:72
armarx::aron::codegenerator::cpp::generator::ObjectClass
Definition: ObjectClass.h:31
armarx::aron::type::detail::SpecializedVariantBase::getMaybe
type::Maybe getMaybe() const override
get the maybe type
Definition: SpecializedVariant.h:92
armarx::aron::codegenerator::cpp::generator::ObjectClass::getPublicVariableDeclarations
std::vector< CppFieldPtr > getPublicVariableDeclarations(const std::string &) const final
Definition: ObjectClass.cpp:85
armarx::aron::codegenerator::cpp::Generator::FromAronType
static std::unique_ptr< Generator > FromAronType(const type::Variant &)
Definition: Generator.cpp:91
armarx::aron::codegenerator::cpp::Generator::ARON_VARIABLE_PREFIX
static const std::string ARON_VARIABLE_PREFIX
Definition: Generator.h:147
armarx::aron::type::Object
The Object class.
Definition: Object.h:42
detail
Definition: OpenCVUtil.cpp:127
armarx::CppBlockPtr
std::shared_ptr< CppBlock > CppBlockPtr
Definition: CppBlock.h:35
armarx::aron::type::Object::getExtends
std::shared_ptr< Object > getExtends() const
Definition: Object.cpp:255
armarx::aron::codegenerator::cpp::Generator::ARON_WRITER_ACCESSOR
static const std::string ARON_WRITER_ACCESSOR
Definition: Generator.h:152
ObjectClass.h
armarx::aron::codegenerator::cpp::Generator::ARON_PATH_ACCESSOR
static const std::string ARON_PATH_ACCESSOR
Definition: Generator.h:150
armarx::aron::codegenerator::cpp::generator::ObjectClass::getWriteBlock
CppBlockPtr getWriteBlock(const std::string &cppAccessor, const Path &, std::string &) const final
Definition: ObjectClass.cpp:197
armarx::aron::type::Object::getObjectName
std::string getObjectName() const
Definition: Object.cpp:249
armarx::aron::type::Object::getTemplateInstantiations
std::vector< std::string > getTemplateInstantiations() const
Definition: Object.cpp:243
armarx::aron::Path
The Path class.
Definition: Path.h:36
armarx::aron::codegenerator::cpp::generator::ObjectClass::getResetSoftBlock
CppBlockPtr getResetSoftBlock(const std::string &cppAccessor) const final
Definition: ObjectClass.cpp:98
armarx::aron::error::ValueNotValidException
The ValueNotValidException class.
Definition: Exception.h:145
armarx::aron::codegenerator::cpp::generator::ObjectClass::ObjectClass
ObjectClass(const type::Object &)
Definition: ObjectClass.cpp:33
armarx::aron::codegenerator::cpp::generator::ObjectClass::getEqualsBlock
CppBlockPtr getEqualsBlock(const std::string &, const std::string &) const final
Definition: ObjectClass.cpp:277
armarx::aron::codegenerator::cpp::generator::ObjectClass::getReadBlock
CppBlockPtr getReadBlock(const std::string &cppAccessor, const std::string &) const final
Definition: ObjectClass.cpp:240
armarx::aron::codegenerator::cpp::generator::Object
Definition: Object.h:43
armarx::aron::codegenerator::cpp::Generator::ARON_OTHER_ACCESSOR
static const std::string ARON_OTHER_ACCESSOR
Definition: Generator.h:155
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::aron::codegenerator::cpp::generator::detail::SpecializedGeneratorBase< type::Object, ObjectClass >::type
type::Object type
Definition: SpecializedGenerator.h:52
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:40
armarx::aron::codegenerator::cpp::generator
Definition: AnyObject.cpp:29
armarx::aron::type::Object::getDirectMemberTypes
std::map< std::string, VariantPtr > getDirectMemberTypes() const
Definition: Object.cpp:122
armarx::aron::similarity::FloatSimilarity::NONE
@ NONE
Definition: FloatSimilarity.h:11
armarx::aron::codegenerator::cpp::generator::ObjectClass::getCopyCtorInitializers
std::pair< std::vector< std::pair< std::string, std::string > >, bool > getCopyCtorInitializers(const std::string &) const final
Definition: ObjectClass.cpp:298
armarx::aron::codegenerator::cpp::Generator::ARON_READER_ACCESSOR
static const std::string ARON_READER_ACCESSOR
Definition: Generator.h:151
armarx::aron::codegenerator::cpp::Generator::getInstantiatedCppTypename
std::string getInstantiatedCppTypename() const
Definition: Generator.cpp:110
armarx::aron::codegenerator::cpp::generator::ObjectClass::getResetHardBlock
CppBlockPtr getResetHardBlock(const std::string &cppAccessor) const final
Definition: ObjectClass.cpp:117
armarx::aron::codegenerator::cpp::generator::ObjectClass::getWriteTypeBlock
CppBlockPtr getWriteTypeBlock(const std::string &typeAccessor, const std::string &cppAccessor, const Path &, std::string &variantAccessor) const final
Definition: ObjectClass.cpp:136
simox
Definition: Impl.cpp:40
armarx::aron::codegenerator::cpp::generator::Dict
Definition: Dict.h:31
armarx::aron::type::Object::getTemplates
std::vector< std::string > getTemplates() const
Definition: Object.cpp:237
armarx::aron::type::defaultconversion::string::Maybe2String
const std::map< type::Maybe, std::string > Maybe2String
Definition: Descriptor.h:125
armarx::aron::codegenerator::cpp::Generator::ARON_TEMPLATE_INSTANTIATIONS_ACCESSOR
static const std::string ARON_TEMPLATE_INSTANTIATIONS_ACCESSOR
Definition: Generator.h:153
armarx::aron::type::Variant::getPath
Path getPath() const
Definition: Variant.h:99