From: g0dil Date: Wed, 3 Feb 2010 15:26:51 +0000 (+0000) Subject: Utils/Console: Replace mkdir() and link() DirectoryNode members by factories X-Git-Url: http://g0dil.de/git?a=commitdiff_plain;h=7d62c38fca85add3e5a04f0518d6f1417fa6ca69;p=senf.git Utils/Console: Replace mkdir() and link() DirectoryNode members by factories git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@1563 270642c3-0616-0410-b53a-bc976706d245 --- diff --git a/senf/Utils/Console/Config.test.cc b/senf/Utils/Console/Config.test.cc index a952491..2e830da 100644 --- a/senf/Utils/Console/Config.test.cc +++ b/senf/Utils/Console/Config.test.cc @@ -73,9 +73,9 @@ SENF_AUTO_UNIT_TEST(configBundle) senf::console::ScopedDirectory<> chroot; senf::console::root().add("chroot", chroot); - root.mkdir("dir1").add("fun1", fty::Command(&fun1)); + root.add("dir1", fty::Directory()).add("fun1", fty::Command(&fun1)); root.add("fun2", fty::Command(&fun2)); - chroot.mkdir("dir1").add("fun1", fty::Command(&fun1)); + chroot.add("dir1", fty::Directory()).add("fun1", fty::Command(&fun1)); chroot.add("fun2", fty::Command(&fun2)); TempFile cfg ("test.cfg"); diff --git a/senf/Utils/Console/ConfigFile.test.cc b/senf/Utils/Console/ConfigFile.test.cc index 5d424c0..8369e66 100644 --- a/senf/Utils/Console/ConfigFile.test.cc +++ b/senf/Utils/Console/ConfigFile.test.cc @@ -150,7 +150,7 @@ SENF_AUTO_UNIT_TEST(configFileSkipGroup) senf::console::ScopedDirectory<> dir2; senf::console::root().add("dir2", dir2); - dir2.mkdir("dir3").add("fun2", fty::Command(&fun2)); + dir2.add("dir3",fty::Directory()).add("fun2", fty::Command(&fun2)); dir2.add("fun1", fty::Command(&fun1)); { @@ -193,12 +193,12 @@ SENF_AUTO_UNIT_TEST(configRestrictAndLink) senf::console::ScopedDirectory<> dir2; dir1.add("dir2", dir2); - dir2.mkdir("dir3").add("fun2", fty::Command(&fun2)); + dir2.add("dir3",fty::Directory()).add("fun2", fty::Command(&fun2)); dir2.add("fun1", fty::Command(&fun1)); senf::console::ScopedDirectory<> dir4; senf::console::root().add("dir4", dir4); - dir4.link("link1", dir2); + dir4.add("link1", fty::Link(dir2)); { senf::console::ConfigFile cfg (cfgf.name(), dir4); diff --git a/senf/Utils/Console/Executor.test.cc b/senf/Utils/Console/Executor.test.cc index 551c0ab..85c419c 100644 --- a/senf/Utils/Console/Executor.test.cc +++ b/senf/Utils/Console/Executor.test.cc @@ -51,8 +51,8 @@ SENF_AUTO_UNIT_TEST(executor) { namespace fty = senf::console::factory; - senf::console::root().mkdir("dir1").mkdir("dir3"); - senf::console::root().mkdir("dir2").doc("Helptext").add("test",fty::Command(&testCommand)); + senf::console::root().add("dir1",fty::Directory()).add("dir3",fty::Directory()); + senf::console::root().add("dir2",fty::Directory()).doc("Helptext").add("test",fty::Command(&testCommand)); senf::console::Executor executor; senf::console::CommandParser parser; @@ -186,8 +186,9 @@ SENF_AUTO_UNIT_TEST(executorChroot) { namespace fty = senf::console::factory; - senf::console::root().mkdir("dir1").mkdir("dir3"); - senf::console::root().mkdir("dir2").doc("Helptext").add("test", fty::Command(&testCommand)); + senf::console::root().add("dir1",fty::Directory()).add("dir3",fty::Directory()); + senf::console::root().add("dir2",fty::Directory().doc("Helptext")) + .add("test", fty::Command(&testCommand)); senf::console::Executor executor; senf::console::CommandParser parser; @@ -222,8 +223,9 @@ SENF_AUTO_UNIT_TEST(executorPolicy) { namespace fty = senf::console::factory; - senf::console::root().mkdir("dir1").mkdir("dir3"); - senf::console::root().mkdir("dir2").doc("Helptext").add("test",fty::Command(&testCommand)); + senf::console::root().add("dir1",fty::Directory()).add("dir3",fty::Directory()); + senf::console::root().add("dir2",fty::Directory().doc("Helptext")) + .add("test",fty::Command(&testCommand)); senf::console::Executor executor; senf::console::CommandParser parser; @@ -255,8 +257,9 @@ SENF_AUTO_UNIT_TEST(executorAuto) { namespace fty = senf::console::factory; - senf::console::root().mkdir("tdir1").mkdir("dir3"); - senf::console::root().mkdir("dir2").doc("Helptext").add("test",fty::Command(&testCommand)); + senf::console::root().add("tdir1",fty::Directory()).add("dir3",fty::Directory()); + senf::console::root().add("dir2",fty::Directory().doc("Helptext")) + .add("test",fty::Command(&testCommand)); senf::console::Executor executor; executor diff --git a/senf/Utils/Console/Node.cci b/senf/Utils/Console/Node.cci index 61c3646..21b38ee 100644 --- a/senf/Utils/Console/Node.cci +++ b/senf/Utils/Console/Node.cci @@ -212,18 +212,6 @@ senf::console::DirectoryNode::operator()(std::string const & name) return getCommand(name); } -prefix_ senf::console::DirectoryNode & -senf::console::DirectoryNode::mkdir(std::string const & name) -{ - return add(name, create()); -} - -prefix_ senf::console::DirectoryNode & -senf::console::DirectoryNode::provideDirectory(std::string const & name) -{ - return hasChild(name) ? getDirectory(name) : mkdir(name); -} - prefix_ senf::console::DirectoryNode::ChildrenRange senf::console::DirectoryNode::children() const { @@ -238,11 +226,6 @@ senf::console::DirectoryNode::completions(std::string const & s) children_.lower_bound(s + "\xff")); } -prefix_ void senf::console::DirectoryNode::link(std::string const & name, GenericNode & target) -{ - add(name, LinkNode::create(target)); -} - prefix_ senf::console::DirectoryNode::DirectoryNode() {} @@ -369,6 +352,15 @@ prefix_ senf::console::SimpleCommandNode::cptr senf::console::SimpleCommandNode: } /////////////////////////////////////////////////////////////////////////// + +prefix_ senf::console::DirectoryNode & senf::console::provideDirectory(DirectoryNode & dir, + std::string const & name) +{ + return dir.hasChild(name) ? dir.getDirectory(name) : dir.add(name, factory::Directory()); +} + + +/////////////////////////////////////////////////////////////////////////// // senf::console::factory::SimpleCommand prefix_ senf::console::factory::SimpleCommand::SimpleCommand(SimpleCommandNode::Function fn) @@ -398,6 +390,50 @@ senf::console::factory::SimpleCommand::shortdoc(std::string const & doc) return *this; } +/////////////////////////////////////////////////////////////////////////// +// senf::console::factory::Directory + +prefix_ senf::console::factory::Directory::Directory() + : node_ (DirectoryNode::create()) +{} + +prefix_ senf::console::DirectoryNode & +senf::console::factory::Directory::create(DirectoryNode & dir, std::string const & name) + const +{ + return dir.add(name, node_); +} + +prefix_ senf::console::factory::Directory const & +senf::console::factory::Directory::doc(std::string const & doc) + const +{ + node_->doc(doc); + return *this; +} + +prefix_ senf::console::factory::Directory const & +senf::console::factory::Directory::shortdoc(std::string const & doc) + const +{ + node_->shortdoc(doc); + return *this; +} + +/////////////////////////////////////////////////////////////////////////// +// senf::console::factory::Link + +prefix_ senf::console::factory::Link::Link(GenericNode & target) + : node_ (LinkNode::create(target)) +{} + +prefix_ senf::console::LinkNode & senf::console::factory::Link::create(DirectoryNode & dir, + std::string const & name) + const +{ + return dir.add(name, node_); +} + ///////////////////////////////cci.e/////////////////////////////////////// #undef prefix_ diff --git a/senf/Utils/Console/Node.hh b/senf/Utils/Console/Node.hh index 0b854fb..7bf1882 100644 --- a/senf/Utils/Console/Node.hh +++ b/senf/Utils/Console/Node.hh @@ -53,12 +53,13 @@ SomeClass() : dir(this) { + namespace fty = senf::console::factory; // You may document the directory here or later when adding it to the tree dir.doc("Manager for something"); // Add a member function (the pointer-to-member is automatically bound to this instance) - dir.add("member", &SomeClass::member) - .doc("Do the member operation"); + dir.add("member", fty::Command(this, &SomeClass::member) + .doc("Do the member operation")); } void member(std::ostream & os, senf::console::ParseCommandInfo const & command) @@ -69,6 +70,8 @@ int main(int, char**) { + namespace fty = senf::console::factory; + // Provide global documentation senf::console::root() .doc("This is someServer server"); @@ -76,13 +79,13 @@ // Add a new directory to the root and document it. All the mutators return the node object // itself so operations can be chained. senf::console::DirectoryNode & mydir ( senf::console::root() - .mkdir("myserver") - .doc("My server specific directory")); + .add("myserver", fty::Directory() + .doc("My server specific directory"))); // Add a command to that directory - mydir.add("mycommand", &mycommand) + mydir.add("mycommand", fty::Command(&mycommand) .doc("mycommand []\n\n" - "If is given, flurgle the , otherwise burgle it"); + "If is given, flurgle the , otherwise burgle it")); // Create a SomeClass instance and add it's directory. SomeClass someClass; @@ -507,11 +510,6 @@ namespace console { \throws std::bad_cast if the child \a name is not a command node. */ - DirectoryNode & mkdir(std::string const & name); - ///< Create sub-directory node - DirectoryNode & provideDirectory(std::string const & name); - ///< Return subdirectory, possibly creating it - ChildrenRange children() const; ///< Return iterator range over all children. /**< The returned range is sorted by child name. */ @@ -519,10 +517,6 @@ namespace console { ///< Return iterator range of completions for \a s /**< The returned range is sorted by child name. */ - void link(std::string const & name, GenericNode & target); - ///< Create a child node which is a link to target. \a s - /**< The new link node will be a child of the node for which this member function is called. */ - ///\} /////////////////////////////////////////////////////////////////////////// @@ -681,6 +675,8 @@ namespace console { std::string shortdoc_; }; + DirectoryNode & provideDirectory(DirectoryNode & dir, std::string const & name); + #ifndef DOXYGEN namespace factory { @@ -703,6 +699,39 @@ namespace factory { SimpleCommandNode::ptr node_; }; + class Directory + : public detail::NodeFactory + { + public: + typedef DirectoryNode node_type; + typedef DirectoryNode & result_type; + + Directory(); + + DirectoryNode & create(DirectoryNode & dir, std::string const & name) const; + + Directory const & doc(std::string const & doc) const; + Directory const & shortdoc(std::string const & doc) const; + + private: + DirectoryNode::ptr node_; + }; + + class Link + : public detail::NodeFactory + { + public: + typedef LinkNode node_type; + typedef LinkNode & result_type; + + explicit Link(GenericNode & target); + + LinkNode & create(DirectoryNode & dir, std::string const & name) const; + + private: + LinkNode::ptr node_; + }; + } #endif diff --git a/senf/Utils/Console/Node.test.cc b/senf/Utils/Console/Node.test.cc index be5964c..ef2041f 100644 --- a/senf/Utils/Console/Node.test.cc +++ b/senf/Utils/Console/Node.test.cc @@ -39,8 +39,12 @@ SENF_AUTO_UNIT_TEST(genericNode) { + namespace fty = senf::console::factory; + senf::console::GenericNode & node ( - senf::console::root().mkdir("dir1").mkdir("dir2").doc("help info")); + senf::console::root() + .add("dir1", fty::Directory()) + .add("dir2", fty::Directory().doc("help info"))); senf::console::GenericNode::weak_ptr wp (node.thisptr()); BOOST_CHECK_EQUAL( node.name(), "dir2" ); @@ -74,6 +78,8 @@ namespace { SENF_AUTO_UNIT_TEST(directoryNode) { + namespace fty = senf::console::factory; + senf::console::DirectoryNode::ptr p (senf::console::DirectoryNode::create()); BOOST_CHECK( & senf::console::root().add("dir1", p) == p.get() ); @@ -88,7 +94,9 @@ SENF_AUTO_UNIT_TEST(directoryNode) BOOST_CHECK_THROW( senf::console::root()["fn"], std::bad_cast ); BOOST_CHECK( &senf::console::root().get("dir1") == p.get() ); - senf::console::root().mkdir("dir2").mkdir("dir3"); + senf::console::root() + .add("dir2", fty::Directory()) + .add("dir3", fty::Directory()); char const * const children[] = { "dir1", "dir2", "fn", "sys" }; BOOST_CHECK_EQUAL_COLLECTIONS( boost::make_transform_iterator(senf::console::root().children().begin(), @@ -118,9 +126,9 @@ SENF_AUTO_UNIT_TEST(directoryNode) BOOST_CHECK_EQUAL( p->shorthelp(), "short doc" ); ss.str(""); - senf::console::root()["dir2"].mkdir("dir4"); - senf::console::root()["dir2"].link("link", *p); - senf::console::root()["dir2"]["dir4"].link("link", senf::console::root()); + senf::console::root()["dir2"].add("dir4", fty::Directory()); + senf::console::root()["dir2"].add("link", fty::Link(*p)); + senf::console::root()["dir2"]["dir4"].add("link", fty::Link(senf::console::root())); senf::console::dump(ss, senf::console::root()["dir2"]); BOOST_CHECK_EQUAL( ss.str(), "dir3/\n" @@ -138,8 +146,10 @@ SENF_AUTO_UNIT_TEST(directoryNode) SENF_AUTO_UNIT_TEST(linkNode) { - senf::console::root().mkdir("dir1"); - senf::console::root().link("link1", senf::console::root()["dir1"]); + namespace fty = senf::console::factory; + + senf::console::root().add("dir1", fty::Directory()); + senf::console::root().add("link1", fty::Link(senf::console::root()["dir1"])); BOOST_CHECK( senf::console::root()["dir1"] == senf::console::root()["link1"] ); @@ -158,6 +168,7 @@ namespace { SENF_AUTO_UNIT_TEST(factory) { namespace fty = senf::console::factory; + senf::console::root().add("fn1", fty::SimpleCommand(&callback)); senf::console::root().add("fn2", fty::SimpleCommand(Functor())); diff --git a/senf/Utils/Console/ParsedCommand.test.cc b/senf/Utils/Console/ParsedCommand.test.cc index 13402dd..197b0ab 100644 --- a/senf/Utils/Console/ParsedCommand.test.cc +++ b/senf/Utils/Console/ParsedCommand.test.cc @@ -290,7 +290,7 @@ namespace { namespace fty = senf::console::factory; senf::console::DirectoryNode & dir ( - senf::console::root()["test"].mkdir("dircb")); + senf::console::root()["test"].add("dircb", fty::Directory())); dir.add("cb1", fty::Command(&cb1)); return dir.thisptr(); } diff --git a/senf/Utils/Console/ProgramOptions.test.cc b/senf/Utils/Console/ProgramOptions.test.cc index fd8bfd0..ba131d3 100644 --- a/senf/Utils/Console/ProgramOptions.test.cc +++ b/senf/Utils/Console/ProgramOptions.test.cc @@ -53,9 +53,9 @@ SENF_AUTO_UNIT_TEST(programOptions) senf::console::ScopedDirectory<> root; senf::console::root().add("root", root); - root.mkdir("dir1").add("fun1", fty::Command(&fun1)); + root.add("dir1", fty::Directory()).add("fun1", fty::Command(&fun1)); root.add("fun2", fty::Command(&fun2)); - root.mkdir("name-with-dashes").add("fun-2", fty::Command(&fun2)); + root.add("name-with-dashes", fty::Directory()).add("fun-2", fty::Command(&fun2)); { char const * argv[] = { "", "--dir1-fun1=foo","--fun2" }; diff --git a/senf/Utils/Console/ScopedDirectory.cci b/senf/Utils/Console/ScopedDirectory.cci index 6f868b7..80b3882 100644 --- a/senf/Utils/Console/ScopedDirectory.cci +++ b/senf/Utils/Console/ScopedDirectory.cci @@ -92,18 +92,6 @@ senf::console::ScopedDirectoryBase::get(std::string const & name) return node().get(name); } -prefix_ senf::console::DirectoryNode & -senf::console::ScopedDirectoryBase::mkdir(std::string const & name) -{ - return node().mkdir(name); -} - -prefix_ void senf::console::ScopedDirectoryBase::link(std::string const & name, - GenericNode & target) -{ - return node().link(name, target); -} - prefix_ senf::console::DirectoryNode::ChildrenRange senf::console::ScopedDirectoryBase::children() const diff --git a/senf/Utils/Console/ScopedDirectory.hh b/senf/Utils/Console/ScopedDirectory.hh index ddbf292..481a4dd 100644 --- a/senf/Utils/Console/ScopedDirectory.hh +++ b/senf/Utils/Console/ScopedDirectory.hh @@ -59,8 +59,6 @@ namespace console { CommandNode & getCommand(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); - void link(std::string const & name, GenericNode & node); DirectoryNode::ChildrenRange children() const; DirectoryNode & doc(std::string const & doc); diff --git a/senf/Utils/Console/ScopedDirectory.test.cc b/senf/Utils/Console/ScopedDirectory.test.cc index 8a8f203..a0248ba 100644 --- a/senf/Utils/Console/ScopedDirectory.test.cc +++ b/senf/Utils/Console/ScopedDirectory.test.cc @@ -103,7 +103,7 @@ SENF_AUTO_UNIT_TEST(scopedDirectoryBase) { senf::console::ScopedDirectory<> dir; senf::console::root().add("dir", dir); - dir.mkdir("foo"); + dir.add("foo",fty::Directory()); dir.add("cb", fty::Command(&callback)); BOOST_CHECK( &dir["foo"] == &dir.get("foo") ); BOOST_CHECK( &dir("cb") == &dir.get("cb") ); diff --git a/senf/Utils/Console/testServer.cc b/senf/Utils/Console/testServer.cc index c18fe70..393ba97 100644 --- a/senf/Utils/Console/testServer.cc +++ b/senf/Utils/Console/testServer.cc @@ -29,6 +29,7 @@ #include namespace kw = senf::console::kw; +namespace fty = senf::console::factory; void echo(std::ostream & output, senf::console::ParseCommandInfo const & command) { @@ -48,10 +49,10 @@ struct TestObject TestObject() : dir(this) { - dir.add("vat", &TestObject::vat) - .arg("vat", "VAT in %", kw::default_value = 19) - .arg("amount", "Amount including VAT") - .doc("Returns the amount of {vat}-% VAT included in {amount}"); + dir.add("vat", fty::Command(this, &TestObject::vat) + .arg("vat", "VAT in %", kw::default_value = 19) + .arg("amount", "Amount including VAT") + .doc("Returns the amount of {vat}-% VAT included in {amount}") ); } double vat (int vat, double amount) @@ -80,13 +81,13 @@ int main(int, char **) .doc("This is the console test application"); senf::console::root() - .mkdir("console") - .doc("Console settings"); + .add("console",fty::Directory() + .doc("Console settings")); senf::console::DirectoryNode & serverDir ( senf::console::root() - .mkdir("server") - .doc("server commands") ); + .add("server",fty::Directory() + .doc("server commands"))); senf::console::ScopedDirectory<> testDir; senf::console::root() @@ -94,25 +95,25 @@ int main(int, char **) .doc("Test functions"); senf::console::root()["console"] - .add("showlog", &enableLogging) - .doc("Enable display of log messages on the current console"); + .add("showlog", fty::Command(&enableLogging) + .doc("Enable display of log messages on the current console")); - senf::console::root().link("sl", senf::console::root()["console"]("showlog")); + senf::console::root().add("sl", fty::Link(senf::console::root()["console"]("showlog"))); serverDir - .add("shutdown", &shutdownServer) - .doc("Terminate server application"); + .add("shutdown", fty::Command(&shutdownServer) + .doc("Terminate server application")); testDir - .add("echo", &echo) - .doc("Example of a function utilizing manual argument parsing"); + .add("echo", fty::Command(&echo) + .doc("Example of a function utilizing manual argument parsing")); TestObject test; testDir .add("extra", test.dir) .doc("Example of an instance directory"); - senf::console::root().link("ex", test.dir); + senf::console::root().add("ex", fty::Link(test.dir)); senf::console::Server::start( senf::INet4SocketAddress(23232u) ) .name("testServer");