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 ScopedDirectory public header */
26 #ifndef HH_ScopedDirectory_
27 #define HH_ScopedDirectory_ 1
30 #include <boost/utility.hpp>
31 #include <boost/type_traits/is_convertible.hpp>
34 //#include "ScopedDirectory.mpp"
35 ///////////////////////////////hh.p////////////////////////////////////////
40 /** \brief Internal: Node creation helper traits (ScopedDirectory proxy)
42 This class is used like NodeCreateTraits to customize the child node creation. This trait
43 class is used however by the ScopedDirectory proxy.
45 template <class Owner, class Object>
46 struct OwnerNodeCreateTraits
48 typedef BOOST_TYPEOF_TPL( senf_console_add_node(
49 * static_cast<DirectoryNode *>(0),
50 * static_cast<Owner *>(0),
51 * static_cast<std::string const *>(0),
52 * static_cast<Object const *>(0),
53 * static_cast<int *>(0)) ) result_type;
55 typedef typename boost::remove_reference<result_type>::type NodeType;
59 static NodeType & create(DirectoryNode & node, Owner & owner,
60 std::string const & name, Object const & ob);
64 /** \brief Internal: Marker base class for all ScopedDirectory proxies
66 class ScopedDirectoryBase
69 DirectoryNode & node() const; ///< Access the proxied DirectoryNode
71 ///////////////////////////////////////////////////////////////////////////
72 ///\name Proxied members (see DirectoryNode)
75 GenericNode::ptr remove(std::string const & name);
76 bool hasChild(std::string const & name) const;
77 DirectoryNode & getDirectory(std::string const & name) const;
78 DirectoryNode & operator[](std::string const & name) const;
79 CommandNode & getCommand(std::string const & name) const;
80 CommandNode & operator()(std::string const & name) const;
81 GenericNode & get(std::string const & name) const;
82 DirectoryNode & mkdir(std::string const & name);
83 DirectoryNode::ChildrenRange children() const;
84 DirectoryNode & doc(std::string const & doc);
89 ScopedDirectoryBase();
90 ~ScopedDirectoryBase();
93 DirectoryNode::ptr node_;
96 /** \brief DirectoryNode member proxy
98 ScopedDirectory is used whenever a class wants to manage it's own directory. The class
99 allows to declare the directory as a public member object. This allows the user of the class
100 to register the directory in the command tree. By using the proxy, the node is automatically
101 detached from the tree (and thereby destroyed) when the object (and thereby the proxy) is
108 ScopedDirectory<MyClass> configDir;
110 MyClass() : configDir(this)
117 The ScopedDirectory proxy implements 'add()' to add new children to the proxied
118 DirectoryNode. All add() variants supported by DirectoryNode are supported by
121 \idea This proxy could be made obsolete by allowing to allocate node objects
122 statically. This could be achieved by moving back to an intrusive_ptr implementation for
123 normal pointing needs with an added twist: Give each node a smart_ptr member pointing to
124 itself with a null deleter. This allows to create weak_ptr's to the nodes which will
125 automatically expire when the node is deleted (either statically or by the
130 template <class Owner=void>
131 class ScopedDirectory : public ScopedDirectoryBase
134 ///////////////////////////////////////////////////////////////////////////
139 ///////////////////////////////////////////////////////////////////////////
140 ///\name Structors and default members
143 ScopedDirectory(Owner * owner);
146 ///////////////////////////////////////////////////////////////////////////
148 template <class Object>
149 typename OwnerNodeCreateTraits<Owner, Object>::NodeType & add(std::string const & name,
151 ///< Create new child node
152 /**< Adds a new child node to the (proxied)
153 DirectoryNode. How the node is added is configured
154 using the OwnerNodeCreateTraits template. The default
155 implementation just forwards the call to the proxied
165 class ScopedDirectory<void> : public ScopedDirectoryBase
168 template <class Object>
169 typename NodeCreateTraits<Object>::NodeType & add(std::string const & name,
174 template <class Owner, class Function>
175 SimpleCommandNode & senf_console_add_node(
176 DirectoryNode & node, Owner & owner, std::string const & name, Function const & fn, ...);
178 template <class Owner>
179 SimpleCommandNode & senf_console_add_node(
180 DirectoryNode & node, Owner & owner, std::string const & name,
181 void (Owner::*fn)(std::ostream &, ParseCommandInfo const &), int);
183 template <class Node>
184 DirectoryNode & senf_console_add_node(
185 DirectoryNode & dir, std::string const & name, Node const & node, int,
186 typename boost::enable_if< boost::is_convertible<Node*, ScopedDirectoryBase*> >::type * = 0);
191 ///////////////////////////////hh.e////////////////////////////////////////
192 #include "ScopedDirectory.cci"
193 //#include "ScopedDirectory.ct"
194 #include "ScopedDirectory.cti"
201 // comment-column: 40
202 // c-file-style: "senf"
203 // indent-tabs-mode: nil
204 // ispell-local-dictionary: "american"
205 // compile-command: "scons -u test"