PPI: Add senf::connect() packet type compatibility check documentation
[senf.git] / Console / OverloadedCommand.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 OverloadedCommand public header */
25
26 #ifndef HH_OverloadedCommand_
27 #define HH_OverloadedCommand_ 1
28
29 // Custom includes
30 #include "Node.hh"
31 #include <boost/intrusive_ptr.hpp>
32 #include "../Utils/intrusive_refcount.hh"
33
34 //#include "OverloadedCommand.mpp"
35 ///////////////////////////////hh.p////////////////////////////////////////
36
37 namespace senf {
38 namespace console {
39
40     class OverloadedCommandNode;
41
42     /** \brief Base class for command overload of OverloadedCommandNode
43
44         This class is the base class of the commands which may be added to an
45         OverloadedCommandNode.
46       */
47     class CommandOverload
48         : public senf::intrusive_refcount
49     {
50     public:
51         ///////////////////////////////////////////////////////////////////////////
52         // Types
53
54         typedef boost::intrusive_ptr<CommandOverload> ptr;
55
56         ///////////////////////////////////////////////////////////////////////////
57
58         virtual ~CommandOverload();
59
60         void execute(std::ostream & os, ParseCommandInfo const & command);
61                                         ///< Call the overload
62                                         /**< If the \a arguments are not acceptable for this
63                                              overload, a SyntaxErrorException must be thrown. 
64                                              Same as operator()() */
65
66         void operator()(std::ostream & os, ParseCommandInfo const & command);
67                                         ///< Call the overload
68                                         /**< If the \a arguments are not acceptable for this
69                                              overload, a SyntaxErrorException must be thrown. 
70                                              Same as execute() */
71
72         void help(std::ostream & os);   ///< Provide help for this specific overload
73
74         OverloadedCommandNode & node(); ///< Access owning node
75                                         /**< \pre The command \e must have been added to an
76                                              OverloadedCommandNode. */
77
78     protected:
79         CommandOverload();
80
81 #ifndef DOXYGEN
82     private:
83 #endif
84         virtual void v_help(std::ostream & os) const = 0;
85         virtual void v_execute(std::ostream & os, ParseCommandInfo const & command) const = 0;
86
87     private:
88         OverloadedCommandNode * node_;
89
90         friend class OverloadedCommandNode;
91     };
92
93     /** \brief Command node which allows multiple registered callbacks
94
95         OverloadedCommandNode is like SimpleCommandNode but allows to register multiple commands to
96         a single node. This works by calling each command in the list consecutively until no
97         'SyntaxErrorException' exception is thrown.
98
99         This works by first adding an OverloadedCommandNode to the directory in question and then
100         adding commands to that node. Commands are derived from CommandOverload. 
101         \code
102         senf::console::DirectoryNode & dir (...);
103         senf::console::OverloadedCommandNode & cmd (
104             dir.add("cmd", senf::console::OverloadedCommandNode::create()) );
105         cmd.add(senf::console::SimpleCommandOverload::create(&callback));
106         cmd.add(senf::console::SimpleCommandOverload::create(&anotherCallback));
107         \endcode
108
109         However, this facility is mostly used not directly but indirectly (and automatically) when
110         adding argument parsing callbacks.
111
112         \warning For this to work, the commands <b>must</b> do all syntax checking before doing any
113             operation
114
115         \ingroup node_tree
116       */
117     class OverloadedCommandNode
118         : public CommandNode
119     {
120     public:
121         ///////////////////////////////////////////////////////////////////////////
122         // Types
123
124         typedef boost::shared_ptr<OverloadedCommandNode> ptr;
125         typedef boost::shared_ptr<OverloadedCommandNode const> cptr;
126         typedef boost::weak_ptr<OverloadedCommandNode> weak_ptr;
127
128         ///////////////////////////////////////////////////////////////////////////
129         ///\name Structors and default members
130         ///@{
131
132         static ptr create();
133
134         ///@}
135         ///////////////////////////////////////////////////////////////////////////
136         
137         template <class Command>
138         Command & add(boost::intrusive_ptr<Command> overload); ///< Add an additional overload
139
140         ptr thisptr();
141         cptr thisptr() const;
142
143         OverloadedCommandNode & doc(std::string const & doc);
144                                         ///< Assign global help for all overloads
145
146     protected:
147
148     private:
149         OverloadedCommandNode();
150
151         virtual void v_help(std::ostream & output) const;
152         virtual void v_execute(std::ostream & output, ParseCommandInfo const & command) const;
153
154         typedef std::vector<CommandOverload::ptr> Overloads;
155
156         Overloads overloads_;
157         std::string doc_;
158     };
159
160     /** \brief Basic command overload
161
162         This is an implementation of CommandOverload which allows to call an arbitrary callback with
163         the correct signature 
164         (<tt>void (std::ostream &, senf::console::ParseCommandInfo const &)</tt>)
165       */
166     class SimpleCommandOverload
167         : public CommandOverload
168     {
169     public:
170         ///////////////////////////////////////////////////////////////////////////
171         // Types
172
173         typedef boost::intrusive_ptr<SimpleCommandOverload> ptr;
174         typedef boost::function<void (std::ostream &, ParseCommandInfo const &)> Function;
175
176         ///////////////////////////////////////////////////////////////////////////
177         ///\name Structors and default members
178         ///@{
179
180         static SimpleCommandOverload::ptr create(Function fn);
181                                         ///< Create new SimpleCommandOverload
182                                         /**< \param[in] fn callback to call */
183
184         ///@}
185         ///////////////////////////////////////////////////////////////////////////
186
187         SimpleCommandOverload & doc(std::string const & doc);
188                                         ///< Assign overload specific documentation
189
190     protected:
191
192     private:
193         SimpleCommandOverload(Function fn);
194
195         virtual void v_help(std::ostream & os) const;
196         virtual void v_execute(std::ostream & os, ParseCommandInfo const & command) const;
197
198         Function fn_;
199         std::string doc_;
200     };
201
202 }}
203
204 ///////////////////////////////hh.e////////////////////////////////////////
205 #include "OverloadedCommand.cci"
206 //#include "OverloadedCommand.ct"
207 #include "OverloadedCommand.cti"
208 #endif
209
210 \f
211 // Local Variables:
212 // mode: c++
213 // fill-column: 100
214 // comment-column: 40
215 // c-file-style: "senf"
216 // indent-tabs-mode: nil
217 // ispell-local-dictionary: "american"
218 // compile-command: "scons -u test"
219 // End: