Variant.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 
25 
26 namespace variant_topic_tools {
27 
28 /*****************************************************************************/
29 /* Constructors and Destructor */
30 /*****************************************************************************/
31 
33 }
34 
36  type(type) {
37 }
38 
40 }
41 
43 }
44 
46 }
47 
48 /*****************************************************************************/
49 /* Accessors */
50 /*****************************************************************************/
51 
52 const DataType& Variant::getType() const {
53  return type;
54 }
55 
56 const std::type_info& Variant::getValueTypeInfo() const {
57  if (value)
58  return value->getTypeInfo();
59  else
60  return typeid(void);
61 }
62 
63 bool Variant::hasType() const {
64  return type.isValid();
65 }
66 
67 bool Variant::isArray() const {
68  if (value)
69  return boost::dynamic_pointer_cast<ArrayVariant::Value>(value) != nullptr;
70  else
71  return false;
72 }
73 
74 bool Variant::isBuiltin() const {
75  if (value)
76  return boost::dynamic_pointer_cast<BuiltinVariant::Value>(value) != nullptr;
77  else
78  return false;
79 }
80 
81 bool Variant::isCollection() const {
82  if (value)
83  return boost::dynamic_pointer_cast<CollectionVariant::Value>(value) != nullptr;
84  else
85  return false;
86 }
87 
88 bool Variant::isMessage() const {
89  if (value)
90  return boost::dynamic_pointer_cast<MessageVariant::Value>(value) != nullptr;
91  else
92  return false;
93 }
94 
95 bool Variant::isEmpty() const {
96  return !value;
97 }
98 
99 const std::type_info& Variant::Value::getTypeInfo() const {
100  return typeid(void);
101 }
102 
103 /*****************************************************************************/
104 /* Methods */
105 /*****************************************************************************/
106 
108  return ArrayVariant(*this);
109 }
110 
112  return BuiltinVariant(*this);
113 }
114 
116  return CollectionVariant(*this);
117 }
118 
120  return MessageVariant(*this);
121 }
122 
124  type.clear();
125  value.reset();
126 }
127 
128 void Variant::read(std::istream& stream) {
129  if (value)
130  value->read(stream);
131 }
132 
133 void Variant::write(std::ostream& stream) const {
134  if (value)
135  value->write(stream);
136 }
137 
139  if (value)
140  return value->createSerializer(type);
141  else
142  return Serializer();
143 }
144 
145 /*****************************************************************************/
146 /* Operators */
147 /*****************************************************************************/
148 
149 std::istream& operator>>(std::istream& stream, Variant& variant) {
150  variant.read(stream);
151  return stream;
152 }
153 
154 std::ostream& operator<<(std::ostream& stream, const Variant& variant) {
155  variant.write(stream);
156  return stream;
157 }
158 
159 }
Variant()
Default constructor.
Definition: Variant.cpp:32
Value()
Default constructor.
Definition: Variant.cpp:42
const DataType & getType() const
Retrieve the data type of this variant.
Definition: Variant.cpp:52
std::istream & operator>>(std::istream &stream, Variant &variant)
Operator for reading the variant from a stream.
Definition: Variant.cpp:149
bool hasType() const
True, if the variant has a type.
Definition: Variant.cpp:63
Serializer createSerializer() const
Create a serializer for this variant.
Definition: Variant.cpp:138
Header file providing the Serializer class interface.
Header file providing the MessageVariant class interface.
void clear()
Clear the variant.
Definition: Variant.cpp:123
Header file providing the CollectionVariant class interface.
Header file providing the Variant class interface.
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
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
bool isArray() const
True, if this variant represents an array.
Definition: Variant.cpp:67
void read(std::istream &stream)
Read the variant from a stream.
Definition: Variant.cpp:128
Header file providing the ArrayVariant class interface.
std::ostream & operator<<(std::ostream &stream, const DataType &dataType)
Operator for writing the data type to a stream.
Definition: DataType.cpp:190
virtual ~Value()
Destructor.
Definition: Variant.cpp:45
Built-in variant value (abstract base)
bool isCollection() const
True, if this variant represents a collection.
Definition: Variant.cpp:81
bool isEmpty() const
True, if the variant is empty, i.e., does not have a value assigned.
Definition: Variant.cpp:95
MessageVariant asMessage() const
Retrieve this variant as message.
Definition: Variant.cpp:119
Message variant value (abstract base)
Header file providing the BuiltinVariant class interface.
Collection variant value (abstract base)
ArrayVariant asArray() const
Retrieve this variant as array.
Definition: Variant.cpp:107
Array variant value (abstract base)
Definition: ArrayVariant.h:86
void write(std::ostream &stream) const
Write the variant to a stream.
Definition: Variant.cpp:133
void clear()
Clear the data type.
Definition: DataType.cpp:152
CollectionVariant asCollection() const
Retrieve this variant as collection.
Definition: Variant.cpp:115
virtual const std::type_info & getTypeInfo() const
Retrieve the variant&#39;s value type information.
Definition: Variant.cpp:99
bool isMessage() const
True, if this variant represents a message.
Definition: Variant.cpp:88
BuiltinVariant asBuiltin() const
Retrieve this variant as builtin.
Definition: Variant.cpp:111
const std::type_info & getValueTypeInfo() const
Retrieve the value type information of this variant.
Definition: Variant.cpp:56


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