CollectionVariant.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright (C) 2014 by Ralf Kaestner *
3  * ralf.kaestner@gmail.com *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the Lesser GNU General Public License as published by*
7  * the Free Software Foundation; either version 3 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * Lesser GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the Lesser GNU General Public License *
16  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
17  ******************************************************************************/
18 
19 #include <sstream>
20 
23 
24 namespace variant_topic_tools {
25 
26 /*****************************************************************************/
27 /* Constructors and Destructor */
28 /*****************************************************************************/
29 
31 }
32 
34  Variant(type) {
35 }
36 
38  Variant(src) {
39 }
40 
42  Variant(src) {
43  if (value)
44  BOOST_ASSERT(boost::dynamic_pointer_cast<Value>(value));
45 }
46 
48 }
49 
51 }
52 
54 }
55 
56 /*****************************************************************************/
57 /* Accessors */
58 /*****************************************************************************/
59 
61  if (value)
62  return boost::dynamic_pointer_cast<Value>(value)->getNumMembers();
63  else
64  return 0;
65 }
66 
67 void CollectionVariant::setMember(int index, const Variant& member) {
68  if (value)
69  boost::dynamic_pointer_cast<Value>(value)->setMember(index, member);
70  else
71  throw NoSuchMemberException(index);
72 }
73 
74 void CollectionVariant::setMember(const std::string& name, const Variant&
75  member) {
76  if (value)
77  boost::dynamic_pointer_cast<Value>(value)->setMember(name, member, 0);
78  else
79  throw NoSuchMemberException(name);
80 }
81 
83  if (value)
84  return boost::dynamic_pointer_cast<Value>(value)->getMember(index);
85  else
86  throw NoSuchMemberException(index);
87 }
88 
89 Variant CollectionVariant::getMember(const std::string& name) const {
90  if (value)
91  return boost::dynamic_pointer_cast<Value>(value)->getMember(name, 0);
92  else
93  throw NoSuchMemberException(name);
94 }
95 
96 bool CollectionVariant::hasMember(const std::string& name) const {
97  if (value)
98  return boost::dynamic_pointer_cast<Value>(value)->hasMember(name, 0);
99  else
100  return false;
101 }
102 
104  if (value)
105  return !boost::dynamic_pointer_cast<Value>(value)->getNumMembers();
106  else
107  return true;
108 }
109 
110 void CollectionVariant::Value::setMember(const std::string& name, const
111  Variant& member, size_t pos) {
112  pos = name.find_first_not_of('/', pos);
113 
114  if (pos != std::string::npos) {
115  size_t i = name.find_first_of('/', pos);
116 
117  if (i != std::string::npos) {
118  Variant currentMember = getMember(name.substr(pos, i-pos));
119 
120  if (currentMember.isCollection()) {
121  CollectionVariant collectionMember = currentMember;
122  collectionMember.setMember(name.substr(i+1), member);
123 
124  return;
125  }
126  }
127  else {
128  setMember(name.substr(pos), member);
129  return;
130  }
131  }
132 
133  throw NoSuchMemberException(name);
134 }
135 
137  size_t pos) const {
138  pos = name.find_first_not_of('/', pos);
139 
140  if (pos != std::string::npos) {
141  size_t i = name.find_first_of('/', pos);
142 
143  if (i != std::string::npos) {
144  Variant currentMember = getMember(name.substr(pos, i-pos));
145 
146  if (currentMember.isCollection()) {
147  CollectionVariant collectionMember = currentMember;
148  return collectionMember.getMember(name.substr(i+1));
149  }
150  }
151  else
152  return getMember(name.substr(pos));
153  }
154 
155  throw NoSuchMemberException(name);
156 }
157 
158 bool CollectionVariant::Value::hasMember(const std::string& name, size_t pos)
159  const {
160  pos = name.find_first_not_of('/', pos);
161 
162  if (pos != std::string::npos) {
163  size_t i = name.find_first_of('/', pos);
164 
165  if (i != std::string::npos) {
166  Variant currentMember = getMember(name.substr(pos, i-pos));
167 
168  if (currentMember.isCollection()) {
169  CollectionVariant collectionMember = currentMember;
170  return collectionMember.hasMember(name.substr(i+1));
171  }
172  }
173  else
174  return hasMember(name.substr(pos));
175  }
176 
177  return false;
178 }
179 
181  const Value& collectionValue = dynamic_cast<const Value&>(value);
182 
183  if (getNumMembers() == collectionValue.getNumMembers()) {
184  for (size_t i = 0; i < getNumMembers(); ++i)
185  if (getMember(i) != collectionValue.getMember(i))
186  return false;
187 
188  return true;
189  }
190  else
191  return false;
192 }
193 
194 /*****************************************************************************/
195 /* Methods */
196 /*****************************************************************************/
197 
198 void CollectionVariant::Value::read(std::istream& stream) {
199  throw InvalidOperationException("Reading a collection variant");
200 }
201 
202 void CollectionVariant::Value::write(std::ostream& stream) const {
203  for (size_t i = 0; i < getNumMembers(); ++i) {
204  if (i)
205  stream << "\n";
206  writeMember(stream, i);
207  }
208 }
209 
210 /*****************************************************************************/
211 /* Operators */
212 /*****************************************************************************/
213 
215  return getMember(index);
216 }
217 
218 Variant CollectionVariant::operator[](const char* name) const {
219  return getMember(std::string(name));
220 }
221 
222 }
bool hasMember(const std::string &name) const
True, if the collection contains the member with the specified name.
Exception thrown in case of an invalid operation.
Definition: Exceptions.h:41
Variant operator[](int index) const
Operator for retrieving the members of the collection by index.
void write(std::ostream &stream) const
Write this variant value to a stream (implementation)
Variant value (abstract base)
Definition: Variant.h:167
void setMember(int index, const Variant &member)
Set a member of the collection by index.
virtual Variant getMember(int index) const =0
Retrieve a member of the collection by index (abstract declaration)
Variant getMember(int index) const
Retrieve a member of the collection by index.
Header file providing the CollectionVariant class interface.
void read(std::istream &stream)
Read the variant from a stream (implementation)
DataType type
The variant&#39;s data type.
Definition: Variant.h:247
ValuePtr value
The variant&#39;s data value.
Definition: Variant.h:251
virtual bool hasMember(const std::string &name) const =0
True, if the collection contains the member with the specified name (abstract declaration) ...
Header file defining exceptions for the variant topic tools.
bool isCollection() const
True, if this variant represents a collection.
Definition: Variant.cpp:81
bool isEmpty() const
True, if the collection is empty.
size_t getNumMembers() const
Retrieve the number of members of the collection.
virtual size_t getNumMembers() const =0
Retrieve the number of members of the collection (abstract declaration)
bool isEqual(const Variant::Value &value) const
True, if this variant value equals another variant value (implementation)
Collection variant value (abstract base)
virtual void setMember(int index, const Variant &member)=0
Set a member of the collection by index (abstract declaration)
Exception thrown in case of a non-existent member.
Definition: Exceptions.h:113


variant_topic_tools
Author(s): Ralf Kaestner
autogenerated on Sat Jan 9 2021 03:56:49