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