Changeset 2248
- Timestamp:
- Apr 22, 2010, 2:57:13 AM (13 years ago)
- Location:
- trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/NEWS
r2217 r2248 4 4 5 5 version 0.7 (released DATE) 6 - utility::load functions now work with std::string as well. 6 7 7 8 A complete list of closed tickets can be found here [[br]] -
trunk/test/utility_test.cc
r2239 r2248 47 47 void test_errno_error_func(void); 48 48 void test_get_map(test::Suite&); 49 void test_load(test::Suite&); 49 50 void test_inverse(test::Suite&); 50 51 … … 170 171 test_errno_error(suite); 171 172 test_ptr_compare(suite); 173 test_load(suite); 172 174 173 175 return suite.return_value(); … … 233 235 if (!post_ok && prior_ok) 234 236 suite.err() << "test_less_nan failed\n"; 237 } 238 239 240 void test_load(test::Suite& suite) 241 { 242 std::istringstream iss(std::string("1.69 3.14")); 243 std::vector<double> double_vec; 244 utility::load(iss, double_vec); 245 suite.add(double_vec.size()==2); 246 247 std::istringstream iss2(std::string("1 2")); 248 std::vector<double> uint_vec; 249 utility::load(iss2, uint_vec); 250 suite.add(uint_vec.size()==2); 251 252 std::istringstream iss3(std::string("1.69 3.14")); 253 std::vector<std::string> str_vec; 254 utility::load(iss3, str_vec); 255 if (str_vec.size()==2) { 256 if (str_vec[1]!="3.14") { 257 suite.out() << "error: load<string>:" << "\n" 258 << " str_vec[1] = " << str_vec[1] << "\n" 259 << " expected: 3.14n"; 260 suite.add(false); 261 } 262 } 263 else { 264 suite.out() << "str_vec.size() is not 2\n"; 265 suite.add(false); 266 } 235 267 } 236 268 -
trunk/yat/utility/utility.h
r2210 r2248 149 149 150 150 \note Requirement on T: utility::convert<T> must be supported 151 (from yat 0.7 T=string is also supported) 151 152 152 153 \since New in yat 0.6 … … 173 174 174 175 \note Requirement on T: utility::convert<T> must be supported 176 (from yat 0.7 T=string is also supported) 175 177 176 178 \since New in yat 0.6 … … 179 181 void load(std::istream& is, std::vector<T>& vec, char sep='\0'); 180 182 183 // private namespace 184 namespace detail { 185 /** 186 Functor used in load function 187 */ 188 template<typename T> 189 struct VectorPusher 190 { 191 /** 192 convert element to T and push on vec's back 193 194 \internal 195 */ 196 void operator()(const std::string& element, std::vector<T>& vec) 197 { 198 if (!element.size()) 199 vec.push_back(std::numeric_limits<T>::quiet_NaN()); 200 else { 201 vec.push_back(theplu::yat::utility::convert<T>(element)); 202 } 203 } 204 }; 205 206 /** 207 specialization for string 208 209 \internal 210 */ 211 template<> 212 struct VectorPusher<std::string> 213 { 214 /** 215 push element on vec's back 216 */ 217 void operator()(const std::string& element, std::vector<std::string>& vec) 218 { 219 vec.push_back(element); 220 } 221 }; 222 223 } // end of namespace detail 224 225 181 226 // template implementations 182 227 … … 287 332 void load(std::istream& is, std::vector<T>& vec, char sep='\0') 288 333 { 334 detail::VectorPusher<T> pusher; 289 335 std::string element; 290 336 bool ok=true; 291 while( ok) {337 while(true) { 292 338 if(sep=='\0') 293 339 ok=(is>>element); … … 297 343 break; 298 344 299 if (!element.size()) 300 vec.push_back(std::numeric_limits<T>::quiet_NaN()); 301 else { 302 vec.push_back(convert<T>(element)); 303 } 345 pusher(element, vec); 304 346 } 305 347 }
Note: See TracChangeset
for help on using the changeset viewer.