Console: Implement enum registration and parsing/formatting
[senf.git] / Console / Traits.hh
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 public header */
25
26 #ifndef HH_Traits_
27 #define HH_Traits_ 1
28
29 // Custom includes
30 #include <iostream>
31 #include <boost/intrusive_ptr.hpp>
32 #include "../Utils/intrusive_refcount.hh"
33 #include "Parse.hh"
34 #include "Node.hh"
35
36 #include "Traits.ih"
37 //#include "Traits.mpp"
38 ///////////////////////////////hh.p////////////////////////////////////////
39
40 namespace senf {
41 namespace console {
42
43     /** \brief Customize return value formating
44
45         ReturnValueTraits provides return value formatting. The default implementation provided here
46         will just write the value to the output stream. 
47
48         To customize this behavior for some type, specialize this class for the type.
49
50         The output should \e not end in a newline since one is added automatically.
51      */
52     template <class Type>
53     struct ReturnValueTraits
54     {
55         typedef Type type;
56
57         static void format(Type const & value, std::ostream & os);
58                                         ///< Write \a value to \a os
59     };
60     
61     template <class Type>
62     void senf_console_format_value(Type const & value, std::ostream & os);
63
64     /** \brief Customize argument parsing
65         
66         ArgumentTraits provides argument parsing, Additionally, this class provides a way to get a
67         string-description of a type and to convert a value back into it's string representation
68         used to display default values.
69         
70         The default implementation provided here will use \c boost::lexical_cast and thereby \c
71         iostreams to convert an argument consisting of a single input token into the required
72         type. Types are named by returning the last component of the fully scoped name (e.g. \c
73         "string" for \c std::string). Values are formatted again using \c boost::lexical_cast.
74
75         To customize this behavior for some type, specialize this class for the type.
76      */
77     template <class Type>
78     struct ArgumentTraits
79     {
80         typedef Type type;
81
82         static void parse(ParseCommandInfo::TokensRange const & tokens, Type & out);
83                                         ///< Parse token range into value
84                                         /**< This function needs to parse \a tokens and write the
85                                              parsed value into \a out. This function needs to parse
86                                              the \e complete list of tokens, additional tokens must
87                                              be considered as syntax error.
88                                              \throws SyntaxErrorException
89                                              \param[in] tokens tokens to parse
90                                              \param[out] out parsed value */
91
92         static std::string description(); ///< String description of type
93                                         /**< Returns the string description of \a Type. Used to
94                                              generate online help. */
95         static std::string str(Type const & value); ///< Stringify value
96                                         /**< To show default values in the online help, this
97                                              function converts a value back into a one-line string
98                                              representation. The default implementation uses the
99                                              ReturnValueTraits for this conversion. */
100     };
101
102     template <class Type>
103     void senf_console_parse_argument(ParseCommandInfo::TokensRange const & tokens, Type & out);
104
105 #   define SENF_CONSOLE_REGISTER_ENUM(Type, Values) SENF_CONSOLE_REGISTER_ENUM_(Type, Values)
106
107 }}
108
109 ///////////////////////////////hh.e////////////////////////////////////////
110 //#include "Traits.cci"
111 //#include "Traits.ct"
112 #include "Traits.cti"
113 #endif
114
115 \f
116 // Local Variables:
117 // mode: c++
118 // fill-column: 100
119 // comment-column: 40
120 // c-file-style: "senf"
121 // indent-tabs-mode: nil
122 // ispell-local-dictionary: "american"
123 // compile-command: "scons -u test"
124 // End: