Console: Implement ObjectDirectory proxy
[senf.git] / Console / Node.cc
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 non-inline non-template implementation */
25
26 #include "Node.hh"
27 //#include "Node.ih"
28
29 // Custom includes
30
31 //#include "Node.mpp"
32 #define prefix_
33 ///////////////////////////////cc.p////////////////////////////////////////
34
35 prefix_ senf::console::DirectoryNode & senf::console::root()
36 {
37     static DirectoryNode::ptr rootNode(new DirectoryNode());
38     return *rootNode;
39 }
40
41 ///////////////////////////////////////////////////////////////////////////
42 // senf::console::GenericNode
43
44 prefix_ std::string senf::console::GenericNode::path()
45     const
46 {
47     std::string path (name());
48     ptr node (parent());
49     while (node) {
50         path = node->name() + "/" + path;
51         node = node->parent();
52     }
53     return path.empty() ? "/" : path;
54 }
55
56 prefix_ bool senf::console::GenericNode::active()
57     const
58 {
59     cptr node (thisptr());
60     while (node->parent())
61         node = node->parent();
62     return node == root().thisptr();
63 }
64
65 ///////////////////////////////////////////////////////////////////////////
66 //senf::console::DirectoryNode
67
68 prefix_ senf::console::GenericNode::ptr
69 senf::console::DirectoryNode::remove(std::string const & name)
70 {
71     ChildMap::iterator i (children_.find(name));
72     if (i == children_.end()) 
73         throw UnknownNodeNameException() << ": '" << name << "'";
74     GenericNode::ptr node (i->second);
75     children_.erase(i);
76     return node;
77 }
78
79 prefix_ void senf::console::DirectoryNode::add(GenericNode::ptr node)
80 {
81     BOOST_ASSERT( ! node->parent() );
82     if (children_.find(node->name()) != children_.end()) {
83         unsigned suffix (0);
84         std::string newName;
85         do {
86             ++suffix;
87             newName = node->name() + boost::lexical_cast<std::string>(suffix);
88         } while (children_.find(newName) != children_.end());
89         name(*node, newName);
90     }
91     children_.insert(std::make_pair(node->name(),node));
92     node->parent_ = this;
93 }
94
95 prefix_ senf::console::GenericNode &
96 senf::console::DirectoryNode::get(std::string const & name)
97     const
98 {
99     ChildMap::const_iterator i (children_.find(name));
100     if (i == children_.end())
101         throw UnknownNodeNameException() << ": '" << name << "'";
102     return *(i->second);
103 }
104
105 prefix_ void senf::console::DirectoryNode::v_help(std::ostream & output)
106     const
107 {
108     output << doc_ << "\n";
109 }
110
111 ///////////////////////////////////////////////////////////////////////////
112 // senf::console::SimpleCommandNode
113
114 prefix_ void senf::console::SimpleCommandNode::v_help(std::ostream & output)
115     const
116 {
117     output << doc_ << "\n";
118 }
119
120 ///////////////////////////////cc.e////////////////////////////////////////
121 #undef prefix_
122 //#include "Node.mpp"
123
124 \f
125 // Local Variables:
126 // mode: c++
127 // fill-column: 100
128 // comment-column: 40
129 // c-file-style: "senf"
130 // indent-tabs-mode: nil
131 // ispell-local-dictionary: "american"
132 // compile-command: "scons -u test"
133 // End: