MessageConstant.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 
33 MessageConstant::MessageConstant(const std::string& name, const Variant&
34  value) {
35  impl.reset(new Impl(name, value));
36 }
37 
38 MessageConstant::MessageConstant(const std::string& name, const DataType&
39  type, const std::string& value) {
40  Variant variantValue = type.createVariant();
41 
42  if (!variantValue.isEmpty()) {
43  if (type.getTypeInfo() != typeid(std::string)) {
44  std::istringstream stream(value);
45  stream >> variantValue;
46  }
47  else
48  variantValue = value;
49 
50  impl.reset(new Impl(name, variantValue));
51  }
52 }
53 
55  MessageMember(src) {
56 }
57 
59  MessageMember(src) {
60  if (impl)
61  BOOST_ASSERT(boost::dynamic_pointer_cast<MessageConstant::Impl>(impl));
62 }
63 
65 }
66 
67 MessageConstant::Impl::Impl(const std::string& name, const Variant& value) :
68  MessageMember::Impl(name),
69  value(value) {
70  if (!value.getType().isValid())
72 }
73 
75 }
76 
77 /*****************************************************************************/
78 /* Accessors */
79 /*****************************************************************************/
80 
82  if (!impl) {
83  static Variant value;
84  return value;
85  }
86  else
87  return boost::static_pointer_cast<Impl>(impl)->value;
88 }
89 
91  return value.getType();
92 }
93 
94 /*****************************************************************************/
95 /* Methods */
96 /*****************************************************************************/
97 
98 void MessageConstant::Impl::write(std::ostream& stream) const {
99  stream << "const " << value.getType() << " " << name << "=" << value;
100 }
101 
102 }
void write(std::ostream &stream) const
Write the message member to a stream (implementation)
const DataType & getType() const
Retrieve the data type of this variant.
Definition: Variant.cpp:52
Exception thrown in case of an invalid data type.
Definition: Exceptions.h:51
std::string name
The name of this message member.
Message constant implementation.
ImplPtr impl
The message member&#39;s implementation.
Variant value
The value of this message constant.
const std::type_info & getTypeInfo() const
Retrieve the type information associated with this data type.
Definition: DataType.cpp:84
bool isValid() const
True, if this data type is valid.
Definition: DataType.cpp:133
const Variant & getValue() const
Retrieve the value of this message constant.
Header file defining exceptions for the variant topic tools.
Impl(const std::string &name, const Variant &value)
Constructor.
Header file providing the MessageConstant class interface.
const DataType & getType() const
Retrieve the type of this message member (implementation)
bool isEmpty() const
True, if the variant is empty, i.e., does not have a value assigned.
Definition: Variant.cpp:95
Variant createVariant() const
Create a variant from this data type.
Definition: DataType.cpp:168
MessageConstant()
Default constructor.


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