switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Utils / Console / ParsedCommand.ih
index 0dbe33b..dfafdb8 100644 (file)
@@ -1,24 +1,29 @@
 // $Id$
 //
-// Copyright (C) 2008 
+// 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 internal header */
 #include <boost/intrusive_ptr.hpp>
 #include "Parse.hh"
 
-///////////////////////////////ih.p////////////////////////////////////////
+//-/////////////////////////////////////////////////////////////////////////////////////////////////
 
 namespace senf {
 namespace console {
 
-    template < class FunctionTraits, 
-               class ReturnType=typename FunctionTraits::result_type, 
+    template < class FunctionTraits,
+               class ReturnType=typename FunctionTraits::result_type,
                unsigned arity=FunctionTraits::arity >
     class ParsedCommandOverload;
 
-    template < class Overload, 
-               unsigned index=0, 
+    template < class Overload,
+               unsigned index=0,
                bool flag=(index < unsigned(Overload::traits::arity)) >
     class ParsedArgumentAttributor;
 
 namespace detail {
 
     /** \brief Internal: Argument information structure
-        
-        This class is used to hold argument information for automatically parsed commands. 
+
+        This class is used to hold argument information for automatically parsed commands.
 
         \see ParsedCommandOverloadBase
      */
@@ -58,32 +63,32 @@ namespace detail {
         : public intrusive_refcount
     {
         typedef boost::intrusive_ptr<ArgumentInfoBase> ptr;
-        
+
         std::string type;
         std::string name;
         std::string defaultDoc;
         bool hasDefault;
         std::string doc;
         bool singleToken;
-        
+
         explicit ArgumentInfoBase(std::string const & type, bool singleToken=false);
         virtual ~ArgumentInfoBase();
 
         virtual std::string defaultValueStr() const = 0;
     };
-    
+
     /** \brief Internal: Argument information structure
-        
-        This class is used to hold argument information for automatically parsed commands. 
+
+        This class is used to hold argument information for automatically parsed commands.
 
         \see ParsedCommandOverloadBase
      */
     template <class ParameterType>
-    struct ArgumentInfo 
+    struct ArgumentInfo
         : public ArgumentInfoBase
     {
         typedef boost::intrusive_ptr<ArgumentInfo> ptr;
-        typedef boost::function<void (ParseCommandInfo::TokensRange const &, 
+        typedef boost::function<void (ParseCommandInfo::TokensRange const &,
                                       ParameterType &)> Parser;
 
         static ptr create();
@@ -94,7 +99,7 @@ namespace detail {
 
         virtual std::string defaultValueStr() const;
     };
-    
+
 #ifndef DOXYGEN
 
     // FirstArgType returns void, if the function has no arguments, otherwise it returns arg1_type
@@ -116,6 +121,7 @@ namespace detail {
     {
         static const bool is_callable = false;
         static const bool is_member = false;
+        static const bool is_simple = false;
     };
 
     template <class FunctionP, class Function>
@@ -129,8 +135,8 @@ namespace detail {
         static const bool has_ostream_arg = boost::is_same<first_arg_type, std::ostream &>::value;
 
         typedef typename boost::mpl::if_c<
-            has_ostream_arg, 
-            typename function_traits_remove_arg<base_traits>::type, 
+            has_ostream_arg,
+            typename function_traits_remove_arg<base_traits>::type,
             base_traits>
         ::type traits;
 
@@ -138,7 +144,8 @@ namespace detail {
 
         static const bool is_callable = true;
         static const bool is_member = boost::is_member_pointer<base_type>::value;
-        
+        static const bool is_simple = false;
+
         typedef typename senf::member_class<base_type>::type class_type;
 
         typedef ParsedCommandOverload<traits> Overload;
@@ -148,21 +155,58 @@ namespace detail {
     // Disable auto-parsing for ParseCommandInfo arg -> register manually parsed command
     template <class FunctionP>
     struct ParsedCommandTraits_i<FunctionP, void (std::ostream &, ParseCommandInfo const &), true>
-    {};
+    {
+        static const bool is_simple = true;
+    };
 
     template <class FunctionP>
     struct ParsedCommandTraits
-        : public ParsedCommandTraits_i< FunctionP, 
+        : public ParsedCommandTraits_i< FunctionP,
                                         typename senf::remove_any_pointer<FunctionP>::type >
     {};
 
     struct ParsedCommandAddNodeAccess;
 
+    // What is THIS about ??
+
+    // Ok, here's the dope: parsed commands may optionally have an std::ostream & first argument. If
+    // this argument is given, then the function will be called with the console output stream as
+    // it's first argument.
+    //
+    // This is implemented in the following way: ParsedCommandOverload (the class responsible for
+    // calling the callback) will ALWAYS pass the stream as first argument. If the user callback
+    // expects os as it's first argument, 'ignoreOneArg' will be false and the user supplied
+    // function will be directly passed to ParsedCommandOverload.
+    //
+    // If however, it does NOT take an std::ostream first argument, 'ignoreOneArg' will be true and
+    // the create member will use boost::bind to DROP the first argument.
+
+    template <class Traits,
+              bool ignoreOneArg=! Traits::has_ostream_arg,
+              unsigned arity=Traits::traits::arity>
+    struct CreateParsedCommandOverload
+    {};
+
+    template <class Traits, unsigned arity>
+    struct CreateParsedCommandOverload<Traits, false, arity>
+    {
+        typedef typename Traits::traits traits;
+
+        template <class Function>
+        static typename senf::console::ParsedCommandOverload<traits>::ptr create(Function fn)
+            { return senf::console::ParsedCommandOverload<traits>::create(fn); };
+    };
+
+#   define BOOST_PP_ITERATION_PARAMS_1 (4, (0, SENF_CONSOLE_MAX_COMMAND_ARITY,                     \
+                                            SENF_ABSOLUTE_INCLUDE_PATH(Utils/Console/ParsedCommand.mpp), \
+                                            4))
+#   include BOOST_PP_ITERATE()
+
 #endif
 
 }}}
 
-///////////////////////////////ih.e////////////////////////////////////////
+//-/////////////////////////////////////////////////////////////////////////////////////////////////
 #endif
 
 \f