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 
36  {
38  }
39 
40  data::VariantPtr VariantWriter::writeList(const std::vector<data::VariantPtr>& elements, const Path& p)
41  {
42  auto o = std::make_shared<data::List>(p);
43  for (const auto& el : elements)
44  {
45  o->addElement(el);
46  }
47  return o;
48  }
49 
50  data::VariantPtr VariantWriter::writeDict(const std::map<std::string, data::VariantPtr>& elements, const std::optional<data::VariantPtr>& extends, const Path& p)
51  {
52  auto o = extends ? data::Dict::DynamicCastAndCheck(extends.value()) : std::make_shared<data::Dict>(p);
53 
54  for(const auto& [key, value] : elements)
55  {
56  o->addElement(key, value);
57  }
58  return o;
59  }
60 
61  data::VariantPtr VariantWriter::writeNDArray(const std::vector<int>& shape, const std::string& typeAsString, const unsigned char* data, const Path& p)
62  {
63  auto o = std::make_shared<data::NDArray>(p);
64  o->setShape(shape);
65  o->setType(typeAsString);
66  int size = shape.empty() ? 0 : std::accumulate(std::begin(shape), std::end(shape), 1, std::multiplies<int>());
67  o->setData(static_cast<unsigned int>(size), data);
68  return o;
69  }
70 
72  {
73  auto o = std::make_shared<data::Int>(p);
74  o->setValue(i);
75  return o;
76  }
77 
79  {
80  auto o = std::make_shared<data::Long>(p);
81  o->setValue(i);
82  return o;
83  }
84 
86  {
87  auto o = std::make_shared<data::Float>(p);
88  o->setValue(i);
89  return o;
90  }
91 
93  {
94  auto o = std::make_shared<data::Double>(p);
95  o->setValue(i);
96  return o;
97  }
98 
99  data::VariantPtr VariantWriter::writeString(const std::string& i, const Path& p)
100  {
101  auto o = std::make_shared<data::String>(p);
102  o->setValue(i);
103  return o;
104  }
105 
107  {
108  auto o = std::make_shared<data::Bool>(p);
109  o->setValue(i);
110  return o;
111  }
112 }
armarx::aron::data::writer::VariantWriter::writeList
data::VariantPtr writeList(const std::vector< data::VariantPtr > &elements, const Path &p=Path()) override
Definition: VariantWriter.cpp:40
armarx::aron::data::ConstVariantVisitor::GetDescriptor
static data::Descriptor GetDescriptor(Input &n)
Definition: VariantVisitor.cpp:35
VariantWriter.h
armarx::aron::data::writer::VariantWriter::writeNDArray
data::VariantPtr writeNDArray(const std::vector< int > &shape, const std::string &typeAsString, const unsigned char *data, const Path &p=Path()) override
Definition: VariantWriter.cpp:61
armarx::aron::data::Descriptor
Descriptor
Definition: Descriptor.h:193
armarx::aron::data::detail::SpecializedVariantBase< data::dto::Dict, Dict >::DynamicCastAndCheck
static PointerType DynamicCastAndCheck(const VariantPtr &n)
Definition: SpecializedVariant.h:135
armarx::aron::data::writer::VariantWriter::getDescriptor
data::Descriptor getDescriptor(ReturnTypeConst &input) final
Definition: VariantWriter.cpp:35
armarx::aron::data::writer::VariantWriter::writeLong
data::VariantPtr writeLong(const long i, const Path &p=Path()) override
Definition: VariantWriter.cpp:78
armarx::aron::Path
The Path class.
Definition: Path.h:36
armarx::aron::data::VariantPtr
std::shared_ptr< Variant > VariantPtr
Definition: forward_declarations.h:11
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::aron::data::writer::VariantWriter::writeString
data::VariantPtr writeString(const std::string &i, const Path &p=Path()) override
Definition: VariantWriter.cpp:99
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::aron::data::writer::VariantWriter::writeFloat
data::VariantPtr writeFloat(const float i, const Path &p=Path()) override
Definition: VariantWriter.cpp:85
armarx::aron::input
ReaderT::InputType & input
Definition: rw.h:19
armarx::aron::data::writer::VariantWriter::writeDouble
data::VariantPtr writeDouble(const double i, const Path &p=Path()) override
Definition: VariantWriter.cpp:92
All.h
armarx::aron::data::writer::VariantWriter::writeBool
data::VariantPtr writeBool(const bool i, const Path &p=Path()) override
Definition: VariantWriter.cpp:106
armarx::aron::data::writer::VariantWriter::writeInt
data::VariantPtr writeInt(const int i, const Path &p=Path()) override
Definition: VariantWriter.cpp:71
armarx::aron::data::writer
Definition: NlohmannJSONWriter.cpp:33
armarx::aron::data::WriterInterface< data::VariantPtr >::ReturnTypeConst
typename std::add_const< ReturnType >::type ReturnTypeConst
Definition: Writer.h:42
VariantVisitor.h
armarx::aron::data::writer::VariantWriter::writeDict
data::VariantPtr writeDict(const std::map< std::string, data::VariantPtr > &elements, const std::optional< data::VariantPtr > &extends=std::nullopt, const Path &p=Path()) override
Definition: VariantWriter.cpp:50