Console: Added Module
[senf.git] / Console / Node.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 Node public header */
25
26 #ifndef HH_Node_
27 #define HH_Node_ 1
28
29 // Custom includes
30 #include <map>
31 #include <boost/intrusive_ptr.hpp>
32 #include <boost/utility.hpp>
33 #include "../Utils/intrusive_refcount.hh"
34 #include "../Utils/Exception.hh"
35
36 //#include "Node.mpp"
37 ///////////////////////////////hh.p////////////////////////////////////////
38
39 namespace senf {
40 namespace console {
41
42     class DirectoryNode;
43     class CommandNode;
44
45     /** \brief
46       */
47     class GenericNode 
48         : public intrusive_refcount_t<GenericNode>
49     {
50     public:
51         ///////////////////////////////////////////////////////////////////////////
52         // Types
53
54         typedef boost::intrusive_ptr<GenericNode> ptr;
55
56         ///////////////////////////////////////////////////////////////////////////
57
58         std::string const & name() const;
59         DirectoryNode & parent() const;
60         bool managed() const;
61
62     protected:
63         explicit GenericNode(std::string const & name, bool managed = false);
64
65         void name(std::string const & name);
66         static void name(GenericNode & node, std::string const & name);
67         void parent(DirectoryNode * parent);
68
69     private:
70         bool release();
71
72         std::string name_;
73         bool managed_;
74         DirectoryNode * parent_;
75
76         friend class intrusive_refcount_base;
77     };
78
79     /** \brief
80       */
81     class DirectoryNode : public GenericNode
82     {
83     public:
84         typedef boost::intrusive_ptr<DirectoryNode> ptr;
85
86         void add(std::auto_ptr<GenericNode> node, bool uniquify = true);
87         void add(GenericNode & node, bool uniquify = true);
88
89         DirectoryNode & operator[](std::string const & name) const;
90         CommandNode & operator()(std::string const & name) const;
91
92     protected:
93         explicit DirectoryNode(std::string const & name, bool managed = false);
94
95     private:
96         void add(GenericNode::ptr node, bool uniquify);
97         GenericNode & lookup(std::string const & name) const;
98
99         typedef std::map<std::string, GenericNode::ptr> ChildMap;
100         ChildMap children_;
101     };
102
103     struct DuplicateNodeNameException : public senf::Exception
104     { DuplicateNodeNameException() : senf::Exception("Duplicate node name") {}};
105
106     struct UnknownNodeNameException : public senf::Exception
107     { UnknownNodeNameException() : senf::Exception("Unknown node name") {}};
108
109     /** \brief
110       */
111     class CommandNode : public GenericNode
112     {
113     public:
114         typedef boost::intrusive_ptr<CommandNode> ptr;
115
116     protected:
117         explicit CommandNode(std::string const & name, bool managed = false);
118
119     private:
120
121     };
122
123 }}
124
125 ///////////////////////////////hh.e////////////////////////////////////////
126 #include "Node.cci"
127 //#include "Node.ct"
128 //#include "Node.cti"
129 #endif
130
131 \f
132 // Local Variables:
133 // mode: c++
134 // fill-column: 100
135 // comment-column: 40
136 // c-file-style: "senf"
137 // indent-tabs-mode: nil
138 // ispell-local-dictionary: "american"
139 // compile-command: "scons -u test"
140 // End: