Whitespce cleanup: Remove whitespace at end-on-line, remove tabs, wrap
[senf.git] / senf / Utils / Console / ParsedCommand.hh
index 45276cb..753bd83 100644 (file)
@@ -1,6 +1,6 @@
 // $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>
 
 #include <vector>
 #include <boost/type_traits/function_traits.hpp>
-#include <boost/type_traits/is_member_pointer.hpp>
+#include <boost/type_traits/is_member_function_pointer.hpp>
 #include <boost/mpl/if.hpp>
 #include <boost/utility.hpp>
 #include <boost/parameter/keyword.hpp>
 #include <boost/parameter/parameters.hpp>
-#include "../../config.hh"
+#include <senf/config.hh>
 #include "OverloadedCommand.hh"
 #include "Traits.hh"
-#include "../../Utils/type_traits.hh"
+#include <senf/Utils/type_traits.hh>
 
 #include "ParsedCommand.ih"
 #include "ParsedCommand.mpp"
@@ -113,7 +113,7 @@ namespace console {
         should do so to make the unwieldy casts unnecessary.
 
         \section overload_parse Custom parameter parsers
-        
+
         By default, parameters are parsed using \c boost::lexical_cast and therefore using \c
         iostreams. This means, that any type which can be read from a stream can automatically be
         used as argument type.
@@ -182,49 +182,56 @@ namespace console {
 
 #ifndef DOXYGEN
 
-#   define BOOST_PP_ITERATION_PARAMS_1 (4, (0, SENF_CONSOLE_MAX_COMMAND_ARITY,                     \
-                                            SENF_ABSOLUTE_INCLUDE_PATH(Utils/Console/ParsedCommand.mpp), \
-                                            1))
+#   define BOOST_PP_ITERATION_PARAMS_1                                  \
+        (4, (0, SENF_CONSOLE_MAX_COMMAND_ARITY,                         \
+             SENF_ABSOLUTE_INCLUDE_PATH(Utils/Console/ParsedCommand.mpp), \
+             1))
 #   include BOOST_PP_ITERATE()
 
 #endif
 
-    /** \brief Generic ParsedCommandOverladBase attributes
+    /** \brief Generic ParsedCommandOverloadBase attributes
 
         Attributes for parsed commands are not set directly on the node. They are set via a special
         attributor temporary returned when adding a parsed command to the tree.
-        
+
         This class is the base class for those attributors. It provides members which do not depend
         in any way on the exact type of command added.
 
         \see \ref console_autoparse
      */
     class ParsedCommandAttributorBase
+        : public detail::NodeFactory
     {
     public:
-        OverloadedCommandNode & node() const; ///< Return the node object
-        operator OverloadedCommandNode & () const; ///< Automatically convert to node object
+        typedef OverloadedCommandNode node_type;
+        typedef OverloadedCommandNode & result_type;
+
+        OverloadedCommandNode & create(DirectoryNode & dir, std::string const & name) const;
 
     protected:
-        ParsedCommandAttributorBase(ParsedCommandOverloadBase & overload, unsigned index);
+        ParsedCommandAttributorBase(ParsedCommandOverloadBase::ptr overload, unsigned index);
+        ParsedCommandAttributorBase(ParsedCommandAttributorBase const & other, unsigned index);
 
-        void argName(std::string const & name) const;
-        void argDoc(std::string const & doc) const;
-        void typeName(std::string const & doc) const;
-        void defaultDoc(std::string const & doc) const;
+        void argName(std::string const & name);
+        void argDoc(std::string const & doc);
+        void typeName(std::string const & doc);
+        void defaultDoc(std::string const & doc);
 
         ParsedCommandOverloadBase & overload() const;
-        void overloadDoc(std::string const & doc) const;
-        void nodeDoc(std::string const & doc) const;
-        void shortDoc(std::string const & doc) const;
-        
+        void overloadDoc(std::string const & doc);
+        void nodeDoc(std::string const & doc);
+        void shortDoc(std::string const & doc);
+
     private:
-        ParsedCommandOverloadBase & overload_;
+        ParsedCommandOverloadBase::ptr overload_;
         unsigned index_;
+        boost::optional<std::string> doc_;
+        boost::optional<std::string> shortdoc_;
     };
 
-    /** \brief Non argument dependent ParsedCommandBase attributes 
-        
+    /** \brief Non argument dependent ParsedCommandBase attributes
+
         Attributes for parsed commands are not set directly on the node. They are set via a special
         attributor temporary returned when adding a parsed command to the tree.
 
@@ -241,7 +248,8 @@ namespace console {
         Overload & overload() const;    ///< Get the command overload
 
     protected:
-        ParsedCommandAttributor(Overload & overload, unsigned index);
+        ParsedCommandAttributor(typename Overload::ptr overload, unsigned index);
+        ParsedCommandAttributor(ParsedCommandAttributorBase const & other, unsigned index);
 
     private:
     };
@@ -257,7 +265,7 @@ namespace console {
         // Either qualify them with their complete namespace
         dir.add(...)
             .arg( senf::console::kw::name = "name" );
-        
+
         // Or use a namespace alias
         namespace kw = senf::console::kw;
         dir.add(...)
@@ -292,7 +300,7 @@ namespace console {
         <pre>
         Usage:
             command [name:type_name]
-        
+
         With:
             name      description
                 default: default_doc
@@ -368,7 +376,7 @@ namespace console {
                                              token list returned by the console/config parser into
                                              the appropriate value. If not set explicitly, this
                                              conversion is supplied by the ArgumentTraits
-                                             class. 
+                                             class.
 
                                              Setting the \a parser attribute allows to use a custom
                                              parser. The parser is an arbitrary callable object with
@@ -387,7 +395,7 @@ namespace console {
 
         Attributes for parsed commands are not set directly on the node. They are set via a special
         attributor temporary returned when adding a parsed command to the tree.
-        
+
         This class adds all those members, which do not depend on any specific argument but which
         need to return the correct attributor type.
 
@@ -398,10 +406,10 @@ namespace console {
         : public ParsedCommandAttributor<Overload>
     {
     public:
-        Self doc(std::string const & doc) const; ///< Set documentation for all overloads
-        Self shortdoc(std::string const & doc) const; ///< Set short documentation for all overloads
-        Self overloadDoc(std::string const & doc) const; ///< Set overload specific documentation
-        Self formatter(typename Overload::Formatter formatter) const; 
+        Self doc(std::string const & doc); ///< Set documentation for all overloads
+        Self shortdoc(std::string const & doc); ///< Set short documentation for all overloads
+        Self overloadDoc(std::string const & doc); ///< Set overload specific documentation
+        Self formatter(typename Overload::Formatter formatter);
                                         ///< Set return value formatter
                                         /**< This member is only available, if the \a ReturnType of
                                              the installed callback is not \c void.
@@ -415,7 +423,8 @@ namespace console {
                                              value and writes it properly formated to \a os. */
 
     protected:
-        ParsedArgumentAttributorBase(Overload & overload, unsigned index);
+        ParsedArgumentAttributorBase(typename Overload::ptr overload, unsigned index);
+        ParsedArgumentAttributorBase(ParsedCommandAttributorBase const & other, unsigned index);
 
     private:
     };
@@ -427,31 +436,32 @@ namespace console {
         : public ParsedCommandAttributor<Overload>
     {
     public:
-        Self doc(std::string const & doc) const; ///< Set documentation for all overloads
-        Self shortdoc(std::string const & doc) const; ///< Set short documentation for all overloads
-        Self overloadDoc(std::string const & doc) const; ///< Set overload specific documentation
+        Self doc(std::string const & doc); ///< Set documentation for all overloads
+        Self shortdoc(std::string const & doc); ///< Set short documentation for all overloads
+        Self overloadDoc(std::string const & doc); ///< Set overload specific documentation
 
     protected:
-        ParsedArgumentAttributorBase(Overload & overload, unsigned index);
+        ParsedArgumentAttributorBase(typename Overload::ptr overload, unsigned index);
+        ParsedArgumentAttributorBase(ParsedCommandAttributorBase const & other, unsigned index);
 
     private:
     };
 
 #endif
-    
+
     /** \brief Argument dependent ParsedCommandBase attributes
 
         Attributes for parsed commands are not set directly on the node. They are set via a special
         attributor temporary returned when adding a parsed command to the tree.
-        
+
         This class adds all those members, which depend on a specific argument. Each call to \c arg
         will advance to the next argument.
 
         \see \ref console_autoparse
      */
-    template < class Overload, unsigned index, bool flag>
+    template <class Overload, unsigned index, bool flag>
     class ParsedArgumentAttributor
-        : public ParsedArgumentAttributorBase< Overload, 
+        : public ParsedArgumentAttributorBase< Overload,
                                                 ParsedArgumentAttributor<Overload, index, flag> >
     {
         typedef boost::parameter::parameters<
@@ -463,10 +473,7 @@ namespace console {
             kw::type::parser> arg_params;
 
     public:
-        typedef OverloadedCommandNode node_type;
-        typedef ParsedArgumentAttributor return_type;
-
-        typedef typename senf::function_traits_arg_type< 
+        typedef typename senf::function_traits_arg_type<
             typename Overload::traits, int(index) >::type arg_type;
         typedef typename senf::remove_cvref< arg_type >::type value_type;
         typedef ParsedArgumentAttributor<Overload, index+1> next_type;
@@ -476,16 +483,16 @@ namespace console {
                                              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>
-                                             library. 
+                                             library.
                                              \code
                                              ...
-                                                 .arg( kw::name          = "name", 
+                                                 .arg( kw::name          = "name",
                                                        kw::default_value = 1 )
                                              ...
                                              \endcode
                                              The valid keywords are defined in the senf::console::kw
                                              namespace.
-                                             
+
                                              Each call to arg() will increment the argument index
                                              and advance to the next argument. This member is only
                                              present, if there is an argument at the current
@@ -502,43 +509,37 @@ namespace console {
 #endif
 
     private:
-        explicit ParsedArgumentAttributor(Overload & overload);
+        explicit ParsedArgumentAttributor(typename Overload::ptr overload);
+        explicit ParsedArgumentAttributor(ParsedCommandAttributorBase const & other);
 
         template <class ArgumentPack>
-        next_type argInfo(ArgumentPack const & args) const;
+        next_type argInfo(ArgumentPack const & args);
         template <class Kw, class ArgumentPack>
-        void argInfo(Kw const &, ArgumentPack const &, boost::mpl::false_) 
-            const;
+        void argInfo(Kw const &, ArgumentPack const &, boost::mpl::false_);
 
         template <class ArgumentPack>
-        void argInfo(boost::parameter::keyword<kw::type::name> const &, 
-                     ArgumentPack const & args, boost::mpl::true_) 
-            const;
+        void argInfo(boost::parameter::keyword<kw::type::name> const &,
+                     ArgumentPack const & args, boost::mpl::true_);
         template <class ArgumentPack>
-        void argInfo(boost::parameter::keyword<kw::type::description> const &, 
-                     ArgumentPack const & args, boost::mpl::true_) 
-            const;
+        void argInfo(boost::parameter::keyword<kw::type::description> const &,
+                     ArgumentPack const & args, boost::mpl::true_);
         template <class ArgumentPack>
-        void argInfo(boost::parameter::keyword<kw::type::default_value> const &, 
-                     ArgumentPack const & args, boost::mpl::true_) 
-            const;
+        void argInfo(boost::parameter::keyword<kw::type::default_value> const &,
+                     ArgumentPack const & args, boost::mpl::true_);
         template <class ArgumentPack>
-        void argInfo(boost::parameter::keyword<kw::type::type_name> const &, 
-                     ArgumentPack const & args, boost::mpl::true_) 
-            const;
+        void argInfo(boost::parameter::keyword<kw::type::type_name> const &,
+                     ArgumentPack const & args, boost::mpl::true_);
         template <class ArgumentPack>
-        void argInfo(boost::parameter::keyword<kw::type::default_doc> const &, 
-                     ArgumentPack const & args, boost::mpl::true_) 
-            const;
+        void argInfo(boost::parameter::keyword<kw::type::default_doc> const &,
+                     ArgumentPack const & args, boost::mpl::true_);
         template <class ArgumentPack>
-        void argInfo(boost::parameter::keyword<kw::type::parser> const &, 
-                     ArgumentPack const & args, boost::mpl::true_) 
-            const;
+        void argInfo(boost::parameter::keyword<kw::type::parser> const &,
+                     ArgumentPack const & args, boost::mpl::true_);
 
         next_type next() const;
 
-        void defaultValue(value_type const & value) const;
-        template <class Fn> void parser(Fn fn) const;
+        void defaultValue(value_type const & value);
+        template <class Fn> void parser(Fn fn);
 
         template <class O, unsigned i, bool f>
         friend class ParsedArgumentAttributor;
@@ -550,7 +551,7 @@ namespace console {
 
     template <class Overload, unsigned index>
     class ParsedArgumentAttributor<Overload, index, false>
-        : public ParsedArgumentAttributorBase< Overload, 
+        : public ParsedArgumentAttributorBase< Overload,
                                                 ParsedArgumentAttributor<Overload, index, false> >
     {
     public:
@@ -558,7 +559,8 @@ namespace console {
         typedef ParsedArgumentAttributor return_type;
 
     private:
-        explicit ParsedArgumentAttributor(Overload & overload);
+        explicit ParsedArgumentAttributor(typename Overload::ptr overload);
+        explicit ParsedArgumentAttributor(ParsedCommandAttributorBase const & other);
 
         template <class O, unsigned i, bool f>
         friend class ParsedArgumentAttributor;
@@ -566,33 +568,74 @@ namespace console {
         friend class detail::ParsedCommandAddNodeAccess;
     };
 
+#endif
+
+namespace factory {
+
+    template <class Signature>
+    SimpleOverloadAttributor
+    Command(boost::function<Signature> fn,
+            typename boost::enable_if_c<detail::ParsedCommandTraits<Signature>::is_simple>::type * = 0);
+
     template <class Function>
-    typename detail::ParsedCommandTraits<Function>::Attributor
-    senf_console_add_node(DirectoryNode & node, std::string const & name, Function fn, int,
-                          typename boost::enable_if_c<
-                              detail::ParsedCommandTraits<Function>::is_callable>::type * = 0);
+    SimpleOverloadAttributor
+    Command(Function fn,
+            typename boost::enable_if_c<detail::ParsedCommandTraits<Function>::is_simple>::type * = 0);
+
+    template <class Owner, class Member>
+    SimpleOverloadAttributor
+    Command(Member memfn, Owner * owner,
+            typename boost::enable_if<boost::is_member_function_pointer<Member> >::type * = 0,
+            typename boost::enable_if_c<detail::ParsedCommandTraits<Member>::is_simple>::type * = 0);
+
+    template <class Owner, class Member>
+    SimpleOverloadAttributor
+    Command(Member memfn, Owner const * owner,
+            typename boost::enable_if<boost::is_member_function_pointer<Member> >::type * = 0,
+            typename boost::enable_if_c<detail::ParsedCommandTraits<Member>::is_simple>::type * = 0);
+
+    template <class CastTo, class Signature>
+    typename senf::console::detail::ParsedCommandTraits<CastTo>::Attributor
+    Command(boost::function<Signature> fn);
+
+    template <class CastTo, class Function>
+    typename senf::console::detail::ParsedCommandTraits<CastTo>::Attributor
+    Command(Function fn,
+            typename boost::enable_if_c<detail::ParsedCommandTraits<Function>::is_callable>::type * = 0,
+            typename boost::disable_if<boost::is_member_function_pointer<Function> >::type * = 0);
 
     template <class Signature>
-    typename detail::ParsedCommandTraits<Signature>::Attributor
-    senf_console_add_node(DirectoryNode & node, std::string const & name, 
-                          boost::function<Signature> fn, int);
+    typename senf::console::detail::ParsedCommandTraits<Signature>::Attributor
+    Command(boost::function<Signature> fn);
+
+    template <class Function>
+    typename senf::console::detail::ParsedCommandTraits<Function>::Attributor
+    Command(Function fn,
+            typename boost::enable_if_c<detail::ParsedCommandTraits<Function>::is_callable>::type * = 0,
+            typename boost::disable_if<boost::is_member_function_pointer<Function> >::type * = 0);
 
-    template <class Owner, class Function>
-    typename detail::ParsedCommandTraits<Function>::Attributor
-    senf_console_add_node(DirectoryNode & node, Owner & owner, std::string const & name,
-                          Function fn, int,
-                          typename boost::enable_if_c<
-                              detail::ParsedCommandTraits<Function>::is_member>::type * = 0);
+    template <class Owner, class Member>
+    typename senf::console::detail::ParsedCommandTraits<Member>::Attributor
+    Command(Member memfn, Owner * owner,
+            typename boost::enable_if<boost::is_member_function_pointer<Member> >::type * = 0);
 
-#endif
+    template <class Owner, class Member>
+    typename senf::console::detail::ParsedCommandTraits<Member>::Attributor
+    Command(Member memfn, Owner const * owner,
+            typename boost::enable_if<boost::is_member_function_pointer<Member> >::type * = 0);
+
+    template <class CastTo, class Owner, class Member>
+    typename senf::console::detail::ParsedCommandTraits<CastTo>::Attributor
+    Command(Member memfn, Owner * owner,
+            typename boost::enable_if<boost::is_member_function_pointer<Member> >::type * = 0);
 
-}}
+    template <class CastTo, class Owner, class Member>
+    typename senf::console::detail::ParsedCommandTraits<CastTo>::Attributor
+    Command(Member memfn, Owner const * owner,
+            typename boost::enable_if<boost::is_member_function_pointer<Member> >::type * = 0);
 
-#include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
 
-BOOST_TYPEOF_REGISTER_TEMPLATE(senf::console::ParsedCommandOverload, (class,unsigned))
-BOOST_TYPEOF_REGISTER_TEMPLATE(senf::console::ParsedArgumentAttributor, (class, unsigned, bool))
-BOOST_TYPEOF_REGISTER_TEMPLATE(boost::function_traits, 1)
+}}}
 
 ///////////////////////////////hh.e////////////////////////////////////////
 #include "ParsedCommand.cci"