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/meta/type_name.h>
28 #include <SimoxUtility/algorithm/vector.hpp>
29 
31 {
32  Pair::Pair(const type::Pair& e) :
33  detail::ContainerGenerator<type::Pair, Pair>(
34  "std::pair<" + ExtractCppTypename(*e.getFirstAcceptedType()) + ", " + ExtractCppTypename(*e.getSecondAcceptedType()) + ">",
35  "std::pair<" + ExtractCppTypename(*e.getFirstAcceptedType()) + ", " + ExtractCppTypename(*e.getSecondAcceptedType()) + ">",
36  simox::meta::get_type_name<data::dto::List>(),
37  simox::meta::get_type_name<type::dto::Pair>(), e)
38  {
39  }
40 
41 
42  std::vector<std::string> Pair::getRequiredIncludes() const
43  {
44  auto child_s1 = FromAronType(*type.getFirstAcceptedType());
45  auto child_s2 = FromAronType(*type.getSecondAcceptedType());
46 
47  auto l1 = child_s1->getRequiredIncludes();
48  auto l2 = child_s2->getRequiredIncludes();
49 
50  return simox::alg::appended(l1, l2);
51  }
52 
53  CppBlockPtr Pair::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  auto child_s1 = FromAronType(*type.getFirstAcceptedType());
59  CppBlockPtr b21 = child_s1->getResetSoftBlock(cppAccessor + nextEl() + "first");
60  block_if_data->appendBlock(b21);
61 
62  auto child_s2 = FromAronType(*type.getSecondAcceptedType());
63  CppBlockPtr b22 = child_s2->getResetSoftBlock(cppAccessor + nextEl() + "second");
64  block_if_data->appendBlock(b22);
65  return this->resolveMaybeResetSoftBlock(block_if_data, cppAccessor);
66  }
67 
68  CppBlockPtr Pair::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 escaped_accessor = EscapeAccessor(cppAccessor);
72  variantAccessor = ARON_VARIANT_RETURN_ACCESSOR+ "_" + escaped_accessor;
73 
74  auto child_s1 = FromAronType(*type.getFirstAcceptedType());
75  std::string accessor_iterator1 = cppAccessor + nextEl() + "first";
76  std::string firstVariantAccessor;
77  Path firstPath = p.withAcceptedTypeIndex(0);
78  CppBlockPtr b21 = child_s1->getWriteTypeBlock(child_s1->getInstantiatedCppTypename(), accessor_iterator1, firstPath, firstVariantAccessor);
79  block_if_data->appendBlock(b21);
80 
81  auto child_s2 = FromAronType(*type.getSecondAcceptedType());
82  std::string accessor_iterator2 = cppAccessor + nextEl() + "second";
83  std::string secondVariantAccessor;
84  Path secondPath = p.withAcceptedTypeIndex(1);
85  CppBlockPtr b22 = child_s2->getWriteTypeBlock(child_s2->getInstantiatedCppTypename(), accessor_iterator2, secondPath, secondVariantAccessor);
86  block_if_data->appendBlock(b22);
87 
88  block_if_data->addLine("auto " + variantAccessor + " = " + ARON_WRITER_ACCESSOR + ".writePair(" + firstVariantAccessor + ", " +
89  secondVariantAccessor + ", " +
91  "armarx::aron::Path("+ARON_PATH_ACCESSOR+", {"+simox::alg::join(p.getPath(), ", ")+"})); // of " + cppAccessor);
92  return block_if_data;
93  }
94 
95  CppBlockPtr Pair::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 escaped_accessor = EscapeAccessor(cppAccessor);
99  variantAccessor = ARON_VARIANT_RETURN_ACCESSOR+ "_" + escaped_accessor;
100 
101  auto child_s1 = FromAronType(*type.getFirstAcceptedType());
102  std::string accessor_iterator1 = cppAccessor + nextEl() + "first";
103  std::string firstVariantAccessor;
104  Path firstPath = p.withElement("\"0\"");
105  CppBlockPtr b21 = child_s1->getWriteBlock(accessor_iterator1, firstPath, firstVariantAccessor);
106  block_if_data->addLine("auto " + firstVariantAccessor + " = " + ARON_WRITER_ACCESSOR + ".writeNull();");
107  block_if_data->appendBlock(b21);
108 
109  auto child_s2 = FromAronType(*type.getSecondAcceptedType());
110  std::string accessor_iterator2 = cppAccessor + nextEl() + "second";
111  std::string secondVariantAccessor;
112  Path secondPath = p.withElement("\"1\"");
113  CppBlockPtr b22 = child_s2->getWriteBlock(accessor_iterator2, secondPath, secondVariantAccessor);
114  block_if_data->addLine("auto " + secondVariantAccessor + " = " + ARON_WRITER_ACCESSOR + ".writeNull();");
115  block_if_data->appendBlock(b22);
116 
117  block_if_data->addLine(variantAccessor + " = " + ARON_WRITER_ACCESSOR + ".writePair("+firstVariantAccessor+", "+
118  secondVariantAccessor+", " +
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 Pair::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 elements_accessor = ARON_VARIABLE_PREFIX + "_" + escaped_accessor + "_pairElements";
128 
129  block_if_data->addLine("std::vector<_Aron_TNonConst> " + elements_accessor + ";");
130  block_if_data->addLine("" + ARON_READER_ACCESSOR + ".readList("+elements_accessor+"); // of " + cppAccessor);
131 
132  auto child_s1 = FromAronType(*type.getFirstAcceptedType());
133  CppBlockPtr b21 = child_s1->getReadBlock(cppAccessor + nextEl() + "first", elements_accessor+"[0]");
134  block_if_data->appendBlock(b21);
135 
136  auto child_s2 = FromAronType(*type.getSecondAcceptedType());
137  CppBlockPtr b22 = child_s2->getReadBlock(cppAccessor + nextEl() + "second", elements_accessor+"[1]");
138  block_if_data->appendBlock(b22);
139 
140  return resolveMaybeReadBlock(block_if_data, cppAccessor, variantAccessor);
141  }
142 }
143 
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::Pair::getRequiredIncludes
std::vector< std::string > getRequiredIncludes() const final
Definition: Pair.cpp:42
armarx::aron::codegenerator::cpp::generator::Pair::getResetSoftBlock
CppBlockPtr getResetSoftBlock(const std::string &cppAccessor) const final
Definition: Pair.cpp:53
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::Pair
Definition: Pair.h:32
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::Pair::getWriteBlock
CppBlockPtr getWriteBlock(const std::string &cppAccessor, const Path &, std::string &variantAccessor) const final
Definition: Pair.cpp:95
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:68
armarx::aron::codegenerator::cpp::Generator::ARON_PATH_ACCESSOR
static const std::string ARON_PATH_ACCESSOR
Definition: Generator.h:150
armarx::aron::type::Pair::getFirstAcceptedType
VariantPtr getFirstAcceptedType() const
Definition: Pair.cpp:50
armarx::aron::Path
The Path class.
Definition: Path.h:36
armarx::aron::type::Pair::getSecondAcceptedType
VariantPtr getSecondAcceptedType() const
Definition: Pair.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::detail::SpecializedGeneratorBase< type::Pair, Pair >::type
type::Pair type
Definition: SpecializedGenerator.h:52
Pair.h
armarx::aron::type::Pair
The Pair class.
Definition: Pair.h:39
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::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: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::codegenerator::cpp::generator::Pair::getReadBlock
CppBlockPtr getReadBlock(const std::string &cppAccessor, const std::string &variantAccessor) const final
Definition: Pair.cpp:123