/home/tetron/hack/vos/libs/vos/vos/iterator.hh
Go to the documentation of this file.00001 /* 00002 00003 This file is part of the Virtual Object System of 00004 the Interreality project (http://interreality.org). 00005 00006 Copyright (C) 2001-2003 Peter Amstutz 00007 00008 This library is free software; you can redistribute it and/or 00009 modify it under the terms of the GNU Lesser General Public 00010 License as published by the Free Software Foundation; either 00011 version 2 of the License, or (at your option) any later version. 00012 00013 This library is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 Lesser General Public License for more details. 00017 00018 You should have received a copy of the GNU Lesser General Public 00019 License along with this library; if not, write to the Free Software 00020 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00021 00022 Peter Amstutz <http://www.interreality.org> 00023 */ 00024 00025 #ifndef _VOS_ITERATOR_HH_ 00026 #define _VOS_ITERATOR_HH_ 00027 00028 #include <iostream> 00029 #include <deque> 00030 #include <stdexcept> 00031 #include <vector> 00032 00033 /** @file 00034 Defines Vobject iterators 00035 */ 00036 00037 namespace VOS 00038 { 00039 class PCRIterator; 00040 typedef PCRIterator ChildListIterator; 00041 typedef PCRIterator ParentSetIterator; 00042 class StringIterator; 00043 typedef StringIterator TypeSetIterator; 00044 class VobjectIterator; 00045 } 00046 00047 #include <vos/vos/parentchildrel.hh> 00048 #include <vos/vos/vobjectstate.hh> 00049 #include <vos/vutil/iterator.hh> 00050 00051 namespace VUtil { 00052 /** @ingroup libvos 00053 Specialization to call release on ParentChildRelation objects. 00054 */ 00055 template<> inline void iteratorReleaseItem(VOS::ParentChildRelation** pcr) 00056 { 00057 (*pcr)->release(); 00058 } 00059 } 00060 00061 namespace VOS 00062 { 00063 class Vobject; 00064 00065 /** @class PCRIterator iterator.hh vos/vos/iterator.hh 00066 @ingroup libvos 00067 00068 Iterator subclass for iterating over pointers to 00069 the ParentChildRelation class. 00070 */ 00071 class VOS_API PCRIterator : public VUtil::Iterator<ParentChildRelation*> 00072 { 00073 public: 00074 PCRIterator() : VUtil::Iterator<ParentChildRelation*>() { } 00075 PCRIterator(const PCRIterator& i) : VUtil::Iterator<ParentChildRelation*>(i) { } 00076 PCRIterator(const VobjectState::ChildList& children, int start, int end) { 00077 00078 for(size_type i = start; i <= (unsigned int)end && i < children.size(); i++) { 00079 if(children[i]) { 00080 children[i]->acquire(); // released by: destructor 00081 (*items).push_back(children[i]); 00082 } 00083 } 00084 } 00085 PCRIterator(const VobjectState::ParentSet& parents) { 00086 items->resize(parents.size()); 00087 int n = 0; 00088 for(VobjectState::ParentSet::const_iterator i = parents.begin(); 00089 i != parents.end(); 00090 i++) 00091 { 00092 (*i)->acquire(); // released by: destructor 00093 (*items)[n] = *i; 00094 n++; 00095 } 00096 } 00097 00098 /** @return the current element, or a null vRef<> if the iterator is at 00099 an invalid position (at the end) 00100 */ 00101 VUtil::vRef<ParentChildRelation> operator*() { 00102 if(pos < items->size()) return VUtil::vRef<ParentChildRelation>((*items)[pos], true); 00103 else return VUtil::vRef<ParentChildRelation>(); 00104 } 00105 00106 /* @return item at position i. As a side effect the iterator 00107 position will be set to i. 00108 @throw std::out_of_range if i is greater than the number of 00109 items. 00110 */ 00111 VUtil::vRef<ParentChildRelation> operator[](size_type i) { 00112 if(pos >= items->size()) 00113 throw std::out_of_range("index out of bounds in VOS::PCRIterator::operator[]"); 00114 setPos(i); 00115 return VUtil::vRef<ParentChildRelation>((*items)[pos], true); 00116 } 00117 }; 00118 00119 /** @class StringIterator iterator.hh vos/vos/iterator.hh 00120 @ingroup libvos 00121 00122 Iterator subclass for iterating over a list of std::string 00123 objects. 00124 */ 00125 class StringIterator : public VUtil::Iterator<std::string> 00126 { 00127 public: 00128 StringIterator() : VUtil::Iterator<std::string>() { } 00129 StringIterator(const StringIterator& tsi) : VUtil::Iterator<std::string>(tsi) { } 00130 StringIterator(const std::set<std::string>& types) { 00131 items->resize(types.size()); 00132 int n = 0; 00133 for(std::set<std::string>::const_iterator i = types.begin(); i != types.end(); i++) 00134 { 00135 (*items)[n] = *i; 00136 n++; 00137 } 00138 } 00139 StringIterator(const std::vector<std::string>& strs) { 00140 items->resize(strs.size()); 00141 for(size_type i = 0; i < strs.size(); i++) 00142 { 00143 (*items)[i] = strs[i]; 00144 } 00145 } 00146 00147 /** @return the current element, or an empty string if the 00148 iterator is at an invalid position (at the end) 00149 */ 00150 std::string operator*() { 00151 if(pos < items->size()) return (*items)[pos]; 00152 else return ""; 00153 } 00154 00155 /* @return item at position i. As a side effect the iterator 00156 position will be set to i. 00157 @throw std::out_of_range if i is greater than the number of 00158 items. 00159 */ 00160 std::string operator[](size_type i) { 00161 if(pos >= items->size()) 00162 throw std::out_of_range("index out of bounds in VOS::StringIterator::operator[]"); 00163 setPos(i); 00164 return (*items)[pos]; 00165 } 00166 }; 00167 00168 } 00169 00170 00171 #endif