Pair.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 "Pair.h"
26 
27 #include <SimoxUtility/algorithm/vector.hpp>
28 #include <SimoxUtility/meta/type_name.h>
29 
31 {
32  Pair::Pair(const type::Pair& e) :
33  detail::ContainerGenerator<type::Pair, Pair>(
34  "std::pair<" + ExtractCppTypename(*e.getFirstAcceptedType()) + ", " +
35  ExtractCppTypename(*e.getSecondAcceptedType()) + ">",
36  "std::pair<" + ExtractCppTypename(*e.getFirstAcceptedType()) + ", " +
37  ExtractCppTypename(*e.getSecondAcceptedType()) + ">",
38  simox::meta::get_type_name<data::dto::List>(),
39  simox::meta::get_type_name<type::dto::Pair>(),
40  e)
41  {
42  }
43 
44  std::vector<std::string>
46  {
47  auto child_s1 = FromAronType(*type.getFirstAcceptedType());
48  auto child_s2 = FromAronType(*type.getSecondAcceptedType());
49 
50  auto l1 = child_s1->getRequiredIncludes();
51  auto l2 = child_s2->getRequiredIncludes();
52 
53  return simox::alg::appended(l1, l2);
54  }
55 
57  Pair::getResetSoftBlock(const std::string& cppAccessor) const
58  {
59  CppBlockPtr block_if_data = std::make_shared<CppBlock>();
60  std::string resolved_accessor = resolveMaybeAccessor(cppAccessor);
61 
62  auto child_s1 = FromAronType(*type.getFirstAcceptedType());
63  CppBlockPtr b21 = child_s1->getResetSoftBlock(cppAccessor + nextEl() + "first");
64  block_if_data->appendBlock(b21);
65 
66  auto child_s2 = FromAronType(*type.getSecondAcceptedType());
67  CppBlockPtr b22 = child_s2->getResetSoftBlock(cppAccessor + nextEl() + "second");
68  block_if_data->appendBlock(b22);
69  return this->resolveMaybeResetSoftBlock(block_if_data, cppAccessor);
70  }
71 
73  Pair::getWriteTypeBlock(const std::string& typeAccessor,
74  const std::string& cppAccessor,
75  const Path& p,
76  std::string& variantAccessor) const
77  {
78  CppBlockPtr block_if_data = std::make_shared<CppBlock>();
79  std::string escaped_accessor = EscapeAccessor(cppAccessor);
80  variantAccessor = ARON_VARIANT_RETURN_ACCESSOR + "_" + escaped_accessor;
81 
82  auto child_s1 = FromAronType(*type.getFirstAcceptedType());
83  std::string accessor_iterator1 = cppAccessor + nextEl() + "first";
84  std::string firstVariantAccessor;
85  Path firstPath = p.withAcceptedTypeIndex(0);
86  CppBlockPtr b21 = child_s1->getWriteTypeBlock(child_s1->getInstantiatedCppTypename(),
87  accessor_iterator1,
88  firstPath,
89  firstVariantAccessor);
90  block_if_data->appendBlock(b21);
91 
92  auto child_s2 = FromAronType(*type.getSecondAcceptedType());
93  std::string accessor_iterator2 = cppAccessor + nextEl() + "second";
94  std::string secondVariantAccessor;
95  Path secondPath = p.withAcceptedTypeIndex(1);
96  CppBlockPtr b22 = child_s2->getWriteTypeBlock(child_s2->getInstantiatedCppTypename(),
97  accessor_iterator2,
98  secondPath,
99  secondVariantAccessor);
100  block_if_data->appendBlock(b22);
101 
102  block_if_data->addLine("auto " + variantAccessor + " = " + ARON_WRITER_ACCESSOR +
103  ".writePair(" + firstVariantAccessor + ", " + secondVariantAccessor +
104  ", " + conversion::Maybe2CppString.at(type.getMaybe()) + ", " +
105  "armarx::aron::Path(" + ARON_PATH_ACCESSOR + ", {" +
106  simox::alg::join(p.getPath(), ", ") + "})); // of " + cppAccessor);
107  return block_if_data;
108  }
109 
111  Pair::getWriteBlock(const std::string& cppAccessor,
112  const Path& p,
113  std::string& variantAccessor) const
114  {
115  CppBlockPtr block_if_data = std::make_shared<CppBlock>();
116  std::string escaped_accessor = EscapeAccessor(cppAccessor);
117  variantAccessor = ARON_VARIANT_RETURN_ACCESSOR + "_" + escaped_accessor;
118 
119  auto child_s1 = FromAronType(*type.getFirstAcceptedType());
120  std::string accessor_iterator1 = cppAccessor + nextEl() + "first";
121  std::string firstVariantAccessor;
122  Path firstPath = p.withElement("\"0\"");
123  CppBlockPtr b21 =
124  child_s1->getWriteBlock(accessor_iterator1, firstPath, firstVariantAccessor);
125  block_if_data->addLine("auto " + firstVariantAccessor + " = " + ARON_WRITER_ACCESSOR +
126  ".writeNull();");
127  block_if_data->appendBlock(b21);
128 
129  auto child_s2 = FromAronType(*type.getSecondAcceptedType());
130  std::string accessor_iterator2 = cppAccessor + nextEl() + "second";
131  std::string secondVariantAccessor;
132  Path secondPath = p.withElement("\"1\"");
133  CppBlockPtr b22 =
134  child_s2->getWriteBlock(accessor_iterator2, secondPath, secondVariantAccessor);
135  block_if_data->addLine("auto " + secondVariantAccessor + " = " + ARON_WRITER_ACCESSOR +
136  ".writeNull();");
137  block_if_data->appendBlock(b22);
138 
139  block_if_data->addLine(variantAccessor + " = " + ARON_WRITER_ACCESSOR + ".writePair(" +
140  firstVariantAccessor + ", " + secondVariantAccessor + ", " +
141  "armarx::aron::Path(" + ARON_PATH_ACCESSOR + ", {" +
142  simox::alg::join(p.getPath(), ", ") + "})); // of " + cppAccessor);
143  return resolveMaybeWriteBlock(block_if_data, cppAccessor);
144  }
145 
147  Pair::getReadBlock(const std::string& cppAccessor, const std::string& variantAccessor) const
148  {
149  CppBlockPtr block_if_data = std::make_shared<CppBlock>();
150  std::string escaped_accessor = EscapeAccessor(cppAccessor);
151  std::string elements_accessor =
152  ARON_VARIABLE_PREFIX + "_" + escaped_accessor + "_pairElements";
153 
154  block_if_data->addLine("std::vector<_Aron_TNonConst> " + elements_accessor + ";");
155  block_if_data->addLine("" + ARON_READER_ACCESSOR + ".readList(" + elements_accessor +
156  "); // of " + cppAccessor);
157 
158  auto child_s1 = FromAronType(*type.getFirstAcceptedType());
159  CppBlockPtr b21 =
160  child_s1->getReadBlock(cppAccessor + nextEl() + "first", elements_accessor + "[0]");
161  block_if_data->appendBlock(b21);
162 
163  auto child_s2 = FromAronType(*type.getSecondAcceptedType());
164  CppBlockPtr b22 =
165  child_s2->getReadBlock(cppAccessor + nextEl() + "second", elements_accessor + "[1]");
166  block_if_data->appendBlock(b22);
167 
168  return resolveMaybeReadBlock(block_if_data, cppAccessor, variantAccessor);
169  }
170 } // namespace armarx::aron::codegenerator::cpp::generator
armarx::aron::Path::withElement
Path withElement(const std::string &, bool escape=false) const
Definition: Path.cpp:163
armarx::aron::codegenerator::cpp::Generator::ARON_VARIANT_RETURN_ACCESSOR
static const std::string ARON_VARIANT_RETURN_ACCESSOR
Definition: Generator.h:167
armarx::aron::codegenerator::cpp::generator::Pair::getRequiredIncludes
std::vector< std::string > getRequiredIncludes() const final
Definition: Pair.cpp:45
armarx::aron::codegenerator::cpp::generator::Pair::getResetSoftBlock
CppBlockPtr getResetSoftBlock(const std::string &cppAccessor) const final
Definition: Pair.cpp:57
armarx::aron::type::detail::SpecializedVariantBase::getMaybe
type::Maybe getMaybe() const override
get the maybe type
Definition: SpecializedVariant.h:87
armarx::aron::Path::withAcceptedTypeIndex
Path withAcceptedTypeIndex(int, bool escape=false) const
Definition: Path.cpp:185
armarx::aron::codegenerator::cpp::generator::Pair
Definition: Pair.h:31
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:160
detail
Definition: OpenCVUtil.cpp:128
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:37
armarx::aron::codegenerator::cpp::Generator::ARON_WRITER_ACCESSOR
static const std::string ARON_WRITER_ACCESSOR
Definition: Generator.h:165
armarx::aron::codegenerator::cpp::generator::Pair::getWriteBlock
CppBlockPtr getWriteBlock(const std::string &cppAccessor, const Path &, std::string &variantAccessor) const final
Definition: Pair.cpp:111
armarx::aron::codegenerator::cpp::generator::Pair::getWriteTypeBlock
CppBlockPtr getWriteTypeBlock(const std::string &typeAccessor, const std::string &cppAccessor, const Path &, std::string &variantAccessor) const final
Definition: Pair.cpp:73
armarx::aron::codegenerator::cpp::Generator::ARON_PATH_ACCESSOR
static const std::string ARON_PATH_ACCESSOR
Definition: Generator.h:163
armarx::aron::type::Pair::getFirstAcceptedType
VariantPtr getFirstAcceptedType() const
Definition: Pair.cpp:50
armarx::aron::Path
The Path class.
Definition: Path.h:35
armarx::aron::type::Pair::getSecondAcceptedType
VariantPtr getSecondAcceptedType() const
Definition: Pair.cpp:56
armarx::aron::codegenerator::cpp::generator::List
Definition: List.h:31
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::aron::codegenerator::cpp::generator::detail::SpecializedGeneratorBase< type::Pair, Pair >::type
type::Pair type
Definition: SpecializedGenerator.h:59
Pair.h
armarx::aron::type::Pair
The Pair class.
Definition: Pair.h:38
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:28
armarx::aron::codegenerator::cpp::generator::Pair::Pair
Pair(const type::Pair &e)
Definition: Pair.cpp:32
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:164
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:48
armarx::aron::Path::getPath
std::vector< std::string > getPath() const
Definition: Path.cpp:87
armarx::aron::codegenerator::cpp::generator::Pair::getReadBlock
CppBlockPtr getReadBlock(const std::string &cppAccessor, const std::string &variantAccessor) const final
Definition: Pair.cpp:147