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