message_collection_impl.hpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2008, Willow Garage, Inc.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the Willow Garage, Inc. nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  *
00029  */
00030 
00040 namespace warehouse_ros
00041 {
00042 
00043 template<class M>
00044   MessageCollection<M>::MessageCollection(MessageCollectionHelper::Ptr collection) :
00045       collection_(collection)
00046   {
00047     typedef typename ros::message_traits::DataType<M> DataType;
00048     const std::string datatype = DataType().value();
00049     typedef typename ros::message_traits::MD5Sum<M> Md5;
00050     const std::string md5 = Md5().value();
00051     md5sum_matches_ = collection_->initialize(datatype, md5);
00052   }
00053 
00054 template<class M>
00055   MessageCollection<M>::MessageCollection(const MessageCollection<M>& other) :
00056       collection_(other.collection_), md5sum_matches_(other.md5sum_matches_)
00057   {
00058   }
00059 
00060 template<class M>
00061   MessageCollection<M>::~MessageCollection()
00062   {
00063   }
00064 
00065 template<class M>
00066   MessageCollection<M>& MessageCollection<M>::operator=(const MessageCollection& other)
00067   {
00068     collection_ = other.collection_;
00069     md5sum_matches_ = other.md5sum_matches_;
00070     return *this;
00071   }
00072 
00073 template<class M>
00074   void MessageCollection<M>::insert(const M& msg, Metadata::Ptr metadata)
00075   {
00076     if (!md5sum_matches_)
00077       throw Md5SumException("Cannot insert additional elements.");
00078 
00079     metadata->append("creation_time", ros::Time::now().toSec());
00080 
00082     size_t serial_size = ros::serialization::serializationLength(msg);
00083     boost::shared_array<uint8_t> buffer(new uint8_t[serial_size]);
00084     ros::serialization::OStream stream(buffer.get(), serial_size);
00085     ros::serialization::serialize(stream, msg);
00086     char* data = (char*)buffer.get();
00087 
00088     collection_->insert(data, serial_size, metadata);
00089   }
00090 
00091 template<class M>
00092   typename QueryResults<M>::range_t MessageCollection<M>::query(Query::ConstPtr query, bool metadata_only,
00093                                                                 const std::string& sort_by, bool ascending) const
00094   {
00095     if (!md5sum_matches_ && !metadata_only)
00096       throw Md5SumException("Can only query metadata.");
00097 
00098     ResultIteratorHelper::Ptr results = collection_->query(query, sort_by, ascending);
00099     return typename QueryResults<M>::range_t(ResultIterator<M>(results, metadata_only), ResultIterator<M>());
00100   }
00101 
00102 template<class M>
00103   std::vector<typename MessageWithMetadata<M>::ConstPtr> MessageCollection<M>::queryList(Query::ConstPtr query,
00104                                                                                          bool metadata_only,
00105                                                                                          const std::string& sort_by,
00106                                                                                          bool ascending) const
00107   {
00108     typename QueryResults<M>::range_t res = this->query(query, metadata_only, sort_by, ascending);
00109     return std::vector<typename MessageWithMetadata<M>::ConstPtr>(res.first, res.second);
00110   }
00111 
00112 template<class M>
00113   typename MessageWithMetadata<M>::ConstPtr MessageCollection<M>::findOne(Query::ConstPtr query,
00114                                                                           const bool metadata_only) const
00115   {
00116     typename QueryResults<M>::range_t res = this->query(query, metadata_only);
00117     if (res.first == res.second)
00118       throw NoMatchingMessageException(collection_->collectionName());
00119     return *res.first;
00120   }
00121 
00122 template<class M>
00123   unsigned MessageCollection<M>::removeMessages(Query::ConstPtr query)
00124   {
00125     return collection_->removeMessages(query);
00126   }
00127 
00128 template<class M>
00129   void MessageCollection<M>::modifyMetadata(Query::ConstPtr q, Metadata::ConstPtr m)
00130   {
00131     collection_->modifyMetadata(q, m);
00132   }
00133 
00134 template<class M>
00135   unsigned MessageCollection<M>::count()
00136   {
00137     return collection_->count();
00138   }
00139 
00140 template<class M>
00141   bool MessageCollection<M>::md5SumMatches() const
00142   {
00143     return md5sum_matches_;
00144   }
00145 
00146 template<class M>
00147   Query::Ptr MessageCollection<M>::createQuery() const
00148   {
00149     return collection_->createQuery();
00150   }
00151 
00152 template<class M>
00153   Metadata::Ptr MessageCollection<M>::createMetadata() const
00154   {
00155     return collection_->createMetadata();
00156   }
00157 
00158 } // namespace


warehouse_ros
Author(s): Bhaskara Marthi , Connor Brew
autogenerated on Thu Jun 23 2016 05:36:41