AnyObject.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 #include "AnyObject.h"
25 
26 #include <SimoxUtility/meta/type_name.h>
27 
28 
30 {
31  /* constructors */
33  detail::AnyGenerator<type::AnyObject, AnyObject>(
34  "armarx::aron::data::Dict",
35  "armarx::aron::data::Dict",
36  simox::meta::get_type_name<data::dto::Dict>(),
37  simox::meta::get_type_name<type::dto::AnyObject>(),
38  e)
39  {
40  // if the type is not known, we only accept shared ptr for unspecified aron dicts
41  if (type.getMaybe() != type::Maybe::SHARED_PTR)
42  {
43  throw error::ValueNotValidException(__PRETTY_FUNCTION__, "At the moment the unknown any object type must be a shared_ptr! ", std::to_string((int) type.getMaybe()) + " aka " + type::defaultconversion::string::Maybe2String.at(type.getMaybe()), type.getPath());
44  }
45 
46  }
47 
48  /* virtual implementations */
49 
50  std::vector<std::string> AnyObject::getRequiredIncludes() const
51  {
52  std::vector<std::string> required_includes = {"<RobotAPI/libraries/aron/core/data/converter/variant/VariantConverter.h>"};
53  return required_includes;
54  }
55 
56  CppBlockPtr AnyObject::getWriteBlock(const std::string& cppAccessor, const Path& p, std::string& variantAccessor) const
57  {
58  auto block_if_data = std::make_shared<CppBlock>();
59  std::string escaped_accessor = this->EscapeAccessor(cppAccessor);
60  variantAccessor = Generator::ARON_VARIANT_RETURN_ACCESSOR + "_" + escaped_accessor;
61 
62  block_if_data->addLine(variantAccessor + " = ::armarx::aron::data::readAndWrite<::armarx::aron::data::FromVariantConverter<WriterT>>(" + cppAccessor + "); // of " + cppAccessor);
63 
64  return block_if_data;
65  }
66 
67  CppBlockPtr AnyObject::getReadBlock(const std::string& cppAccessor, const std::string& variantAccessor) const
68  {
69  auto block_if_data = std::make_shared<CppBlock>();
70  std::string escaped_accessor = this->EscapeAccessor(cppAccessor);
71 
72  block_if_data->addLine(cppAccessor + " = ::armarx::aron::data::Dict::DynamicCastAndCheck(::armarx::aron::data::readAndWrite<::armarx::aron::data::ToVariantConverter<ReaderT>>(" + variantAccessor + ")); // of " + cppAccessor);
73 
74  return block_if_data;
75  }
76 
77  CppBlockPtr AnyObject::getWriteTypeBlock(const std::string& typeAccessor, const std::string& accessor, const Path& p, std::string& variantAccessor) const
78  {
79  CppBlockPtr b = std::make_shared<CppBlock>();
80  std::string escaped_accessor = EscapeAccessor(accessor);
81  variantAccessor = ARON_VARIANT_RETURN_ACCESSOR + "_" + escaped_accessor;
82 
83  b->addLine("auto " + variantAccessor + " = " + ARON_WRITER_ACCESSOR + ".writeAnyObject(" + conversion::Maybe2CppString.at(type.getMaybe()) + ", " +
84  "::armarx::aron::Path("+ARON_PATH_ACCESSOR+", {"+simox::alg::join(p.getPath(), ", ")+"})); // of " + typeAccessor);
85  return b;
86  }
87 }
armarx::aron::type::AnyObject
The AnyObject class.
Definition: AnyObject.h:37
armarx::aron::codegenerator::cpp::Generator::ARON_VARIANT_RETURN_ACCESSOR
static const std::string ARON_VARIANT_RETURN_ACCESSOR
Definition: Generator.h:154
armarx::aron::type::detail::SpecializedVariantBase::getMaybe
type::Maybe getMaybe() const override
get the maybe type
Definition: SpecializedVariant.h:92
armarx::aron::codegenerator::cpp::generator::AnyObject::getWriteBlock
CppBlockPtr getWriteBlock(const std::string &cppAccessor, const Path &p, std::string &variantAccessor) const final
Definition: AnyObject.cpp:56
armarx::aron::codegenerator::cpp::Generator::EscapeAccessor
static std::string EscapeAccessor(const std::string &)
Definition: Generator.cpp:53
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
armarx::aron::codegenerator::cpp::Generator::ARON_PATH_ACCESSOR
static const std::string ARON_PATH_ACCESSOR
Definition: Generator.h:150
armarx::aron::Path
The Path class.
Definition: Path.h:36
AnyObject.h
armarx::aron::error::ValueNotValidException
The ValueNotValidException class.
Definition: Exception.h:145
armarx::aron::codegenerator::cpp::generator::AnyObject::getWriteTypeBlock
CppBlockPtr getWriteTypeBlock(const std::string &typeAccessor, const std::string &cppAccessor, const Path &p, std::string &variantAccessor) const final
Definition: AnyObject.cpp:77
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::aron::codegenerator::cpp::generator::detail::SpecializedGeneratorBase< type::AnyObject, AnyObject >::type
type::AnyObject type
Definition: SpecializedGenerator.h:52
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::AnyObject::getRequiredIncludes
std::vector< std::string > getRequiredIncludes() const final
Definition: AnyObject.cpp:50
armarx::aron::codegenerator::cpp::generator::AnyObject::AnyObject
AnyObject(const type::AnyObject &e)
Definition: AnyObject.cpp:32
armarx::aron::codegenerator::cpp::generator::AnyObject::getReadBlock
CppBlockPtr getReadBlock(const std::string &cppAccessor, const std::string &variantAccessor) const final
Definition: AnyObject.cpp:67
armarx::aron::codegenerator::cpp::generator::AnyObject
Definition: AnyObject.h:32
simox
Definition: Impl.cpp:40
armarx::aron::codegenerator::cpp::generator::Dict
Definition: Dict.h:31
armarx::aron::type::defaultconversion::string::Maybe2String
const std::map< type::Maybe, std::string > Maybe2String
Definition: Descriptor.h:125
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