Console: Add lots of documentation
[senf.git] / Console / ObjectDirectory.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 ObjectDirectory public header */
25
26 #ifndef HH_ObjectDirectory_
27 #define HH_ObjectDirectory_ 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 "ObjectDirectory.mpp"
35 ///////////////////////////////hh.p////////////////////////////////////////
36
37 namespace senf {
38 namespace console {
39
40     /** \brief Internal: Node creation helper traits (ObjectDirectory proxy)
41         
42         This class is used like NodeCreateTraits to customize the child node creation. This trait
43         class is used however by the ObjectDirectory 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         struct Creator {
57             static NodeType & create(DirectoryNode & node, Owner & owner, 
58                                      std::string const & name, Object const & ob);
59         };
60     };
61     
62     /** \brief Internal: Marker base class for all ObjectDirectory proxies
63      */
64     struct ObjectDirectoryBase {};
65
66     /** \brief DirectoryNode member proxy
67
68         ObjectDirectory is used whenever a class wants to manage it's own directory. The class
69         allows to declare the directory as a public member object. This allows the user of the class
70         to register the directory in the command tree. By using the proxy, the node is automatically
71         detached from the tree (and thereby destroyed) when the object (and thereby the proxy) is
72         destroyed.
73
74         \code
75         class MyClass
76         {
77         public:
78             ObjectDirectory<MyClass> configDir;
79
80             MyClass() : configDir(this) 
81             {
82                 configDIr.add(...);
83             }
84         };
85         \endcode
86
87         The ObjectDirectory proxy implements 'add()' to add new children to the proxied
88         DirectoryNode. All add() variants supported by DirectoryNode are supported by
89         ObjectDirectory.
90
91         \idea This proxy could be made obsolete by allowing to allocate node objects
92             statically. This could be achieved by moving back to an intrusive_ptr implementation for
93             normal pointing needs with an added twist: Give each node a smart_ptr member pointing to
94             itself with a null deleter. This allows to create weak_ptr's to the nodes which will
95             automatically expire when the node is deleted (either statically or by the
96             intrusive_ptr).
97       */
98     template <class Owner>
99     class ObjectDirectory : public ObjectDirectoryBase
100     {
101     public:
102         ///////////////////////////////////////////////////////////////////////////
103         // Types
104         
105         typedef Owner owner;
106
107         ///////////////////////////////////////////////////////////////////////////
108         ///\name Structors and default members
109         ///@{
110
111         ObjectDirectory(Owner * owner);
112         ~ObjectDirectory();
113
114         ///@}
115         ///////////////////////////////////////////////////////////////////////////
116
117         template <class Object>
118         typename OwnerNodeCreateTraits<Owner, Object>::NodeType & add(std::string const & name,
119                                                                       Object const & ob);
120                                         ///< Create new child node
121                                         /**< Adds a new child node to the (proxied)
122                                              DirectoryNode. How the node is added is configured
123                                              using the OwnerNodeCreateTraits template. The default
124                                              implementation just forwards the call to the proxied
125                                              directory node. */
126
127         DirectoryNode & node() const;   ///< Access the proxied DirectoryNode
128
129     protected:
130
131     private:
132         DirectoryNode::ptr node_;
133         Owner * owner_;
134     };
135
136 #ifndef DOXYGEN
137     template <class Owner, class Function>
138     SimpleCommandNode & senf_console_add_node(
139         DirectoryNode & node, Owner & owner, std::string const & name, Function const & fn);
140
141     template <class Owner>
142     SimpleCommandNode & senf_console_add_node(
143         DirectoryNode & node, Owner & owner, std::string const & name, 
144         void (Owner::*fn)(std::ostream & output, CommandNode::Arguments const & arguments));
145
146     template <class Node>
147     DirectoryNode & senf_console_add_node(
148         DirectoryNode & dir, std::string const & name, Node const & node, int, 
149         typename boost::enable_if< boost::is_convertible<Node*, ObjectDirectoryBase*> >::type * = 0);
150 #endif
151
152 }}
153
154 ///////////////////////////////hh.e////////////////////////////////////////
155 //#include "ObjectDirectory.cci"
156 //#include "ObjectDirectory.ct"
157 #include "ObjectDirectory.cti"
158 #endif
159
160 \f
161 // Local Variables:
162 // mode: c++
163 // fill-column: 100
164 // comment-column: 40
165 // c-file-style: "senf"
166 // indent-tabs-mode: nil
167 // ispell-local-dictionary: "american"
168 // compile-command: "scons -u test"
169 // End: