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/ArrayDataType.h" 00020 #include "variant_topic_tools/BuiltinDataType.h" 00021 #include "variant_topic_tools/DataType.h" 00022 #include "variant_topic_tools/DataTypeRegistry.h" 00023 #include "variant_topic_tools/MessageDataType.h" 00024 #include <variant_topic_tools/Serializer.h> 00025 #include "variant_topic_tools/Variant.h" 00026 00027 namespace variant_topic_tools { 00028 00029 /*****************************************************************************/ 00030 /* Constructors and Destructor */ 00031 /*****************************************************************************/ 00032 00033 DataType::DataType() { 00034 } 00035 00036 DataType::DataType(const char* identifier) { 00037 DataTypeRegistry registry; 00038 DataType dataType = registry.getDataType(identifier); 00039 00040 impl = dataType.impl; 00041 } 00042 00043 DataType::DataType(const std::string& identifier) { 00044 DataTypeRegistry registry; 00045 DataType dataType = registry.getDataType(identifier); 00046 00047 impl = dataType.impl; 00048 } 00049 00050 DataType::DataType(const std::type_info& typeInfo) { 00051 DataTypeRegistry registry; 00052 DataType dataType = registry.getDataType(typeInfo); 00053 00054 impl = dataType.impl; 00055 } 00056 00057 DataType::DataType(const DataType& src) { 00058 if (src.impl) 00059 impl.reset(new boost::shared_ptr<Impl>(*src.impl)); 00060 } 00061 00062 DataType::~DataType() { 00063 } 00064 00065 DataType::Impl::Impl() { 00066 } 00067 00068 DataType::Impl::~Impl() { 00069 } 00070 00071 /*****************************************************************************/ 00072 /* Accessors */ 00073 /*****************************************************************************/ 00074 00075 const std::string& DataType::getIdentifier() const { 00076 if (!impl) { 00077 static std::string identifier; 00078 return identifier; 00079 } 00080 else 00081 return (*impl)->getIdentifier(); 00082 } 00083 00084 const std::type_info& DataType::getTypeInfo() const { 00085 if (impl) 00086 return (*impl)->getTypeInfo(); 00087 else 00088 return typeid(void); 00089 } 00090 00091 size_t DataType::getSize() const { 00092 if (impl) 00093 return (*impl)->getSize(); 00094 else 00095 return 0; 00096 } 00097 00098 bool DataType::isArray() const { 00099 if (impl) 00100 return boost::dynamic_pointer_cast<ArrayDataType::Impl>(*impl) != nullptr; 00101 else 00102 return false; 00103 } 00104 00105 bool DataType::isBuiltin() const { 00106 if (impl) 00107 return boost::dynamic_pointer_cast<BuiltinDataType::Impl>(*impl) != nullptr; 00108 else 00109 return false; 00110 } 00111 00112 bool DataType::isMessage() const { 00113 if (impl) 00114 return boost::dynamic_pointer_cast<MessageDataType::Impl>(*impl) != nullptr; 00115 else 00116 return false; 00117 } 00118 00119 bool DataType::isFixedSize() const { 00120 if (impl) 00121 return (*impl)->isFixedSize(); 00122 else 00123 return true; 00124 } 00125 00126 bool DataType::isSimple() const { 00127 if (impl) 00128 return (*impl)->isSimple(); 00129 else 00130 return true; 00131 } 00132 00133 bool DataType::isValid() const { 00134 return impl != nullptr; 00135 } 00136 00137 bool DataType::hasTypeInfo() const { 00138 if (impl) 00139 return ((*impl)->getTypeInfo() != typeid(void)); 00140 else 00141 return false; 00142 } 00143 00144 const std::type_info& DataType::Impl::getTypeInfo() const { 00145 return typeid(void); 00146 } 00147 00148 /*****************************************************************************/ 00149 /* Methods */ 00150 /*****************************************************************************/ 00151 00152 void DataType::clear() { 00153 impl.reset(); 00154 } 00155 00156 void DataType::write(std::ostream& stream) const { 00157 if (impl) 00158 stream << (*impl)->getIdentifier(); 00159 } 00160 00161 Serializer DataType::createSerializer() const { 00162 if (impl) 00163 return (*impl)->createSerializer(*this); 00164 else 00165 return Serializer(); 00166 } 00167 00168 Variant DataType::createVariant() const { 00169 if (impl) 00170 return (*impl)->createVariant(*this); 00171 else 00172 return Variant(); 00173 } 00174 00175 /*****************************************************************************/ 00176 /* Operators */ 00177 /*****************************************************************************/ 00178 00179 DataType& DataType::operator=(const DataType& src) { 00180 if (impl && src.impl) 00181 *impl = *src.impl; 00182 else if (src.impl) 00183 impl.reset(new boost::shared_ptr<Impl>(*src.impl)); 00184 else 00185 impl = src.impl; 00186 00187 return *this; 00188 } 00189 00190 std::ostream& operator<<(std::ostream& stream, const DataType& dataType) { 00191 dataType.write(stream); 00192 return stream; 00193 } 00194 00195 }