MessageVariant.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 
35  CollectionVariant(type) {
36  if (type.isValid())
37  value.reset(new ValueImplV(members));
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  members) :
61  members(members) {
62 }
63 
65  members(src.members) {
66 }
67 
69 }
70 
71 /*****************************************************************************/
72 /* Accessors */
73 /*****************************************************************************/
74 
76  const Value& messageValue = dynamic_cast<const Value&>(value);
77 
78  for (size_t i = 0; i < getNumMembers(); ++i)
79  setMember(i, messageValue.getMember(i));
80 }
81 
83  return members.getNumFields();
84 }
85 
87  member) {
88  members.getField(index).setValue(member);
89 }
90 
91 void MessageVariant::ValueImplV::setMember(const std::string& name, const
92  Variant& member) {
93  members.getField(name).setValue(member);
94 }
95 
97  return members.getField(index).getValue();
98 }
99 
100 Variant MessageVariant::ValueImplV::getMember(const std::string& name) const {
101  return members.getField(name).getValue();
102 }
103 
104 const std::string& MessageVariant::ValueImplV::getMemberName(int index)
105  const {
106  return members.getField(index).getName();
107 }
108 
109 bool MessageVariant::ValueImplV::hasMember(const std::string& name) const {
110  return members.hasField(name);
111 }
112 
113 /*****************************************************************************/
114 /* Methods */
115 /*****************************************************************************/
116 
117 void MessageVariant::Value::writeMember(std::ostream& stream, int index)
118  const {
119  Variant member = getMember(index);
120 
121  if (!member.getType().isBuiltin()) {
122  stream << getMemberName(index) << ":";
123 
124  std::stringstream memberStream;
125  std::string line;
126 
127  memberStream << member;
128 
129  while (std::getline(memberStream, line))
130  stream << "\n " << line;
131  }
132  else
133  stream << getMemberName(index) << ": " << member;
134 }
135 
137  return Variant::ValuePtr(new ValueImplV(*this));
138 }
139 
141  const {
142  MessageFieldCollection<Serializer> memberSerializers;
143 
144  for (size_t i = 0; i < members.getNumFields(); ++i)
145  memberSerializers.appendField(members[i].getName(),
146  members[i].getValue().createSerializer());
147 
148  return MessageSerializer(memberSerializers);
149 }
150 
151 }
const DataType & getType() const
Retrieve the data type of this variant.
Definition: Variant.cpp:52
bool hasMember(const std::string &name) const
True, if the variant collection contains the member with the specified name (implementation) ...
ValueImplV(const MessageFieldCollection< Variant > &members=MessageFieldCollection< Variant >())
Default constructor.
Header file providing the MessageSerializer class interface.
Variant value (abstract base)
Definition: Variant.h:167
Header file providing the MessageVariant class interface.
virtual Variant getMember(int index) const =0
Retrieve a member of the collection by index (abstract declaration)
MessageFieldCollection< Variant > members
The message members.
DataType type
The variant&#39;s data type.
Definition: Variant.h:247
bool isValid() const
True, if this data type is valid.
Definition: DataType.cpp:133
void appendField(const MessageField< T > &field)
Append a field to the message field collection.
ValuePtr value
The variant&#39;s data value.
Definition: Variant.h:251
Serializer createSerializer(const DataType &type) const
Create a serializer for this variant (implementation)
Variant getMember(int index) const
Retrieve a member of the variant collection by index (implementation)
void setValue(const Variant::Value &value)
Set the variant&#39;s value (implementation)
const std::string & getMemberName(int index) const
Retrieve the name of the message member with the specified index (implementation) ...
Message variant value (variant-typed implementation)
Header file providing the MessageDataType class interface.
void setMember(int index, const Variant &member)
Set a member of the variant collection by index (implementation)
ValuePtr clone() const
Clone this variant value (implementation)
Message variant value (abstract base)
bool isBuiltin() const
True, if this data type represents a built-in type.
Definition: DataType.cpp:105
MessageVariant()
Default constructor.
boost::shared_ptr< Value > ValuePtr
Declaration of the variant value pointer type.
Definition: Variant.h:155
Header file providing the MessageVariable class interface.
size_t getNumMembers() const
Retrieve the number of members of the variant collection (implementation)
Forward declaration of the message field collection.
Definition: Forwards.h:64
void writeMember(std::ostream &stream, int index) const
Write the variant collection member with the specified index to a stream (implementation) ...


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