Generator.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 // STD/STL
25 
26 #include "Generator.h"
27 
28 #include <SimoxUtility/algorithm/string.h>
29 #include <SimoxUtility/meta/type_name.h>
30 
31 #include "Factory.h"
32 
34 {
35  // constantes
36  const std::string Generator::ARON_VARIABLE_PREFIX = "aron";
37 
38  const std::string Generator::ARON_MAYBE_TYPE_ACCESSOR =
39  Generator::ARON_VARIABLE_PREFIX + "_maybeType";
44  const std::string Generator::ARON_VARIANT_RETURN_ACCESSOR =
48 
49  const SerializerFactoryPtr Generator::FACTORY = SerializerFactoryPtr(new GeneratorFactory());
50 
51  // static methods
52  std::string
53  Generator::EscapeAccessor(const std::string& accessor)
54  {
55  const static std::map<std::string, std::string> ESCAPE_ACCESSORS = {{"->", "_ptr_"},
56  {".", "_dot_"},
57  {"[", "_lbrC_"},
58  {"]", "_rbrC_"},
59  {"(", "_lbrR_"},
60  {")", "_rbrR_"},
61  {"*", "_ast_"}};
62 
63  std::string escaped_accessor = accessor;
64  for (const auto& [key, value] : ESCAPE_ACCESSORS)
65  {
66  escaped_accessor = simox::alg::replace_all(escaped_accessor, key, value);
67  }
68  return escaped_accessor;
69  }
70 
71  std::string
73  {
74  auto cpp = Generator::FromAronType(n);
76  return cpp->getInstantiatedCppTypename();
77  }
78 
79  std::vector<std::string>
80  Generator::ExtractCppTypenames(const std::vector<type::VariantPtr>& n)
81  {
82  std::vector<std::string> ret;
83  for (const auto& v : n)
84  {
85  ret.push_back(ExtractCppTypename(*v));
86  }
87  return ret;
88  }
89 
90  std::unique_ptr<Generator>
92  {
93  return FACTORY->create(n, n.getPath());
94  }
95 
96  // constructors
97  Generator::Generator(const std::string& instantiatedCppTypename,
98  const std::string& classCppTypename,
99  const std::string& aronDataTypename,
100  const std::string& aronTypeTypename) :
101  instantiatedCppTypename(instantiatedCppTypename),
102  classCppTypename(classCppTypename),
103  aronDataTypename(aronDataTypename),
104  aronTypeTypename(aronTypeTypename)
105  {
106  }
107 
108  // public methods
109  std::string
111  {
112  return instantiatedCppTypename;
113  }
114 
115  std::string
117  {
118  switch (getType().getMaybe())
119  {
120  case type::Maybe::NONE:
122  case type::Maybe::OPTIONAL:
123  return "std::optional<" + getInstantiatedCppTypename() + ">";
124  case type::Maybe::RAW_PTR:
125  return getInstantiatedCppTypename() + "*";
126  case type::Maybe::SHARED_PTR:
127  return "std::shared_ptr<" + getInstantiatedCppTypename() + ">";
128  case type::Maybe::UNIQUE_PTR:
129  return "std::unique_ptr<" + getInstantiatedCppTypename() + ">";
130  default:
131  throw error::ValueNotValidException(__PRETTY_FUNCTION__,
132  "Received unknown maybe enum",
133  std::to_string((int)getType().getMaybe()),
134  getType().getPath());
135  }
136  }
137 
138  std::string
140  {
141  switch (getType().getMaybe())
142  {
143  case type::Maybe::NONE:
145  case type::Maybe::OPTIONAL:
146  return "std::make_optional<" + getInstantiatedCppTypename() + ">";
147  case type::Maybe::RAW_PTR:
148  return getInstantiatedCppTypename() + "*";
149  case type::Maybe::SHARED_PTR:
150  return "std::make_shared<" + getInstantiatedCppTypename() + ">";
151  case type::Maybe::UNIQUE_PTR:
152  return "std::make_unique<" + getInstantiatedCppTypename() + ">";
153  default:
154  throw error::ValueNotValidException(__PRETTY_FUNCTION__,
155  "Received unknown maybe enum",
156  std::to_string((int)getType().getMaybe()),
157  getType().getPath());
158  }
159  }
160 
161  std::string
163  {
164  return classCppTypename;
165  }
166 
167  std::string
169  {
170  switch (getType().getMaybe())
171  {
172  case type::Maybe::NONE:
173  return getClassCppTypename();
174  case type::Maybe::OPTIONAL:
175  return "std::optional<" + getClassCppTypename() + ">";
176  case type::Maybe::RAW_PTR:
177  return getClassCppTypename() + "*";
178  case type::Maybe::SHARED_PTR:
179  return "std::shared_ptr<" + getClassCppTypename() + ">";
180  case type::Maybe::UNIQUE_PTR:
181  return "std::unique_ptr<" + getClassCppTypename() + ">";
182  default:
183  throw error::ValueNotValidException(__PRETTY_FUNCTION__,
184  "Received unknown maybe enum",
185  std::to_string((int)getType().getMaybe()),
186  getType().getPath());
187  }
188  }
189 
190  CppCtorPtr
191  Generator::toCtor(const std::string& name) const
192  {
193  CppBlockPtr b = this->getCtorBlock("");
194  auto initList = this->getCtorInitializers("");
195 
196  if (b->size() > 0 || initList.second)
197  {
198  CppCtorPtr c = CppCtorPtr(new CppCtor(name + "()"));
199  c->addInitListEntries(initList.first);
200  c->setBlock(b);
201  return c;
202  }
203 
204  CppCtorPtr c = CppCtorPtr(new CppCtor(name + "() = default;"));
205  return c;
206  }
207 
208  CppCtorPtr
209  Generator::toCopyCtor(const std::string& name) const
210  {
211  CppBlockPtr b = this->getCopyCtorBlock("");
212  auto initList = this->getCopyCtorInitializers("");
213 
214  if (b->size() > 0 || initList.second)
215  {
216  CppCtorPtr c =
217  CppCtorPtr(new CppCtor(name + "(const " + name + "& " + ARON_OTHER_ACCESSOR + ")"));
218  c->addInitListEntries(initList.first);
219  c->setBlock(b);
220  return c;
221  }
222 
224  new CppCtor(name + "(const " + name + "& " + ARON_OTHER_ACCESSOR + ") = default;"));
225  return c;
226  }
227 
229  Generator::toDtor(const std::string& name) const
230  {
231  CppBlockPtr b = this->getDtorBlock("");
232  if (b->size() > 0)
233  {
234  CppMethodPtr m = CppMethodPtr(new CppMethod("virtual ~" + name + "()"));
235  m->setBlock(b);
236 
237  m->setEnforceBlockGeneration(true);
238  return m;
239  }
240 
241  CppMethodPtr m = CppMethodPtr(new CppMethod("virtual ~" + name + "() = default;"));
242  m->setEnforceBlockGeneration(false);
243  return m;
244  }
245 
248  {
249  std::stringstream doc;
250  doc << "@brief resetSoft() - This method resets all member variables with respect to the "
251  "current parameterization. \n";
252  doc << "@return - nothing";
253 
254  CppMethodPtr m =
255  CppMethodPtr(new CppMethod("virtual void resetSoft() override", doc.str()));
256  CppBlockPtr b = this->getResetSoftBlock("");
257  m->setBlock(b);
258 
259  m->setEnforceBlockGeneration(true);
260  return m;
261  }
262 
265  {
266  std::stringstream doc;
267  doc << "@brief resetHard() - This method resets member variables according to the XML type "
268  "description. \n";
269  doc << "@return - nothing";
270 
271  CppMethodPtr m =
272  CppMethodPtr(new CppMethod("virtual void resetHard() override", doc.str()));
273  CppBlockPtr b = this->getResetHardBlock("");
274  m->setBlock(b);
275 
276  m->setEnforceBlockGeneration(true);
277  return m;
278  }
279 
282  {
283  std::stringstream doc;
284  doc << "@brief writeType() - This method returns a new type from the class structure using "
285  "a type writer implementation. This function is static. \n";
286  doc << "@return - the result of the writer implementation";
287 
289  "template<class WriterT>\nstatic typename WriterT::ReturnType writeType(WriterT& " +
290  ARON_WRITER_ACCESSOR + ", std::vector<std::string> " +
291  ARON_TEMPLATE_INSTANTIATIONS_ACCESSOR + " = {}, armarx::aron::type::Maybe " +
293  " = ::armarx::aron::type::Maybe::NONE, const ::armarx::aron::Path& " +
294  ARON_PATH_ACCESSOR + " = ::armarx::aron::Path())",
295  doc.str()));
296  CppBlockPtr b = std::make_shared<CppBlock>();
297  b->addLine("using _Aron_T [[maybe_unused]] = typename WriterT::ReturnType;");
298 
299  std::string dummy;
300  b->appendBlock(this->getWriteTypeBlock("", "", Path(), dummy));
301  m->setBlock(b);
302 
303  m->setEnforceBlockGeneration(true);
304  return m;
305  }
306 
309  {
310  std::stringstream doc;
311  doc << "@brief write() - This method returns a new type from the member data types using a "
312  "data writer implementation. \n";
313  doc << "@param w - The writer implementation\n";
314  doc << "@return - the result of the writer implementation";
315 
317  new CppMethod("template<class WriterT>\ntypename WriterT::ReturnType write(WriterT& " +
318  ARON_WRITER_ACCESSOR + ", const ::armarx::aron::Path& " +
319  ARON_PATH_ACCESSOR + " = armarx::aron::Path()) const",
320  doc.str()));
321  CppBlockPtr b = std::make_shared<CppBlock>();
322  b->addLine("using _Aron_T [[maybe_unused]] = typename WriterT::ReturnType;");
323 
324  b->addLine("try");
325  std::string dummy;
326  b->addBlock(this->getWriteBlock("", Path(), dummy));
327  b->addLine("catch(const std::exception& " + ARON_VARIABLE_PREFIX + "_e)");
328  b->addLineAsBlock("throw ::armarx::aron::error::AronException(__PRETTY_FUNCTION__, "
329  "std::string(\"An error occured during the write method of an aron "
330  "generated class. The full error log was:\\n\") + " +
331  ARON_VARIABLE_PREFIX + "_e.what());");
332 
333  m->setBlock(b);
334  m->setEnforceBlockGeneration(true);
335  return m;
336  }
337 
340  {
341  std::stringstream doc;
342  doc << "@brief read() - This method sets the struct members to new values given in a data "
343  "reader implementation. \n";
344  doc << "@param r - The reader implementation\n";
345  doc << "@return - nothing";
346 
347  CppMethodPtr m = CppMethodPtr(new CppMethod("template<class ReaderT>\nvoid read(ReaderT& " +
349  ", typename ReaderT::InputType& input)",
350  doc.str()));
351  CppBlockPtr b = std::make_shared<CppBlock>();
352  b->addLine("using _Aron_T [[maybe_unused]] = typename ReaderT::InputType;");
353  b->addLine("using _Aron_TNonConst [[maybe_unused]] = typename ReaderT::InputTypeNonConst;");
354 
355  b->addLine("this->resetSoft();");
356  b->addLine("ARMARX_CHECK_AND_THROW(!" + ARON_READER_ACCESSOR +
357  ".readNull(input), ::armarx::aron::error::AronException(__PRETTY_FUNCTION__, "
358  "\"The input to the read method must not be null.\"));");
359 
360  b->addLine("try");
361  b->addBlock(this->getReadBlock("", "input"));
362  b->addLine("catch(const std::exception& " + ARON_VARIABLE_PREFIX + "_e)");
363  b->addLineAsBlock("throw ::armarx::aron::error::AronException(__PRETTY_FUNCTION__, "
364  "std::string(\"An error occured during the read method of an aron "
365  "generated class. The full error log was:\\n\") + " +
366  ARON_VARIABLE_PREFIX + "_e.what());");
367  m->setBlock(b);
368 
369  m->setEnforceBlockGeneration(true);
370  return m;
371  }
372 
375  {
376  std::stringstream doc;
377  doc << "@brief " << info.methodName
378  << "() - This method returns a new data from the member data types using a writer "
379  "implementation. \n";
380  doc << "@return - the result of the writer implementation";
381 
382  CppMethodPtr m =
383  CppMethodPtr(new CppMethod(info.returnType + " " + info.methodName + "() const" +
384  (info.override ? " override" : ""),
385  doc.str()));
386  m->addLine(info.writerClassType + " writer;");
387  m->addLine("return " + info.enforceConversion + "(this->write(writer))" +
388  info.enforceMemberAccess + ";");
389 
390  m->setEnforceBlockGeneration(true);
391  return m;
392  }
393 
396  {
397  std::stringstream doc;
398  doc << "@brief " << info.methodName
399  << " - This method sets the struct members to new values given in a reader "
400  "implementation. \n";
401  doc << "@return - nothing";
402 
403  CppMethodPtr m =
404  CppMethodPtr(new CppMethod("void " + info.methodName + "(const " + info.argumentType +
405  "& input)" + (info.override ? " override" : ""),
406  doc.str()));
407  m->addLine(info.readerClassType + " reader;");
408  m->addLine("this->read(reader, " + info.enforceConversion + "(input)" +
409  info.enforceMemberAccess + ");");
410 
411  m->setEnforceBlockGeneration(true);
412  return m;
413  }
414 
417  {
418  std::stringstream doc;
419  doc << "@brief " << info.methodName
420  << "() - This method sets the struct members to new values given in a reader "
421  "implementation. \n";
422  doc << "@return - nothing";
423 
424  CppMethodPtr m =
425  CppMethodPtr(new CppMethod("static " + info.returnType + " " + info.methodName +
426  "(const " + info.argumentType + "& input)",
427  doc.str()));
428  m->addLine(info.returnType + " t;");
429  m->addLine("t.fromAron(input);");
430  m->addLine("return t;");
431 
432  m->setEnforceBlockGeneration(true);
433  return m;
434  }
435 
438  {
439  std::stringstream doc;
440  doc << "@brief " << info.methodName
441  << "() - This method returns a new type from the member data types using a writer "
442  "implementation. \n";
443  doc << "@return - the result of the writer implementation";
444 
446  new CppMethod("static " + info.returnType + " " + info.methodName + "()", doc.str()));
447  m->addLine(info.writerClassType + " writer;");
448  m->addLine("return " + info.enforceConversion + "(writeType(writer))" +
449  info.enforceMemberAccess + ";");
450 
451  m->setEnforceBlockGeneration(true);
452  return m;
453  }
454 
457  {
458  std::stringstream doc;
459  doc << "@brief operator==() - This method checks whether all values equal another "
460  "instance. \n";
461  doc << "@param i - The other instance\n";
462  doc << "@return - true, if all members are the same, false otherwise";
463 
465  "bool operator==(const " + this->getFullClassCppTypename() + "& i) const", doc.str()));
466  CppBlockPtr b = this->getEqualsBlock("", "i");
467  b->addLine("return true;");
468  m->setBlock(b);
469 
470  m->setEnforceBlockGeneration(true);
471  return m;
472  }
473 
474  // defaulted implementations of the blocks
475  std::vector<std::string>
477  {
478  return {};
479  }
480 
481  std::vector<CppFieldPtr>
482  Generator::getPublicVariableDeclarations(const std::string& name) const
483  {
484  auto field = std::make_shared<CppField>(this->getFullInstantiatedCppTypename(), name);
485  return {field};
486  }
487 
488  std::pair<std::vector<std::pair<std::string, std::string>>, bool>
489  Generator::getCtorInitializers(const std::string& name) const
490  {
491  return {{}, false};
492  }
493 
495  Generator::getCtorBlock(const std::string&) const
496  {
497  return std::make_shared<CppBlock>();
498  }
499 
500  std::pair<std::vector<std::pair<std::string, std::string>>, bool>
501  Generator::getCopyCtorInitializers(const std::string& name) const
502  {
503  const auto& t = getType();
504  if (t.getMaybe() == type::Maybe::UNIQUE_PTR) // unique ptrs cant be copied
505  {
506  return {{{name,
507  ARON_OTHER_ACCESSOR + "." + name + " ? " +
508  resolveMaybeGenerator("*" + ARON_OTHER_ACCESSOR + "." + name) +
509  " : nullptr"}},
510  true};
511  }
512 
513  return {{{name, ARON_OTHER_ACCESSOR + "." + name}}, false};
514  }
515 
517  Generator::getCopyCtorBlock(const std::string&) const
518  {
519  return std::make_shared<CppBlock>();
520  }
521 
523  Generator::getDtorBlock(const std::string&) const
524  {
525  return std::make_shared<CppBlock>();
526  }
527 
529  Generator::getResetHardBlock(const std::string& cppAccessor) const
530  {
531  CppBlockPtr block_if_data = std::make_shared<CppBlock>();
532  block_if_data->addLine(cppAccessor + " = " + this->getFullInstantiatedCppTypename() +
533  "();");
534  return resolveMaybeResetHardBlock(block_if_data, cppAccessor);
535  }
536 
538  Generator::getResetSoftBlock(const std::string& cppAccessor) const
539  {
540  auto block_if_data = std::make_shared<CppBlock>();
541  block_if_data->addLine(cppAccessor + " = " + this->getFullInstantiatedCppTypename() +
542  "();");
543  return this->resolveMaybeResetSoftBlock(block_if_data, cppAccessor);
544  }
545 
547  Generator::getEqualsBlock(const std::string& accessor,
548  const std::string& otherInstanceAccessor) const
549  {
550  CppBlockPtr block_if_data = std::make_shared<CppBlock>();
551  std::string resolved_accessor = this->resolveMaybeAccessor(accessor);
552  std::string other_instance_resolved_accessor =
553  this->resolveMaybeAccessor(otherInstanceAccessor);
554 
555  block_if_data->addLine("if (not (" + resolved_accessor +
556  " == " + other_instance_resolved_accessor + "))");
557  block_if_data->addLineAsBlock("return false;");
558  return resolveMaybeEqualsBlock(block_if_data, accessor, otherInstanceAccessor);
559  }
560 
561  // Helper methods
562  std::string
563  Generator::resolveMaybeAccessor(const std::string& s) const
564  {
565  const auto& t = getType();
566  if (t.getMaybe() == type::Maybe::OPTIONAL)
567  {
568  return s + ".value()";
569  }
570  if (t.getMaybe() == type::Maybe::RAW_PTR || t.getMaybe() == type::Maybe::SHARED_PTR ||
571  t.getMaybe() == type::Maybe::UNIQUE_PTR)
572  {
573  return "*" + s;
574  }
575  return s;
576  }
577 
578  std::string
579  Generator::resolveMaybeGenerator(const std::string& args) const
580  {
581  const auto& t = getType();
582  if (t.getMaybe() == type::Maybe::OPTIONAL)
583  {
584  return "std::make_optional<" + getInstantiatedCppTypename() + ">(" + args + ")";
585  }
586  if (t.getMaybe() == type::Maybe::RAW_PTR)
587  {
588  return "new " + getInstantiatedCppTypename() + "(" + args + ")";
589  }
590  if (t.getMaybe() == type::Maybe::SHARED_PTR)
591  {
592  return "std::make_shared<" + getInstantiatedCppTypename() + ">(" + args + ")";
593  }
594  if (t.getMaybe() == type::Maybe::UNIQUE_PTR)
595  {
596  return "std::make_unique<" + getInstantiatedCppTypename() + ">(" + args + ")";
597  }
598  return "";
599  }
600 
601  std::string
602  Generator::resolveMaybeGeneratorWithSetter(const std::string& s, const std::string& args) const
603  {
604  auto gen = resolveMaybeGenerator(args);
605  if (!gen.empty())
606  {
607  return s + " = " + gen + ";";
608  }
609  return "";
610  }
611 
612  std::string
614  {
615  const auto& type = getType();
616  switch (type.getMaybe())
617  {
618  case type::Maybe::NONE:
619  return ".";
620  case type::Maybe::OPTIONAL: //[[fallthrough]];
621  case type::Maybe::RAW_PTR: //[[fallthrough]];
622  case type::Maybe::SHARED_PTR: //[[fallthrough]];
623  case type::Maybe::UNIQUE_PTR:
624  return "->";
625  }
626  throw error::ValueNotValidException(__PRETTY_FUNCTION__,
627  "Received unknown maybe enum",
628  std::to_string((int)type.getMaybe()),
629  type.getPath());
630  }
631 
632  std::string
633  Generator::toPointerAccessor(const std::string& cppAccessor) const
634  {
635  const auto& type = getType();
636  switch (type.getMaybe())
637  {
638  case type::Maybe::RAW_PTR:
639  return cppAccessor;
640  case type::Maybe::SHARED_PTR: //[[fallthrough]];
641  case type::Maybe::UNIQUE_PTR:
642  return cppAccessor + ".get()";
643 
644  case type::Maybe::NONE: //[[fallthrough]];
645  case type::Maybe::OPTIONAL:
646  break;
647  }
648  throw error::ValueNotValidException(__PRETTY_FUNCTION__,
649  "Received invalid maybe enum (not a pointer?)",
650  std::to_string((int)type.getMaybe()),
651  type.getPath());
652  }
653 
656  const std::string& cppAccessor) const
657  {
658  const auto& type = getType();
659  if (type.getMaybe() == type::Maybe::OPTIONAL)
660  {
661  CppBlockPtr b = std::make_shared<CppBlock>();
662  b->addLine(cppAccessor + " = std::nullopt;");
663  return b;
664  }
665  else if (type.getMaybe() == type::Maybe::RAW_PTR ||
666  type.getMaybe() == type::Maybe::SHARED_PTR ||
667  type.getMaybe() == type::Maybe::UNIQUE_PTR)
668  {
669  CppBlockPtr b = std::make_shared<CppBlock>();
670  b->addLine(cppAccessor + " = nullptr;");
671  return b;
672  }
673  return block_if_data;
674  }
675 
678  const std::string& cppAccessor) const
679  {
680  const auto& type = getType();
681  if (type.getMaybe() == type::Maybe::OPTIONAL || type.getMaybe() == type::Maybe::RAW_PTR ||
682  type.getMaybe() == type::Maybe::SHARED_PTR ||
683  type.getMaybe() == type::Maybe::UNIQUE_PTR)
684  {
685  CppBlockPtr b = std::make_shared<CppBlock>();
686  b->addLine("if (" + cppAccessor + ") // if " + cppAccessor + " contains data");
687  b->addBlock(block_if_data);
688  return b;
689  }
690  return block_if_data;
691  }
692 
695  const std::string& cppAccessor) const
696  {
697  const auto& type = getType();
698  if (type.getMaybe() == type::Maybe::OPTIONAL || type.getMaybe() == type::Maybe::RAW_PTR ||
699  type.getMaybe() == type::Maybe::SHARED_PTR ||
700  type.getMaybe() == type::Maybe::UNIQUE_PTR)
701  {
702  CppBlockPtr b = std::make_shared<CppBlock>();
703  b->addLine("if (" + cppAccessor + ") // if " + cppAccessor + " contains data");
704  b->addBlock(block_if_data);
705  return b;
706  }
707  return block_if_data;
708  }
709 
712  const std::string& cppAccessor,
713  const std::string& variantAccessor) const
714  {
715  const auto& type = getType();
716  if (type.getMaybe() == type::Maybe::OPTIONAL || type.getMaybe() == type::Maybe::RAW_PTR ||
717  type.getMaybe() == type::Maybe::SHARED_PTR ||
718  type.getMaybe() == type::Maybe::UNIQUE_PTR)
719  {
720  CppBlockPtr b = std::make_shared<CppBlock>();
721  b->addLine("if (not (" + ARON_READER_ACCESSOR + ".readNull(" + variantAccessor +
722  "))) // if aron contains data");
723  {
724  CppBlockPtr ifb = std::make_shared<CppBlock>();
725  ifb->addLine(resolveMaybeGeneratorWithSetter(cppAccessor));
726  ifb->appendBlock(block_if_data);
727  b->addBlock(ifb);
728  }
729  return b;
730  }
731  return block_if_data;
732  }
733 
736  const std::string& accessor,
737  const std::string& otherInstanceAccessor) const
738  {
739  const auto& type = getType();
740  if (type.getMaybe() == type::Maybe::RAW_PTR || type.getMaybe() == type::Maybe::SHARED_PTR || type.getMaybe() == type::Maybe::UNIQUE_PTR)
741  {
742  CppBlockPtr b = std::make_shared<CppBlock>();
743  b->addLine("if (not (static_cast<bool>(" + accessor + ") == static_cast<bool>(" + otherInstanceAccessor + "))) // check if both contain data");
744  b->addLineAsBlock("return false;");
745  b->addLine("if (static_cast<bool>(" + accessor + ") && static_cast<bool>(" + otherInstanceAccessor + "))");
746  b->addBlock(block_if_data);
747  return b;
748  }
749 
750  if (type.getMaybe() == type::Maybe::OPTIONAL)
751  {
752  CppBlockPtr b = std::make_shared<CppBlock>();
753  b->addLine("if (not ( " + accessor + ".has_value() == " + otherInstanceAccessor + ".has_value())) // check if both contain data");
754  b->addLineAsBlock("return false;");
755  b->addLine("if ( " + accessor + ".has_value() && " + otherInstanceAccessor + ".has_value())");
756  b->addBlock(block_if_data);
757  return b;
758  }
759  return block_if_data;
760  }
761 } // namespace armarx::aron::codegenerator::cpp
armarx::aron::codegenerator::cpp::Generator::getFullInstantiatedCppTypename
std::string getFullInstantiatedCppTypename() const
Definition: Generator.cpp:116
armarx::aron::codegenerator::cpp::Generator::toResetHardMethod
CppMethodPtr toResetHardMethod() const
Definition: Generator.cpp:264
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
Definition: AnyObject.cpp:29
armarx::aron::codegenerator::cpp::Generator::ARON_VARIANT_RETURN_ACCESSOR
static const std::string ARON_VARIANT_RETURN_ACCESSOR
Definition: Generator.h:154
armarx::aron::codegenerator::cpp::Generator::getFullClassCppTypename
std::string getFullClassCppTypename() const
Definition: Generator.cpp:168
armarx::aron::codegenerator::ReaderInfo::argumentType
std::string argumentType
Definition: ReaderInfo.h:35
armarx::aron::ret
ReaderT::InputType T & ret
Definition: rw.h:21
armarx::aron::codegenerator::ReaderInfo::methodName
std::string methodName
Definition: ReaderInfo.h:34
armarx::aron::codegenerator::WriterInfo::writerClassType
std::string writerClassType
Definition: WriterInfo.h:36
armarx::aron::codegenerator::cpp::Generator::toCtor
CppCtorPtr toCtor(const std::string &) const
Definition: Generator.cpp:191
armarx::aron::codegenerator::cpp::Generator::getReadBlock
virtual CppBlockPtr getReadBlock(const std::string &cppAccessor, const std::string &variantAccessor) const =0
armarx::aron::codegenerator::cpp::Generator::getCopyCtorInitializers
virtual std::pair< std::vector< std::pair< std::string, std::string > >, bool > getCopyCtorInitializers(const std::string &) const
Definition: Generator.cpp:501
armarx::CppCtorPtr
std::shared_ptr< CppCtor > CppCtorPtr
Definition: CppCtor.h:31
armarx::aron::codegenerator::cpp::Generator::toSpecializedDataWriterMethod
CppMethodPtr toSpecializedDataWriterMethod(const WriterInfo &info) const
Definition: Generator.cpp:374
armarx::aron::codegenerator::WriterInfo::enforceMemberAccess
std::string enforceMemberAccess
Definition: WriterInfo.h:39
cpp
IceStorm Admin cpp
Definition: CMakeLists.txt:87
ARMARX_CHECK_NOT_NULL
#define ARMARX_CHECK_NOT_NULL(ptr)
This macro evaluates whether ptr is not null and if it turns out to be false it will throw an Express...
Definition: ExpressionException.h:206
armarx::aron::codegenerator::cpp::Generator::FromAronType
static std::unique_ptr< Generator > FromAronType(const type::Variant &)
Definition: Generator.cpp:91
armarx::aron::codegenerator::cpp::Generator::EscapeAccessor
static std::string EscapeAccessor(const std::string &)
Definition: Generator.cpp:53
armarx::aron::codegenerator::WriterInfo::returnType
std::string returnType
Definition: WriterInfo.h:35
armarx::aron::codegenerator::cpp::Generator::resolveMaybeGeneratorWithSetter
std::string resolveMaybeGeneratorWithSetter(const std::string &, const std::string &args="") const
Definition: Generator.cpp:602
armarx::aron::codegenerator::cpp::SerializerFactoryPtr
std::shared_ptr< GeneratorFactory > SerializerFactoryPtr
Definition: Generator.h:59
armarx::aron::codegenerator::cpp::Generator::ARON_VARIABLE_PREFIX
static const std::string ARON_VARIABLE_PREFIX
Definition: Generator.h:147
armarx::aron::codegenerator::cpp::Generator::resolveMaybeAccessor
std::string resolveMaybeAccessor(const std::string &) const
Definition: Generator.cpp:563
armarx::CppBlockPtr
std::shared_ptr< CppBlock > CppBlockPtr
Definition: CppBlock.h:35
armarx::aron::codegenerator::cpp::Generator::ARON_WRITER_ACCESSOR
static const std::string ARON_WRITER_ACCESSOR
Definition: Generator.h:152
armarx::aron::codegenerator::cpp::Generator::toResetSoftMethod
CppMethodPtr toResetSoftMethod() const
Definition: Generator.cpp:247
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::aron::codegenerator::cpp::Generator::Generator
Generator()=delete
armarx::aron::codegenerator::ReaderInfo::readerClassType
std::string readerClassType
Definition: ReaderInfo.h:36
armarx::aron::codegenerator::cpp::Generator::getEqualsBlock
virtual CppBlockPtr getEqualsBlock(const std::string &cppAccessorThis, const std::string &cppAccessorOther) const
Definition: Generator.cpp:547
armarx::aron::codegenerator::cpp::Generator::toDtor
CppMethodPtr toDtor(const std::string &) const
Definition: Generator.cpp:229
armarx::aron::codegenerator::cpp::Generator::ARON_PATH_ACCESSOR
static const std::string ARON_PATH_ACCESSOR
Definition: Generator.h:150
armarx::aron::codegenerator::ReaderInfo
Definition: ReaderInfo.h:32
armarx::aron::codegenerator::cpp::Generator::toWriteTypeMethod
CppMethodPtr toWriteTypeMethod() const
Definition: Generator.cpp:281
armarx::aron::codegenerator::StaticReaderInfo::returnType
std::string returnType
Definition: ReaderInfo.h:48
Generator.h
armarx::aron::codegenerator::WriterInfo::enforceConversion
std::string enforceConversion
Definition: WriterInfo.h:38
armarx::aron::type::Variant
The Variant class.
Definition: Variant.h:59
armarx::aron::codegenerator::cpp::Generator::resolveMaybeGenerator
std::string resolveMaybeGenerator(const std::string &args="") const
Definition: Generator.cpp:579
armarx::aron::codegenerator::cpp::Generator::toSpecializedStaticDataReaderMethod
CppMethodPtr toSpecializedStaticDataReaderMethod(const StaticReaderInfo &info) const
Definition: Generator.cpp:416
armarx::aron::error::ValueNotValidException
The ValueNotValidException class.
Definition: Exception.h:145
armarx::aron::codegenerator::StaticReaderInfo::methodName
std::string methodName
Definition: ReaderInfo.h:46
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::aron::codegenerator::cpp::Generator::ARON_OTHER_ACCESSOR
static const std::string ARON_OTHER_ACCESSOR
Definition: Generator.h:155
armarx::aron::codegenerator::cpp::Generator::getFullInstantiatedCppTypenameGenerator
std::string getFullInstantiatedCppTypenameGenerator() const
Definition: Generator.cpp:139
armarx::aron::codegenerator::cpp::Generator::getCtorBlock
virtual CppBlockPtr getCtorBlock(const std::string &) const
Definition: Generator.cpp:495
armarx::CppMethodPtr
std::shared_ptr< CppMethod > CppMethodPtr
Definition: CppMethod.h:32
armarx::aron::codegenerator::cpp::Generator::getRequiredIncludes
virtual std::vector< std::string > getRequiredIncludes() const
Definition: Generator.cpp:476
armarx::aron::codegenerator::cpp::Generator::toReadMethod
CppMethodPtr toReadMethod() const
Definition: Generator.cpp:339
armarx::aron::codegenerator::cpp::Generator::getPublicVariableDeclarations
virtual std::vector< CppFieldPtr > getPublicVariableDeclarations(const std::string &) const
Definition: Generator.cpp:482
armarx::aron::codegenerator::cpp::Generator::getCopyCtorBlock
virtual CppBlockPtr getCopyCtorBlock(const std::string &) const
Definition: Generator.cpp:517
armarx::CppCtor
Definition: CppCtor.h:33
armarx::aron::codegenerator::ReaderInfo::override
bool override
Definition: ReaderInfo.h:40
armarx::aron::codegenerator::cpp::Generator::toEqualsMethod
CppMethodPtr toEqualsMethod() const
Definition: Generator.cpp:456
armarx::aron::codegenerator::cpp::Generator::resolveMaybeReadBlock
CppBlockPtr resolveMaybeReadBlock(const CppBlockPtr &, const std::string &, const std::string &) const
Definition: Generator.cpp:711
armarx::aron::codegenerator::ReaderInfo::enforceMemberAccess
std::string enforceMemberAccess
Definition: ReaderInfo.h:39
armarx::aron::codegenerator::StaticReaderInfo::argumentType
std::string argumentType
Definition: ReaderInfo.h:47
armarx::aron::codegenerator::cpp::Generator::toPointerAccessor
std::string toPointerAccessor(const std::string &) const
Definition: Generator.cpp:633
armarx::aron::codegenerator::WriterInfo::methodName
std::string methodName
Definition: WriterInfo.h:34
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:40
armarx::aron::codegenerator::cpp::Generator::resolveMaybeResetHardBlock
CppBlockPtr resolveMaybeResetHardBlock(const CppBlockPtr &, const std::string &) const
Definition: Generator.cpp:655
armarx::aron::codegenerator::ReaderInfo::enforceConversion
std::string enforceConversion
Definition: ReaderInfo.h:38
armarx::aron::codegenerator::cpp::Generator::resolveMaybeEqualsBlock
CppBlockPtr resolveMaybeEqualsBlock(const CppBlockPtr &, const std::string &, const std::string &) const
Definition: Generator.cpp:735
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
armarx::aron::codegenerator::StaticReaderInfo
Definition: ReaderInfo.h:44
armarx::aron::similarity::FloatSimilarity::NONE
@ NONE
Definition: FloatSimilarity.h:11
armarx::aron::codegenerator::cpp::Generator::ExtractCppTypename
static std::string ExtractCppTypename(const type::Variant &)
Definition: Generator.cpp:72
armarx::aron::codegenerator::WriterInfo::override
bool override
Definition: WriterInfo.h:40
armarx::aron::codegenerator::cpp::Generator::getDtorBlock
virtual CppBlockPtr getDtorBlock(const std::string &) const
Definition: Generator.cpp:523
armarx::aron::codegenerator::cpp::Generator::getType
virtual const type::Variant & getType() const =0
armarx::aron::codegenerator::cpp::Generator::getCtorInitializers
virtual std::pair< std::vector< std::pair< std::string, std::string > >, bool > getCtorInitializers(const std::string &) const
Definition: Generator.cpp:489
armarx::aron::codegenerator::cpp::Generator::ExtractCppTypenames
static std::vector< std::string > ExtractCppTypenames(const std::vector< type::VariantPtr > &)
Definition: Generator.cpp:80
armarx::aron::codegenerator::cpp::Generator::toSpecializedDataReaderMethod
CppMethodPtr toSpecializedDataReaderMethod(const ReaderInfo &info) const
Definition: Generator.cpp:395
armarx::armem::server::ltm::detail::mixin::Path
std::filesystem::path Path
Definition: DiskStorageMixin.h:17
armarx::aron::codegenerator::cpp::Generator::toSpecializedTypeWriterMethod
CppMethodPtr toSpecializedTypeWriterMethod(const WriterInfo &info) const
Definition: Generator.cpp:437
armarx::aron::codegenerator::cpp::Generator::nextEl
std::string nextEl() const
Definition: Generator.cpp:613
armarx::aron::codegenerator::cpp::Generator::resolveMaybeResetSoftBlock
CppBlockPtr resolveMaybeResetSoftBlock(const CppBlockPtr &, const std::string &) const
Definition: Generator.cpp:677
armarx::aron::codegenerator::cpp::Generator::ARON_READER_ACCESSOR
static const std::string ARON_READER_ACCESSOR
Definition: Generator.h:151
armarx::aron::codegenerator::cpp::Generator::getWriteBlock
virtual CppBlockPtr getWriteBlock(const std::string &cppAccessor, const Path &, std::string &variantAccessor) const =0
armarx::CppMethod
Definition: CppMethod.h:34
armarx::aron::codegenerator::cpp::Generator::toWriteMethod
CppMethodPtr toWriteMethod() const
Definition: Generator.cpp:308
armarx::aron::codegenerator::cpp::Generator::getResetHardBlock
virtual CppBlockPtr getResetHardBlock(const std::string &cppAccessor) const
Definition: Generator.cpp:529
armarx::aron::codegenerator::cpp::Generator::getClassCppTypename
std::string getClassCppTypename() const
Definition: Generator.cpp:162
armarx::aron::codegenerator::cpp::Generator::getInstantiatedCppTypename
std::string getInstantiatedCppTypename() const
Definition: Generator.cpp:110
armarx::aron::codegenerator::cpp::Generator::toCopyCtor
CppCtorPtr toCopyCtor(const std::string &) const
Definition: Generator.cpp:209
armarx::aron::codegenerator::cpp::Generator::getResetSoftBlock
virtual CppBlockPtr getResetSoftBlock(const std::string &cppAccessor) const
Definition: Generator.cpp:538
Factory.h
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
armarx::aron::codegenerator::cpp::Generator::resolveMaybeWriteBlock
CppBlockPtr resolveMaybeWriteBlock(const CppBlockPtr &, const std::string &) const
Definition: Generator.cpp:694
armarx::aron::codegenerator::WriterInfo
Definition: WriterInfo.h:32
armarx::aron::codegenerator::cpp::Generator::ARON_TEMPLATE_INSTANTIATIONS_ACCESSOR
static const std::string ARON_TEMPLATE_INSTANTIATIONS_ACCESSOR
Definition: Generator.h:153
armarx::aron::codegenerator::cpp::Generator::getWriteTypeBlock
virtual CppBlockPtr getWriteTypeBlock(const std::string &typeAccessor, const std::string &cppAccessor, const Path &, std::string &variantAccessor) const =0
armarx::aron::type::Variant::getPath
Path getPath() const
Definition: Variant.h:99