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 
37  {
39  }
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
armarx::aron::data::writer::VariantWriter::writeList
data::VariantPtr writeList(const std::vector< data::VariantPtr > &elements, const Path &p=Path()) override
Definition: VariantWriter.cpp:42
armarx::aron::data::ConstVariantVisitor::GetDescriptor
static data::Descriptor GetDescriptor(Input &n)
Definition: VariantVisitor.cpp:36
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:68
armarx::aron::data::Descriptor
Descriptor
Definition: Descriptor.h:179
armarx::aron::data::detail::SpecializedVariantBase< data::dto::Dict, Dict >::DynamicCastAndCheck
static PointerType DynamicCastAndCheck(const VariantPtr &n)
Definition: SpecializedVariant.h:134
armarx::aron::data::writer::VariantWriter::getDescriptor
data::Descriptor getDescriptor(ReturnTypeConst &input) final
Definition: VariantWriter.cpp:36
armarx::aron::data::writer::VariantWriter::writeLong
data::VariantPtr writeLong(const long i, const Path &p=Path()) override
Definition: VariantWriter.cpp:93
armarx::aron::Path
The Path class.
Definition: Path.h:35
armarx::aron::data::VariantPtr
std::shared_ptr< Variant > VariantPtr
Definition: forward_declarations.h:11
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:855
armarx::aron::data::writer::VariantWriter::writeString
data::VariantPtr writeString(const std::string &i, const Path &p=Path()) override
Definition: VariantWriter.cpp:117
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:101
armarx::aron::input
ReaderT::InputType & input
Definition: rw.h:12
armarx::aron::data::writer::VariantWriter::writeDouble
data::VariantPtr writeDouble(const double i, const Path &p=Path()) override
Definition: VariantWriter.cpp:109
All.h
armarx::aron::data::writer::VariantWriter::writeBool
data::VariantPtr writeBool(const bool i, const Path &p=Path()) override
Definition: VariantWriter.cpp:125
armarx::aron::data::writer::VariantWriter::writeInt
data::VariantPtr writeInt(const int i, const Path &p=Path()) override
Definition: VariantWriter.cpp:85
armarx::aron::data::writer
Definition: NlohmannJSONWriter.cpp:34
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:53