Utils/Console: Parse char arguments as integers
[senf.git] / senf / Utils / Console / Traits.ih
1 // $Id$
2 //
3 // Copyright (C) 2008 
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief Traits internal header */
25
26 #ifndef IH_SENF_Scheduler_Console_Traits_
27 #define IH_SENF_Scheduler_Console_Traits_ 1
28
29 // Custom includes
30 #include <string>
31 #include <limits>
32 #include <boost/preprocessor/seq/for_each.hpp>
33 #include <boost/preprocessor/stringize.hpp>
34 #include <boost/preprocessor/facilities/empty.hpp>
35 #include <boost/bimap.hpp>
36 #include <boost/assign/list_inserter.hpp>
37 #include <boost/algorithm/string/case_conv.hpp>
38 #include <boost/mpl/if.hpp>
39 #include <senf/Utils/singleton.hh>
40
41 ///////////////////////////////ih.p////////////////////////////////////////
42
43 namespace senf {
44 namespace console {
45
46     template <class _> struct ArgumentTraits;
47     template <class _> struct ReturnValueTraits;
48
49 namespace detail {
50     
51     template <class CharT>
52     struct MatchingShortType
53         : public boost::mpl::if_c<std::numeric_limits<CharT>::is_signed,short,unsigned short>
54     {};
55
56     template <class CharT>
57     struct CharArgumentTraits
58         : public ArgumentTraits<typename MatchingShortType<CharT>::type>
59     {
60         typedef ArgumentTraits<typename MatchingShortType<CharT>::type> base;
61         typedef CharT type;
62         static void parse(ParseCommandInfo::TokensRange const & tokens, CharT & out);
63         static std::string description();
64     };
65
66     template <class CharT>
67     struct CharReturnValueTraits
68         : public ReturnValueTraits<typename MatchingShortType<CharT>::type>
69     {
70         typedef CharT type;
71     };
72
73 #ifndef DOXYGEN
74     struct StringILess
75     {
76         bool operator()(std::string const & left, std::string const & right) const
77             { return boost::algorithm::to_lower_copy(left) 
78                   < boost::algorithm::to_lower_copy(right); }
79     };
80
81     typedef boost::bimap<boost::bimaps::set_of<std::string, StringILess>, long> EnumTable;
82
83     long parseEnum(EnumTable const & table, ParseCommandInfo::TokensRange const & tokens);
84     std::string formatEnum(EnumTable const & table, long value);
85
86 #   define SENF_CONSOLE_REGISTER_ENUM_ELT(r,d,e) \
87         ( BOOST_PP_STRINGIZE(e), static_cast<long>(d e) )
88
89 #   define SENF_CONSOLE_REGISTER_ENUM_(Prefix, Type, Values)                                      \
90         inline senf::console::detail::EnumTable & senf_console_enum_table(Prefix Type)            \
91         {                                                                                         \
92             static senf::console::detail::EnumTable table;                                        \
93             if (table.empty())                                                                    \
94                 boost::assign::insert(table)                                                      \
95                     BOOST_PP_SEQ_FOR_EACH( SENF_CONSOLE_REGISTER_ENUM_ELT, Prefix, Values );      \
96             return table;                                                                         \
97         }                                                                                         \
98         inline void senf_console_parse_argument(                                                  \
99             senf::console::ParseCommandInfo::TokensRange const & tokens, Prefix Type & out)       \
100         {                                                                                         \
101             out = static_cast<Prefix Type>(                                                       \
102                 senf::console::detail::parseEnum(                                                 \
103                     senf_console_enum_table( Prefix Type() ), tokens) );                          \
104         }                                                                                         \
105         inline void senf_console_format_value(Prefix Type value, std::ostream & os)               \
106         {                                                                                         \
107             os << senf::console::detail::formatEnum(                                              \
108                 senf_console_enum_table( Prefix Type() ), static_cast<long>(value) );             \
109         }
110
111 #endif
112
113 }}}
114
115 ///////////////////////////////ih.e////////////////////////////////////////
116 #endif
117
118 \f
119 // Local Variables:
120 // mode: c++
121 // fill-column: 100
122 // comment-column: 40
123 // c-file-style: "senf"
124 // indent-tabs-mode: nil
125 // ispell-local-dictionary: "american"
126 // compile-command: "scons -u test"
127 // End: