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