![]() |
User Manual, Developers Guide and API Documentation |
![]() |
00001 /******************************************************************************* 00002 * This file is part of openWNS (open Wireless Network Simulator) 00003 * _____________________________________________________________________________ 00004 * 00005 * Copyright (C) 2004-2007 00006 * Chair of Communication Networks (ComNets) 00007 * Kopernikusstr. 16, D-52074 Aachen, Germany 00008 * phone: ++49-241-80-27910, 00009 * fax: ++49-241-80-22242 00010 * email: info@openwns.org 00011 * www: http://www.openwns.org 00012 * _____________________________________________________________________________ 00013 * 00014 * openWNS is free software; you can redistribute it and/or modify it under the 00015 * terms of the GNU Lesser General Public License version 2 as published by the 00016 * Free Software Foundation; 00017 * 00018 * openWNS is distributed in the hope that it will be useful, but WITHOUT ANY 00019 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 00020 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 00021 * details. 00022 * 00023 * You should have received a copy of the GNU Lesser General Public License 00024 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00025 * 00026 ******************************************************************************/ 00027 00028 #ifndef WNS_PYCONFIG_SEQUENCE_HPP 00029 #define WNS_PYCONFIG_SEQUENCE_HPP 00030 00031 #include <WNS/pyconfig/Object.hpp> 00032 #include <WNS/pyconfig/Converter.hpp> 00033 00034 #include <WNS/Assure.hpp> 00035 #include <WNS/Exception.hpp> 00036 00037 namespace wns { namespace pyconfig { 00038 00039 template <typename T, typename ITER> 00040 class TypedIterator : 00041 public Converter<T>, 00042 public ITER 00043 { 00044 public: 00045 typedef ITER IteratorPolicy; 00046 typedef T ConvertTarget; 00047 typedef TypedIterator<T, ITER> MyKind; 00048 00049 TypedIterator() : 00050 Converter<T>(), 00051 ITER() 00052 { 00053 } 00054 00055 explicit 00056 TypedIterator(Object thang) : 00057 Converter<T>(), 00058 ITER(thang) 00059 { 00060 } 00061 00062 bool 00063 operator==(const MyKind& other) const 00064 { 00065 return IteratorPolicy::obj() == other.obj(); 00066 } // == 00067 00068 bool 00069 operator!=(const MyKind& other) const 00070 { 00071 return IteratorPolicy::obj() != other.obj(); 00072 } // != 00073 00074 T operator*() 00075 { 00076 assure(!IteratorPolicy::obj().isNull(), "This is the end, my friend.\n"); 00077 00078 T value; 00079 convert(value, IteratorPolicy::obj()); 00080 return value; 00081 } // * 00082 00083 TypedIterator<T, ITER>& 00084 operator++() 00085 { 00086 assure(!IteratorPolicy::obj().isNull(), "This is the end, my friend.\n"); 00087 00088 IteratorPolicy::next(); 00089 return *this; 00090 } // pre++ 00091 00092 TypedIterator<T, ITER> 00093 operator++(int) 00094 { 00095 assure(!IteratorPolicy::obj().isNull(), "This is the end, my friend.\n"); 00096 00097 MyKind that = *this; 00098 IteratorPolicy::next(); 00099 return that; 00100 } // post++ 00101 00102 }; 00103 00104 00105 class Sequence 00106 { 00107 friend class View; 00108 public: 00109 explicit 00110 Sequence(Object _sequence); 00111 00112 ~Sequence(); 00113 00114 Sequence(Object _sequence, const std::string& pathName); 00115 00116 Sequence(const Sequence& other); 00117 00118 Sequence& 00119 operator=(const Sequence& other); 00120 00121 00122 bool 00123 empty() const; 00124 00125 int 00126 size() const; 00127 00128 template <typename T> 00129 T 00130 at(int n) const 00131 { 00132 Object obj = sequence.getItem(n); 00133 assure(!obj.isNull(), "This is the end, my friend."); 00134 00135 T value; 00136 Converter<T> converter; 00137 bool ok = converter.convert(value, obj); 00138 obj.decref(); 00139 00140 if(!ok) 00141 throw Exception("couldn't convert."); 00142 00143 return value; 00144 } // at 00145 00146 bool 00147 isSequenceAt(int n) const; 00148 00149 Sequence 00150 getSequenceAt(int n) const; 00151 00152 // 00153 // iteration protocol 00154 // 00155 class IterPolicy 00156 { 00157 protected: 00158 IterPolicy(); 00159 IterPolicy(Object sequence); 00160 00161 virtual 00162 ~IterPolicy(); 00163 00164 IterPolicy(const IterPolicy& other); 00165 00166 IterPolicy& 00167 operator=(const IterPolicy& other); 00168 00169 virtual void 00170 next(); 00171 00172 virtual Object 00173 obj() const; 00174 00175 private: 00176 Object iter; 00177 Object nextObject; 00178 }; 00179 00180 template <typename T> 00181 class iterator : 00182 public TypedIterator<T, IterPolicy> 00183 { 00184 public: 00185 iterator() : 00186 TypedIterator<T, IterPolicy>() 00187 { 00188 } 00189 00190 iterator(Object thang) : 00191 TypedIterator<T, IterPolicy>(thang) 00192 { 00193 } 00194 }; 00195 00196 template <typename T> 00197 iterator<T> 00198 begin() 00199 { 00200 return iterator<T>(sequence); 00201 } // begin 00202 00203 template <typename T> 00204 iterator<T> 00205 end() 00206 { 00207 return iterator<T>(); 00208 } // end 00209 00210 static Sequence 00211 fromString(const std::string& s); 00212 00213 private: 00214 Object sequence; 00215 std::string pathName; 00216 }; 00217 00218 }} 00219 00220 #endif // NOT defined WNS_PYCONFIG_SEQUENCE 00221 00222
1.5.5