1712f9f325f3251746cb41373f06c200b43334b4
[senf.git] / Utils / Console / ScopedDirectory.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 ScopedDirectory public header */
25
26 #ifndef HH_SENF_Scheduler_Console_ScopedDirectory_
27 #define HH_SENF_Scheduler_Console_ScopedDirectory_ 1
28
29 // Custom includes
30 #include <boost/utility.hpp>
31 #include <boost/type_traits/is_convertible.hpp>
32 #include "Node.hh"
33 #include "LazyDirectory.hh" // For ScopedDirectory template default arg
34
35 //#include "ScopedDirectory.mpp"
36 ///////////////////////////////hh.p////////////////////////////////////////
37
38 namespace senf {
39 namespace console {
40
41     /** \brief Internal: Node creation helper traits (ScopedDirectory proxy)
42         
43         This class is used like NodeCreateTraits to customize the child node creation. This trait
44         class is used however by the ScopedDirectory proxy.
45      */
46     template <class Owner, class Object>
47     struct OwnerNodeCreateTraits
48     {
49         typedef BOOST_TYPEOF_TPL( senf_console_add_node( 
50                                       * static_cast<DirectoryNode *>(0),
51                                       * static_cast<Owner *>(0),
52                                       * static_cast<std::string const *>(0),
53                                       * static_cast<Object *>(0),
54                                       * static_cast<int *>(0)) ) base_type;
55         typedef typename senf::remove_cvref<base_type>::type value_type;
56
57         typedef typename value_type::node_type NodeType;
58         typedef typename value_type::return_type result_type;
59
60         /// Internal
61         struct Creator {
62             static result_type create(DirectoryNode & node, Owner & owner, 
63                                      std::string const & name, Object & ob);
64         };
65     };
66     
67     /** \brief Internal: Marker base class for all ScopedDirectory proxies
68      */
69     class ScopedDirectoryBase
70     {
71     public:
72         DirectoryNode & node() const;   ///< Access the proxied DirectoryNode
73         operator DirectoryNode & () const; ///< Access the proxied DirectoryNode
74
75         ///////////////////////////////////////////////////////////////////////////
76         ///\name Proxied members (see DirectoryNode)
77         ///\{
78
79         GenericNode::ptr remove(std::string const & name);
80         bool hasChild(std::string const & name) const;
81         DirectoryNode & getDirectory(std::string const & name) const;
82         DirectoryNode & operator[](std::string const & name) const;
83         CommandNode & getCommand(std::string const & name) const;
84         CommandNode & operator()(std::string const & name) const;
85         GenericNode & get(std::string const & name) const;
86         DirectoryNode & mkdir(std::string const & name);
87         void link(std::string const & name, GenericNode & node);
88         DirectoryNode::ChildrenRange children() const;
89         DirectoryNode & doc(std::string const & doc);
90
91         ///\}
92
93     protected:
94         ScopedDirectoryBase();
95         ~ScopedDirectoryBase();
96
97     private:
98         DirectoryNode::ptr node_;
99     };
100
101     /** \brief DirectoryNode member proxy
102
103         ScopedDirectory is used whenever a class wants to manage it's own directory. The class
104         allows to declare the directory as a public member object. This allows the user of the class
105         to register the directory in the command tree. By using the proxy, the node is automatically
106         detached from the tree (and thereby destroyed) when the object (and thereby the proxy) is
107         destroyed.
108
109         \code
110         class MyClass
111         {
112         public:
113             ScopedDirectory<MyClass> configDir;
114
115             MyClass() : configDir(this) 
116             {
117                 configDIr.add(...);
118             }
119         };
120         \endcode
121
122         The ScopedDirectory proxy implements 'add()' to add new children to the proxied
123         DirectoryNode. All add() variants supported by DirectoryNode are supported by
124         ScopedDirectory.
125
126         \idea This proxy could be made obsolete by allowing to allocate node objects
127             statically. This could be achieved by moving back to an intrusive_ptr implementation for
128             normal pointing needs with an added twist: Give each node a smart_ptr member pointing to
129             itself with a null deleter. This allows to create weak_ptr's to the nodes which will
130             automatically expire when the node is deleted (either statically or by the
131             intrusive_ptr).
132
133         \ingroup node_tree
134       */
135     template <class Owner>
136     class ScopedDirectory : public ScopedDirectoryBase
137     {
138     public:
139         ///////////////////////////////////////////////////////////////////////////
140         // Types
141         
142         typedef Owner owner;
143
144         ///////////////////////////////////////////////////////////////////////////
145         ///\name Structors and default members
146         ///@{
147
148         explicit ScopedDirectory(Owner * owner);
149
150         ///@}
151         ///////////////////////////////////////////////////////////////////////////
152
153         template <class Object>
154         typename OwnerNodeCreateTraits<Owner, Object>::result_type add(std::string const & name,
155                                                                        Object const & ob);
156                                         ///< Create new child node
157                                         /**< Adds a new child node to the (proxied)
158                                              DirectoryNode. How the node is added is configured
159                                              using the OwnerNodeCreateTraits template. The default
160                                              implementation just forwards the call to the proxied
161                                              directory node. */
162
163         template <class Object>
164         typename OwnerNodeCreateTraits<Owner, Object>::result_type add(std::string const & name,
165                                                                        Object & ob);
166                                         ///< Create new child node
167                                         /**< \see add() */
168
169     protected:
170
171     private:
172         Owner * owner_;
173     };
174
175 #ifndef DOXYGEN
176
177     template <>
178     class ScopedDirectory<void> : public ScopedDirectoryBase
179     {
180     public:
181         template <class Object>
182         typename NodeCreateTraits<Object>::result_type add(std::string const & name,
183                                                            Object const & ob);
184
185         template <class Object>
186         typename NodeCreateTraits<Object>::result_type add(std::string const & name,
187                                                            Object & ob);
188     };
189
190     template <class Owner, class Function>
191     SimpleCommandNode & senf_console_add_node(
192         DirectoryNode & node, Owner & owner, std::string const & name,
193         SimpleCommandNode::Function fn, int);
194
195     template <class Owner>
196     SimpleCommandNode & senf_console_add_node(
197         DirectoryNode & node, Owner & owner, std::string const & name,
198         void (Owner::*fn)(std::ostream &, ParseCommandInfo const &), int);
199
200     template <class Owner>
201     DirectoryNode & senf_console_add_node(DirectoryNode & node, Owner & owner, 
202                                           std::string const & name, DirectoryNode & dir, int);
203
204     template <class Node>
205     DirectoryNode & senf_console_add_node(
206         DirectoryNode & dir, std::string const & name, Node const & node, int, 
207         typename boost::enable_if< boost::is_convertible<Node*, ScopedDirectoryBase*> >::type * = 0);
208 #endif
209
210 }}
211
212 ///////////////////////////////hh.e////////////////////////////////////////
213 #include "ScopedDirectory.cci"
214 //#include "ScopedDirectory.ct"
215 #include "ScopedDirectory.cti"
216 #endif
217
218 \f
219 // Local Variables:
220 // mode: c++
221 // fill-column: 100
222 // comment-column: 40
223 // c-file-style: "senf"
224 // indent-tabs-mode: nil
225 // ispell-local-dictionary: "american"
226 // compile-command: "scons -u test"
227 // End: