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 "variant_topic_tools/ArrayVariant.h" 00020 #include "variant_topic_tools/BuiltinVariant.h" 00021 #include "variant_topic_tools/CollectionVariant.h" 00022 #include "variant_topic_tools/MessageVariant.h" 00023 #include "variant_topic_tools/Serializer.h" 00024 #include "variant_topic_tools/Variant.h" 00025 00026 namespace variant_topic_tools { 00027 00028 /*****************************************************************************/ 00029 /* Constructors and Destructor */ 00030 /*****************************************************************************/ 00031 00032 Variant::Variant() { 00033 } 00034 00035 Variant::Variant(const DataType& type) : 00036 type(type) { 00037 } 00038 00039 Variant::~Variant() { 00040 } 00041 00042 Variant::Value::Value() { 00043 } 00044 00045 Variant::Value::~Value() { 00046 } 00047 00048 /*****************************************************************************/ 00049 /* Accessors */ 00050 /*****************************************************************************/ 00051 00052 const DataType& Variant::getType() const { 00053 return type; 00054 } 00055 00056 const std::type_info& Variant::getValueTypeInfo() const { 00057 if (value) 00058 return value->getTypeInfo(); 00059 else 00060 return typeid(void); 00061 } 00062 00063 bool Variant::hasType() const { 00064 return type.isValid(); 00065 } 00066 00067 bool Variant::isArray() const { 00068 if (value) 00069 return boost::dynamic_pointer_cast<ArrayVariant::Value>(value); 00070 else 00071 return false; 00072 } 00073 00074 bool Variant::isBuiltin() const { 00075 if (value) 00076 return boost::dynamic_pointer_cast<BuiltinVariant::Value>(value); 00077 else 00078 return false; 00079 } 00080 00081 bool Variant::isCollection() const { 00082 if (value) 00083 return boost::dynamic_pointer_cast<CollectionVariant::Value>(value); 00084 else 00085 return false; 00086 } 00087 00088 bool Variant::isMessage() const { 00089 if (value) 00090 return boost::dynamic_pointer_cast<MessageVariant::Value>(value); 00091 else 00092 return false; 00093 } 00094 00095 bool Variant::isEmpty() const { 00096 return !value; 00097 } 00098 00099 const std::type_info& Variant::Value::getTypeInfo() const { 00100 return typeid(void); 00101 } 00102 00103 /*****************************************************************************/ 00104 /* Methods */ 00105 /*****************************************************************************/ 00106 00107 ArrayVariant Variant::asArray() const { 00108 return ArrayVariant(*this); 00109 } 00110 00111 BuiltinVariant Variant::asBuiltin() const { 00112 return BuiltinVariant(*this); 00113 } 00114 00115 CollectionVariant Variant::asCollection() const { 00116 return CollectionVariant(*this); 00117 } 00118 00119 MessageVariant Variant::asMessage() const { 00120 return MessageVariant(*this); 00121 } 00122 00123 void Variant::clear() { 00124 type.clear(); 00125 value.reset(); 00126 } 00127 00128 void Variant::read(std::istream& stream) { 00129 if (value) 00130 value->read(stream); 00131 } 00132 00133 void Variant::write(std::ostream& stream) const { 00134 if (value) 00135 value->write(stream); 00136 } 00137 00138 Serializer Variant::createSerializer() const { 00139 if (value) 00140 return value->createSerializer(type); 00141 else 00142 return Serializer(); 00143 } 00144 00145 /*****************************************************************************/ 00146 /* Operators */ 00147 /*****************************************************************************/ 00148 00149 std::istream& operator>>(std::istream& stream, Variant& variant) { 00150 variant.read(stream); 00151 return stream; 00152 } 00153 00154 std::ostream& operator<<(std::ostream& stream, const Variant& variant) { 00155 variant.write(stream); 00156 return stream; 00157 } 00158 00159 }