JsonArray.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 Simon Ottenhaus (simon dot ottenhaus at kit dot edu)
17* @copyright http://www.gnu.org/licenses/gpl-2.0.txt
18* GNU General Public License
19*/
20
21#include "JsonArray.h"
22
24
25namespace armarx
26{
30
31 JsonArray::JsonArray(const std::vector<JsonValue>& values)
32 {
33 for (const JsonValue& v : values)
34 {
35 add(v);
36 }
37 }
38
39 void
41 {
42 writer->startArray();
43
44 for (JsonDataPtr e : elements)
45 {
46 e->writeJson(writer);
47 }
48
49 writer->endArray();
50 }
51
52 void
54 {
55 elements.push_back(value);
56 }
57
58 void
60 {
61 add(value.toSharedPtr());
62 }
63
64 void
66 {
67 add(value.toSharedPtr());
68 }
69
70 void
71 JsonArray::set(uint index, const JsonDataPtr& value)
72 {
73 elements.at(index) = value;
74 }
75
76 void
77 JsonArray::set(uint index, const JsonValue& value)
78 {
79 JsonDataPtr valuePtr(new JsonValue(value));
80 set(index, valuePtr);
81 }
82
83 void
85 {
86 if (index >= elements.size())
87 {
88 throw LocalException("Out of range.");
89 }
90
91 elements.erase(elements.begin() + index);
92 }
93
94 uint
96 {
97 return elements.size();
98 }
99
100 void
102 {
103 elements.clear();
104 }
105
108 {
109 JsonArrayPtr ptr(new JsonArray(*this));
110 return ptr;
111 }
112
115 {
116 JsonArrayPtr a(new JsonArray);
117 for (const JsonDataPtr& e : elements)
118 {
119 a->add(e->clone());
120 }
121 return a;
122 }
123} // namespace armarx
uint8_t index
void remove(uint index)
Definition JsonArray.cpp:84
JsonArrayPtr toSharedPtr() const
void add(const JsonDataPtr &value)
Definition JsonArray.cpp:53
void writeJson(const JsonWriterPtr &writer) override
Definition JsonArray.cpp:40
void set(uint index, const JsonDataPtr &value)
Definition JsonArray.cpp:71
JsonDataPtr clone() override
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::shared_ptr< JsonData > JsonDataPtr
Definition JsonData.h:31
std::shared_ptr< JsonArray > JsonArrayPtr
Definition JsonArray.h:32
std::shared_ptr< JsonWriter > JsonWriterPtr
Definition JsonWriter.h:30