Fix documentation
[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     node->parent_ = 0;
77     node->name_.clear();
78     return node;
79 }
80
81 prefix_ void senf::console::DirectoryNode::add(GenericNode::ptr node)
82 {
83     BOOST_ASSERT( ! node->parent() );
84     if (node->name().empty()) {
85         node->name("unnamed");
86         SENF_LOG((senf::log::MESSAGE)("Adding 'unnamed' node"));
87     }
88     if (children_.find(node->name()) != children_.end()) {
89         unsigned suffix (0);
90         std::string newName;
91         do {
92             ++suffix;
93             newName = node->name() + "-" + boost::lexical_cast<std::string>(suffix);
94         } while (children_.find(newName) != children_.end());
95         SENF_LOG((senf::log::MESSAGE)("Uniquifying node '" << node->name() << "' to '" 
96                                       << newName << "'"));
97         node->name(newName);
98     }
99     children_.insert(std::make_pair(node->name(),node));
100     node->parent_ = this;
101 }
102
103 prefix_ senf::console::GenericNode &
104 senf::console::DirectoryNode::get(std::string const & name)
105     const
106 {
107     ChildMap::const_iterator i (children_.find(name));
108     if (i == children_.end())
109         throw UnknownNodeNameException() << ": '" << name << "'";
110     return *(i->second);
111 }
112
113 prefix_ void senf::console::DirectoryNode::v_help(std::ostream & output)
114     const
115 {
116     output << doc_ << "\n";
117 }
118
119 ///////////////////////////////////////////////////////////////////////////
120 // senf::console::SimpleCommandNode
121
122 prefix_ void senf::console::SimpleCommandNode::v_help(std::ostream & output)
123     const
124 {
125     output << doc_ << "\n";
126 }
127
128 ///////////////////////////////cc.e////////////////////////////////////////
129 #undef prefix_
130 //#include "Node.mpp"
131
132 \f
133 // Local Variables:
134 // mode: c++
135 // fill-column: 100
136 // comment-column: 40
137 // c-file-style: "senf"
138 // indent-tabs-mode: nil
139 // ispell-local-dictionary: "american"
140 // compile-command: "scons -u test"
141 // End: