ArrayVariant.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 
23 
24 namespace variant_topic_tools {
25 
26 /*****************************************************************************/
27 /* Constructors and Destructor */
28 /*****************************************************************************/
29 
31 }
32 
34  size_t numMembers) :
35  CollectionVariant(type) {
36  if (type.isValid())
37  value.reset(new ValueImplV(memberType, numMembers));
38 }
39 
41  CollectionVariant(src) {
42 }
43 
45  CollectionVariant(src) {
46  if (value)
47  BOOST_ASSERT(boost::dynamic_pointer_cast<Value>(value));
48 }
49 
51 }
52 
54 }
55 
57 }
58 
60  numMembers) :
61  memberType(memberType),
62  numMembers(numMembers),
63  members(numMembers) {
64  for (size_t i = 0; i < numMembers; ++i)
65  members[i] = memberType.createVariant();
66 }
67 
71  members(src.members) {
72 }
73 
75 }
76 
77 /*****************************************************************************/
78 /* Accessors */
79 /*****************************************************************************/
80 
82  const Value& arrayValue = dynamic_cast<const Value&>(value);
83 
84  resize(arrayValue.getNumMembers());
85 
86  for (size_t i = 0; i < getNumMembers(); ++i)
87  setMember(i, arrayValue.getMember(i));
88 }
89 
90 void ArrayVariant::Value::setMember(const std::string& name, const Variant&
91  member) {
92  int index;
93 
94  try {
95  index = boost::lexical_cast<int>(name);
96  }
97  catch (...) {
98  throw NoSuchMemberException(name);
99  }
100 
101  return setMember(index, member);
102 }
103 
104 Variant ArrayVariant::Value::getMember(const std::string& name) const {
105  int index;
106 
107  try {
108  index = boost::lexical_cast<int>(name);
109  }
110  catch (...) {
111  throw NoSuchMemberException(name);
112  }
113 
114  return getMember(index);
115 }
116 
117 bool ArrayVariant::Value::hasMember(const std::string& name) const {
118  int index;
119 
120  try {
121  index = boost::lexical_cast<int>(name);
122  }
123  catch (...) {
124  return false;
125  }
126 
127  return (index < getNumMembers());
128 }
129 
130 
132  return members.size();
133 }
134 
136  member) {
137  if (index < members.size())
138  members[index] = member;
139  else
140  throw NoSuchMemberException(index);
141 }
142 
144  if (index < members.size())
145  return members[index];
146  else
147  throw NoSuchMemberException(index);
148 }
149 
150 /*****************************************************************************/
151 /* Methods */
152 /*****************************************************************************/
153 
154 void ArrayVariant::addMember(const Variant& member) {
155  if (value) {
156  if (member.getType().isValid())
157  boost::dynamic_pointer_cast<Value>(value)->addMember(member);
158  else
159  throw InvalidDataTypeException();
160  }
161  else
162  throw InvalidOperationException("Adding a member to an invalid array");
163 }
164 
166  if (value)
167  boost::dynamic_pointer_cast<Value>(value)->resize(numMembers);
168  else if (numMembers)
169  throw InvalidOperationException("Resizing an invalid array");
170 }
171 
173  if (value)
174  boost::dynamic_pointer_cast<Value>(value)->clear();
175 }
176 
177 void ArrayVariant::Value::writeMember(std::ostream& stream, int index)
178  const {
179  if (!getMember(index).getType().isBuiltin()) {
180  stream << boost::lexical_cast<std::string>(index) << ":";
181 
182  std::stringstream memberStream;
183  std::string line;
184 
185  memberStream << getMember(index);
186 
187  while (std::getline(memberStream, line))
188  stream << "\n " << line;
189  }
190  else
191  stream << boost::lexical_cast<std::string>(index) << ": " <<
192  getMember(index);
193 }
194 
196  if (!numMembers) {
197  if (member.getType() == memberType)
198  members.push_back(member);
199  else
201  member.getType().getIdentifier());
202  }
203  else
204  throw InvalidOperationException("Adding a member to a non-dynamic array");
205 }
206 
208  if (!this->numMembers || (numMembers == this->numMembers)) {
209  if (numMembers != members.size()) {
210  size_t i = members.size();
211 
212  members.resize(numMembers);
213 
214  for ( ; i < members.size(); ++i)
216  }
217  }
218  else
219  throw InvalidOperationException("Resizing a non-dynamic array");
220 }
221 
223  if (!numMembers)
224  members.clear();
225  else
226  throw InvalidOperationException("Clearing a non-dynamic array");
227 }
228 
230  return Variant::ValuePtr(new ValueImplV(*this));
231 }
232 
234  const {
235  if (!members.empty())
236  return ArraySerializer(members.front().createSerializer(), numMembers);
237  else
238  return ArraySerializer(Serializer(), 0);
239 }
240 
241 /*****************************************************************************/
242 /* Operators */
243 /*****************************************************************************/
244 
246  addMember(member);
247  return *this;
248 }
249 
250 }
ValuePtr clone() const
Clone this variant value (implementation)
Header file providing the ArraySerializer class interface.
const DataType & getType() const
Retrieve the data type of this variant.
Definition: Variant.cpp:52
Exception thrown in case of an invalid operation.
Definition: Exceptions.h:41
void clear()
Clear the array.
void resize(size_t numMembers)
Resize the array.
Exception thrown in case of an invalid data type.
Definition: Exceptions.h:51
Variant value (abstract base)
Definition: Variant.h:167
Header file providing the ArrayDataType class interface.
ArrayVariant & operator+=(const Variant &member)
Operator for adding a member to the array.
bool hasMember(const std::string &name) const
True, if the variant collection contains the member with the specified name (implementation) ...
Variant getMember(int index) const
Retrieve a member of the variant collection by index (implementation)
DataType memberType
The array member type.
Definition: ArrayVariant.h:193
void writeMember(std::ostream &stream, int index) const
Write the variant collection member with the specified index to a stream (implementation) ...
Variant getMember(const std::string &name) const
Retrieve a member of the variant collection by name (implementation)
bool isBuiltin() const
True, if this variant represents a built-in.
Definition: Variant.cpp:74
DataType type
The variant&#39;s data type.
Definition: Variant.h:247
ArrayVariant()
Default constructor.
bool isValid() const
True, if this data type is valid.
Definition: DataType.cpp:133
ValuePtr value
The variant&#39;s data value.
Definition: Variant.h:251
size_t getNumMembers() const
Retrieve the number of members of the variant collection (implementation)
const std::string & getIdentifier() const
Retrieve the identifier representing this data type.
Definition: DataType.cpp:75
Header file defining exceptions for the variant topic tools.
std::vector< Variant > members
The array members.
Definition: ArrayVariant.h:201
size_t numMembers
The number of array members.
Definition: ArrayVariant.h:197
Array variant value (variant-typed implementation)
Definition: ArrayVariant.h:140
Serializer createSerializer(const DataType &type) const
Create a serializer for this variant (implementation)
Header file providing the ArrayVariant class interface.
void addMember(const Variant &member)
Add a member to the array (implementation)
void setMember(const std::string &name, const Variant &member)
Set a member of the variant collection by name (implementation)
void setValue(const Variant::Value &value)
Set the variant&#39;s value (implementation)
Exception thrown in case of a data type mismatch.
Definition: Exceptions.h:92
virtual size_t getNumMembers() const =0
Retrieve the number of members of the collection (abstract declaration)
void setMember(int index, const Variant &member)
Set a member of the variant collection by index (implementation)
ValueImplV(const DataType &memberType=DataType(), size_t numMembers=0)
Default constructor.
Variant createVariant() const
Create a variant from this data type.
Definition: DataType.cpp:168
Array variant value (abstract base)
Definition: ArrayVariant.h:86
boost::shared_ptr< Value > ValuePtr
Declaration of the variant value pointer type.
Definition: Variant.h:155
void addMember(const Variant &member)
Add a member to the array.
void clear()
Clear the array (implementation)
Exception thrown in case of a non-existent member.
Definition: Exceptions.h:113
void resize(size_t numMembers)
Resize the array (implementation)


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