minor documentation fix
[senf.git] / Console / Node.hh
index 154bc97..10ac02e 100644 (file)
 
 // Custom includes
 #include <map>
-#include <boost/intrusive_ptr.hpp>
+#include <boost/shared_ptr.hpp>
+#include <boost/weak_ptr.hpp>
+#include <boost/enable_shared_from_this.hpp>
 #include <boost/utility.hpp>
-#include "../Utils/intrusive_refcount.hh"
+#include <boost/range/iterator_range.hpp>
+#include <boost/typeof/typeof.hpp>
+#include <boost/type_traits/remove_reference.hpp>
 #include "../Utils/Exception.hh"
+#include "../Utils/mpl.hh"
+#include "Parse.hh"
 
 //#include "Node.mpp"
 ///////////////////////////////hh.p////////////////////////////////////////
 
+#include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
+
 namespace senf {
 namespace console {
 
@@ -45,87 +53,229 @@ namespace console {
     /** \brief
       */
     class GenericNode 
-        : public intrusive_refcount_t<GenericNode>
+        : public boost::enable_shared_from_this<GenericNode>
     {
     public:
         ///////////////////////////////////////////////////////////////////////////
         // Types
 
-        typedef boost::intrusive_ptr<GenericNode> ptr;
+        typedef boost::shared_ptr<GenericNode> ptr;
+        typedef boost::shared_ptr<GenericNode const> cptr;
+        typedef boost::weak_ptr<GenericNode> weak_ptr;
 
         ///////////////////////////////////////////////////////////////////////////
 
+        virtual ~GenericNode();
+
         std::string const & name() const;
-        DirectoryNode & parent() const;
-        bool managed() const;
+        boost::shared_ptr<DirectoryNode> parent() const;
+
+        std::string path() const;
+
+        ptr unlink();
+
+        bool active() const;
+
+        void help(std::ostream & output) const;
+
+        ptr thisptr();
+        cptr thisptr() const;
 
     protected:
-        explicit GenericNode(std::string const & name, bool managed = false);
+        GenericNode();
 
         void name(std::string const & name);
         static void name(GenericNode & node, std::string const & name);
         void parent(DirectoryNode * parent);
 
     private:
-        bool release();
+        virtual void v_help(std::ostream & output) const = 0;
 
         std::string name_;
-        bool managed_;
         DirectoryNode * parent_;
 
         friend class intrusive_refcount_base;
+        friend class DirectoryNode;
+    };
+
+    class SimpleCommandNode;
+
+    template <class Object>
+    struct NodeCreateTraits
+    {
+        typedef BOOST_TYPEOF_TPL( senf_console_add_node( 
+                                      * static_cast<DirectoryNode *>(0),
+                                      * static_cast<std::string const *>(0),
+                                      * static_cast<Object const *>(0),
+                                      0) ) result_type;
+
+        typedef typename boost::remove_reference<result_type>::type NodeType;
+
+        struct Creator {
+            static NodeType & create(DirectoryNode & node, std::string const & name, 
+                                     Object const & ob);
+        };
     };
 
     /** \brief
+        ///\fixme Provide a default name for added nodes if 'name' is empty ?
       */
     class DirectoryNode : public GenericNode
     {
+        typedef std::map<std::string, GenericNode::ptr> ChildMap;
+
     public:
-        typedef boost::intrusive_ptr<DirectoryNode> ptr;
+        ///////////////////////////////////////////////////////////////////////////
+        // Types
 
-        void add(std::auto_ptr<GenericNode> node, bool uniquify = true);
-        void add(GenericNode & node, bool uniquify = true);
+        typedef boost::shared_ptr<DirectoryNode> ptr;
+        typedef boost::shared_ptr<DirectoryNode const> cptr;
+        typedef boost::weak_ptr<DirectoryNode> weak_ptr;
+
+        typedef boost::iterator_range<ChildMap::const_iterator> ChildrenRange;
+        typedef ChildMap::const_iterator child_iterator;
+
+        ///////////////////////////////////////////////////////////////////////////
+        ///\name Structors and default members
+        ///\{
+
+        static std::auto_ptr<DirectoryNode> create();
+
+        ///\}
+        ///////////////////////////////////////////////////////////////////////////
+        ///\name Children
+        ///\{
+
+        template <class NodeType>
+        NodeType & add(std::string const & name, std::auto_ptr<NodeType> node);
+
+        template <class NodeType>
+        NodeType & add(std::string const & name, boost::shared_ptr<NodeType> node);
+
+        template <class Object>
+        typename NodeCreateTraits<Object>::NodeType & add (std::string const & name, 
+                                                           Object const & ob);
+
+        GenericNode::ptr remove(std::string const & name);
 
         DirectoryNode & operator[](std::string const & name) const;
         CommandNode & operator()(std::string const & name) const;
+        GenericNode & get(std::string const & name) const;
+
+        DirectoryNode & mkdir(std::string const & name);
+        
+        ChildrenRange children() const;
+
+        ///\}
+        ///////////////////////////////////////////////////////////////////////////
+
+        template <class ForwardRange>
+        GenericNode & traverse(ForwardRange const & range);
+
+        DirectoryNode & doc(std::string const & doc);
+
+        ptr thisptr();
+        cptr thisptr() const;
 
     protected:
-        explicit DirectoryNode(std::string const & name, bool managed = false);
+        DirectoryNode();
 
     private:
-        void add(GenericNode::ptr node, bool uniquify);
-        GenericNode & lookup(std::string const & name) const;
+        void add(GenericNode::ptr node);
+        virtual void v_help(std::ostream & output) const;
 
-        typedef std::map<std::string, GenericNode::ptr> ChildMap;
         ChildMap children_;
+        std::string doc_;
+
+        friend DirectoryNode & root();
     };
 
-    struct DuplicateNodeNameException : public senf::Exception
-    { DuplicateNodeNameException() : senf::Exception("Duplicate node name") {}};
+    BOOST_TYPEOF_REGISTER_TYPE(DirectoryNode);
 
     struct UnknownNodeNameException : public senf::Exception
     { UnknownNodeNameException() : senf::Exception("Unknown node name") {}};
 
+    // We need this specialization since we cannot passe auto_ptr via const & !!
+    template <class Type>
+    struct NodeCreateTraits< std::auto_ptr<Type> >
+    {};
+
+    template <class Type>
+    struct NodeCreateTraits< boost::shared_ptr<Type> >
+    {};
+
     /** \brief
       */
     class CommandNode : public GenericNode
     {
     public:
-        typedef boost::intrusive_ptr<CommandNode> ptr;
+        ///////////////////////////////////////////////////////////////////////////
+        // Types
+
+        typedef boost::shared_ptr<CommandNode> ptr;
+        typedef boost::shared_ptr<CommandNode const> cptr;
+        typedef boost::weak_ptr<CommandNode> weak_ptr;
+
+        typedef ParseCommandInfo::ArgumentsRange Arguments;
+
+        ///////////////////////////////////////////////////////////////////////////
+
+        virtual void operator()(std::ostream & output, Arguments const & arguments) = 0;
+
+        ptr thisptr();
+        cptr thisptr() const;
 
     protected:
-        explicit CommandNode(std::string const & name, bool managed = false);
+        CommandNode();
 
     private:
+    };
 
+    /** \brief
+      */
+    class SimpleCommandNode : public CommandNode
+    {
+    public:
+        ///////////////////////////////////////////////////////////////////////////
+        // Types
+
+        typedef boost::function<void (std::ostream &, Arguments const &)> Function;
+
+        ///////////////////////////////////////////////////////////////////////////
+
+        virtual void operator()(std::ostream & output, Arguments const & arguments);
+
+        ptr thisptr();
+        cptr thisptr() const;
+
+        static std::auto_ptr<SimpleCommandNode> create(Function const & fn);
+
+        SimpleCommandNode & doc(std::string const & doc);
+
+    protected:
+        SimpleCommandNode(Function const & fn);
+
+    private:
+        virtual void v_help(std::ostream & output) const;
+
+        Function fn_;
+        std::string doc_;
     };
 
+    template <class Function>
+    SimpleCommandNode & senf_console_add_node(DirectoryNode & node, std::string const & name, 
+                                              Function const & fn, ...);
+
+    BOOST_TYPEOF_REGISTER_TYPE(SimpleCommandNode);
+
+    DirectoryNode & root();
+
 }}
 
 ///////////////////////////////hh.e////////////////////////////////////////
 #include "Node.cci"
-//#include "Node.ct"
-//#include "Node.cti"
+#include "Node.ct"
+#include "Node.cti"
 #endif
 
 \f