PageRenderTime 131ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llcommon/stringize.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 107 lines | 35 code | 11 blank | 61 comment | 0 complexity | 7cc879a2adfd934ba67a6c481a26e803 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file stringize.h
  3. * @author Nat Goodspeed
  4. * @date 2008-12-17
  5. * @brief stringize(item) template function and STRINGIZE(expression) macro
  6. *
  7. * $LicenseInfo:firstyear=2008&license=viewerlgpl$
  8. * Second Life Viewer Source Code
  9. * Copyright (C) 2010, Linden Research, Inc.
  10. *
  11. * This library is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation;
  14. * version 2.1 of the License only.
  15. *
  16. * This library is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with this library; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. *
  25. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  26. * $/LicenseInfo$
  27. */
  28. #if ! defined(LL_STRINGIZE_H)
  29. #define LL_STRINGIZE_H
  30. #include <sstream>
  31. #include <boost/lambda/lambda.hpp>
  32. /**
  33. * stringize(item) encapsulates an idiom we use constantly, using
  34. * operator<<(std::ostringstream&, TYPE) followed by std::ostringstream::str()
  35. * to render a string expressing some item.
  36. */
  37. template <typename T>
  38. std::string stringize(const T& item)
  39. {
  40. std::ostringstream out;
  41. out << item;
  42. return out.str();
  43. }
  44. /**
  45. * stringize_f(functor)
  46. */
  47. template <typename Functor>
  48. std::string stringize_f(Functor const & f)
  49. {
  50. std::ostringstream out;
  51. f(out);
  52. return out.str();
  53. }
  54. /**
  55. * STRINGIZE(item1 << item2 << item3 ...) effectively expands to the
  56. * following:
  57. * @code
  58. * std::ostringstream out;
  59. * out << item1 << item2 << item3 ... ;
  60. * return out.str();
  61. * @endcode
  62. */
  63. #define STRINGIZE(EXPRESSION) (stringize_f(boost::lambda::_1 << EXPRESSION))
  64. /**
  65. * destringize(str)
  66. * defined for symmetry with stringize
  67. * *NOTE - this has distinct behavior from boost::lexical_cast<T> regarding
  68. * leading/trailing whitespace and handling of bad_lexical_cast exceptions
  69. */
  70. template <typename T>
  71. T destringize(std::string const & str)
  72. {
  73. T val;
  74. std::istringstream in(str);
  75. in >> val;
  76. return val;
  77. }
  78. /**
  79. * destringize_f(str, functor)
  80. */
  81. template <typename Functor>
  82. void destringize_f(std::string const & str, Functor const & f)
  83. {
  84. std::istringstream in(str);
  85. f(in);
  86. }
  87. /**
  88. * DESTRINGIZE(str, item1 >> item2 >> item3 ...) effectively expands to the
  89. * following:
  90. * @code
  91. * std::istringstream in(str);
  92. * in >> item1 >> item2 >> item3 ... ;
  93. * @endcode
  94. */
  95. #define DESTRINGIZE(STR, EXPRESSION) (destringize_f((STR), (boost::lambda::_1 >> EXPRESSION)))
  96. #endif /* ! defined(LL_STRINGIZE_H) */