$search
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 00041 namespace mongo_ros 00042 { 00043 00044 using std::vector; 00045 using std::string; 00046 00047 template <class M> 00048 ResultIterator<M>::ResultIterator 00049 (boost::shared_ptr<mongo::DBClientConnection> conn, 00050 const string& ns, const mongo::Query& query, 00051 boost::shared_ptr<mongo::GridFS> gfs, 00052 const bool metadata_only) : 00053 metadata_only_(metadata_only), 00054 cursor_(new Cursor(conn->query(ns, query))), 00055 gfs_(gfs) 00056 { 00057 if ((*cursor_)->more()) 00058 next_ = (*cursor_)->nextSafe(); 00059 } 00060 00061 template <class M> 00062 ResultIterator<M>::ResultIterator () : 00063 metadata_only_(false) 00064 { 00065 } 00066 00067 template <class M> 00068 ResultIterator<M>::ResultIterator (const ResultIterator<M>& res) : 00069 metadata_only_(res.metadata_only_), cursor_(res.cursor_), 00070 next_(res.next_), gfs_(res.gfs_) 00071 { 00072 } 00073 00074 template <class M> 00075 void ResultIterator<M>::increment () 00076 { 00077 ROS_ASSERT (next_); 00078 if ((*cursor_)->more()) 00079 next_ = (*cursor_)->nextSafe(); 00080 else 00081 next_.reset(); 00082 } 00083 00084 template <class M> 00085 typename MessageWithMetadata<M>::ConstPtr 00086 ResultIterator<M>::dereference () const 00087 { 00088 ROS_ASSERT (next_); 00089 // Get the raw message if necessary, else 00090 // use a default constructed one 00091 typename MessageWithMetadata<M>::Ptr m(new MessageWithMetadata<M>(next_->copy())); 00092 if (!metadata_only_) 00093 { 00094 mongo::OID blob_id; 00095 (*next_)["blob_id"].Val(blob_id); 00096 mongo::BSONObj q = BSON ("_id" << blob_id); 00097 mongo::GridFile f = gfs_->findFile(q); 00098 ROS_ASSERT(f.exists()); 00099 std::stringstream ss (ios_base::out); 00100 f.write(ss); 00101 std::string str = ss.str(); 00102 00103 uint8_t* buf = (uint8_t*) str.c_str(); 00104 ros::serialization::IStream istream(buf, str.size()); 00105 ros::serialization::Serializer<M>::read(istream, *m); 00106 } 00107 00108 return m; 00109 } 00110 00111 template <class M> 00112 bool ResultIterator<M>::equal (const ResultIterator<M>& other) const 00113 { 00114 // Incomplete; the only case we care about is whether we're at the end yet 00115 if (next_ && other.next_) 00116 ROS_WARN ("Unexpected case of equality check of two not-past-the-end " 00117 "iterators in ResultIterator"); 00118 return (!next_ && !other.next_); 00119 } 00120 00121 00122 } // namespace