Fix documentation build under maverick (doxygen 1.7.1)
[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
40 //-/////////////////////////////////////////////////////////////////////////////////////////////////
41
42 namespace senf {
43 namespace console {
44
45     template <class _> struct ArgumentTraits;
46     template <class _> struct ReturnValueTraits;
47
48 namespace detail {
49
50     template <class CharT>
51     struct MatchingShortType
52         : public boost::mpl::if_c<std::numeric_limits<CharT>::is_signed,short,unsigned short>
53     {};
54
55     template <class CharT>
56     struct CharArgumentTraits
57         : public ArgumentTraits<typename MatchingShortType<CharT>::type>
58     {
59         typedef ArgumentTraits<typename MatchingShortType<CharT>::type> base;
60         typedef CharT type;
61         static void parse(ParseCommandInfo::TokensRange const & tokens, CharT & out);
62         static std::string description();
63     };
64
65     template <class CharT>
66     struct CharReturnValueTraits
67         : public ReturnValueTraits<typename MatchingShortType<CharT>::type>
68     {
69         typedef CharT type;
70     };
71
72 #ifndef DOXYGEN
73     struct StringILess
74     {
75         bool operator()(std::string const & left, std::string const & right) const
76             { return boost::algorithm::to_lower_copy(left)
77                   < boost::algorithm::to_lower_copy(right); }
78     };
79
80     typedef boost::bimap<boost::bimaps::set_of<std::string, StringILess>, long> EnumTable;
81
82     long parseEnum(EnumTable const & table, ParseCommandInfo::TokensRange const & tokens);
83     std::string formatEnum(EnumTable const & table, long value);
84
85 #   define SENF_CONSOLE_REGISTER_ENUM_ELT(r,d,e)                        \
86         BOOST_PP_IF( SENF_CONSOLE_REGISTER_ENUM_HASKEY(e),              \
87                      SENF_CONSOLE_REGISTER_ENUM_WITHKEY,                \
88                      SENF_CONSOLE_REGISTER_ENUM_NOKEY )(d, e)
89
90 #   define SENF_CONSOLE_REGISTER_ENUM_GOBBLE__key(k,e)
91 #   define SENF_CONSOLE_REGISTER_ENUM_GET_KEY__key(k,e) k
92 #   define SENF_CONSOLE_REGISTER_ENUM_GET_ENUM__key(k,e) e
93 #   define SENF_CONSOLE_REGISTER_ENUM_HASKEY(e)                         \
94         BOOST_PP_IS_EMPTY( SENF_CAT_RECURS1(SENF_CONSOLE_REGISTER_ENUM_GOBBLE__, e) )
95
96 #   define SENF_CONSOLE_REGISTER_ENUM_KEY_GETENUM(e)                    \
97         SENF_CAT_RECURS2(SENF_CONSOLE_REGISTER_ENUM_GET_ENUM__, e)
98 #   define SENF_CONSOLE_REGISTER_ENUM_KEY_GETKEY(e)                     \
99         SENF_CAT_RECURS3(SENF_CONSOLE_REGISTER_ENUM_GET_KEY__, e)
100
101 #   define SENF_CONSOLE_REGISTER_ENUM_NOKEY(prefix, e)                  \
102         ( BOOST_PP_STRINGIZE(e), static_cast<long>(prefix e) )
103
104 #   define SENF_CONSOLE_REGISTER_ENUM_WITHKEY(prefix, e)                \
105         ( SENF_CONSOLE_REGISTER_ENUM_KEY_GETKEY(e),                     \
106           static_cast<long>(prefix SENF_CONSOLE_REGISTER_ENUM_KEY_GETENUM(e)) )
107
108
109 #   define SENF_CONSOLE_REGISTER_ENUM_(Prefix, Type, Values)                                      \
110         inline senf::console::detail::EnumTable & senf_console_enum_table(Prefix Type)            \
111         {                                                                                         \
112             static senf::console::detail::EnumTable table;                                        \
113             if (table.empty())                                                                    \
114                 boost::assign::insert(table)                                                      \
115                     BOOST_PP_SEQ_FOR_EACH( SENF_CONSOLE_REGISTER_ENUM_ELT, Prefix, Values );      \
116             return table;                                                                         \
117         }                                                                                         \
118         inline void senf_console_parse_argument(                                                  \
119             senf::console::ParseCommandInfo::TokensRange const & tokens, Prefix Type & out)       \
120         {                                                                                         \
121             out = static_cast<Prefix Type>(                                                       \
122                 senf::console::detail::parseEnum(                                                 \
123                     senf_console_enum_table( Prefix Type() ), tokens) );                          \
124         }                                                                                         \
125         inline void senf_console_format_value(Prefix Type value, std::ostream & os)               \
126         {                                                                                         \
127             os << senf::console::detail::formatEnum(                                              \
128                 senf_console_enum_table( Prefix Type() ), static_cast<long>(value) );             \
129         }
130
131 #endif
132
133 }}}
134
135 //-/////////////////////////////////////////////////////////////////////////////////////////////////
136 #endif
137
138 \f
139 // Local Variables:
140 // mode: c++
141 // fill-column: 100
142 // comment-column: 40
143 // c-file-style: "senf"
144 // indent-tabs-mode: nil
145 // ispell-local-dictionary: "american"
146 // compile-command: "scons -u test"
147 // End: