HowTos/NewPacket: Remove old code from example
[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
43       */
44     class CommandOverload
45         : public senf::intrusive_refcount
46     {
47     public:
48         ///////////////////////////////////////////////////////////////////////////
49         // Types
50
51         typedef boost::intrusive_ptr<CommandOverload> ptr;
52         typedef CommandNode::Arguments Arguments;
53
54         ///////////////////////////////////////////////////////////////////////////
55
56         virtual ~CommandOverload();
57
58         void operator()(std::ostream & os, Arguments const & arguments);
59         void help(std::ostream & os);
60
61         OverloadedCommandNode & node();
62
63     protected:
64         CommandOverload();
65
66 #ifndef DOXYGEN
67     private:
68 #endif
69         virtual void v_help(std::ostream & os) const = 0;
70         virtual void v_execute(std::ostream & os, Arguments const & arguments) const = 0;
71
72     private:
73         OverloadedCommandNode * node_;
74
75         friend class OverloadedCommandNode;
76     };
77
78     /** \brief Command node which allows multiple registered callbacks
79
80         OverloadedCommand is like SimpleCommand but allows to register multiple commands to a single
81         node. This works by calling each command in the list consecutively until no 'SyntaxError'
82         exception is thrown. 
83
84         \warning For this to work, the commands <b>must</b> do all syntax checking before doing any
85             operation
86
87         \ingroup node_tree
88       */
89     class OverloadedCommandNode
90         : public CommandNode
91     {
92     public:
93         ///////////////////////////////////////////////////////////////////////////
94         // Types
95
96         typedef boost::shared_ptr<OverloadedCommandNode> ptr;
97         typedef boost::shared_ptr<OverloadedCommandNode const> cptr;
98         typedef boost::weak_ptr<OverloadedCommandNode> weak_ptr;
99
100         ///////////////////////////////////////////////////////////////////////////
101         ///\name Structors and default members
102         ///@{
103
104         static ptr create();
105
106         ///@}
107         ///////////////////////////////////////////////////////////////////////////
108
109         void add(CommandOverload::ptr overload);
110
111         ptr thisptr();
112         cptr thisptr() const;
113
114         OverloadedCommandNode & doc(std::string const & doc);
115
116     protected:
117
118     private:
119         OverloadedCommandNode();
120
121         virtual void v_help(std::ostream & output) const;
122         virtual void v_execute(std::ostream & output, Arguments const & arguments) const;
123
124         typedef std::vector<CommandOverload::ptr> Overloads;
125
126         Overloads overloads_;
127         std::string doc_;
128     };
129
130     /** \brief
131       */
132     class SimpleCommandOverload
133         : public CommandOverload
134     {
135     public:
136         ///////////////////////////////////////////////////////////////////////////
137         // Types
138
139         typedef boost::intrusive_ptr<SimpleCommandOverload> ptr;
140         typedef boost::function<void (std::ostream &, Arguments const &)> Function;
141
142         ///////////////////////////////////////////////////////////////////////////
143         ///\name Structors and default members
144         ///@{
145
146         static SimpleCommandOverload::ptr create(Function fn);
147
148         ///@}
149         ///////////////////////////////////////////////////////////////////////////
150
151         SimpleCommandOverload & doc(std::string const & doc);
152
153     protected:
154
155     private:
156         SimpleCommandOverload(Function fn);
157
158         virtual void v_help(std::ostream & os) const;
159         virtual void v_execute(std::ostream & os, Arguments const & arguments) const;
160
161         Function fn_;
162         std::string doc_;
163     };
164
165 }}
166
167 ///////////////////////////////hh.e////////////////////////////////////////
168 #include "OverloadedCommand.cci"
169 //#include "OverloadedCommand.ct"
170 //#include "OverloadedCommand.cti"
171 #endif
172
173 \f
174 // Local Variables:
175 // mode: c++
176 // fill-column: 100
177 // comment-column: 40
178 // c-file-style: "senf"
179 // indent-tabs-mode: nil
180 // ispell-local-dictionary: "american"
181 // compile-command: "scons -u test"
182 // End: