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 b->addLine("using _Aron_TElement [[maybe_unused]] = typename ReaderT::ElementType;");
376
377 b->addLine("this->resetSoft();");
378 b->addLine("ARMARX_CHECK_AND_THROW(!" + ARON_READER_ACCESSOR +
379 ".readNull(input), ::armarx::aron::error::AronException(__PRETTY_FUNCTION__, "
380 "\"The input to the read method must not be null.\"));");
381
382 b->addLine("try");
383 b->addBlock(this->getReadBlock("", "input"));
384 b->addLine("catch(const std::exception& " + ARON_VARIABLE_PREFIX + "_e)");
385 b->addLineAsBlock("throw ::armarx::aron::error::AronException(__PRETTY_FUNCTION__, "
386 "std::string(\"An error occured during the read method of an aron "
387 "generated class. The full error log was:\\n\") + " +
388 ARON_VARIABLE_PREFIX + "_e.what());");
389 m->setBlock(b);
390
391 m->setEnforceBlockGeneration(true);
392 return m;
393 }
394
397 {
398 std::stringstream doc;
399 doc << "@brief " << info.methodName
400 << "() - This method returns a new data from the member data types using a writer "
401 "implementation. \n";
402 doc << "@return - the result of the writer implementation";
403
404 CppMethodPtr m =
405 CppMethodPtr(new CppMethod(info.returnType + " " + info.methodName + "() const" +
406 (info.override ? " override" : ""),
407 doc.str()));
408 m->addLine(info.writerClassType + " writer;");
409 m->addLine("return " + info.enforceConversion + "(this->write(writer))" +
410 info.enforceMemberAccess + ";");
411
412 m->setEnforceBlockGeneration(true);
413 return m;
414 }
415
418 {
419 std::stringstream doc;
420 doc << "@brief " << info.methodName
421 << " - This method sets the struct members to new values given in a reader "
422 "implementation. \n";
423 doc << "@return - nothing";
424
425 CppMethodPtr m =
426 CppMethodPtr(new CppMethod("void " + info.methodName + "(const " + info.argumentType +
427 "& input)" + (info.override ? " override" : ""),
428 doc.str()));
429 m->addLine(info.readerClassType + " reader;");
430 m->addLine("this->read(reader, " + info.enforceConversion + "(input)" +
431 info.enforceMemberAccess + ");");
432
433 m->setEnforceBlockGeneration(true);
434 return m;
435 }
436
439 {
440 std::stringstream doc;
441 doc << "@brief " << info.methodName
442 << "() - This method sets the struct members to new values given in a reader "
443 "implementation. \n";
444 doc << "@return - nothing";
445
446 CppMethodPtr m =
447 CppMethodPtr(new CppMethod("static " + info.returnType + " " + info.methodName +
448 "(const " + info.argumentType + "& input)",
449 doc.str()));
450 m->addLine(info.returnType + " t;");
451 m->addLine("t.fromAron(input);");
452 m->addLine("return t;");
453
454 m->setEnforceBlockGeneration(true);
455 return m;
456 }
457
460 {
461 std::stringstream doc;
462 doc << "@brief " << info.methodName
463 << "() - This method returns a new type from the member data types using a writer "
464 "implementation. \n";
465 doc << "@return - the result of the writer implementation";
466
468 new CppMethod("static " + info.returnType + " " + info.methodName + "()", doc.str()));
469 m->addLine(info.writerClassType + " writer;");
470 m->addLine("return " + info.enforceConversion + "(writeType(writer))" +
471 info.enforceMemberAccess + ";");
472
473 m->setEnforceBlockGeneration(true);
474 return m;
475 }
476
479 {
480 std::stringstream doc;
481 doc << "@brief operator==() - This method checks whether all values equal another "
482 "instance. \n";
483 doc << "@param i - The other instance\n";
484 doc << "@return - true, if all members are the same, false otherwise";
485
487 "bool operator==(const " + this->getFullClassCppTypename() + "& i) const", doc.str()));
488 CppBlockPtr b = this->getEqualsBlock("", "i");
489 b->addLine("return true;");
490 m->setBlock(b);
491
492 m->setEnforceBlockGeneration(true);
493 return m;
494 }
495
496 // defaulted implementations of the blocks
497 std::vector<std::string>
499 {
500 return {};
501 }
502
503 std::vector<CppFieldPtr>
504 Generator::getPublicVariableDeclarations(const std::string& name) const
505 {
506 auto field = std::make_shared<CppField>(this->getFullInstantiatedCppTypename(), name);
507 return {field};
508 }
509
510 std::pair<std::vector<std::pair<std::string, std::string>>, bool>
511 Generator::getCtorInitializers(const std::string& name) const
512 {
513 return {{}, false};
514 }
515
517 Generator::getCtorBlock(const std::string&) const
518 {
519 return std::make_shared<CppBlock>();
520 }
521
522 std::pair<std::vector<std::pair<std::string, std::string>>, bool>
523 Generator::getCopyCtorInitializers(const std::string& name) const
524 {
525 const auto& t = getType();
526 if (t.getMaybe() == type::Maybe::UNIQUE_PTR) // unique ptrs cant be copied
527 {
528 return {{{name,
529 ARON_OTHER_ACCESSOR + "." + name + " ? " +
530 resolveMaybeGenerator("*" + ARON_OTHER_ACCESSOR + "." + name) +
531 " : nullptr"}},
532 true};
533 }
534
535 return {{{name, ARON_OTHER_ACCESSOR + "." + name}}, false};
536 }
537
539 Generator::getCopyCtorBlock(const std::string&) const
540 {
541 return std::make_shared<CppBlock>();
542 }
543
545 Generator::getDtorBlock(const std::string&) const
546 {
547 return std::make_shared<CppBlock>();
548 }
549
551 Generator::getResetHardBlock(const std::string& cppAccessor) const
552 {
553 CppBlockPtr block_if_data = std::make_shared<CppBlock>();
554 block_if_data->addLine(cppAccessor + " = " + this->getFullInstantiatedCppTypename() +
555 "();");
556 return resolveMaybeResetHardBlock(block_if_data, cppAccessor);
557 }
558
560 Generator::getResetSoftBlock(const std::string& cppAccessor) const
561 {
562 auto block_if_data = std::make_shared<CppBlock>();
563 block_if_data->addLine(cppAccessor + " = " + this->getFullInstantiatedCppTypename() +
564 "();");
565 return this->resolveMaybeResetSoftBlock(block_if_data, cppAccessor);
566 }
567
569 Generator::getEqualsBlock(const std::string& accessor,
570 const std::string& otherInstanceAccessor) const
571 {
572 CppBlockPtr block_if_data = std::make_shared<CppBlock>();
573 std::string resolved_accessor = this->resolveMaybeAccessor(accessor);
574 std::string other_instance_resolved_accessor =
575 this->resolveMaybeAccessor(otherInstanceAccessor);
576
577 block_if_data->addLine("if (not (" + resolved_accessor +
578 " == " + other_instance_resolved_accessor + "))");
579 block_if_data->addLineAsBlock("return false;");
580 return resolveMaybeEqualsBlock(block_if_data, accessor, otherInstanceAccessor);
581 }
582
583 // Helper methods
584 std::string
585 Generator::resolveMaybeAccessor(const std::string& s) const
586 {
587 const auto& t = getType();
588 if (t.getMaybe() == type::Maybe::OPTIONAL)
589 {
590 return s + ".value()";
591 }
592 if (t.getMaybe() == type::Maybe::RAW_PTR || t.getMaybe() == type::Maybe::SHARED_PTR ||
593 t.getMaybe() == type::Maybe::UNIQUE_PTR)
594 {
595 return "*" + s;
596 }
597 return s;
598 }
599
600 std::string
601 Generator::resolveMaybeGenerator(const std::string& args) const
602 {
603 const auto& t = getType();
604 if (t.getMaybe() == type::Maybe::OPTIONAL)
605 {
606 return "std::make_optional<" + getInstantiatedCppTypename() + ">(" + args + ")";
607 }
608 if (t.getMaybe() == type::Maybe::RAW_PTR)
609 {
610 return "new " + getInstantiatedCppTypename() + "(" + args + ")";
611 }
612 if (t.getMaybe() == type::Maybe::SHARED_PTR)
613 {
614 return "std::make_shared<" + getInstantiatedCppTypename() + ">(" + args + ")";
615 }
616 if (t.getMaybe() == type::Maybe::UNIQUE_PTR)
617 {
618 return "std::make_unique<" + getInstantiatedCppTypename() + ">(" + args + ")";
619 }
620 return "";
621 }
622
623 std::string
624 Generator::resolveMaybeGeneratorWithSetter(const std::string& s, const std::string& args) const
625 {
626 auto gen = resolveMaybeGenerator(args);
627 if (!gen.empty())
628 {
629 return s + " = " + gen + ";";
630 }
631 return "";
632 }
633
634 std::string
636 {
637 const auto& type = getType();
638 switch (type.getMaybe())
639 {
640 case type::Maybe::NONE:
641 return ".";
642 case type::Maybe::OPTIONAL: //[[fallthrough]];
643 case type::Maybe::RAW_PTR: //[[fallthrough]];
644 case type::Maybe::SHARED_PTR: //[[fallthrough]];
645 case type::Maybe::UNIQUE_PTR:
646 return "->";
647 }
648 throw error::ValueNotValidException(__PRETTY_FUNCTION__,
649 "Received unknown maybe enum",
650 std::to_string((int)type.getMaybe()),
651 type.getPath());
652 }
653
654 std::string
655 Generator::toPointerAccessor(const std::string& cppAccessor) const
656 {
657 const auto& type = getType();
658 switch (type.getMaybe())
659 {
660 case type::Maybe::RAW_PTR:
661 return cppAccessor;
662 case type::Maybe::SHARED_PTR: //[[fallthrough]];
663 case type::Maybe::UNIQUE_PTR:
664 return cppAccessor + ".get()";
665
666 case type::Maybe::NONE: //[[fallthrough]];
667 case type::Maybe::OPTIONAL:
668 break;
669 }
670 throw error::ValueNotValidException(__PRETTY_FUNCTION__,
671 "Received invalid maybe enum (not a pointer?)",
672 std::to_string((int)type.getMaybe()),
673 type.getPath());
674 }
675
678 const std::string& cppAccessor) const
679 {
680 const auto& type = getType();
681 if (type.getMaybe() == type::Maybe::OPTIONAL)
682 {
683 CppBlockPtr b = std::make_shared<CppBlock>();
684 b->addLine(cppAccessor + " = std::nullopt;");
685 return b;
686 }
687 else if (type.getMaybe() == type::Maybe::RAW_PTR ||
688 type.getMaybe() == type::Maybe::SHARED_PTR ||
689 type.getMaybe() == type::Maybe::UNIQUE_PTR)
690 {
691 CppBlockPtr b = std::make_shared<CppBlock>();
692 b->addLine(cppAccessor + " = nullptr;");
693 return b;
694 }
695 return block_if_data;
696 }
697
700 const std::string& cppAccessor) const
701 {
702 const auto& type = getType();
703 if (type.getMaybe() == type::Maybe::OPTIONAL || type.getMaybe() == type::Maybe::RAW_PTR ||
704 type.getMaybe() == type::Maybe::SHARED_PTR ||
705 type.getMaybe() == type::Maybe::UNIQUE_PTR)
706 {
707 CppBlockPtr b = std::make_shared<CppBlock>();
708 b->addLine("if (" + cppAccessor + ") // if " + cppAccessor + " contains data");
709 b->addBlock(block_if_data);
710 return b;
711 }
712 return block_if_data;
713 }
714
717 const std::string& cppAccessor) const
718 {
719 const auto& type = getType();
720 if (type.getMaybe() == type::Maybe::OPTIONAL || type.getMaybe() == type::Maybe::RAW_PTR ||
721 type.getMaybe() == type::Maybe::SHARED_PTR ||
722 type.getMaybe() == type::Maybe::UNIQUE_PTR)
723 {
724 CppBlockPtr b = std::make_shared<CppBlock>();
725 b->addLine("if (" + cppAccessor + ") // if " + cppAccessor + " contains data");
726 b->addBlock(block_if_data);
727 return b;
728 }
729 return block_if_data;
730 }
731
734 const std::string& cppAccessor,
735 const std::string& variantAccessor) const
736 {
737 const auto& type = getType();
738 if (type.getMaybe() == type::Maybe::OPTIONAL || type.getMaybe() == type::Maybe::RAW_PTR ||
739 type.getMaybe() == type::Maybe::SHARED_PTR ||
740 type.getMaybe() == type::Maybe::UNIQUE_PTR)
741 {
742 CppBlockPtr b = std::make_shared<CppBlock>();
743 b->addLine("if (not (" + ARON_READER_ACCESSOR + ".readNull(" + variantAccessor +
744 "))) // if aron contains data");
745 {
746 CppBlockPtr ifb = std::make_shared<CppBlock>();
747 ifb->addLine(resolveMaybeGeneratorWithSetter(cppAccessor));
748 ifb->appendBlock(block_if_data);
749 b->addBlock(ifb);
750 }
751 return b;
752 }
753 return block_if_data;
754 }
755
758 const std::string& accessor,
759 const std::string& otherInstanceAccessor) const
760 {
761 const auto& type = getType();
762 if (type.getMaybe() == type::Maybe::RAW_PTR || type.getMaybe() == type::Maybe::SHARED_PTR ||
763 type.getMaybe() == type::Maybe::UNIQUE_PTR)
764 {
765 CppBlockPtr b = std::make_shared<CppBlock>();
766 b->addLine("if (not (static_cast<bool>(" + accessor + ") == static_cast<bool>(" +
767 otherInstanceAccessor + "))) // check if both contain data");
768 b->addLineAsBlock("return false;");
769 b->addLine("if (static_cast<bool>(" + accessor + ") && static_cast<bool>(" +
770 otherInstanceAccessor + "))");
771 b->addBlock(block_if_data);
772 return b;
773 }
774
775 if (type.getMaybe() == type::Maybe::OPTIONAL)
776 {
777 CppBlockPtr b = std::make_shared<CppBlock>();
778 b->addLine("if (not ( " + accessor + ".has_value() == " + otherInstanceAccessor +
779 ".has_value())) // check if both contain data");
780 b->addLineAsBlock("return false;");
781 b->addLine("if ( " + accessor + ".has_value() && " + otherInstanceAccessor +
782 ".has_value())");
783 b->addBlock(block_if_data);
784 return b;
785 }
786 return block_if_data;
787 }
788} // 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