special_values_formatter.hpp 3.6 KB

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