List.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 "List.h"
26 
27 #include <SimoxUtility/meta/type_name.h>
28 
29 
31 {
32  // constructors
33  List::List(const type::List& e) :
34  detail::ContainerGenerator<type::List, List>(
35  "std::vector<" + FromAronType(*e.getAcceptedType())->getFullInstantiatedCppTypename() + ">",
36  "std::vector<" + FromAronType(*e.getAcceptedType())->getFullInstantiatedCppTypename() + ">",
37  simox::meta::get_type_name<data::dto::List>(),
38  simox::meta::get_type_name<type::dto::List>(), e)
39  {
40  }
41 
42 
43  std::vector<std::string> List::getRequiredIncludes() const
44  {
46  return c->getRequiredIncludes();
47  }
48 
49  CppBlockPtr List::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 List::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  CppBlockPtr b2 = type_s->getWriteTypeBlock(type_s->getInstantiatedCppTypename(), cppAccessor + nextEl() + "accepted_type", nextPath, nextVariantAccessor);
66  b->appendBlock(b2);
67 
68  b->addLine("auto " + variantAccessor + " = " + ARON_WRITER_ACCESSOR + ".writeList(" + nextVariantAccessor + ", " +
70  "armarx::aron::Path("+ARON_PATH_ACCESSOR+", {"+simox::alg::join(p.getPath(), ", ")+"})); // of " + cppAccessor);
71  return b;
72  }
73 
74  CppBlockPtr List::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 + "_listElements";
81  block_if_data->addLine("std::vector<_Aron_T> " + elementsAccessor + ";");
82 
83  std::string accessor_iterator = ARON_VARIABLE_PREFIX + "_" + escaped_accessor + "_it";
84 
85  auto type_s = FromAronType(*type.getAcceptedType());
86  block_if_data->addLine("for(unsigned int " + accessor_iterator + " = 0; " + accessor_iterator + " < " + cppAccessor + nextEl() + "size(); ++" + accessor_iterator + ")");
87  {
88  std::string nextVariantAccessor;
89  auto for_loop = std::make_shared<CppBlock>();
90  Path nextPath = p.withElement("std::to_string(" + accessor_iterator + ")");
91  auto child_b = type_s->getWriteBlock(cppAccessor + nextEl() + "at(" + accessor_iterator + ")", nextPath, nextVariantAccessor);
92  for_loop->addLine("auto " + nextVariantAccessor + " = " + ARON_WRITER_ACCESSOR + ".writeNull();");
93  for_loop->appendBlock(child_b);
94  for_loop->addLine(elementsAccessor + ".push_back(" + nextVariantAccessor + ");");
95  block_if_data->addBlock(for_loop);
96  }
97 
98  Path path = this->type.getPath();
99  block_if_data->addLine(variantAccessor + " = " + ARON_WRITER_ACCESSOR + ".writeList(" + elementsAccessor + ", " +
100  "armarx::aron::Path("+ARON_PATH_ACCESSOR+", {"+simox::alg::join(p.getPath(), ", ")+"})); // of " + cppAccessor);
101 
102  return resolveMaybeWriteBlock(block_if_data, cppAccessor);
103  }
104 
105  CppBlockPtr List::getReadBlock(const std::string& cppAccessor, const std::string& variantAccessor) const
106  {
107  CppBlockPtr block_if_data = std::make_shared<CppBlock>();
108  std::string escaped_accessor = EscapeAccessor(cppAccessor);
109  std::string elements_accessor = ARON_VARIABLE_PREFIX + "_" + escaped_accessor + "_listElements";
110  std::string accessor_iterator_value = ARON_VARIABLE_PREFIX + "_" + escaped_accessor + "_listValue";
111 
112  block_if_data->addLine("std::vector<_Aron_TNonConst> " + elements_accessor + ";");
113  block_if_data->addLine("" + ARON_READER_ACCESSOR + ".readList(" + variantAccessor + ", " + elements_accessor + ");");
114  block_if_data->addLine("for (const auto& " + accessor_iterator_value + " : " + elements_accessor + ")");
115  {
116  CppBlockPtr for_loop = std::make_shared<CppBlock>();
117 
118  auto type_s = FromAronType(*type.getAcceptedType());
119 
120  std::string accessor_iterator_tmp = ARON_VARIABLE_PREFIX + "_" + escaped_accessor + "_listTmp";
121  for_loop->addLine(type_s->getFullInstantiatedCppTypename() + " " + accessor_iterator_tmp + ";");
122  for_loop->appendBlock(type_s->getReadBlock(accessor_iterator_tmp, accessor_iterator_value));
123  for_loop->addLine(cppAccessor + nextEl() + "push_back(" + accessor_iterator_tmp + ");");
124  block_if_data->addBlock(for_loop);
125  }
126  return resolveMaybeReadBlock(block_if_data, cppAccessor, variantAccessor);
127  }
128 }
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::List::getReadBlock
CppBlockPtr getReadBlock(const std::string &cppAccessor, const std::string &variantAccessor) const final
Definition: List.cpp:105
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::type::List
The List class.
Definition: List.h:39
armarx::aron::codegenerator::cpp::Generator::ARON_VARIABLE_PREFIX
static const std::string ARON_VARIABLE_PREFIX
Definition: Generator.h:147
armarx::aron::codegenerator::cpp::generator::List::List
List(const type::List &)
Definition: List.cpp:33
detail
Definition: OpenCVUtil.cpp:127
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::List::getResetSoftBlock
CppBlockPtr getResetSoftBlock(const std::string &cppAccessor) const final
Definition: List.cpp:49
armarx::aron::codegenerator::cpp::Generator::ARON_PATH_ACCESSOR
static const std::string ARON_PATH_ACCESSOR
Definition: Generator.h:150
List.h
armarx::aron::Path
The Path class.
Definition: Path.h:36
armarx::aron::codegenerator::cpp::generator::List::getWriteTypeBlock
CppBlockPtr getWriteTypeBlock(const std::string &typeAccessor, const std::string &cppAccessor, const Path &, std::string &variantAccessor) const final
Definition: List.cpp:56
armarx::aron::codegenerator::cpp::generator::List
Definition: List.h:32
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::aron::codegenerator::cpp::generator::List::getRequiredIncludes
std::vector< std::string > getRequiredIncludes() const final
Definition: List.cpp:43
armarx::aron::codegenerator::cpp::generator::detail::SpecializedGeneratorBase< type::List, List >::type
type::List 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::type::List::getAcceptedType
VariantPtr getAcceptedType() const
Definition: List.cpp:43
armarx::aron::codegenerator::cpp::generator
Definition: AnyObject.cpp:29
armarx::aron::Path::withAcceptedType
Path withAcceptedType(bool escape=false) const
Definition: Path.cpp:172
armarx::aron::codegenerator::cpp::generator::List::getWriteBlock
CppBlockPtr getWriteBlock(const std::string &cppAccessor, const Path &, std::string &variantAccessor) const final
Definition: List.cpp:74
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
simox
Definition: Impl.cpp:40
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
armarx::aron::type::Variant::getPath
Path getPath() const
Definition: Variant.h:99