switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Utils / Console / ParsedCommand.hh
index 753bd83..7b34dd9 100644 (file)
@@ -2,23 +2,28 @@
 //
 // Copyright (C) 2008
 // Fraunhofer Institute for Open Communication Systems (FOKUS)
-// Competence Center NETwork research (NET), St. Augustin, GERMANY
-//     Stefan Bund <g0dil@berlios.de>
 //
-// This program is free software; you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation; either version 2 of the License, or
-// (at your option) any later version.
+// The contents of this file are subject to the Fraunhofer FOKUS Public License
+// Version 1.0 (the "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at 
+// http://senf.berlios.de/license.html
 //
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-// GNU General Public License for more details.
+// The Fraunhofer FOKUS Public License Version 1.0 is based on, 
+// but modifies the Mozilla Public License Version 1.1.
+// See the full license text for the amendments.
 //
-// You should have received a copy of the GNU General Public License
-// along with this program; if not, write to the
-// Free Software Foundation, Inc.,
-// 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+// Software distributed under the License is distributed on an "AS IS" basis, 
+// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
+// for the specific language governing rights and limitations under the License.
+//
+// The Original Code is Fraunhofer FOKUS code.
+//
+// The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
+// (registered association), Hansastraße 27 c, 80686 Munich, Germany.
+// All Rights Reserved.
+//
+// Contributor(s):
+//   Stefan Bund <g0dil@berlios.de>
 
 /** \file
     \brief ParsedCommand public header */
@@ -44,7 +49,7 @@
 
 #include "ParsedCommand.ih"
 #include "ParsedCommand.mpp"
-///////////////////////////////hh.p////////////////////////////////////////
+//-/////////////////////////////////////////////////////////////////////////////////////////////////
 
 namespace senf {
 namespace console {
@@ -64,12 +69,13 @@ namespace console {
 
         \section overload_add Adding argument parsing callbacks to the tree
 
-        Adding appropriate callbacks to the tree is very simple: just path a function pointer to
-        DirectoryNode::add() or a member function pointer to ScopedDirectory::add().
+        To add overloads to the tree, use the senf::console::factory::Command factory:
         \code
+        namespace fty = senf::console::factory;
+
         std::string taskStatus(int id);
 
-        senf::console::root().add("taskStatus", &taskStatus);
+        senf::console::root().add("taskStatus", fty::Command(&taskStatus));
         \endcode
 
         There are quite a number of additional parameters available to be set. These parameters are
@@ -77,11 +83,11 @@ namespace console {
         calls after adding the node:
 
         \code
-        senf::console::root().add("taskStatus", &taskStatus)
+        senf::console::root().add("taskStatus", fty::Command(&taskStatus)
             .doc("Query the current task status")
             .arg( name = "id",
                   description = "numeric id of task to check, -1 for the current task."
-                  default_value = -1 );
+                  default_value = -1 ) );
         \endcode
 
         You may also add an additional \c std::ostream & Argument as first argument to the
@@ -95,17 +101,19 @@ namespace console {
         std::string taskStatus(std::string const & name);
 
         senf::console::root()
-            .add("taskStatus", static_cast<std::string (*)(int)>(&taskStatus))
+            .add("taskStatus", fty::Command(static_cast<std::string (*)(int)>(
+                                                &taskStatus))
             .doc("Query the current task status")
             .overloadDoc("Query status by id")
             .arg( name = "id",
                   description = "numeric id of task to check, -1 for the current task."
-                  default_value = -1 );
+                  default_value = -1 ) );
         senf::console::root()
-            .add("taskStatus", static_cast<std::string (*)(std::string const &)>(&taskStatus))
+            .add("taskStatus", fty::Command(static_cast<std::string (*)(std::string const &)>(
+                                                 &taskStatus))
             .overloadDoc("Query status by name")
             .arg( name = "name",
-                  description = "name of task to check" );
+                  description = "name of task to check" ) );
         \endcode
 
         We can see here, that taking the address of an overloaded function requires a cast. If you
@@ -174,10 +182,6 @@ namespace console {
     {
     public:
         typedef boost::intrusive_ptr<ParsedCommandOverload> ptr;
-
-#ifdef DOXYGEN
-        static ptr create(Function fn);
-#endif
     };
 
 #ifndef DOXYGEN
@@ -207,8 +211,6 @@ namespace console {
         typedef OverloadedCommandNode node_type;
         typedef OverloadedCommandNode & result_type;
 
-        OverloadedCommandNode & create(DirectoryNode & dir, std::string const & name) const;
-
     protected:
         ParsedCommandAttributorBase(ParsedCommandOverloadBase::ptr overload, unsigned index);
         ParsedCommandAttributorBase(ParsedCommandAttributorBase const & other, unsigned index);
@@ -224,10 +226,14 @@ namespace console {
         void shortDoc(std::string const & doc);
 
     private:
+        OverloadedCommandNode & create(DirectoryNode & dir, std::string const & name) const;
+
         ParsedCommandOverloadBase::ptr overload_;
         unsigned index_;
         boost::optional<std::string> doc_;
         boost::optional<std::string> shortdoc_;
+
+        friend class senf::console::DirectoryNode;
     };
 
     /** \brief Non argument dependent ParsedCommandBase attributes
@@ -257,24 +263,26 @@ namespace console {
     /** \brief Keyword argument tags
 
         The tags defined in this namespace are used as keyword arguments via the <a
-        href="http://www.boost.org/doc/libs/1_33_1/libs/parameter/doc/html/index.html">Boost.Parameter</a>
+        href="http://www.boost.org/doc/libs/release/libs/parameter/doc/html/index.html">Boost.Parameter</a>
         library.
 
         For the keyword tags, the standard C++ scoping rules apply
         \code
+        namespace fty=senf::console::factory;
+
         // Either qualify them with their complete namespace
-        dir.add(...)
-            .arg( senf::console::kw::name = "name" );
+        dir.add(..., fty::Command(...)
+            .arg( senf::console::kw::name = "name" ) );
 
         // Or use a namespace alias
         namespace kw = senf::console::kw;
-        dir.add(...)
-            .arg( kw::name = "name" );
+        dir.add(..., fty::Command(...)
+            .arg( kw::name = "name" ) );
 
         // Or import the keywords into the current namespace (beware of name collisions)
         using namespace senf::console::kw;
-        dir.add(...)
-            .arg( name = "name" );
+        dir.add(..., fty::Command(...)
+            .arg( name = "name" ) );
         \endcode
 
         The second alternative is preferred, the <tt>using namespace</tt> directive may be used as
@@ -288,12 +296,12 @@ namespace console {
         \code
         void command(int);
 
-        dir.add("command", &command)
+        dir.add("command", fty::Command(&command)
             .arg( kw::name          = "name",
                   kw::description   = "description",
                   kw::default_value = 1,
                   kw::type_name     = "type_name",
-                  kw::default_doc   = "default_doc" );
+                  kw::default_doc   = "default_doc" ) );
         \endcode
         Will create the following documentation:
         \htmlonly
@@ -308,6 +316,8 @@ namespace console {
         \endhtmlonly
 
         \see \ref senf::console::ParsedArgumentAttributor::arg()
+
+        \ingroup console_commands
      */
     namespace kw {
         BOOST_PARAMETER_KEYWORD(type, name) ///< Argument name
@@ -482,7 +492,7 @@ namespace console {
                                         /**< This member changes the attributes for the current
                                              argument. The attributes are passed to arg() as keyword
                                              arguments using the <a
-                                             href="http://www.boost.org/doc/libs/1_33_1/libs/parameter/doc/html/index.html">Boost.Parameter</a>
+                                             href="http://www.boost.org/doc/libs/release/libs/parameter/doc/html/index.html">Boost.Parameter</a>
                                              library.
                                              \code
                                              ...
@@ -498,14 +508,12 @@ namespace console {
                                              present, if there is an argument at the current
                                              index. */
 
-#ifndef DOXYVEN
-
+#ifndef DOXYGEN
 #       define BOOST_PP_ITERATION_PARAMS_1                                                        \
             (4, (1, BOOST_PARAMETER_MAX_ARITY,                                                    \
                  SENF_ABSOLUTE_INCLUDE_PATH(Utils/Console/ParsedCommand.mpp),                     \
                  5))
 #       include BOOST_PP_ITERATE()
-
 #endif
 
     private:
@@ -572,6 +580,67 @@ namespace console {
 
 namespace factory {
 
+#ifdef DOXYGEN
+
+    /** \brief OverloadedCommandNode factory
+
+        This factory will create new OverloadedCommandNode instances <em>or add new overloads</em>
+        to an existing OverloadedCommandNode. The factory supports automatic argument parsing.
+
+        Commands are added to the tree using
+        \code
+        namespace fty = senf::console::factory;
+        node.add("name", fty::Command(function));
+        \endcode
+
+        The Command factory supports the following features:
+        \li Automatic argument parsing
+        \li Automatic binding of member functions. Pass the owning instance as second argument to
+            the factory
+        \li Conversion to a compatible signature. Pass the signature as template argument to the
+            factory
+
+        If the signature of the command added matches
+        \code
+        void (std::ostream &, senf::console::ParsedCommandInfo const &)
+        \endcode
+        The command is added using manual argument parsing, otherwise it is added using automatic
+        argument parsing.
+
+        See the <a href="classsenf_1_1console_1_1factory_1_1Command-members.html">List of all
+        members</a> for additional attributes.
+
+        \note This class is for exposition only, the real interface consists of several overloaded
+            factory functions.
+
+        \see \ref console_manualparse \n
+            \ref console_autoparse
+     */
+    class Command : public ParsedArgumentAttributor
+    {
+    public:
+        typedef OverloadedCommandNode node_type;
+        typedef unspecified result_type;
+
+        Command(unspecified fn);        ///< Create a node calling \a fn
+        template <class Signature>
+        Command(unspecified fn);        ///< Create a node calling \a fn with signature \a Signature
+                                        /**< The given \a Signature must be compatible with \a fn
+                                             for each argument and the return value. */
+
+        Command(member_function_pointer fn, Owner const * owner);
+                                        ///< Create a node calling member function \a fn on \a owner
+        template <class Signature>
+        Command(member_function_pointer fn, Owner const * owner);
+                                        ///< Create a node calling member function \a fn on \a owner
+                                        ///  with the given \a Signature
+                                        /**< The given \a Signature must be compatible with \a fn
+                                             for each argument and the return value. */
+
+    };
+
+#else
+
     template <class Signature>
     SimpleOverloadAttributor
     Command(boost::function<Signature> fn,
@@ -634,10 +703,11 @@ namespace factory {
     Command(Member memfn, Owner const * owner,
             typename boost::enable_if<boost::is_member_function_pointer<Member> >::type * = 0);
 
+#endif
 
 }}}
 
-///////////////////////////////hh.e////////////////////////////////////////
+//-/////////////////////////////////////////////////////////////////////////////////////////////////
 #include "ParsedCommand.cci"
 #include "ParsedCommand.ct"
 #include "ParsedCommand.cti"