IntEnum.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 "IntEnum.h"
26
27#include <SimoxUtility/meta/type_name.h>
28
30{
31 // constructors
34 e.getEnumName(),
35 e.getEnumName(),
36 simox::meta::get_type_name<data::dto::NDArray>(),
37 simox::meta::get_type_name<type::dto::IntEnum>(),
38 e)
39 {
40 if (type.getMaybe() != type::Maybe::NONE)
41 {
42 //due to this being a specialized variant type the getMaybe() function will not actually check the xml
43 //the maybe flag is always set to the default value of NONE
44 std::string message = "\n\nSomehow the maybe flag of a top level int enum "
45 "declaration is set. This is not valid!\n"
46 "Maybe value is: " +
47 std::to_string((int)type.getMaybe()) +
48 "\n"
49 "Path is: " +
50 type.getPath().toString() +
51 "\n"
52 "Check your xml definition! \n";
53 throw error::ValueNotValidException(
54 __PRETTY_FUNCTION__, message, std::to_string((int)type.getMaybe()));
55 }
56 }
57
58 std::pair<std::vector<std::pair<std::string, std::string>>, bool>
59 IntEnum::getCtorInitializers(const std::string& name) const
60 {
61 if (type.getMaybe() == type::Maybe::OPTIONAL)
62 {
63 return {{{name, "std::nullopt"}}, true};
64 }
65
66 return {{{name, getInstantiatedCppTypename() + "::" + type.getDefaultValueName()}}, true};
67 }
68
70 IntEnum::getResetSoftBlock(const std::string& accessor) const
71 {
72 CppBlockPtr block_if_data = std::make_shared<CppBlock>();
73 block_if_data->addLine(accessor + nextEl() + "resetSoft();");
74 return this->resolveMaybeResetSoftBlock(block_if_data, accessor);
75 }
76
78 IntEnum::getResetHardBlock(const std::string& accessor) const
79 {
80 CppBlockPtr b = std::make_shared<CppBlock>();
81 if (type.getMaybe() != type::Maybe::NONE)
82 {
83 b->addLine(accessor + " = {};");
84 }
85 else
86 {
87 b->addLine(accessor + nextEl() + "resetHard();");
88 }
89 return this->resolveMaybeResetHardBlock(b, accessor);
90 }
91
93 IntEnum::getWriteTypeBlock(const std::string& typeAccessor,
94 const std::string& cppAccessor,
95 const Path& p,
96 std::string& variantAccessor) const
97 {
98 CppBlockPtr b = std::make_shared<CppBlock>();
99 std::string escaped_accessor = EscapeAccessor(cppAccessor);
100 variantAccessor = ARON_VARIANT_RETURN_ACCESSOR + "_" + escaped_accessor;
101
102 b->addLine("auto " + variantAccessor + " = " + getInstantiatedCppTypename() +
103 "::writeType(" + ARON_WRITER_ACCESSOR + ", " + "{}, " +
104 conversion::Maybe2CppString.at(type.getMaybe()) + ", " + "armarx::aron::Path(" +
105 ARON_PATH_ACCESSOR + ", {" + simox::alg::join(p.getPath(), ", ") +
106 "})); // of " + cppAccessor);
107 return b;
108 }
109
111 IntEnum::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 block_if_data->addLine(variantAccessor + " = " + cppAccessor + nextEl() + "write(" +
120 ARON_WRITER_ACCESSOR + ", " + "armarx::aron::Path(" +
121 ARON_PATH_ACCESSOR + ", {" + simox::alg::join(p.getPath(), ", ") +
122 "})); // of " + cppAccessor);
123
124 return resolveMaybeWriteBlock(block_if_data, cppAccessor);
125 }
126
128 IntEnum::getReadBlock(const std::string& cppAccessor, const std::string& variantAccessor) const
129 {
130 CppBlockPtr block_if_data = std::make_shared<CppBlock>();
131 if (const auto reset = resolveMaybeGeneratorWithSetter(cppAccessor); !reset.empty())
132 {
133 block_if_data->addLine(reset);
134 }
135 block_if_data->addLine(cppAccessor + nextEl() + "read(" + ARON_READER_ACCESSOR + ", " +
136 variantAccessor + ");");
137 return resolveMaybeReadBlock(block_if_data, cppAccessor, variantAccessor);
138 }
139} // namespace armarx::aron::codegenerator::cpp::generator
The Path class.
Definition Path.h:36
std::vector< std::string > getPath() const
Definition Path.cpp:87
CppBlockPtr resolveMaybeResetHardBlock(const CppBlockPtr &, const std::string &) const
CppBlockPtr resolveMaybeReadBlock(const CppBlockPtr &, const std::string &, const std::string &) const
std::string resolveMaybeGeneratorWithSetter(const std::string &, const std::string &args="") const
static std::string EscapeAccessor(const std::string &)
Definition Generator.cpp:53
CppBlockPtr resolveMaybeResetSoftBlock(const CppBlockPtr &, const std::string &) const
static const std::string ARON_READER_ACCESSOR
Definition Generator.h:164
static const std::string ARON_PATH_ACCESSOR
Definition Generator.h:163
static const std::string ARON_WRITER_ACCESSOR
Definition Generator.h:165
static const std::string ARON_VARIANT_RETURN_ACCESSOR
Definition Generator.h:167
CppBlockPtr resolveMaybeWriteBlock(const CppBlockPtr &, const std::string &) const
CppBlockPtr getReadBlock(const std::string &cppAccessor, const std::string &variantAccessor) const final
Definition IntEnum.cpp:128
CppBlockPtr getResetHardBlock(const std::string &cppAccessor) const final
Definition IntEnum.cpp:78
std::pair< std::vector< std::pair< std::string, std::string > >, bool > getCtorInitializers(const std::string &name) const final
Definition IntEnum.cpp:59
CppBlockPtr getWriteTypeBlock(const std::string &typeAccessor, const std::string &cppAccessor, const Path &, std::string &variantAccessor) const final
Definition IntEnum.cpp:93
CppBlockPtr getWriteBlock(const std::string &cppAccessor, const Path &, std::string &variantAccessor) const final
Definition IntEnum.cpp:111
CppBlockPtr getResetSoftBlock(const std::string &cppAccessor) const final
Definition IntEnum.cpp:70
SpecializedGeneratorBase(const std::string &instantiatedCppTypename, const std::string &classCppTypename, const std::string &aronDataTypename, const std::string &aronTypeTypename, const type::IntEnum &t)
The IntEnum class.
Definition IntEnum.h:43
const std::map< type::Maybe, std::string > Maybe2CppString
Definition Generator.h:48
A convenience header to include all aron files (full include, not forward declared)
std::shared_ptr< CppBlock > CppBlockPtr
Definition CppBlock.h:37
Definition Impl.cpp:41