Search:

SENF Extensible Network Framework

  • Home
  • Download
  • Wiki
  • BerliOS
  • ChangeLog
  • Browse SVN
  • Bug Tracker
  • Overview
  • Examples
  • HowTos
  • Glossary
  • PPI
  • Packets
  • Scheduler
  • Socket
  • Utils
  • Console
  • Daemon
  • Logger
  • Termlib
  • Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

Node.cci

Go to the documentation of this file.
00001 // $Id: Node.cci 1742 2010-11-04 14:51:56Z g0dil $
00002 //
00003 // Copyright (C) 2008
00004 // Fraunhofer (FOKUS)
00005 // Competence Center NETwork research (NET), St. Augustin, GERMANY
00006 //     Stefan Bund <g0dil@berlios.de>
00007 //
00008 // This program is free software; you can redistribute it and/or modify
00009 // it under the terms of the GNU General Public License as published by
00010 // the Free Software Foundation; either version 2 of the License, or
00011 // (at your option) any later version.
00012 //
00013 // This program is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU General Public License
00019 // along with this program; if not, write to the
00020 // Free Software Foundation, Inc.,
00021 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00022 
00026 #include "Node.ih"
00027 
00028 // Custom includes
00029 
00030 #define prefix_ inline
00031 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00032 
00033 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00034 // senf::console::GenericNode
00035 
00036 prefix_ senf::console::GenericNode::ptr senf::console::GenericNode::thisptr()
00037 {
00038     return shared_from_this();
00039 }
00040 
00041 prefix_ senf::console::GenericNode::cptr senf::console::GenericNode::thisptr()
00042     const
00043 {
00044     return shared_from_this();
00045 }
00046 
00047 prefix_ senf::console::GenericNode::~GenericNode()
00048 {}
00049 
00050 prefix_ std::string const & senf::console::GenericNode::name()
00051     const
00052 {
00053     return name_;
00054 }
00055 
00056 prefix_ senf::console::GenericNode::GenericNode()
00057     : parent_ (0)
00058 {}
00059 
00060 prefix_ void senf::console::GenericNode::name(std::string const & name)
00061 {
00062     name_ = name;
00063 }
00064 
00065 prefix_ boost::shared_ptr<senf::console::DirectoryNode> senf::console::GenericNode::parent()
00066     const
00067 {
00068     return boost::static_pointer_cast<DirectoryNode>(
00069         parent_ ? parent_->shared_from_this() : ptr() );
00070 }
00071 
00072 prefix_ senf::console::GenericNode::ptr senf::console::GenericNode::unlink()
00073 {
00074     if (parent_)
00075         return parent()->remove(name());
00076     else
00077         return thisptr();
00078 }
00079 
00080 prefix_ void senf::console::GenericNode::help(std::ostream & output)
00081     const
00082 {
00083     v_help(output);
00084 }
00085 
00086 prefix_ std::string senf::console::GenericNode::shorthelp()
00087     const
00088 {
00089     return v_shorthelp();
00090 }
00091 
00092 prefix_ bool senf::console::GenericNode::operator==(GenericNode & other)
00093     const
00094 {
00095     return this == & other;
00096 }
00097 
00098 prefix_ bool senf::console::GenericNode::operator!=(GenericNode & other)
00099     const
00100 {
00101     return this != & other;
00102 }
00103 
00104 prefix_ bool senf::console::GenericNode::isDirectory()
00105     const
00106 {
00107     return dynamic_cast<DirectoryNode const *>(this);
00108 }
00109 
00110 prefix_ bool senf::console::GenericNode::isLink()
00111     const
00112 {
00113     return dynamic_cast<LinkNode const *>(this);
00114 }
00115 
00116 prefix_ bool senf::console::GenericNode::isCommand()
00117     const
00118 {
00119     return dynamic_cast<CommandNode const *>(this);
00120 }
00121 
00122 prefix_ senf::console::GenericNode const & senf::console::GenericNode::followLink()
00123     const
00124 {
00125     return isLink()
00126         ? dynamic_cast<LinkNode const *>(this)->follow()
00127         : *this;
00128 }
00129 
00130 prefix_ senf::console::GenericNode & senf::console::GenericNode::followLink()
00131 {
00132     return isLink()
00133         ? dynamic_cast<LinkNode *>(this)->follow()
00134         : *this;
00135 }
00136 
00137 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00138 // senf::console::LinkNode
00139 
00140 prefix_ senf::console::GenericNode & senf::console::LinkNode::follow()
00141     const
00142 {
00143     return *node_;
00144 }
00145 
00146 prefix_ senf::console::LinkNode::ptr senf::console::LinkNode::create(GenericNode & node)
00147 {
00148     GenericNode::ptr p (node.thisptr());
00149     while ( p->isLink() )
00150         p = dynamic_cast<LinkNode&>(*p).follow().thisptr();
00151     return ptr(new LinkNode(*p));
00152 }
00153 
00154 prefix_ senf::console::LinkNode::LinkNode(GenericNode & node)
00155     : node_ (node.thisptr())
00156 {}
00157 
00158 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00159 // senf::console::DirectoryNode
00160 
00161 prefix_ senf::console::DirectoryNode::ptr senf::console::DirectoryNode::create()
00162 {
00163     return ptr(new DirectoryNode());
00164 }
00165 
00166 prefix_ bool senf::console::DirectoryNode::hasChild(std::string const & name)
00167     const
00168 {
00169     ChildMap::const_iterator i (children_.find(name));
00170     return i != children_.end();
00171 }
00172 
00173 prefix_ senf::console::GenericNode &
00174 senf::console::DirectoryNode::get(std::string const & name)
00175     const
00176 {
00177     return getLink(name).followLink();
00178 }
00179 
00180 prefix_ senf::console::DirectoryNode &
00181 senf::console::DirectoryNode::getDirectory(std::string const & name)
00182     const
00183 {
00184     try {
00185         return dynamic_cast<DirectoryNode&>(get(name));
00186     }
00187     SENF_WRAP_EXC(std::bad_cast)
00188 }
00189 
00190 prefix_ senf::console::DirectoryNode &
00191 senf::console::DirectoryNode::operator[](std::string const & name)
00192     const
00193 {
00194     return getDirectory(name);
00195 }
00196 
00197 prefix_ senf::console::CommandNode &
00198 senf::console::DirectoryNode::getCommand(std::string const & name)
00199     const
00200 {
00201     try {
00202         return dynamic_cast<CommandNode&>(get(name));
00203     }
00204     SENF_WRAP_EXC(std::bad_cast)
00205 }
00206 
00207 prefix_ senf::console::CommandNode &
00208 senf::console::DirectoryNode::operator()(std::string const & name)
00209     const
00210 {
00211     return getCommand(name);
00212 }
00213 
00214 prefix_ senf::console::DirectoryNode::ChildrenRange senf::console::DirectoryNode::children()
00215     const
00216 {
00217     return boost::make_iterator_range(children_.begin(), children_.end());
00218 }
00219 
00220 prefix_ senf::console::DirectoryNode::ChildrenRange
00221 senf::console::DirectoryNode::completions(std::string const & s)
00222     const
00223 {
00224     return boost::make_iterator_range(children_.lower_bound(s),
00225                                       children_.lower_bound(s + "\xff"));
00226 }
00227 
00228 prefix_ senf::console::DirectoryNode::DirectoryNode()
00229 {}
00230 
00231 prefix_ senf::console::DirectoryNode &
00232 senf::console::DirectoryNode::doc(std::string const & doc)
00233 {
00234     doc_ = doc;
00235     return *this;
00236 }
00237 
00238 prefix_ senf::console::DirectoryNode &
00239 senf::console::DirectoryNode::shortdoc(std::string const & doc)
00240 {
00241     shortdoc_ = doc;
00242     return *this;
00243 }
00244 
00245 prefix_ senf::console::DirectoryNode::ptr senf::console::DirectoryNode::thisptr()
00246 {
00247     return boost::static_pointer_cast<DirectoryNode>(shared_from_this());
00248 }
00249 
00250 prefix_ senf::console::DirectoryNode::cptr senf::console::DirectoryNode::thisptr()
00251     const
00252 {
00253     return boost::static_pointer_cast<DirectoryNode const>(shared_from_this());
00254 }
00255 
00256 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00257 // senf::console::detail::NodeTraverser
00258 #ifndef DOXYGEN
00259 
00260 prefix_ senf::console::detail::NodeTraverser::NodeTraverser(DirectoryNode & root,
00261                                                             DirectoryNode & dir,
00262                                                             bool autocomplete)
00263     : root_ (root), dir_ (dir.thisptr()), autocomplete_ (autocomplete), init_ (false)
00264 {}
00265 
00266 #endif
00267 
00268 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00269 // senf::console::CommandNode
00270 
00271 prefix_ senf::console::CommandNode::ptr senf::console::CommandNode::thisptr()
00272 {
00273     return boost::static_pointer_cast<CommandNode>(shared_from_this());
00274 }
00275 
00276 prefix_ senf::console::CommandNode::cptr senf::console::CommandNode::thisptr()
00277     const
00278 {
00279     return boost::static_pointer_cast<CommandNode const>(shared_from_this());
00280 }
00281 
00282 prefix_ senf::console::CommandNode::CommandNode()
00283 {}
00284 
00285 prefix_ void senf::console::CommandNode::execute(std::ostream & output,
00286                                                  ParseCommandInfo const & command)
00287     const
00288 {
00289     boost::any rv;
00290     execute(rv, output, command);
00291 }
00292 
00293 prefix_ void senf::console::CommandNode::execute(boost::any & rv, std::ostream & output,
00294                                                  ParseCommandInfo const & command)
00295     const
00296 {
00297     rv = boost::any();
00298     v_execute(rv, output, command);
00299 }
00300 
00301 prefix_ void senf::console::CommandNode::operator()(std::ostream & output,
00302                                                     ParseCommandInfo const & command)
00303     const
00304 {
00305     execute(output, command);
00306 }
00307 
00308 prefix_ void senf::console::CommandNode::operator()(boost::any & rv, std::ostream & output,
00309                                                     ParseCommandInfo const & command)
00310     const
00311 {
00312     execute(rv, output, command);
00313 }
00314 
00315 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00316 // senf::console::SimpleCommandNode
00317 
00318 prefix_ senf::console::SimpleCommandNode::SimpleCommandNode(Function const & fn)
00319     : fn_ (fn)
00320 {}
00321 
00322 prefix_ senf::console::SimpleCommandNode::ptr
00323 senf::console::SimpleCommandNode::create(Function const & fn)
00324 {
00325     return ptr(new SimpleCommandNode(fn));
00326 }
00327 
00328 prefix_ senf::console::SimpleCommandNode &
00329 senf::console::SimpleCommandNode::doc(std::string const & doc)
00330 {
00331     doc_ = doc;
00332     return *this;
00333 }
00334 
00335 prefix_ senf::console::SimpleCommandNode &
00336 senf::console::SimpleCommandNode::shortdoc(std::string const & doc)
00337 {
00338     shortdoc_ = doc;
00339     return *this;
00340 }
00341 
00342 prefix_ senf::console::SimpleCommandNode::ptr senf::console::SimpleCommandNode::thisptr()
00343 {
00344     return boost::static_pointer_cast<SimpleCommandNode>(shared_from_this());
00345 }
00346 
00347 prefix_ senf::console::SimpleCommandNode::cptr senf::console::SimpleCommandNode::thisptr()
00348     const
00349 {
00350     return boost::static_pointer_cast<SimpleCommandNode const>(shared_from_this());
00351 }
00352 
00353 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00354 
00355 prefix_ senf::console::DirectoryNode & senf::console::provideDirectory(DirectoryNode & dir,
00356                                                                        std::string const & name)
00357 {
00358     return dir.hasChild(name) ? dir.getDirectory(name) : dir.add(name, factory::Directory());
00359 }
00360 
00361 
00362 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00363 // senf::console::factory::SimpleCommand
00364 
00365 prefix_ senf::console::factory::SimpleCommand::SimpleCommand(SimpleCommandNode::Function fn)
00366     : node_ (SimpleCommandNode::create(fn))
00367 {}
00368 
00369 prefix_ senf::console::SimpleCommandNode &
00370 senf::console::factory::SimpleCommand::create(DirectoryNode & dir, std::string const & name)
00371     const
00372 {
00373     return dir.add(name, node_);
00374 }
00375 
00376 prefix_ senf::console::factory::SimpleCommand const &
00377 senf::console::factory::SimpleCommand::doc(std::string const & doc)
00378     const
00379 {
00380     node_->doc(doc);
00381     return *this;
00382 }
00383 
00384 prefix_ senf::console::factory::SimpleCommand const &
00385 senf::console::factory::SimpleCommand::shortdoc(std::string const & doc)
00386     const
00387 {
00388     node_->shortdoc(doc);
00389     return *this;
00390 }
00391 
00392 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00393 // senf::console::factory::Directory
00394 
00395 prefix_ senf::console::factory::Directory::Directory()
00396     : node_ (DirectoryNode::create())
00397 {}
00398 
00399 prefix_ senf::console::DirectoryNode &
00400 senf::console::factory::Directory::create(DirectoryNode & dir, std::string const & name)
00401     const
00402 {
00403     return dir.add(name, node_);
00404 }
00405 
00406 prefix_ senf::console::factory::Directory const &
00407 senf::console::factory::Directory::doc(std::string const & doc)
00408     const
00409 {
00410     node_->doc(doc);
00411     return *this;
00412 }
00413 
00414 prefix_ senf::console::factory::Directory const &
00415 senf::console::factory::Directory::shortdoc(std::string const & doc)
00416     const
00417 {
00418     node_->shortdoc(doc);
00419     return *this;
00420 }
00421 
00422 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00423 // senf::console::factory::Link
00424 
00425 prefix_ senf::console::factory::Link::Link(GenericNode & target)
00426     : node_ (LinkNode::create(target))
00427 {}
00428 
00429 prefix_ senf::console::LinkNode & senf::console::factory::Link::create(DirectoryNode & dir,
00430                                                                        std::string const & name)
00431     const
00432 {
00433     return dir.add(name, node_);
00434 }
00435 
00436 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00437 #undef prefix_
00438 
00439 
00440 // Local Variables:
00441 // mode: c++
00442 // fill-column: 100
00443 // comment-column: 40
00444 // c-file-style: "senf"
00445 // indent-tabs-mode: nil
00446 // ispell-local-dictionary: "american"
00447 // compile-command: "scons -u test"
00448 // End:

Contact: senf-dev@lists.berlios.de | © 2006-2010 Fraunhofer Institute for Open Communication Systems, Network Research