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
armarx::aron::codegenerator::cpp::generator::Object::getWriteTypeBlock
CppBlockPtr getWriteTypeBlock(const std::string &typeAccessor, const std::string &cppAccessor, const Path &, std::string &variantAccessor) const final
Definition: Object.cpp:141
armarx::aron::codegenerator::cpp::Generator::ARON_VARIANT_RETURN_ACCESSOR
static const std::string ARON_VARIANT_RETURN_ACCESSOR
Definition: Generator.h:154
armarx::aron::ret
ReaderT::InputType T & ret
Definition: rw.h:21
armarx::aron::codegenerator::cpp::generator::Object::Object
Object(const type::Object &)
Definition: Object.cpp:110
armarx::aron::type::detail::SpecializedVariantBase::getMaybe
type::Maybe getMaybe() const override
get the maybe type
Definition: SpecializedVariant.h:92
armarx::aron::codegenerator::cpp::generator::Object::getWriteBlock
CppBlockPtr getWriteBlock(const std::string &cppAccessor, const Path &, std::string &variantAccessor) const final
Definition: Object.cpp:166
armarx::aron::type::Object::getObjectNameWithTemplates
std::string getObjectNameWithTemplates() const
Definition: Object.cpp:203
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::cpp::Generator::resolveMaybeGeneratorWithSetter
std::string resolveMaybeGeneratorWithSetter(const std::string &, const std::string &args="") const
Definition: Generator.cpp:602
armarx::aron::codegenerator::cpp::generator::Object::getResetHardBlock
CppBlockPtr getResetHardBlock(const std::string &cppAccessor) const final
Definition: Object.cpp:203
Object.h
armarx::aron::type::Object
The Object class.
Definition: Object.h:42
detail
Definition: OpenCVUtil.cpp:127
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::Object::getRequiredIncludes
std::vector< std::string > getRequiredIncludes() const final
Definition: Object.cpp:122
armarx::aron::codegenerator::cpp::generator::checkForInstantiationTypenameReplacement
std::string checkForInstantiationTypenameReplacement(const type::Object &t)
Definition: Object.cpp:74
armarx::aron::codegenerator::cpp::Generator::ARON_PATH_ACCESSOR
static const std::string ARON_PATH_ACCESSOR
Definition: Generator.h:150
armarx::aron::type::Object::getTemplateInstantiations
std::vector< std::string > getTemplateInstantiations() const
Definition: Object.cpp:243
armarx::aron::Path
The Path class.
Definition: Path.h:36
armarx::aron::codegenerator::cpp::generator::Object
Definition: Object.h:43
armarx::aron::codegenerator::cpp::generator::Object::getReadBlock
CppBlockPtr getReadBlock(const std::string &cppAccessor, const std::string &variantAccessor) const final
Definition: Object.cpp:184
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::aron::codegenerator::cpp::generator::detail::SpecializedGeneratorBase< type::Object, Object >::type
type::Object type
Definition: SpecializedGenerator.h:52
armarx::aron::codegenerator::cpp::generator::checkForClassNameReplacement
std::string checkForClassNameReplacement(const type::Object &t)
Definition: Object.cpp:86
armarx::aron::codegenerator::cpp::Generator::resolveMaybeReadBlock
CppBlockPtr resolveMaybeReadBlock(const CppBlockPtr &, const std::string &, const std::string &) const
Definition: Generator.cpp:711
armarx::aron::codegenerator::cpp::generator
Definition: AnyObject.cpp:29
armarx::aron::codegenerator::cpp::Generator::resolveMaybeResetHardBlock
CppBlockPtr resolveMaybeResetHardBlock(const CppBlockPtr &, const std::string &) const
Definition: Generator.cpp:655
armarx::aron::codegenerator::cpp::generator::Object::getResetSoftBlock
CppBlockPtr getResetSoftBlock(const std::string &cppAccessor) const final
Definition: Object.cpp:216
armarx::aron::codegenerator::cpp::generator::checkForAdditionalIncludes
std::vector< std::string > checkForAdditionalIncludes(const type::Object &t)
Definition: Object.cpp:98
armarx::aron::codegenerator::cpp::generator::checkForAllowedReplacement
bool checkForAllowedReplacement(const type::Object &t)
Definition: Object.cpp:59
armarx::aron::codegenerator::cpp::Generator::nextEl
std::string nextEl() const
Definition: Generator.cpp:613
armarx::aron::Path::getRootIdentifier
std::string getRootIdentifier() const
Definition: Path.cpp:61
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::getResetHardBlock
virtual CppBlockPtr getResetHardBlock(const std::string &cppAccessor) const
Definition: Generator.cpp:529
armarx::aron::type::Object::getMemberTypes
std::map< std::string, VariantPtr > getMemberTypes() const
Definition: Object.cpp:108
simox
Definition: Impl.cpp:40
armarx::aron::codegenerator::cpp::generator::Dict
Definition: Dict.h:31
armarx::aron::type::Object::getObjectNameWithTemplateInstantiations
std::string getObjectNameWithTemplateInstantiations() const
Definition: Object.cpp:193
armarx::aron::codegenerator::cpp::Generator::resolveMaybeWriteBlock
CppBlockPtr resolveMaybeWriteBlock(const CppBlockPtr &, const std::string &) const
Definition: Generator.cpp:694
armarx::aron::codegenerator::cpp::generator::DTO_REPLACEMENTS
const std::map< std::string, DTOObjectReplacement > DTO_REPLACEMENTS
Definition: Object.cpp:32
armarx::aron::codegenerator::cpp::conversion::Maybe2CppString
const std::map< type::Maybe, std::string > Maybe2CppString
Definition: Generator.h:49
armarx::aron::Path::getPath
std::vector< std::string > getPath() const
Definition: Path.cpp:85
armarx::aron::type::Variant::getPath
Path getPath() const
Definition: Variant.h:99