VariantWriter.cpp
Go to the documentation of this file.
1/*
2* This file is part of ArmarX.
3*
4* ArmarX is free software; you can redistribute it and/or modify
5* it under the terms of the GNU General Public License version 2 as
6* published by the Free Software Foundation.
7*
8* ArmarX is distributed in the hope that it will be useful, but
9* WITHOUT ANY WARRANTY; without even the implied warranty of
10* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11* GNU General Public License for more details.
12*
13* You should have received a copy of the GNU General Public License
14* along with this program. If not, see <http://www.gnu.org/licenses/>.
15*
16* @author Fabian Peller (fabian dot peller at kit dot edu)
17* @copyright http://www.gnu.org/licenses/gpl-2.0.txt
18* GNU General Public License
19*/
20
21// STD/STL
22#include <memory>
23#include <numeric>
24
25// Header
26#include "VariantWriter.h"
27
28// ArmarX
31
33{
34
40
41 void
43 const std::optional<float>& importance)
44 {
46 output->setImportance(importance);
47 }
48
50 VariantWriter::writeList(const std::vector<data::VariantPtr>& elements, const Path& p)
51 {
52 auto o = std::make_shared<data::List>(p);
53 for (const auto& el : elements)
54 {
55 o->addElement(el);
56 }
57 return o;
58 }
59
61 VariantWriter::writeDict(const std::map<std::string, data::VariantPtr>& elements,
62 const std::optional<data::VariantPtr>& extends,
63 const Path& p)
64 {
65 auto o = extends ? data::Dict::DynamicCastAndCheck(extends.value())
66 : std::make_shared<data::Dict>(p);
67
68 for (const auto& [key, value] : elements)
69 {
70 o->addElement(key, value);
71 }
72 return o;
73 }
74
76 VariantWriter::writeNDArray(const std::vector<int>& shape,
77 const std::string& typeAsString,
78 const unsigned char* data,
79 const Path& p)
80 {
81 auto o = std::make_shared<data::NDArray>(p);
82 o->setShape(shape);
83 o->setType(typeAsString);
84 int size =
85 shape.empty()
86 ? 0
87 : std::accumulate(std::begin(shape), std::end(shape), 1, std::multiplies<int>());
88 o->setData(static_cast<unsigned int>(size), data);
89 return o;
90 }
91
93 VariantWriter::writeInt(const int i, const Path& p)
94 {
95 auto o = std::make_shared<data::Int>(p);
96 o->setValue(i);
97 return o;
98 }
99
101 VariantWriter::writeLong(const long i, const Path& p)
102 {
103 auto o = std::make_shared<data::Long>(p);
104 o->setValue(i);
105 return o;
106 }
107
109 VariantWriter::writeFloat(const float i, const Path& p)
110 {
111 auto o = std::make_shared<data::Float>(p);
112 o->setValue(i);
113 return o;
114 }
115
117 VariantWriter::writeDouble(const double i, const Path& p)
118 {
119 auto o = std::make_shared<data::Double>(p);
120 o->setValue(i);
121 return o;
122 }
123
125 VariantWriter::writeString(const std::string& i, const Path& p)
126 {
127 auto o = std::make_shared<data::String>(p);
128 o->setValue(i);
129 return o;
130 }
131
133 VariantWriter::writeBool(const bool i, const Path& p)
134 {
135 auto o = std::make_shared<data::Bool>(p);
136 o->setValue(i);
137 return o;
138 }
139} // namespace armarx::aron::data::writer
The Path class.
Definition Path.h:36
typename std::add_const< ReturnType >::type ReturnTypeConst
Definition Writer.h:42
data::VariantPtr writeFloat(const float i, const Path &p=Path()) override
data::VariantPtr writeDouble(const double i, const Path &p=Path()) override
data::VariantPtr writeBool(const bool i, const Path &p=Path()) override
void writeImportance(ReturnType &output, const std::optional< float > &importance) final
data::VariantPtr writeList(const std::vector< data::VariantPtr > &elements, const Path &p=Path()) override
data::VariantPtr writeNDArray(const std::vector< int > &shape, const std::string &typeAsString, const unsigned char *data, const Path &p=Path()) override
data::VariantPtr writeString(const std::string &i, const Path &p=Path()) override
data::VariantPtr writeLong(const long i, const Path &p=Path()) override
data::Descriptor getDescriptor(ReturnTypeConst &input) final
data::VariantPtr writeInt(const int i, const Path &p=Path()) override
data::VariantPtr writeDict(const std::map< std::string, data::VariantPtr > &elements, const std::optional< data::VariantPtr > &extends=std::nullopt, const Path &p=Path()) override
#define ARMARX_CHECK_NOT_NULL(ptr)
This macro evaluates whether ptr is not null and if it turns out to be false it will throw an Express...
A convenience header to include all aron files (full include, not forward declared)
std::shared_ptr< Variant > VariantPtr
static data::Descriptor GetDescriptor(Input &n)