Tuple.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 "Tuple.h"
26 
27 #include <SimoxUtility/meta/type_name.h>
28 #include <SimoxUtility/algorithm/vector.hpp>
29 
31 {
33  detail::ContainerGenerator<type::Tuple, Tuple>(
34  "std::tuple<" + simox::alg::join(ExtractCppTypenames(e.getAcceptedTypes()), ", ") + ">",
35  "std::tuple<" + simox::alg::join(ExtractCppTypenames(e.getAcceptedTypes()), ", ") + ">",
36  simox::meta::get_type_name<data::dto::List>(),
37  simox::meta::get_type_name<type::dto::Tuple>(), e)
38  {
39  }
40 
41 
42  std::vector<std::string> Tuple::getRequiredIncludes() const
43  {
44  std::vector<std::string> ret;
45  for (const auto& child : type.getAcceptedTypes())
46  {
47  auto child_s = FromAronType(*child);
48  ret = simox::alg::appended(ret, child_s->getRequiredIncludes());
49  }
50  return ret;
51  }
52 
53  CppBlockPtr Tuple::getResetSoftBlock(const std::string& cppAccessor) const
54  {
55  CppBlockPtr block_if_data = std::make_shared<CppBlock>();
56  std::string resolved_accessor = resolveMaybeAccessor(cppAccessor);
57 
58  unsigned int i = 0;
59  for (const auto& child : type.getAcceptedTypes())
60  {
61  auto child_s = FromAronType(*child);
62  CppBlockPtr b2 = child_s->getResetSoftBlock("std::get<" + std::to_string(i++) + ">(" + resolved_accessor + ")");
63  block_if_data->appendBlock(b2);
64  }
65  return this->resolveMaybeResetSoftBlock(block_if_data, cppAccessor);
66  }
67 
68  CppBlockPtr Tuple::getWriteTypeBlock(const std::string& typeAccessor, const std::string& cppAccessor, const Path& p, std::string& variantAccessor) const
69  {
70  CppBlockPtr block_if_data = std::make_shared<CppBlock>();
71  std::string resolved_accessor = resolveMaybeAccessor(cppAccessor);
72  std::string escaped_accessor = EscapeAccessor(cppAccessor);
73  variantAccessor = ARON_VARIANT_RETURN_ACCESSOR+ "_" + escaped_accessor;
74 
75  const std::string acceptedTypesAccessor = variantAccessor + "_tupleAcceptedTypes";
76  block_if_data->addLine("std::vector<_Aron_T> " + acceptedTypesAccessor + ";");
77 
78  unsigned int i = 0;
79  for (const auto& type : type.getAcceptedTypes())
80  {
81  std::string accessor_iterator = "std::get<" + std::to_string(i) + ">("+resolved_accessor+");";
82  auto type_s = FromAronType(*type);
83  std::string nextVariantAccessor;
84  Path nextPath = p.withAcceptedTypeIndex(i++);
85  CppBlockPtr b2 = type_s->getWriteTypeBlock(type_s->getInstantiatedCppTypename(), accessor_iterator, nextPath, nextVariantAccessor);
86  block_if_data->appendBlock(b2);
87  block_if_data->addLine(acceptedTypesAccessor + ".push_back(" + nextVariantAccessor + ");");
88  }
89  block_if_data->addLine("auto " + variantAccessor + " = " + ARON_WRITER_ACCESSOR + ".writeTuple(" + acceptedTypesAccessor + ", " +
91  "armarx::aron::Path("+ARON_PATH_ACCESSOR+", {"+simox::alg::join(p.getPath(), ", ")+"})); // of " + cppAccessor);
92  return block_if_data;
93  }
94 
95  CppBlockPtr Tuple::getWriteBlock(const std::string& cppAccessor, const Path& p, std::string& variantAccessor) const
96  {
97  CppBlockPtr block_if_data = std::make_shared<CppBlock>();
98  std::string resolved_accessor = resolveMaybeAccessor(cppAccessor);
99  std::string escaped_accessor = EscapeAccessor(cppAccessor);
100  variantAccessor = ARON_VARIANT_RETURN_ACCESSOR+ "_" + escaped_accessor;
101 
102  const std::string elementsAccessor = variantAccessor + "_tupleElements";
103  block_if_data->addLine("std::vector<_Aron_T> " + elementsAccessor + ";");
104 
105  unsigned int i = 0;
106  for (const auto& type : type.getAcceptedTypes())
107  {
108  std::string accessor_iterator = "std::get<" + std::to_string(i) + ">("+resolved_accessor+");";
109  auto type_s = FromAronType(*type);
110  std::string nextVariantAccessor;
111  Path nextPath = p.withElement("\"" + std::to_string(i++) + "\"");
112  CppBlockPtr b2 = type_s->getWriteBlock(accessor_iterator, nextPath, nextVariantAccessor);
113  block_if_data->addLine("auto " + nextVariantAccessor + " = " + ARON_WRITER_ACCESSOR + ".writeNull();");
114  block_if_data->appendBlock(b2);
115  block_if_data->addLine(elementsAccessor + ".push_back(" + nextVariantAccessor + ");");
116  }
117 
118  block_if_data->addLine(variantAccessor+ " = " + ARON_WRITER_ACCESSOR + ".writeTuple("+elementsAccessor+", "+
119  "armarx::aron::Path("+ARON_PATH_ACCESSOR+", {" + simox::alg::join(p.getPath(), ", ") + "})); // of " + cppAccessor);
120  return resolveMaybeWriteBlock(block_if_data, cppAccessor);
121  }
122 
123  CppBlockPtr Tuple::getReadBlock(const std::string& cppAccessor, const std::string& variantAccessor) const
124  {
125  CppBlockPtr block_if_data = std::make_shared<CppBlock>();
126  std::string escaped_accessor = EscapeAccessor(cppAccessor);
127  std::string resolved_accessor = resolveMaybeAccessor(cppAccessor);
128  std::string elements_accessor = ARON_VARIABLE_PREFIX + "_" + escaped_accessor + "_tupleElements";
129 
130  block_if_data->addLine("std::vector<_Aron_TNonConst> " + elements_accessor + ";");
131  block_if_data->addLine("" + ARON_READER_ACCESSOR + ".readList("+elements_accessor+"); // of " + cppAccessor);
132 
133  unsigned int i = 0;
134  for (const auto& type : type.getAcceptedTypes())
135  {
136  auto type_s = FromAronType(*type);
137  CppBlockPtr b2 = type_s->getReadBlock("std::get<" + std::to_string(i) + ">(" + resolved_accessor + ")", elements_accessor+"[" + std::to_string(i) + "]");
138  block_if_data->appendBlock(b2);
139  i++;
140  }
141  return resolveMaybeReadBlock(block_if_data, cppAccessor, variantAccessor);
142  }
143 }
144 
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
Tuple.h
armarx::aron::ret
ReaderT::InputType T & ret
Definition: rw.h:21
armarx::aron::codegenerator::cpp::generator::Tuple
Definition: Tuple.h:32
armarx::aron::type::detail::SpecializedVariantBase::getMaybe
type::Maybe getMaybe() const override
get the maybe type
Definition: SpecializedVariant.h:92
armarx::aron::Path::withAcceptedTypeIndex
Path withAcceptedTypeIndex(int, bool escape=false) const
Definition: Path.cpp:183
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
armarx::aron::codegenerator::cpp::Generator::ARON_PATH_ACCESSOR
static const std::string ARON_PATH_ACCESSOR
Definition: Generator.h:150
armarx::aron::type::Tuple
The Tuple class.
Definition: Tuple.h:39
armarx::aron::Path
The Path class.
Definition: Path.h:36
armarx::aron::codegenerator::cpp::generator::Tuple::getWriteBlock
CppBlockPtr getWriteBlock(const std::string &cppAccessor, const Path &, std::string &variantAccessor) const final
Definition: Tuple.cpp:95
armarx::aron::codegenerator::cpp::generator::Tuple::getReadBlock
CppBlockPtr getReadBlock(const std::string &cppAccessor, const std::string &variantAccessor) const final
Definition: Tuple.cpp:123
armarx::aron::codegenerator::cpp::generator::Tuple::Tuple
Tuple(const type::Tuple &e)
Definition: Tuple.cpp:32
armarx::aron::codegenerator::cpp::generator::List
Definition: List.h:32
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::aron::type::Tuple::getAcceptedTypes
std::vector< VariantPtr > getAcceptedTypes() const
Definition: Tuple.cpp:59
armarx::aron::codegenerator::cpp::generator::detail::SpecializedGeneratorBase< type::Tuple, Tuple >::type
type::Tuple type
Definition: SpecializedGenerator.h:52
armarx::aron::codegenerator::cpp::generator::Tuple::getWriteTypeBlock
CppBlockPtr getWriteTypeBlock(const std::string &typeAccessor, const std::string &cppAccessor, const Path &, std::string &variantAccessor) const final
Definition: Tuple.cpp:68
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::Tuple::getResetSoftBlock
CppBlockPtr getResetSoftBlock(const std::string &cppAccessor) const final
Definition: Tuple.cpp:53
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:40
armarx::aron::codegenerator::cpp::generator
Definition: AnyObject.cpp:29
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::generator::Tuple::getRequiredIncludes
std::vector< std::string > getRequiredIncludes() const final
Definition: Tuple.cpp:42
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