string_cast.hpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // (C) Copyright Gennadiy Rozental 2001.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // See http://www.boost.org/libs/test for the library home page.
  6. //
  7. // File : $RCSfile$
  8. //
  9. // Version : $Revision$
  10. //
  11. // Description : trivial utility to cast to/from strings
  12. // ***************************************************************************
  13. #ifndef BOOST_TEST_UTILS_STRING_CAST_HPP
  14. #define BOOST_TEST_UTILS_STRING_CAST_HPP
  15. // Boost.Test
  16. #include <boost/test/utils/basic_cstring/basic_cstring.hpp>
  17. // STL
  18. #include <sstream>
  19. #include <boost/test/detail/suppress_warnings.hpp>
  20. //____________________________________________________________________________//
  21. namespace boost {
  22. namespace unit_test {
  23. namespace utils {
  24. // ************************************************************************** //
  25. // ************** string_cast ************** //
  26. // ************************************************************************** //
  27. template<typename T>
  28. inline std::string
  29. string_cast( T const& t )
  30. {
  31. std::ostringstream buff;
  32. buff << t;
  33. return buff.str();
  34. }
  35. //____________________________________________________________________________//
  36. // ************************************************************************** //
  37. // ************** string_as ************** //
  38. // ************************************************************************** //
  39. template<typename T>
  40. inline bool
  41. string_as( const_string str, T& res )
  42. {
  43. std::istringstream buff( std::string( str.begin(), str.end() ) );
  44. buff >> res;
  45. return !buff.fail() && buff.eof();
  46. }
  47. //____________________________________________________________________________//
  48. } // namespace utils
  49. } // namespace unit_test
  50. } // namespace boost
  51. #include <boost/test/detail/enable_warnings.hpp>
  52. #endif // BOOST_TEST_UTILS_STRING_CAST_HPP