Changeset 2248


Ignore:
Timestamp:
Apr 22, 2010, 2:57:13 AM (12 years ago)
Author:
Peter
Message:

extend load functions to work also for T=std::string

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/NEWS

    r2217 r2248  
    44
    55version 0.7 (released DATE)
     6  - utility::load functions now work with std::string as well.
    67
    78  A complete list of closed tickets can be found here [[br]]
  • trunk/test/utility_test.cc

    r2239 r2248  
    4747void test_errno_error_func(void);
    4848void test_get_map(test::Suite&);
     49void test_load(test::Suite&);
    4950void test_inverse(test::Suite&);
    5051
     
    170171  test_errno_error(suite);
    171172  test_ptr_compare(suite);
     173  test_load(suite);
    172174
    173175  return suite.return_value();
     
    233235  if (!post_ok && prior_ok)
    234236    suite.err() << "test_less_nan failed\n";
     237}
     238
     239
     240void 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  }
    235267}
    236268
  • trunk/yat/utility/utility.h

    r2210 r2248  
    149149
    150150     \note Requirement on T: utility::convert<T> must be supported
     151     (from yat 0.7 T=string is also supported)
    151152
    152153     \since New in yat 0.6
     
    173174     
    174175     \note Requirement on T: utility::convert<T> must be supported
     176     (from yat 0.7 T=string is also supported)
    175177
    176178     \since New in yat 0.6
     
    179181  void load(std::istream& is, std::vector<T>& vec, char sep='\0');
    180182 
     183// private namespace
     184namespace 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
    181226  // template implementations
    182227
     
    287332  void load(std::istream& is, std::vector<T>& vec, char sep='\0')
    288333  {
     334    detail::VectorPusher<T> pusher;
    289335    std::string element;
    290336    bool ok=true;
    291     while(ok) {
     337    while(true) {
    292338      if(sep=='\0')
    293339        ok=(is>>element);
     
    297343        break;
    298344     
    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);
    304346    }
    305347  }           
Note: See TracChangeset for help on using the changeset viewer.