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 
29 
31 {
32  // constructors
33  Dict::Dict(const type::Dict& e) :
34  detail::ContainerGenerator<type::Dict, Dict>(
35  "std::map<std::string, " + FromAronType(*e.getAcceptedType())->getFullInstantiatedCppTypename() + ">",
36  "std::map<std::string, " + FromAronType(*e.getAcceptedType())->getFullInstantiatedCppTypename() + ">",
37  simox::meta::get_type_name<data::dto::Dict>(),
38  simox::meta::get_type_name<type::dto::Dict>(), e)
39  {
40  }
41 
42  // virtual implementations
43  std::vector<std::string> Dict::getRequiredIncludes() const
44  {
46  return c->getRequiredIncludes();
47  }
48 
49  CppBlockPtr Dict::getResetSoftBlock(const std::string& cppAccessor) const
50  {
51  CppBlockPtr block_if_data = std::make_shared<CppBlock>();
52  block_if_data->addLine(cppAccessor + nextEl() + "clear();");
53  return this->resolveMaybeResetSoftBlock(block_if_data, cppAccessor);
54  }
55 
56  CppBlockPtr Dict::getWriteTypeBlock(const std::string& typeAccessor, const std::string& cppAccessor, const Path& p, std::string& variantAccessor) const
57  {
58  CppBlockPtr b = std::make_shared<CppBlock>();
59  std::string escaped_accessor = EscapeAccessor(cppAccessor);
60  variantAccessor = ARON_VARIANT_RETURN_ACCESSOR+ "_" + escaped_accessor;
61 
62  auto type_s = FromAronType(*type.getAcceptedType());
63  std::string nextVariantAccessor;
64  Path nextPath = p.withAcceptedType(true);
65  b->appendBlock(type_s->getWriteTypeBlock(type_s->getInstantiatedCppTypename(), cppAccessor + nextEl() + "accepted_type", nextPath, nextVariantAccessor));
66 
67  b->addLine("auto " + variantAccessor + " = " + ARON_WRITER_ACCESSOR + ".writeDict(" + nextVariantAccessor + ", " +
69  "armarx::aron::Path("+ARON_PATH_ACCESSOR+", {"+simox::alg::join(p.getPath(), ", ")+"})); // of " + cppAccessor);
70 
71  return b;
72  }
73 
74  CppBlockPtr Dict::getWriteBlock(const std::string& cppAccessor, const Path& p, std::string& variantAccessor) const
75  {
76  CppBlockPtr block_if_data = std::make_shared<CppBlock>();
77  std::string escaped_accessor = EscapeAccessor(cppAccessor);
78  variantAccessor = ARON_VARIANT_RETURN_ACCESSOR+ "_" + escaped_accessor;
79 
80  const std::string elementsAccessor = variantAccessor + "_dictElements";
81  block_if_data->addLine("std::map<std::string, _Aron_T> " + elementsAccessor + ";");
82 
83  std::string accessor_iterator_key = ARON_VARIABLE_PREFIX + "_" + escaped_accessor + "_key";
84  std::string accessor_iterator_val = ARON_VARIABLE_PREFIX + "_" + escaped_accessor + "_value";
85  std::string resolved_accessor = resolveMaybeAccessor(cppAccessor);
86 
87  block_if_data->addLine("for (const auto& [" + accessor_iterator_key + ", " + accessor_iterator_val + "] : " + resolved_accessor + ") ");
88  {
89  auto type_s = FromAronType(*type.getAcceptedType());
90  CppBlockPtr for_loop = std::make_shared<CppBlock>();
91  std::string nextVariantAccessor;
92  Path nextPath = p.withElement(accessor_iterator_key);
93  auto child_b = type_s->getWriteBlock(accessor_iterator_val, nextPath, nextVariantAccessor);
94  for_loop->addLine("auto " + nextVariantAccessor + " = " + ARON_WRITER_ACCESSOR + ".writeNull();");
95  for_loop->appendBlock(child_b);
96  for_loop->addLine(elementsAccessor + ".emplace(" + accessor_iterator_key + ", " + nextVariantAccessor + ");");
97  block_if_data->addBlock(for_loop);
98  }
99 
100  block_if_data->addLine(variantAccessor + " = " + ARON_WRITER_ACCESSOR +
101  ".writeDict(" + elementsAccessor + ", " +
102  "std::nullopt, " +
103  "armarx::aron::Path("+ARON_PATH_ACCESSOR+", {"+simox::alg::join(p.getPath(), ", ")+"})); // of " + cppAccessor);
104 
105  return resolveMaybeWriteBlock(block_if_data, cppAccessor);
106  }
107 
108  CppBlockPtr Dict::getReadBlock(const std::string& cppAccessor, const std::string& variantAccessor) const
109  {
110  CppBlockPtr block_if_data = std::make_shared<CppBlock>();
111  std::string escaped_accessor = EscapeAccessor(cppAccessor);
112  std::string elements_accessor = ARON_VARIABLE_PREFIX + "_" + escaped_accessor + "_dictElements";
113  std::string accessor_iterator_value = ARON_VARIABLE_PREFIX + "_" + escaped_accessor + "_dictValue";
114  std::string accessor_iterator_key = ARON_VARIABLE_PREFIX + "_" + escaped_accessor + "_dictKey";
115 
116  block_if_data->addLine("std::map<std::string, _Aron_TNonConst> " + elements_accessor + ";");
117  block_if_data->addLine("" + ARON_READER_ACCESSOR + ".readDict(" + variantAccessor + ", " + elements_accessor + ");");
118  block_if_data->addLine("for (const auto& [" + accessor_iterator_key + ", " + accessor_iterator_value + "] : " + elements_accessor + ")");
119  {
120  auto type_s = FromAronType(*type.getAcceptedType());
121  CppBlockPtr for_loop = std::make_shared<CppBlock>();
122  std::string accessor_iterator_tmp = ARON_VARIABLE_PREFIX + "_" + escaped_accessor + "_dictTmp";
123  for_loop->addLine(type_s->getFullInstantiatedCppTypename() + " " + accessor_iterator_tmp +";");
124  for_loop->appendBlock(type_s->getReadBlock(accessor_iterator_tmp, accessor_iterator_value));
125  for_loop->addLine(cppAccessor + nextEl() + "insert({" + accessor_iterator_key + ", " + accessor_iterator_tmp + "});");
126  block_if_data->addBlock(for_loop);
127  }
128  return resolveMaybeReadBlock(block_if_data, cppAccessor, variantAccessor);
129  }
130 }
131 
armarx::aron::Path::withElement
Path withElement(const std::string &, bool escape=false) const
Definition: Path.cpp:161
armarx::aron::codegenerator::cpp::Generator::ARON_VARIANT_RETURN_ACCESSOR
static const std::string ARON_VARIANT_RETURN_ACCESSOR
Definition: Generator.h:154
armarx::aron::codegenerator::cpp::generator::Dict::getWriteBlock
CppBlockPtr getWriteBlock(const std::string &cppAccessor, const Path &, std::string &variantAccessor) const final
Definition: Dict.cpp:74
armarx::aron::codegenerator::cpp::generator::Dict::getResetSoftBlock
CppBlockPtr getResetSoftBlock(const std::string &cppAccessor) const final
Definition: Dict.cpp:49
armarx::aron::type::detail::SpecializedVariantBase::getMaybe
type::Maybe getMaybe() const override
get the maybe type
Definition: SpecializedVariant.h:92
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::ARON_VARIABLE_PREFIX
static const std::string ARON_VARIABLE_PREFIX
Definition: Generator.h:147
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
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::aron::codegenerator::cpp::Generator::ARON_PATH_ACCESSOR
static const std::string ARON_PATH_ACCESSOR
Definition: Generator.h:150
armarx::aron::codegenerator::cpp::generator::Dict::Dict
Dict(const type::Dict &)
Definition: Dict.cpp:33
armarx::aron::Path
The Path class.
Definition: Path.h:36
armarx::aron::type::Dict
The Dict class.
Definition: Dict.h:40
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::aron::codegenerator::cpp::generator::Dict::getWriteTypeBlock
CppBlockPtr getWriteTypeBlock(const std::string &typeAccessor, const std::string &cppAccessor, const Path &, std::string &variantAccessor) const final
Definition: Dict.cpp:56
armarx::aron::codegenerator::cpp::generator::detail::SpecializedGeneratorBase< type::Dict, Dict >::type
type::Dict type
Definition: SpecializedGenerator.h:52
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::Dict::getRequiredIncludes
std::vector< std::string > getRequiredIncludes() const final
Definition: Dict.cpp:43
armarx::aron::Path::withAcceptedType
Path withAcceptedType(bool escape=false) const
Definition: Path.cpp:172
armarx::aron::codegenerator::cpp::generator::Dict::getReadBlock
CppBlockPtr getReadBlock(const std::string &cppAccessor, const std::string &variantAccessor) const final
Definition: Dict.cpp:108
armarx::aron::type::Dict::getAcceptedType
VariantPtr getAcceptedType() const
Definition: Dict.cpp:42
armarx::aron::codegenerator::cpp::Generator::nextEl
std::string nextEl() const
Definition: Generator.cpp:613
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
Dict.h
simox
Definition: Impl.cpp:40
armarx::aron::codegenerator::cpp::generator::Dict
Definition: Dict.h:31
armarx::aron::codegenerator::cpp::Generator::resolveMaybeWriteBlock
CppBlockPtr resolveMaybeWriteBlock(const CppBlockPtr &, const std::string &) const
Definition: Generator.cpp:694
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