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
42 VariantWriter::writeList(const std::vector<data::VariantPtr>& elements, const Path& p)
43 {
44 auto o = std::make_shared<data::List>(p);
45 for (const auto& el : elements)
46 {
47 o->addElement(el);
48 }
49 return o;
50 }
51
53 VariantWriter::writeDict(const std::map<std::string, data::VariantPtr>& elements,
54 const std::optional<data::VariantPtr>& extends,
55 const Path& p)
56 {
57 auto o = extends ? data::Dict::DynamicCastAndCheck(extends.value())
58 : std::make_shared<data::Dict>(p);
59
60 for (const auto& [key, value] : elements)
61 {
62 o->addElement(key, value);
63 }
64 return o;
65 }
66
68 VariantWriter::writeNDArray(const std::vector<int>& shape,
69 const std::string& typeAsString,
70 const unsigned char* data,
71 const Path& p)
72 {
73 auto o = std::make_shared<data::NDArray>(p);
74 o->setShape(shape);
75 o->setType(typeAsString);
76 int size =
77 shape.empty()
78 ? 0
79 : std::accumulate(std::begin(shape), std::end(shape), 1, std::multiplies<int>());
80 o->setData(static_cast<unsigned int>(size), data);
81 return o;
82 }
83
85 VariantWriter::writeInt(const int i, const Path& p)
86 {
87 auto o = std::make_shared<data::Int>(p);
88 o->setValue(i);
89 return o;
90 }
91
93 VariantWriter::writeLong(const long i, const Path& p)
94 {
95 auto o = std::make_shared<data::Long>(p);
96 o->setValue(i);
97 return o;
98 }
99
101 VariantWriter::writeFloat(const float i, const Path& p)
102 {
103 auto o = std::make_shared<data::Float>(p);
104 o->setValue(i);
105 return o;
106 }
107
109 VariantWriter::writeDouble(const double i, const Path& p)
110 {
111 auto o = std::make_shared<data::Double>(p);
112 o->setValue(i);
113 return o;
114 }
115
117 VariantWriter::writeString(const std::string& i, const Path& p)
118 {
119 auto o = std::make_shared<data::String>(p);
120 o->setValue(i);
121 return o;
122 }
123
125 VariantWriter::writeBool(const bool i, const Path& p)
126 {
127 auto o = std::make_shared<data::Bool>(p);
128 o->setValue(i);
129 return o;
130 }
131} // 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
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
A convenience header to include all aron files (full include, not forward declared)
std::shared_ptr< Variant > VariantPtr
static data::Descriptor GetDescriptor(Input &n)