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/ArraySerializer.h" 00020 #include "variant_topic_tools/ArrayVariant.h" 00021 00022 namespace variant_topic_tools { 00023 00024 /*****************************************************************************/ 00025 /* Constructors and Destructor */ 00026 /*****************************************************************************/ 00027 00028 ArraySerializer::ArraySerializer() { 00029 } 00030 00031 ArraySerializer::ArraySerializer(const Serializer& memberSerializer, size_t 00032 numMembers) { 00033 impl.reset(new ImplV(memberSerializer, numMembers)); 00034 } 00035 00036 ArraySerializer::ArraySerializer(const ArraySerializer& src) : 00037 Serializer(src) { 00038 } 00039 00040 ArraySerializer::ArraySerializer(const Serializer& src) : 00041 Serializer(src) { 00042 if (impl) 00043 BOOST_ASSERT(boost::dynamic_pointer_cast<Impl>(impl)); 00044 } 00045 00046 ArraySerializer::~ArraySerializer() { 00047 } 00048 00049 ArraySerializer::Impl::Impl() { 00050 } 00051 00052 ArraySerializer::Impl::~Impl() { 00053 } 00054 00055 ArraySerializer::ImplV::ImplV(const Serializer& memberSerializer, size_t 00056 numMembers) : 00057 memberSerializer(memberSerializer), 00058 numMembers(numMembers) { 00059 } 00060 00061 ArraySerializer::ImplV::~ImplV() { 00062 } 00063 00064 /*****************************************************************************/ 00065 /* Accessors */ 00066 /*****************************************************************************/ 00067 00068 size_t ArraySerializer::ImplV::getSerializedLength(const Variant& value) 00069 const { 00070 ArrayVariant arrayValue = value; 00071 size_t length = 0; 00072 00073 if (!numMembers) 00074 length += sizeof(uint32_t); 00075 00076 for (size_t i = 0; i < arrayValue.getNumMembers(); ++i) 00077 length += memberSerializer.getSerializedLength(arrayValue[i]); 00078 00079 return length; 00080 } 00081 00082 /*****************************************************************************/ 00083 /* Methods */ 00084 /*****************************************************************************/ 00085 00086 void ArraySerializer::ImplV::serialize(ros::serialization::OStream& stream, 00087 const Variant& value) { 00088 ArrayVariant arrayValue = value; 00089 00090 if (!numMembers) 00091 stream.next((uint32_t)arrayValue.getNumMembers()); 00092 00093 for (size_t i = 0; i < arrayValue.getNumMembers(); ++i) 00094 memberSerializer.serialize(stream, arrayValue[i]); 00095 } 00096 00097 void ArraySerializer::ImplV::deserialize(ros::serialization::IStream& stream, 00098 Variant& value) { 00099 ArrayVariant arrayValue = value; 00100 00101 if (!numMembers) { 00102 uint32_t numMembers = 0; 00103 stream.next(numMembers); 00104 00105 arrayValue.resize(numMembers); 00106 } 00107 00108 for (size_t i = 0; i < arrayValue.getNumMembers(); ++i) { 00109 Variant member = arrayValue[i]; 00110 memberSerializer.deserialize(stream, member); 00111 } 00112 } 00113 00114 }