2 #ifndef DATETIME_SPECIAL_VALUE_FORMATTER_HPP___
3 #define DATETIME_SPECIAL_VALUE_FORMATTER_HPP___
5 /* Copyright (c) 2004 CrystalClear Software, Inc.
6 * Use, modification and distribution is subject to the
7 * Boost Software License, Version 1.0. (See accompanying
8 * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
15 #include "boost/date_time/special_defs.hpp"
17 namespace boost { namespace date_time {
20 //! Class that provides generic formmatting ostream formatting for special values
21 /*! This class provides for the formmating of special values to an output stream.
22 * In particular, it produces strings for the values of negative and positive
23 * infinity as well as not_a_date_time.
25 * While not a facet, this class is used by the date and time facets for formatting
26 * special value types.
29 template <class CharT, class OutItrT = std::ostreambuf_iterator<CharT, std::char_traits<CharT> > >
30 class special_values_formatter
33 typedef std::basic_string<CharT> string_type;
34 typedef CharT char_type;
35 typedef std::vector<string_type> collection_type;
36 static const char_type default_special_value_names[3][17];
38 //! Construct special values formatter using default strings.
39 /*! Default strings are not-a-date-time -infinity +infinity
41 special_values_formatter()
43 std::copy(&default_special_value_names[0],
44 &default_special_value_names[3],
45 std::back_inserter(m_special_value_names));
48 //! Construct special values formatter from array of strings
49 /*! This constructor will take pair of iterators from an array of strings
50 * that represent the special values and copy them for use in formatting
53 * const char* const special_value_names[]={"nadt","-inf","+inf" };
55 * special_value_formatter svf(&special_value_names[0], &special_value_names[3]);
58 special_values_formatter(const char_type* const* begin, const char_type* const* end)
60 std::copy(begin, end, std::back_inserter(m_special_value_names));
62 special_values_formatter(typename collection_type::iterator beg, typename collection_type::iterator end)
64 std::copy(beg, end, std::back_inserter(m_special_value_names));
67 OutItrT put_special(OutItrT next,
68 const boost::date_time::special_values& value) const
71 unsigned int index = value;
72 if (index < m_special_value_names.size()) {
73 std::copy(m_special_value_names[index].begin(),
74 m_special_value_names[index].end(),
80 collection_type m_special_value_names;
83 //! Storage for the strings used to indicate special values
84 /* using c_strings to initialize these worked fine in testing, however,
85 * a project that compiled its objects separately, then linked in a separate
86 * step wound up with redefinition errors for the values in this array.
87 * Initializing individual characters eliminated this problem */
88 template <class CharT, class OutItrT>
89 const typename special_values_formatter<CharT, OutItrT>::char_type special_values_formatter<CharT, OutItrT>::default_special_value_names[3][17] = {
90 {'n','o','t','-','a','-','d','a','t','e','-','t','i','m','e'},
91 {'-','i','n','f','i','n','i','t','y'},
92 {'+','i','n','f','i','n','i','t','y'} };
94 } } //namespace boost::date_time