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