AronDTOWriter.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 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22
23// Header
24#include "AronDTOWriter.h"
25
26// STD/STL
27#include <cstring>
28#include <functional>
29#include <numeric>
30#include <utility>
31
32// ArmarX
34
36
38{
39
42 {
43 if (not input)
44 {
46 }
47 return aron::data::Aron2Descriptor(*input);
48 }
49
50 data::dto::GenericDataPtr
51 AronDTOWriter::writeList(const std::vector<data::dto::GenericDataPtr>& elements, const Path&)
52 {
53 data::dto::ListPtr o = new data::dto::List();
54 o->elements = elements;
55 return o;
56 }
57
58 data::dto::GenericDataPtr
59 AronDTOWriter::writeDict(const std::map<std::string, data::dto::GenericDataPtr>& elements,
60 const std::optional<data::dto::GenericDataPtr>& extends,
61 const Path&)
62 {
63 if (extends)
64 {
65 auto o = DictCastAndCheck(extends.value());
66 o->elements.insert(elements.begin(), elements.end());
67 return o;
68 }
69
70 data::dto::DictPtr o = new data::dto::Dict();
71 o->elements = elements;
72 return o;
73 }
74
75 data::dto::GenericDataPtr
76 AronDTOWriter::writeDict(std::map<std::string, data::dto::GenericDataPtr>&& elements,
77 const std::optional<data::dto::GenericDataPtr>& extends,
78 const Path& p)
79 {
80 if (extends)
81 {
82 const auto& constElements = elements;
83 return writeDict(constElements, extends, p);
84 }
85
86 data::dto::DictPtr o = new data::dto::Dict();
87 o->elements = std::move(elements);
88 return o;
89 }
90
91 data::dto::GenericDataPtr
92 AronDTOWriter::writeNDArray(const std::vector<int>& shape,
93 const std::string& typeAsString,
94 const unsigned char* data,
95 const Path& p)
96 {
97 if (typeAsString.empty())
98 {
100 throw error::AronException(__PRETTY_FUNCTION__, "The type cannot be empty", p);
101 }
102
103 data::dto::NDArrayPtr o = new data::dto::NDArray();
104 o->shape = shape;
105 o->type = typeAsString;
106
107 const int size =
108 shape.empty()
109 ? 0
110 : std::accumulate(std::begin(shape), std::end(shape), 1, std::multiplies<int>());
111
112 o->data = std::vector<unsigned char>(static_cast<unsigned int>(size));
113 if (size > 0)
114 {
115 std::memcpy(o->data.data(), data, static_cast<unsigned int>(size));
116 }
117 return o;
118 }
119
120 data::dto::GenericDataPtr
121 AronDTOWriter::writeInt(const int i, const Path&)
122 {
123 data::dto::AronIntPtr o = new data::dto::AronInt();
124 o->value = i;
125 return o;
126 }
127
128 data::dto::GenericDataPtr
129 AronDTOWriter::writeLong(const long i, const Path&)
130 {
131 data::dto::AronLongPtr o = new data::dto::AronLong();
132 o->value = i;
133 return o;
134 }
135
136 data::dto::GenericDataPtr
137 AronDTOWriter::writeFloat(const float i, const Path&)
138 {
139 data::dto::AronFloatPtr o = new data::dto::AronFloat();
140 o->value = i;
141 return o;
142 }
143
144 data::dto::GenericDataPtr
145 AronDTOWriter::writeDouble(const double i, const Path&)
146 {
147 data::dto::AronDoublePtr o = new data::dto::AronDouble();
148 o->value = i;
149 return o;
150 }
151
152 data::dto::GenericDataPtr
153 AronDTOWriter::writeString(const std::string& i, const Path&)
154 {
155 data::dto::AronStringPtr o = new data::dto::AronString();
156 o->value = i;
157 return o;
158 }
159
160 data::dto::GenericDataPtr
161 AronDTOWriter::writeBool(const bool i, const Path&)
162 {
163 data::dto::AronBoolPtr o = new data::dto::AronBool();
164 o->value = i;
165 return o;
166 }
167
169 AronDTOWriter::DictCastAndCheck(const data::dto::GenericDataPtr& data)
170 {
171 auto o = data::dto::DictPtr::dynamicCast(data);
172 if (not o)
173 {
175 throw error::AronException(__PRETTY_FUNCTION__,
176 "Expected an aron dict but got something else.");
177 }
178 return o;
179 }
180
181 data::dto::AronIntPtr
182 AronDTOWriter::IntCastAndCheck(const data::dto::GenericDataPtr& data)
183 {
184 auto o = data::dto::AronIntPtr::dynamicCast(data);
185 if (not o)
186 {
188 throw error::AronException(__PRETTY_FUNCTION__,
189 "Expected an aron int but got something else.");
190 }
191 return o;
192 }
193} // namespace armarx::aron::data::writer
The Path class.
Definition Path.h:36
typename std::add_const< ReturnType >::type ReturnTypeConst
Definition Writer.h:45
data::dto::GenericDataPtr writeInt(const int i, const Path &p=Path()) override
data::dto::GenericDataPtr writeString(const std::string &i, const Path &p=Path()) override
data::dto::GenericDataPtr writeDict(const std::map< std::string, data::dto::GenericDataPtr > &elements, const std::optional< data::dto::GenericDataPtr > &extends=std::nullopt, const Path &p=Path()) override
static data::dto::DictPtr DictCastAndCheck(const data::dto::GenericDataPtr &data)
Narrow the generic result of write() to a dict, throwing if it is not one.
data::dto::GenericDataPtr writeBool(const bool i, const Path &p=Path()) override
data::dto::GenericDataPtr writeFloat(const float i, const Path &p=Path()) override
data::dto::GenericDataPtr writeLong(const long i, const Path &p=Path()) override
data::dto::GenericDataPtr writeDouble(const double i, const Path &p=Path()) override
static data::dto::AronIntPtr IntCastAndCheck(const data::dto::GenericDataPtr &data)
As DictCastAndCheck(), for generated int enums.
data::Descriptor getDescriptor(ReturnTypeConst &input) final
data::dto::GenericDataPtr writeNDArray(const std::vector< int > &shape, const std::string &typeAsString, const unsigned char *data, const Path &p=Path()) override
data::dto::GenericDataPtr writeList(const std::vector< data::dto::GenericDataPtr > &elements, const Path &p=Path()) override
A base class for aron exceptions.
Definition Exception.h:37
::IceInternal::Handle< Dict > DictPtr
Definition Commit.h:18
A convenience header to include all aron files (full include, not forward declared)
data::Descriptor Aron2Descriptor(const data::dto::GenericData &t)
Definition Descriptor.h:237
#define ARMARX_TRACE
Definition trace.h:75