MessageConstant.cpp
Go to the documentation of this file.
00001 /******************************************************************************
00002  * Copyright (C) 2014 by Ralf Kaestner                                        *
00003  * ralf.kaestner@gmail.com                                                    *
00004  *                                                                            *
00005  * This program is free software; you can redistribute it and/or modify       *
00006  * it under the terms of the Lesser GNU General Public License as published by*
00007  * the Free Software Foundation; either version 3 of the License, or          *
00008  * (at your option) any later version.                                        *
00009  *                                                                            *
00010  * This program is distributed in the hope that it will be useful,            *
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the               *
00013  * Lesser GNU General Public License for more details.                        *
00014  *                                                                            *
00015  * You should have received a copy of the Lesser GNU General Public License   *
00016  * along with this program. If not, see <http://www.gnu.org/licenses/>.       *
00017  ******************************************************************************/
00018 
00019 #include <sstream>
00020 
00021 #include "variant_topic_tools/Exceptions.h"
00022 #include "variant_topic_tools/MessageConstant.h"
00023 
00024 namespace variant_topic_tools {
00025 
00026 /*****************************************************************************/
00027 /* Constructors and Destructor                                               */
00028 /*****************************************************************************/
00029 
00030 MessageConstant::MessageConstant() {
00031 }
00032 
00033 MessageConstant::MessageConstant(const std::string& name, const Variant&
00034     value) {
00035   impl.reset(new Impl(name, value));
00036 }
00037 
00038 MessageConstant::MessageConstant(const std::string& name, const DataType&
00039     type, const std::string& value) {
00040   Variant variantValue = type.createVariant();
00041   
00042   if (!variantValue.isEmpty()) {
00043     if (type.getTypeInfo() != typeid(std::string)) {
00044       std::istringstream stream(value);
00045       stream >> variantValue;
00046     }
00047     else
00048       variantValue = value;
00049     
00050     impl.reset(new Impl(name, variantValue));
00051   }
00052 }
00053 
00054 MessageConstant::MessageConstant(const MessageConstant& src) :
00055   MessageMember(src) {
00056 }
00057 
00058 MessageConstant::MessageConstant(const MessageMember& src) :
00059   MessageMember(src) {
00060   if (impl)
00061     BOOST_ASSERT(boost::dynamic_pointer_cast<MessageConstant::Impl>(impl));
00062 }
00063 
00064 MessageConstant::~MessageConstant() {
00065 }
00066 
00067 MessageConstant::Impl::Impl(const std::string& name, const Variant& value) :
00068   MessageMember::Impl(name),
00069   value(value) {
00070   if (!value.getType().isValid())
00071     throw InvalidDataTypeException();
00072 }
00073 
00074 MessageConstant::Impl::~Impl() {
00075 }
00076 
00077 /*****************************************************************************/
00078 /* Accessors                                                                 */
00079 /*****************************************************************************/
00080 
00081 const Variant& MessageConstant::getValue() const {
00082   if (!impl) {
00083     static Variant value;
00084     return value;
00085   }
00086   else
00087     return boost::static_pointer_cast<Impl>(impl)->value;
00088 }
00089 
00090 const DataType& MessageConstant::Impl::getType() const {
00091   return value.getType();
00092 }
00093 
00094 /*****************************************************************************/
00095 /* Methods                                                                   */
00096 /*****************************************************************************/
00097 
00098 void MessageConstant::Impl::write(std::ostream& stream) const {
00099   stream << "const " << value.getType() << " " << name << "=" << value;
00100 }
00101 
00102 }


variant_topic_tools
Author(s): Ralf Kaestner
autogenerated on Tue Jul 9 2019 03:18:42