12fbb658b364091929a87b85bf231e175b44d34c
[senf.git] / 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_Traits_
27 #define IH_Traits_ 1
28
29 // Custom includes
30 #include <string>
31 #include <boost/preprocessor/seq/for_each.hpp>
32 #include <boost/preprocessor/stringize.hpp>
33 #include <boost/bimap.hpp>
34 #include <boost/assign/list_inserter.hpp>
35 #include "../Utils/singleton.hh"
36
37 ///////////////////////////////ih.p////////////////////////////////////////
38
39 namespace senf {
40 namespace console {
41 namespace detail {
42
43     typedef boost::bimap<std::string, long> EnumTable;
44
45     long parseEnum(EnumTable const & table, ParseCommandInfo::TokensRange const & tokens);
46     std::string formatEnum(EnumTable const & table, long value);
47
48     template <class EnumType>
49     struct EnumTraits : public senf::singleton< EnumTraits<EnumType> >
50     {
51         using senf::singleton< EnumTraits<EnumType> >::instance;
52         EnumTable table;
53     };
54
55 #   define SENF_CONSOLE_REGISTER_ENUM_ELT(r,d,e) (BOOST_PP_STRINGIZE(e), static_cast<long>(e))
56
57 #   define SENF_CONSOLE_REGISTER_ENUM_(Type, Values)                                              \
58         void senf_console_init_enum_table(Type)                                                   \
59         {                                                                                         \
60             senf::console::detail::EnumTraits<Type> & traits (                                    \
61                 senf::console::detail::EnumTraits<Type>::instance() );                            \
62             if (traits.table.empty())                                                             \
63                 boost::assign::insert(traits.table)                                               \
64                     BOOST_PP_SEQ_FOR_EACH( SENF_CONSOLE_REGISTER_ENUM_ELT, _, Values );           \
65         }                                                                                         \
66         void senf_console_parse_argument(                                                         \
67             senf::console::ParseCommandInfo::TokensRange const & tokens, Type & out)              \
68         {                                                                                         \
69             senf_console_init_enum_table( Type() );                                               \
70             out = static_cast<Type>(                                                              \
71                 senf::console::detail::parseEnum(                                                 \
72                     senf::console::detail::EnumTraits<Type>::instance().table, tokens));          \
73         }                                                                                         \
74         void senf_console_format_value(Type value, std::ostream & os)                             \
75         {                                                                                         \
76             senf_console_init_enum_table( Type() );                                               \
77             os << senf::console::detail::formatEnum(                                              \
78                 senf::console::detail::EnumTraits<Type>::instance().table,                        \
79                 static_cast<long>(value) );                                                       \
80         }
81
82 }}}
83
84 ///////////////////////////////ih.e////////////////////////////////////////
85 #endif
86
87 \f
88 // Local Variables:
89 // mode: c++
90 // fill-column: 100
91 // comment-column: 40
92 // c-file-style: "senf"
93 // indent-tabs-mode: nil
94 // ispell-local-dictionary: "american"
95 // compile-command: "scons -u test"
96 // End: