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

OverloadedCommand.hh

Go to the documentation of this file.
00001 // $Id: OverloadedCommand.hh 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 #ifndef HH_SENF_Scheduler_Console_OverloadedCommand_
00027 #define HH_SENF_Scheduler_Console_OverloadedCommand_ 1
00028 
00029 // Custom includes
00030 #include "Node.hh"
00031 #include <boost/intrusive_ptr.hpp>
00032 #include <boost/range/iterator_range.hpp>
00033 #include <senf/Utils/intrusive_refcount.hh>
00034 #include <boost/optional.hpp>
00035 
00036 //#include "OverloadedCommand.mpp"
00037 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00038 
00039 namespace senf {
00040 namespace console {
00041 
00042     class OverloadedCommandNode;
00043 
00048     struct ArgumentDoc {
00049         std::string name; 
00050         std::string type; 
00051         std::string defaultValue; 
00052         std::string doc; 
00053         bool singleToken; 
00054     };
00055 
00061     class CommandOverload
00062         : public senf::intrusive_refcount
00063     {
00064     public:
00065         //-////////////////////////////////////////////////////////////////////////
00066         // Types
00067 
00068         typedef boost::intrusive_ptr<CommandOverload> ptr;
00069         typedef boost::intrusive_ptr<CommandOverload const> cptr;
00070 
00071         //-////////////////////////////////////////////////////////////////////////
00072 
00073         virtual ~CommandOverload();
00074 
00075         void execute(boost::any & rv, std::ostream & os, ParseCommandInfo const & command);
00077 
00081         void operator()(boost::any & rv, std::ostream & os, ParseCommandInfo const & command);
00083 
00087         unsigned numArguments() const;  
00088         void argumentDoc(unsigned index, ArgumentDoc & doc) const;
00090 
00096         std::string doc() const;        
00097 
00098         OverloadedCommandNode & node() const; 
00099 
00102         unsigned overloadIndex() const; 
00103 
00104     protected:
00105         CommandOverload();
00106 
00107 #ifndef DOXYGEN
00108     private:
00109 #endif
00110         virtual unsigned v_numArguments() const = 0;
00112 
00115         virtual void v_argumentDoc(unsigned index, ArgumentDoc & doc) const = 0;
00117 
00121         virtual std::string v_doc() const = 0;
00123 
00126         virtual void v_execute(boost::any & rv, std::ostream & os, ParseCommandInfo const & command)
00127             const = 0;
00129 
00132     private:
00133         OverloadedCommandNode * node_;
00134 
00135         friend class OverloadedCommandNode;
00136     };
00137 
00162     class OverloadedCommandNode
00163         : public CommandNode
00164     {
00165         typedef std::vector<CommandOverload::ptr> Overloads;
00166 
00167     public:
00168         //-////////////////////////////////////////////////////////////////////////
00169         // Types
00170 
00171         typedef boost::shared_ptr<OverloadedCommandNode> ptr;
00172         typedef boost::shared_ptr<OverloadedCommandNode const> cptr;
00173         typedef boost::weak_ptr<OverloadedCommandNode> weak_ptr;
00174 
00175         typedef OverloadedCommandNode node_type;
00176         typedef OverloadedCommandNode & return_type;
00177 
00178         typedef boost::iterator_range<Overloads::const_iterator> OverloadsRange;
00179 
00180         //-////////////////////////////////////////////////////////////////////////
00182         //\{
00183 
00184         static ptr create();
00185 
00186         //\}
00187         //-////////////////////////////////////////////////////////////////////////
00188 
00189         template <class Command>
00190         Command & add(boost::intrusive_ptr<Command> overload); 
00191 
00192         OverloadedCommandNode & doc(std::string const & doc);
00194         OverloadedCommandNode & shortdoc(std::string const & doc);
00196 
00197         unsigned overloadIndex(CommandOverload const & overload);
00199 
00202         OverloadsRange overloads() const; 
00203 
00204         ptr thisptr();
00205         cptr thisptr() const;
00206 
00207         static OverloadedCommandNode & insertOverload(DirectoryNode & dir, std::string const & name,
00208                                                       CommandOverload::ptr overload);
00209 
00210     private:
00211         OverloadedCommandNode();
00212 
00213         virtual void v_help(std::ostream & output) const;
00214         virtual std::string v_shorthelp() const;
00215         virtual void v_execute(boost::any & rv, std::ostream & os, ParseCommandInfo const & command)
00216             const;
00217 
00218         Overloads overloads_;
00219         std::string doc_;
00220         std::string shortdoc_;
00221     };
00222 
00229     class SimpleCommandOverload
00230         : public CommandOverload
00231     {
00232     public:
00233         //-////////////////////////////////////////////////////////////////////////
00234         // Types
00235 
00236         typedef boost::intrusive_ptr<SimpleCommandOverload> ptr;
00237         typedef boost::function<void (std::ostream &, ParseCommandInfo const &)> Function;
00238 
00239         //-////////////////////////////////////////////////////////////////////////
00241         //\{
00242 
00243         static SimpleCommandOverload::ptr create(Function fn);
00245 
00247         //\}
00248         //-////////////////////////////////////////////////////////////////////////
00249 
00250         SimpleCommandOverload & doc(std::string const & doc);
00252 
00253     private:
00254         explicit SimpleCommandOverload(Function fn);
00255 
00256         virtual unsigned v_numArguments() const;
00257         virtual void v_argumentDoc(unsigned index, ArgumentDoc & doc) const;
00258         virtual std::string v_doc() const;
00259         virtual void v_execute(boost::any & rv, std::ostream & os, ParseCommandInfo const & command)
00260             const;
00261 
00262         Function fn_;
00263         std::string doc_;
00264     };
00265 
00266     class SimpleOverloadAttributor
00267         : public detail::NodeFactory
00268     {
00269     public:
00270         typedef OverloadedCommandNode node_type;
00271         typedef OverloadedCommandNode & result_type;
00272 
00273         explicit SimpleOverloadAttributor(SimpleCommandOverload::Function fn);
00274 
00275         SimpleOverloadAttributor const & doc(std::string const & doc) const;
00276         SimpleOverloadAttributor const & shortdoc(std::string const & doc) const;
00277         SimpleOverloadAttributor const & overloadDoc(std::string const & doc) const;
00278 
00279         OverloadedCommandNode & create(DirectoryNode & dir, std::string const & name) const;
00280 
00281     private:
00282         SimpleCommandOverload::ptr overload_;
00283         mutable boost::optional<std::string> doc_;
00284         mutable boost::optional<std::string> shortdoc_;
00285     };
00286 
00287 }}
00288 
00289 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00290 #include "OverloadedCommand.cci"
00291 //#include "OverloadedCommand.ct"
00292 #include "OverloadedCommand.cti"
00293 #endif
00294 
00295 
00296 // Local Variables:
00297 // mode: c++
00298 // fill-column: 100
00299 // comment-column: 40
00300 // c-file-style: "senf"
00301 // indent-tabs-mode: nil
00302 // ispell-local-dictionary: "american"
00303 // compile-command: "scons -u test"
00304 // End:

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