4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
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.
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.
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.
24 \brief Node public header */
31 #include <boost/shared_ptr.hpp>
32 #include <boost/weak_ptr.hpp>
33 #include <boost/enable_shared_from_this.hpp>
34 #include <boost/utility.hpp>
35 #include <boost/range/iterator_range.hpp>
36 #include <boost/typeof/typeof.hpp>
37 #include <boost/type_traits/remove_reference.hpp>
38 #include "../Utils/Exception.hh"
39 #include "../Utils/mpl.hh"
43 ///////////////////////////////hh.p////////////////////////////////////////
45 #include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
56 : public boost::enable_shared_from_this<GenericNode>
59 ///////////////////////////////////////////////////////////////////////////
62 typedef boost::shared_ptr<GenericNode> ptr;
63 typedef boost::shared_ptr<GenericNode const> cptr;
64 typedef boost::weak_ptr<GenericNode> weak_ptr;
66 ///////////////////////////////////////////////////////////////////////////
68 virtual ~GenericNode();
70 std::string const & name() const;
71 boost::shared_ptr<DirectoryNode> parent() const;
73 std::string path() const;
79 void help(std::ostream & output) const;
87 void name(std::string const & name);
88 static void name(GenericNode & node, std::string const & name);
89 void parent(DirectoryNode * parent);
92 virtual void v_help(std::ostream & output) const = 0;
95 DirectoryNode * parent_;
97 friend class intrusive_refcount_base;
98 friend class DirectoryNode;
101 class SimpleCommandNode;
103 template <class Object>
104 struct NodeCreateTraits
106 typedef BOOST_TYPEOF_TPL( senf_console_add_node(
107 * static_cast<DirectoryNode *>(0),
108 * static_cast<std::string const *>(0),
109 * static_cast<Object const *>(0),
112 typedef typename boost::remove_reference<result_type>::type NodeType;
115 static NodeType & create(DirectoryNode & node, std::string const & name,
121 ///\fixme Provide a default name for added nodes if 'name' is empty ?
123 class DirectoryNode : public GenericNode
125 typedef std::map<std::string, GenericNode::ptr> ChildMap;
128 ///////////////////////////////////////////////////////////////////////////
131 typedef boost::shared_ptr<DirectoryNode> ptr;
132 typedef boost::shared_ptr<DirectoryNode const> cptr;
133 typedef boost::weak_ptr<DirectoryNode> weak_ptr;
135 typedef boost::iterator_range<ChildMap::const_iterator> ChildrenRange;
136 typedef ChildMap::const_iterator child_iterator;
138 ///////////////////////////////////////////////////////////////////////////
139 ///\name Structors and default members
142 static std::auto_ptr<DirectoryNode> create();
145 ///////////////////////////////////////////////////////////////////////////
149 template <class NodeType>
150 NodeType & add(std::string const & name, std::auto_ptr<NodeType> node);
152 template <class NodeType>
153 NodeType & add(std::string const & name, boost::shared_ptr<NodeType> node);
155 template <class Object>
156 typename NodeCreateTraits<Object>::NodeType & add (std::string const & name,
159 GenericNode::ptr remove(std::string const & name);
161 DirectoryNode & operator[](std::string const & name) const;
162 CommandNode & operator()(std::string const & name) const;
163 GenericNode & get(std::string const & name) const;
165 DirectoryNode & mkdir(std::string const & name);
167 ChildrenRange children() const;
170 ///////////////////////////////////////////////////////////////////////////
172 template <class ForwardRange>
173 GenericNode & traverse(ForwardRange const & range);
175 DirectoryNode & doc(std::string const & doc);
178 cptr thisptr() const;
184 void add(GenericNode::ptr node);
185 virtual void v_help(std::ostream & output) const;
190 friend DirectoryNode & root();
193 BOOST_TYPEOF_REGISTER_TYPE(DirectoryNode);
195 struct UnknownNodeNameException : public senf::Exception
196 { UnknownNodeNameException() : senf::Exception("Unknown node name") {}};
198 // We need this specialization since we cannot passe auto_ptr via const & !!
199 template <class Type>
200 struct NodeCreateTraits< std::auto_ptr<Type> >
203 template <class Type>
204 struct NodeCreateTraits< boost::shared_ptr<Type> >
209 class CommandNode : public GenericNode
212 ///////////////////////////////////////////////////////////////////////////
215 typedef boost::shared_ptr<CommandNode> ptr;
216 typedef boost::shared_ptr<CommandNode const> cptr;
217 typedef boost::weak_ptr<CommandNode> weak_ptr;
219 typedef ParseCommandInfo::ArgumentsRange Arguments;
221 ///////////////////////////////////////////////////////////////////////////
223 virtual void operator()(std::ostream & output, Arguments const & arguments) = 0;
226 cptr thisptr() const;
236 class SimpleCommandNode : public CommandNode
239 ///////////////////////////////////////////////////////////////////////////
242 typedef boost::function<void (std::ostream &, Arguments const &)> Function;
244 ///////////////////////////////////////////////////////////////////////////
246 virtual void operator()(std::ostream & output, Arguments const & arguments);
249 cptr thisptr() const;
251 static std::auto_ptr<SimpleCommandNode> create(Function const & fn);
253 SimpleCommandNode & doc(std::string const & doc);
256 SimpleCommandNode(Function const & fn);
259 virtual void v_help(std::ostream & output) const;
265 template <class Function>
266 SimpleCommandNode & senf_console_add_node(DirectoryNode & node, std::string const & name,
267 Function const & fn, ...);
269 BOOST_TYPEOF_REGISTER_TYPE(SimpleCommandNode);
271 DirectoryNode & root();
275 ///////////////////////////////hh.e////////////////////////////////////////
285 // comment-column: 40
286 // c-file-style: "senf"
287 // indent-tabs-mode: nil
288 // ispell-local-dictionary: "american"
289 // compile-command: "scons -u test"