Dict.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 "Dict.h"
26
27#include <SimoxUtility/meta/type_name.h>
28
30{
31 // constructors
33 detail::ContainerGenerator<type::Dict, Dict>(
34 "std::map<std::string, " +
35 FromAronType(*e.getAcceptedType())->getFullInstantiatedCppTypename() + ">",
36 "std::map<std::string, " +
37 FromAronType(*e.getAcceptedType())->getFullInstantiatedCppTypename() + ">",
38 simox::meta::get_type_name<data::dto::Dict>(),
39 simox::meta::get_type_name<type::dto::Dict>(),
40 e)
41 {
42 }
43
44 // virtual implementations
45 std::vector<std::string>
47 {
48 auto c = FromAronType(*type.getAcceptedType());
49 return c->getRequiredIncludes();
50 }
51
53 Dict::getResetSoftBlock(const std::string& cppAccessor) const
54 {
55 CppBlockPtr block_if_data = std::make_shared<CppBlock>();
56 block_if_data->addLine(cppAccessor + nextEl() + "clear();");
57 return this->resolveMaybeResetSoftBlock(block_if_data, cppAccessor);
58 }
59
61 Dict::getWriteTypeBlock(const std::string& typeAccessor,
62 const std::string& cppAccessor,
63 const Path& p,
64 std::string& variantAccessor) const
65 {
66 CppBlockPtr b = std::make_shared<CppBlock>();
67 std::string escaped_accessor = EscapeAccessor(cppAccessor);
68 variantAccessor = ARON_VARIANT_RETURN_ACCESSOR + "_" + escaped_accessor;
69
70 auto type_s = FromAronType(*type.getAcceptedType());
71 std::string nextVariantAccessor;
72 Path nextPath = p.withAcceptedType(true);
73 b->appendBlock(type_s->getWriteTypeBlock(type_s->getInstantiatedCppTypename(),
74 cppAccessor + nextEl() + "accepted_type",
75 nextPath,
76 nextVariantAccessor));
77
78 b->addLine("auto " + variantAccessor + " = " + ARON_WRITER_ACCESSOR + ".writeDict(" +
79 nextVariantAccessor + ", " + conversion::Maybe2CppString.at(type.getMaybe()) +
80 ", " + "armarx::aron::Path(" + ARON_PATH_ACCESSOR + ", {" +
81 simox::alg::join(p.getPath(), ", ") + "})); // of " + cppAccessor);
82
83 return b;
84 }
85
87 Dict::getWriteBlock(const std::string& cppAccessor,
88 const Path& p,
89 std::string& variantAccessor) const
90 {
91 CppBlockPtr block_if_data = std::make_shared<CppBlock>();
92 std::string escaped_accessor = EscapeAccessor(cppAccessor);
93 variantAccessor = ARON_VARIANT_RETURN_ACCESSOR + "_" + escaped_accessor;
94
95 const std::string elementsAccessor = variantAccessor + "_dictElements";
96 block_if_data->addLine("std::map<std::string, _Aron_T> " + elementsAccessor + ";");
97
98 std::string accessor_iterator_key = ARON_VARIABLE_PREFIX + "_" + escaped_accessor + "_key";
99 std::string accessor_iterator_val =
100 ARON_VARIABLE_PREFIX + "_" + escaped_accessor + "_value";
101 std::string resolved_accessor = resolveMaybeAccessor(cppAccessor);
102
103 block_if_data->addLine("for (const auto& [" + accessor_iterator_key + ", " +
104 accessor_iterator_val + "] : " + resolved_accessor + ") ");
105 {
106 auto type_s = FromAronType(*type.getAcceptedType());
107 CppBlockPtr for_loop = std::make_shared<CppBlock>();
108 std::string nextVariantAccessor;
109 Path nextPath = p.withElement(accessor_iterator_key);
110 auto child_b =
111 type_s->getWriteBlock(accessor_iterator_val, nextPath, nextVariantAccessor);
112 for_loop->addLine("auto " + nextVariantAccessor + " = " + ARON_WRITER_ACCESSOR +
113 ".writeNull();");
114 for_loop->appendBlock(child_b);
115 for_loop->addLine(elementsAccessor + ".emplace(" + accessor_iterator_key + ", " +
116 nextVariantAccessor + ");");
117 block_if_data->addBlock(for_loop);
118 }
119
120 block_if_data->addLine(variantAccessor + " = " + ARON_WRITER_ACCESSOR + ".writeDict(" +
121 elementsAccessor + ", " + "std::nullopt, " + "armarx::aron::Path(" +
122 ARON_PATH_ACCESSOR + ", {" + simox::alg::join(p.getPath(), ", ") +
123 "})); // of " + cppAccessor);
124
125 return resolveMaybeWriteBlock(block_if_data, cppAccessor);
126 }
127
129 Dict::getReadBlock(const std::string& cppAccessor, const std::string& variantAccessor) const
130 {
131 CppBlockPtr block_if_data = std::make_shared<CppBlock>();
132 std::string escaped_accessor = EscapeAccessor(cppAccessor);
133 std::string elements_accessor =
134 ARON_VARIABLE_PREFIX + "_" + escaped_accessor + "_dictElements";
135 std::string accessor_iterator_value =
136 ARON_VARIABLE_PREFIX + "_" + escaped_accessor + "_dictValue";
137 std::string accessor_iterator_key =
138 ARON_VARIABLE_PREFIX + "_" + escaped_accessor + "_dictKey";
139
140 block_if_data->addLine("std::map<std::string, _Aron_TNonConst> " + elements_accessor + ";");
141 block_if_data->addLine("" + ARON_READER_ACCESSOR + ".readDict(" + variantAccessor + ", " +
142 elements_accessor + ");");
143 block_if_data->addLine("for (const auto& [" + accessor_iterator_key + ", " +
144 accessor_iterator_value + "] : " + elements_accessor + ")");
145 {
146 auto type_s = FromAronType(*type.getAcceptedType());
147 CppBlockPtr for_loop = std::make_shared<CppBlock>();
148 std::string accessor_iterator_tmp =
149 ARON_VARIABLE_PREFIX + "_" + escaped_accessor + "_dictTmp";
150 for_loop->addLine(type_s->getFullInstantiatedCppTypename() + " " +
151 accessor_iterator_tmp + ";");
152 for_loop->appendBlock(
153 type_s->getReadBlock(accessor_iterator_tmp, accessor_iterator_value));
154 for_loop->addLine(cppAccessor + nextEl() + "insert({" + accessor_iterator_key + ", " +
155 accessor_iterator_tmp + "});");
156 block_if_data->addBlock(for_loop);
157 }
158 return resolveMaybeReadBlock(block_if_data, cppAccessor, variantAccessor);
159 }
160} // namespace armarx::aron::codegenerator::cpp::generator
constexpr T c
The Path class.
Definition Path.h:36
std::vector< std::string > getPath() const
Definition Path.cpp:87
Path withElement(const std::string &, bool escape=false) const
Definition Path.cpp:163
Path withAcceptedType(bool escape=false) const
Definition Path.cpp:174
std::string resolveMaybeAccessor(const std::string &) const
CppBlockPtr resolveMaybeReadBlock(const CppBlockPtr &, const std::string &, const std::string &) const
static std::string EscapeAccessor(const std::string &)
Definition Generator.cpp:53
CppBlockPtr resolveMaybeResetSoftBlock(const CppBlockPtr &, const std::string &) const
static const std::string ARON_READER_ACCESSOR
Definition Generator.h:164
static const std::string ARON_PATH_ACCESSOR
Definition Generator.h:163
static const std::string ARON_VARIABLE_PREFIX
Definition Generator.h:160
static const std::string ARON_WRITER_ACCESSOR
Definition Generator.h:165
static std::unique_ptr< Generator > FromAronType(const type::Variant &)
Definition Generator.cpp:91
static const std::string ARON_VARIANT_RETURN_ACCESSOR
Definition Generator.h:167
CppBlockPtr resolveMaybeWriteBlock(const CppBlockPtr &, const std::string &) const
CppBlockPtr getReadBlock(const std::string &cppAccessor, const std::string &variantAccessor) const final
Definition Dict.cpp:129
std::vector< std::string > getRequiredIncludes() const final
Definition Dict.cpp:46
CppBlockPtr getWriteTypeBlock(const std::string &typeAccessor, const std::string &cppAccessor, const Path &, std::string &variantAccessor) const final
Definition Dict.cpp:61
CppBlockPtr getWriteBlock(const std::string &cppAccessor, const Path &, std::string &variantAccessor) const final
Definition Dict.cpp:87
CppBlockPtr getResetSoftBlock(const std::string &cppAccessor) const final
Definition Dict.cpp:53
The Dict class.
Definition Dict.h:40
const std::map< type::Maybe, std::string > Maybe2CppString
Definition Generator.h:48
A convenience header to include all aron files (full include, not forward declared)
std::shared_ptr< CppBlock > CppBlockPtr
Definition CppBlock.h:37
Definition Impl.cpp:41