CollectionVariant.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/CollectionVariant.h"
00023 
00024 namespace variant_topic_tools {
00025 
00026 /*****************************************************************************/
00027 /* Constructors and Destructor                                               */
00028 /*****************************************************************************/
00029 
00030 CollectionVariant::CollectionVariant() {
00031 }
00032 
00033 CollectionVariant::CollectionVariant(const DataType& type) :
00034   Variant(type) {
00035 }
00036 
00037 CollectionVariant::CollectionVariant(const CollectionVariant& src) :
00038   Variant(src) {
00039 }
00040 
00041 CollectionVariant::CollectionVariant(const Variant& src) :
00042   Variant(src) {
00043   if (value)
00044     BOOST_ASSERT(boost::dynamic_pointer_cast<Value>(value));
00045 }
00046 
00047 CollectionVariant::~CollectionVariant() {
00048 }
00049 
00050 CollectionVariant::Value::Value() {
00051 }
00052 
00053 CollectionVariant::Value::~Value() {
00054 }
00055 
00056 /*****************************************************************************/
00057 /* Accessors                                                                 */
00058 /*****************************************************************************/
00059 
00060 size_t CollectionVariant::getNumMembers() const {
00061   if (value)
00062     return boost::dynamic_pointer_cast<Value>(value)->getNumMembers();
00063   else
00064     return 0;
00065 }
00066 
00067 void CollectionVariant::setMember(int index, const Variant& member) {
00068   if (value)
00069     boost::dynamic_pointer_cast<Value>(value)->setMember(index, member);
00070   else
00071     throw NoSuchMemberException(index);
00072 }
00073 
00074 void CollectionVariant::setMember(const std::string& name, const Variant&
00075     member) {
00076   if (value)
00077     boost::dynamic_pointer_cast<Value>(value)->setMember(name, member, 0);
00078   else
00079     throw NoSuchMemberException(name);
00080 }
00081 
00082 Variant CollectionVariant::getMember(int index) const {
00083   if (value)
00084     return boost::dynamic_pointer_cast<Value>(value)->getMember(index);
00085   else
00086     throw NoSuchMemberException(index);
00087 }
00088 
00089 Variant CollectionVariant::getMember(const std::string& name) const {
00090   if (value)
00091     return boost::dynamic_pointer_cast<Value>(value)->getMember(name, 0);
00092   else
00093     throw NoSuchMemberException(name);
00094 }
00095 
00096 bool CollectionVariant::hasMember(const std::string& name) const {
00097   if (value)
00098     return boost::dynamic_pointer_cast<Value>(value)->hasMember(name, 0);
00099   else
00100     return false;
00101 }
00102 
00103 bool CollectionVariant::isEmpty() const {
00104   if (value)
00105     return !boost::dynamic_pointer_cast<Value>(value)->getNumMembers();
00106   else
00107     return true;
00108 }
00109 
00110 void CollectionVariant::Value::setMember(const std::string& name, const
00111     Variant& member, size_t pos) {
00112   pos = name.find_first_not_of('/', pos);
00113 
00114   if (pos != std::string::npos) {
00115     size_t i = name.find_first_of('/', pos);
00116 
00117     if (i != std::string::npos) {
00118       Variant currentMember = getMember(name.substr(pos, i-pos));
00119 
00120       if (currentMember.isCollection()) {
00121         CollectionVariant collectionMember = currentMember;
00122         collectionMember.setMember(name.substr(i+1), member);
00123 
00124         return;
00125       }
00126     }
00127     else {
00128       setMember(name.substr(pos), member);
00129       return;
00130     }
00131   }
00132 
00133   throw NoSuchMemberException(name);
00134 }
00135 
00136 Variant CollectionVariant::Value::getMember(const std::string& name,
00137     size_t pos) const {
00138   pos = name.find_first_not_of('/', pos);
00139 
00140   if (pos != std::string::npos) {
00141     size_t i = name.find_first_of('/', pos);
00142 
00143     if (i != std::string::npos) {
00144       Variant currentMember = getMember(name.substr(pos, i-pos));
00145 
00146       if (currentMember.isCollection()) {
00147         CollectionVariant collectionMember = currentMember;
00148         return collectionMember.getMember(name.substr(i+1));
00149       }
00150     }
00151     else
00152       return getMember(name.substr(pos));
00153   }
00154 
00155   throw NoSuchMemberException(name);
00156 }
00157 
00158 bool CollectionVariant::Value::hasMember(const std::string& name, size_t pos)
00159     const {
00160   pos = name.find_first_not_of('/', pos);
00161 
00162   if (pos != std::string::npos) {
00163     size_t i = name.find_first_of('/', pos);
00164 
00165     if (i != std::string::npos) {
00166       Variant currentMember = getMember(name.substr(pos, i-pos));
00167 
00168       if (currentMember.isCollection()) {
00169         CollectionVariant collectionMember = currentMember;
00170         return collectionMember.hasMember(name.substr(i+1));
00171       }
00172     }
00173     else
00174       return hasMember(name.substr(pos));
00175   }
00176 
00177   return false;
00178 }
00179 
00180 bool CollectionVariant::Value::isEqual(const Variant::Value& value) const {
00181   const Value& collectionValue = dynamic_cast<const Value&>(value);
00182 
00183   if (getNumMembers() == collectionValue.getNumMembers()) {
00184     for (size_t i = 0; i < getNumMembers(); ++i)
00185       if (getMember(i) != collectionValue.getMember(i))
00186         return false;
00187 
00188     return true;
00189   }
00190   else
00191     return false;
00192 }
00193 
00194 /*****************************************************************************/
00195 /* Methods                                                                   */
00196 /*****************************************************************************/
00197 
00198 void CollectionVariant::Value::read(std::istream& stream) {
00199   throw InvalidOperationException("Reading a collection variant");
00200 }
00201 
00202 void CollectionVariant::Value::write(std::ostream& stream) const {
00203   for (size_t i = 0; i < getNumMembers(); ++i) {
00204     if (i)
00205       stream << "\n";
00206     writeMember(stream, i);
00207   }
00208 }
00209 
00210 /*****************************************************************************/
00211 /* Operators                                                                 */
00212 /*****************************************************************************/
00213 
00214 Variant CollectionVariant::operator[](int index) const {
00215   return getMember(index);
00216 }
00217 
00218 Variant CollectionVariant::operator[](const char* name) const {
00219   return getMember(std::string(name));
00220 }
00221 
00222 }


variant_topic_tools
Author(s): Ralf Kaestner
autogenerated on Fri Aug 5 2016 06:06:27