Object.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 "Object.h"
26
27#include <SimoxUtility/algorithm/vector.hpp>
28#include <SimoxUtility/meta/type_name.h>
29
31{
32 const std::map<std::string, DTOObjectReplacement> DTO_REPLACEMENTS = {
33 // ONLY CORE TYPE REPLACEMENTS HERE THAT REQUIRE A toAron fromAron CONVERSION
34 {"armarx::arondto::DateTime",
35 {"armarx::arondto::DateTime", // original typename
36 "armarx::arondto::DateTime", // original instantiated typename (in case of templates)
37 "armarx::core::time::DateTime", // replaced typename
38 "armarx::core::time::DateTime", // replaced instantiated typename
39 {"<RobotAPI/libraries/aron/common/rw/time.h>"}, // additional includes
40 {}}}, // disallowedBases
41 {"armarx::arondto::Duration",
42 {"armarx::arondto::Duration",
43 "armarx::arondto::Duration",
44 "armarx::core::time::Duration",
45 "armarx::core::time::Duration",
46 {"<RobotAPI/libraries/aron/common/rw/time.h>"},
47 {"armarx::arondto::Frequency", "armarx::arondto::DateTime"}}}
48
49 // {"armarx::armem::arondto::MemoryID",
50 // {"armarx::armem::arondto::MemoryID",
51 // "armarx::armem::arondto::MemoryID",
52 // "armarx::armem::MemoryID",
53 // "armarx::armem::MemoryID",
54 // {"<RobotAPI/libraries/aron/common/rw/memoryid.h>"},
55 // {}}}
56 };
57
58 bool
60 {
61 std::string name = t.getObjectNameWithTemplates();
62 if (DTO_REPLACEMENTS.count(name) > 0)
63 {
64 auto x = DTO_REPLACEMENTS.at(name);
65 bool b = std::find(x.disallowedBases.begin(),
66 x.disallowedBases.end(),
67 t.getPath().getRootIdentifier()) == x.disallowedBases.end();
68 return b;
69 }
70 return false;
71 }
72
73 std::string
75 {
76 std::string name = t.getObjectNameWithTemplates();
77 if (checkForAllowedReplacement(t) && DTO_REPLACEMENTS.count(name) > 0)
78 {
79 auto x = DTO_REPLACEMENTS.at(name);
80 return x.replacedInstantiatedTypename;
81 }
83 }
84
85 std::string
87 {
88 std::string name = t.getObjectNameWithTemplates();
89 if (checkForAllowedReplacement(t) && DTO_REPLACEMENTS.count(name) > 0)
90 {
91 auto x = DTO_REPLACEMENTS.at(name);
92 return x.replacedTypename;
93 }
94 return name;
95 }
96
97 std::vector<std::string>
99 {
100 std::string name = t.getObjectNameWithTemplates();
101 if (checkForAllowedReplacement(t) && DTO_REPLACEMENTS.count(name) > 0)
102 {
103 auto x = DTO_REPLACEMENTS.at(name);
104 return x.additionalIncludes;
105 }
106 return {};
107 }
108
109 // constructors
111 detail::ContainerGenerator<type::Object, Object>(
114 simox::meta::get_type_name<data::dto::Dict>(),
115 simox::meta::get_type_name<type::dto::AronObject>(),
116 e),
117 has_been_replaced(checkForAllowedReplacement(e))
118 {
119 }
120
121 std::vector<std::string>
123 {
124 std::vector<std::string> ret;
125
126 // check for includes of members
127 for (const auto& [key, child] : type.getMemberTypes())
128 {
129 (void)key;
130 auto child_s = FromAronType(*child);
131 ret = simox::alg::appended(ret, child_s->getRequiredIncludes());
132 }
133
134 // also check for additional includes from dto replacements
135 ret = simox::alg::appended(ret, checkForAdditionalIncludes(type));
136
137 return ret;
138 }
139
141 Object::getWriteTypeBlock(const std::string& typeAccessor,
142 const std::string& cppAccessor,
143 const Path& p,
144 std::string& variantAccessor) const
145 {
146 CppBlockPtr b = std::make_shared<CppBlock>();
147 std::string escaped_accessor = EscapeAccessor(cppAccessor);
148 variantAccessor = ARON_VARIANT_RETURN_ACCESSOR + "_" + escaped_accessor;
149
150 std::vector<std::string> templatesQuoted;
151 for (const auto& t : type.getTemplateInstantiations())
152 {
153 templatesQuoted.push_back("\"" + t + "\"");
154 }
155
156 b->addLine("auto " + variantAccessor + " = " +
157 type.getObjectNameWithTemplateInstantiations() + "::writeType(" +
158 ARON_WRITER_ACCESSOR + ", " + "{" + simox::alg::join(templatesQuoted, ", ") +
159 "}, " + conversion::Maybe2CppString.at(type.getMaybe()) + ", " +
160 "armarx::aron::Path(" + ARON_PATH_ACCESSOR + ", {" +
161 simox::alg::join(p.getPath(), ", ") + "})); // of " + cppAccessor);
162 return b;
163 }
164
166 Object::getWriteBlock(const std::string& cppAccessor,
167 const Path& p,
168 std::string& variantAccessor) const
169 {
170 CppBlockPtr block_if_data = std::make_shared<CppBlock>();
171 std::string escaped_accessor = EscapeAccessor(cppAccessor);
172 std::string resolved_accessor = resolveMaybeAccessor(cppAccessor);
173 variantAccessor = ARON_VARIANT_RETURN_ACCESSOR + "_" + escaped_accessor;
174
175 block_if_data->addLine("armarx::aron::write(" + ARON_WRITER_ACCESSOR + ", " +
176 resolved_accessor + ", " + variantAccessor + ", " +
177 "armarx::aron::Path(" + ARON_PATH_ACCESSOR + ", {" +
178 simox::alg::join(p.getPath(), ", ") + "})); // of " + cppAccessor);
179
180 return resolveMaybeWriteBlock(block_if_data, cppAccessor);
181 }
182
184 Object::getReadBlock(const std::string& cppAccessor, const std::string& variantAccessor) const
185 {
186 CppBlockPtr block_if_data = std::make_shared<CppBlock>();
187 std::string escaped_accessor = EscapeAccessor(cppAccessor);
188 std::string resolved_accessor = resolveMaybeAccessor(cppAccessor);
189
190 if (const auto reset = resolveMaybeGeneratorWithSetter(cppAccessor); !reset.empty())
191 {
192 block_if_data->addLine(reset);
193 }
194
195 block_if_data->addLine("armarx::aron::read(" + ARON_READER_ACCESSOR + ", " +
196 variantAccessor + ", " + resolved_accessor + "); // of " +
197 cppAccessor);
198
199 return resolveMaybeReadBlock(block_if_data, cppAccessor, variantAccessor);
200 }
201
203 Object::getResetHardBlock(const std::string& cppAccessor) const
204 {
205 if (has_been_replaced)
206 {
207 return Base::getResetHardBlock(cppAccessor);
208 }
209
210 CppBlockPtr block_if_data = std::make_shared<CppBlock>();
211 block_if_data->addLine(cppAccessor + nextEl() + "resetHard();");
212 return resolveMaybeResetHardBlock(block_if_data, cppAccessor);
213 }
214
216 Object::getResetSoftBlock(const std::string& cppAccessor) const
217 {
218 if (has_been_replaced)
219 {
220 return Base::getResetHardBlock(cppAccessor);
221 }
222
223 auto block_if_data = std::make_shared<CppBlock>();
224 block_if_data->addLine(cppAccessor + nextEl() + "resetSoft();");
225 return this->resolveMaybeResetSoftBlock(block_if_data, cppAccessor);
226 }
227} // namespace armarx::aron::codegenerator::cpp::generator
The Path class.
Definition Path.h:36
std::vector< std::string > getPath() const
Definition Path.cpp:87
std::string getRootIdentifier() const
Definition Path.cpp:63
std::string resolveMaybeAccessor(const std::string &) const
CppBlockPtr resolveMaybeResetHardBlock(const CppBlockPtr &, 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:53
CppBlockPtr resolveMaybeResetSoftBlock(const CppBlockPtr &, const std::string &) const
static const std::string ARON_READER_ACCESSOR
Definition Generator.h:164
virtual CppBlockPtr getResetHardBlock(const std::string &cppAccessor) const
static const std::string ARON_PATH_ACCESSOR
Definition Generator.h:163
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 Object.cpp:184
std::vector< std::string > getRequiredIncludes() const final
Definition Object.cpp:122
CppBlockPtr getResetHardBlock(const std::string &cppAccessor) const final
Definition Object.cpp:203
CppBlockPtr getWriteTypeBlock(const std::string &typeAccessor, const std::string &cppAccessor, const Path &, std::string &variantAccessor) const final
Definition Object.cpp:141
CppBlockPtr getWriteBlock(const std::string &cppAccessor, const Path &, std::string &variantAccessor) const final
Definition Object.cpp:166
CppBlockPtr getResetSoftBlock(const std::string &cppAccessor) const final
Definition Object.cpp:216
The Object class.
Definition Object.h:43
std::string getObjectNameWithTemplateInstantiations() const
Definition Object.cpp:193
std::string getObjectNameWithTemplates() const
Definition Object.cpp:203
const std::map< type::Maybe, std::string > Maybe2CppString
Definition Generator.h:48
std::string checkForClassNameReplacement(const type::Object &t)
Definition Object.cpp:86
bool checkForAllowedReplacement(const type::Object &t)
Definition Object.cpp:59
std::vector< std::string > checkForAdditionalIncludes(const type::Object &t)
Definition Object.cpp:98
const std::map< std::string, DTOObjectReplacement > DTO_REPLACEMENTS
Definition Object.cpp:32
std::string checkForInstantiationTypenameReplacement(const type::Object &t)
Definition Object.cpp:74
A convenience header to include all aron files (full include, not forward declared)
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::shared_ptr< CppBlock > CppBlockPtr
Definition CppBlock.h:37
Definition Impl.cpp:41