53fe98452d64db262a695afdadede46c3887bfcf
[platform/upstream/boost.git] / boost / date_time / special_values_formatter.hpp
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: 2008-02-27 12:00:24 -0800 (Wed, 27 Feb 2008) $
11  */
12
13 #include <vector>
14 #include <string>
15 #include "boost/date_time/special_defs.hpp"
16
17 namespace boost { namespace date_time {
18
19
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.  
24    *
25    *  While not a facet, this class is used by the date and time facets for formatting
26    *  special value types.
27    *
28    */
29   template <class CharT, class OutItrT = std::ostreambuf_iterator<CharT, std::char_traits<CharT> > >
30   class special_values_formatter  
31   {
32   public:
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];
37
38     //! Construct special values formatter using default strings.
39     /*! Default strings are not-a-date-time -infinity +infinity
40      */
41     special_values_formatter() 
42     {
43       std::copy(&default_special_value_names[0], 
44                 &default_special_value_names[3], 
45                 std::back_inserter(m_special_value_names));      
46     }
47
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
51      *  special values.  
52      *@code
53      *  const char* const special_value_names[]={"nadt","-inf","+inf" };
54      *
55      *  special_value_formatter svf(&special_value_names[0], &special_value_names[3]);
56      *@endcode
57      */
58     special_values_formatter(const char_type* const* begin, const char_type* const* end) 
59     {
60       std::copy(begin, end, std::back_inserter(m_special_value_names));
61     }
62     special_values_formatter(typename collection_type::iterator beg, typename collection_type::iterator end)
63     {
64       std::copy(beg, end, std::back_inserter(m_special_value_names));
65     }
66
67     OutItrT put_special(OutItrT next, 
68                         const boost::date_time::special_values& value) const 
69     {
70       
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(),
75                   next);
76       }
77       return next;
78     }
79   protected:
80     collection_type m_special_value_names;
81   };
82
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'} };
93
94  } } //namespace boost::date_time
95
96 #endif