Utils: Refactor hexdump() helper to move code out of template function
[senf.git] / Console / Node.cci
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 Node inline non-template implementation */
25
26 #include "Node.ih"
27
28 // Custom includes
29 #include "../Utils/senfassert.hh"
30
31 #define prefix_ inline
32 ///////////////////////////////cci.p///////////////////////////////////////
33
34 ///////////////////////////////////////////////////////////////////////////
35 // senf::console::GenericNode
36
37 prefix_ senf::console::GenericNode::~GenericNode()
38 {}
39
40 prefix_ std::string const & senf::console::GenericNode::name()
41     const
42 {
43     return name_;
44 }
45
46 prefix_ senf::console::GenericNode::GenericNode()
47     : parent_ (0)
48 {}
49
50 prefix_ void senf::console::GenericNode::name(std::string const & name)
51 {
52     name_ = name;
53 }
54
55 prefix_ boost::shared_ptr<senf::console::DirectoryNode> senf::console::GenericNode::parent()
56     const
57 {
58     return boost::static_pointer_cast<DirectoryNode>(
59         parent_ ? parent_->shared_from_this() : ptr() );
60 }
61
62 prefix_ senf::console::GenericNode::ptr senf::console::GenericNode::unlink()
63 {
64     if (parent_)
65         return parent()->remove(name());
66     else
67         return thisptr();
68 }
69
70 prefix_ void senf::console::GenericNode::help(std::ostream & output)
71     const
72 {
73     v_help(output);
74 }
75
76 prefix_ senf::console::GenericNode::ptr senf::console::GenericNode::thisptr()
77 {
78     return shared_from_this();
79 }
80
81 prefix_ senf::console::GenericNode::cptr senf::console::GenericNode::thisptr()
82     const
83 {
84     return shared_from_this();
85 }
86
87 prefix_ bool senf::console::GenericNode::operator==(GenericNode & other)
88     const
89 {
90     return this == & other;
91 }
92
93 prefix_ bool senf::console::GenericNode::operator!=(GenericNode & other)
94     const
95 {
96     return this != & other;
97 }
98
99 ///////////////////////////////////////////////////////////////////////////
100 // senf::console::DirectoryNode
101
102 prefix_ senf::console::DirectoryNode::ptr senf::console::DirectoryNode::create()
103 {
104     return ptr(new DirectoryNode());
105 }
106
107 prefix_ bool senf::console::DirectoryNode::hasChild(std::string const & name)
108     const
109 {
110     ChildMap::const_iterator i (children_.find(name));
111     return i != children_.end();
112 }
113
114 prefix_ senf::console::DirectoryNode &
115 senf::console::DirectoryNode::getDirectory(std::string const & name)
116     const
117 {
118     try {
119         return dynamic_cast<DirectoryNode&>(get(name));
120     }
121     SENF_WRAP_EXC(std::bad_cast)
122 }
123
124 prefix_ senf::console::DirectoryNode &
125 senf::console::DirectoryNode::operator[](std::string const & name)
126     const
127 {
128     return getDirectory(name);
129 }
130
131 prefix_ senf::console::CommandNode &
132 senf::console::DirectoryNode::getCommand(std::string const & name)
133     const
134 {
135     try {
136         return dynamic_cast<CommandNode&>(get(name));
137     }
138     SENF_WRAP_EXC(std::bad_cast)
139 }
140
141 prefix_ senf::console::CommandNode &
142 senf::console::DirectoryNode::operator()(std::string const & name)
143     const
144 {
145     return getCommand(name);
146 }
147
148 prefix_ senf::console::DirectoryNode &
149 senf::console::DirectoryNode::mkdir(std::string const & name)
150 {
151     return add(name, create());
152 }
153
154 prefix_ senf::console::DirectoryNode::ChildrenRange senf::console::DirectoryNode::children()
155     const
156 {
157     return boost::make_iterator_range(children_.begin(), children_.end());
158 }
159
160 prefix_ senf::console::DirectoryNode::ChildrenRange
161 senf::console::DirectoryNode::completions(std::string const & s)
162     const
163 {
164     return boost::make_iterator_range(children_.lower_bound(s),
165                                       children_.lower_bound(s + "\xff"));
166 }
167
168 prefix_ senf::console::DirectoryNode::DirectoryNode()
169 {}
170
171 prefix_ senf::console::DirectoryNode &
172 senf::console::DirectoryNode::doc(std::string const & doc)
173 {
174     doc_ = doc;
175     return *this;
176 }
177
178 prefix_ senf::console::DirectoryNode::ptr senf::console::DirectoryNode::thisptr()
179 {
180     return boost::static_pointer_cast<DirectoryNode>(shared_from_this());
181 }
182
183 prefix_ senf::console::DirectoryNode::cptr senf::console::DirectoryNode::thisptr()
184     const
185 {
186     return boost::static_pointer_cast<DirectoryNode const>(shared_from_this());
187 }
188
189 ///////////////////////////////////////////////////////////////////////////
190 // senf::console::detail::NodeTraverser
191
192 prefix_ senf::console::detail::NodeTraverser::NodeTraverser(DirectoryNode & root,
193                                                             DirectoryNode & dir,
194                                                             bool autocomplete)
195     : root_ (root), dir_ (dir.thisptr()), autocomplete_ (autocomplete), init_ (false)
196 {}
197
198 ///////////////////////////////////////////////////////////////////////////
199 // senf::console::SyntaxErrorException
200
201 prefix_ senf::console::SyntaxErrorException::SyntaxErrorException(std::string const & msg)
202     : message_(msg)
203 {}
204
205 prefix_ senf::console::SyntaxErrorException::~SyntaxErrorException()
206     throw()
207 {}
208
209 prefix_ std::string const & senf::console::SyntaxErrorException::message()
210     const
211 {
212     return message_;
213 }
214
215 ///////////////////////////////////////////////////////////////////////////
216 // senf::console::CommandNode
217
218 prefix_ senf::console::CommandNode::ptr senf::console::CommandNode::thisptr()
219 {
220     return boost::static_pointer_cast<CommandNode>(shared_from_this());
221 }
222
223 prefix_ senf::console::CommandNode::cptr senf::console::CommandNode::thisptr()
224     const
225 {
226     return boost::static_pointer_cast<CommandNode const>(shared_from_this());
227 }
228
229 prefix_ senf::console::CommandNode::CommandNode()
230 {}
231
232 prefix_ void senf::console::CommandNode::execute(std::ostream & output,
233                                                  ParseCommandInfo const & command)
234     const
235 {
236     v_execute(output, command);
237 }
238
239 prefix_ void senf::console::CommandNode::operator()(std::ostream & output,
240                                                     ParseCommandInfo const & command)
241     const
242 {
243     execute(output, command);
244 }
245
246 ///////////////////////////////////////////////////////////////////////////
247 // senf::console::SimpleCommandNode
248
249 prefix_ senf::console::SimpleCommandNode::SimpleCommandNode(Function const & fn)
250     : fn_ (fn)
251 {}
252
253 prefix_ senf::console::SimpleCommandNode::ptr
254 senf::console::SimpleCommandNode::create(Function const & fn)
255 {
256     return ptr(new SimpleCommandNode(fn));
257 }
258
259 prefix_ senf::console::SimpleCommandNode &
260 senf::console::SimpleCommandNode::doc(std::string const & doc)
261 {
262     doc_ = doc;
263     return *this;
264 }
265
266 prefix_ senf::console::SimpleCommandNode::ptr senf::console::SimpleCommandNode::thisptr()
267 {
268     return boost::static_pointer_cast<SimpleCommandNode>(shared_from_this());
269 }
270
271 prefix_ senf::console::SimpleCommandNode::cptr senf::console::SimpleCommandNode::thisptr()
272     const
273 {
274     return boost::static_pointer_cast<SimpleCommandNode const>(shared_from_this());
275 }
276
277 #ifndef DOXYGEN
278
279 prefix_ senf::console::SimpleCommandNode &
280 senf::console::senf_console_add_node(DirectoryNode & node, std::string const & name,
281                                      SimpleCommandNode::Function fn, int)
282 {
283     return node.add(name, SimpleCommandNode::create(fn));
284 }
285
286 #endif
287
288 ///////////////////////////////cci.e///////////////////////////////////////
289 #undef prefix_
290
291 \f
292 // Local Variables:
293 // mode: c++
294 // fill-column: 100
295 // comment-column: 40
296 // c-file-style: "senf"
297 // indent-tabs-mode: nil
298 // ispell-local-dictionary: "american"
299 // compile-command: "scons -u test"
300 // End: