special_values_formatter.hpp 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #ifndef DATETIME_SPECIAL_VALUE_FORMATTER_HPP___
  2. #define DATETIME_SPECIAL_VALUE_FORMATTER_HPP___
  3. /* Copyright (c) 2004 CrystalClear Software, Inc.
  4. * Use, modification and distribution is subject to the
  5. * Boost Software License, Version 1.0. (See accompanying
  6. * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  7. * Author: Jeff Garland
  8. * $Date$
  9. */
  10. #include <vector>
  11. #include <string>
  12. #include <iterator>
  13. #include "boost/date_time/special_defs.hpp"
  14. namespace boost { namespace date_time {
  15. //! Class that provides generic formmatting ostream formatting for special values
  16. /*! This class provides for the formmating of special values to an output stream.
  17. * In particular, it produces strings for the values of negative and positive
  18. * infinity as well as not_a_date_time.
  19. *
  20. * While not a facet, this class is used by the date and time facets for formatting
  21. * special value types.
  22. *
  23. */
  24. template <class CharT, class OutItrT = std::ostreambuf_iterator<CharT, std::char_traits<CharT> > >
  25. class special_values_formatter
  26. {
  27. public:
  28. typedef std::basic_string<CharT> string_type;
  29. typedef CharT char_type;
  30. typedef std::vector<string_type> collection_type;
  31. static const char_type default_special_value_names[3][17];
  32. //! Construct special values formatter using default strings.
  33. /*! Default strings are not-a-date-time -infinity +infinity
  34. */
  35. special_values_formatter()
  36. {
  37. std::copy(&default_special_value_names[0],
  38. &default_special_value_names[3],
  39. std::back_inserter(m_special_value_names));
  40. }
  41. //! Construct special values formatter from array of strings
  42. /*! This constructor will take pair of iterators from an array of strings
  43. * that represent the special values and copy them for use in formatting
  44. * special values.
  45. *@code
  46. * const char* const special_value_names[]={"nadt","-inf","+inf" };
  47. *
  48. * special_value_formatter svf(&special_value_names[0], &special_value_names[3]);
  49. *@endcode
  50. */
  51. special_values_formatter(const char_type* const* begin, const char_type* const* end)
  52. {
  53. std::copy(begin, end, std::back_inserter(m_special_value_names));
  54. }
  55. special_values_formatter(typename collection_type::iterator beg, typename collection_type::iterator end)
  56. {
  57. std::copy(beg, end, std::back_inserter(m_special_value_names));
  58. }
  59. OutItrT put_special(OutItrT next,
  60. const boost::date_time::special_values& value) const
  61. {
  62. unsigned int index = value;
  63. if (index < m_special_value_names.size()) {
  64. std::copy(m_special_value_names[index].begin(),
  65. m_special_value_names[index].end(),
  66. next);
  67. }
  68. return next;
  69. }
  70. protected:
  71. collection_type m_special_value_names;
  72. };
  73. //! Storage for the strings used to indicate special values
  74. /* using c_strings to initialize these worked fine in testing, however,
  75. * a project that compiled its objects separately, then linked in a separate
  76. * step wound up with redefinition errors for the values in this array.
  77. * Initializing individual characters eliminated this problem */
  78. template <class CharT, class OutItrT>
  79. const typename special_values_formatter<CharT, OutItrT>::char_type special_values_formatter<CharT, OutItrT>::default_special_value_names[3][17] = {
  80. {'n','o','t','-','a','-','d','a','t','e','-','t','i','m','e'},
  81. {'-','i','n','f','i','n','i','t','y'},
  82. {'+','i','n','f','i','n','i','t','y'} };
  83. } } //namespace boost::date_time
  84. #endif