// Define callback function. void mycommand(std::ostream & os, senf::console::ParseCommandInfo const & command) { // ... os << "!! Important message ...\n"; } class SomeClass { public: // Declare a directory node (proxy) for use by this class. This must be public so we can add // it to the node tree later. senf::console::ScopedDirectory<SomeClass> dir; 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", fty::Command(&SomeClass::member, this) .doc("Do the member operation")); } void member(std::ostream & os, senf::console::ParseCommandInfo const & command) { // ... } }; int main(int, char**) { namespace fty = senf::console::factory; // Provide global documentation senf::console::root() .doc("This is someServer server"); // 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() .add("myserver", fty::Directory() .doc("My server specific directory"))); // Add a command to that directory mydir.add("mycommand", fty::Command(&mycommand) .doc("mycommand <foo> [<bar>]\n\n" "If <bar> is given, flurgle the <foo>, otherwise burgle it")); // Create a SomeClass instance and add it's directory. SomeClass someClass; mydir.add("someClass", someClass.dir); // Start the interactive console server senf::console::Server::start(senf::INet4SocketAddress(senf::INet4Address::None, 23232u)) .name("someServer"); }
There is a single root node, the senf::console::DirectoryNode called senf::console::root(). From this node, the tree is traversed.
All nodes are allocated on the heap and are managed using a smart pointer.
void callback(std::ostream & os, senf::console::ParseCommandInfo const & command) { ... } // ... myDirectory.add("foo",fty::Command(&callback));
Every node is identified among it's siblings by it's name. The name of the node is set when adding the node to the tree. If the name is empty or non-unique, a unique name will be automatically provided.
To remove a node from the tree, just use the nodes senf::console::GenericNode::unlink() or the parents senf::console::DirectoryNode::remove() member. This call removes the node from it's parent and returns a (smart) node pointer.
myDirectory.add("bar", myDirectory.remove("foo"));
myDirectory.add("foo", fty::Command(&fooCallback).doc("The foo method") );
ptr
or weak_ptr
) to hold the node.
Another possibility is to traverse the tree explicitly. For this purpose, the operators '[]' and '()' have been overloaded in senf::console::DirectoryNode.
senf::console::root().getDirectory("myDirectory").getCommand("foo") \\ or more concise but otherwise completely identical senf::console::root()["myDirectory"]("foo")
getDirectory and the '[]' operator will return a senf::console::DirectoryNode whereas getCommand and the '()' operator will return a senf::console::CommandNode. If the node is not found or is not of the correct type, an exception will be raised.
The senf::console::ScopedDirectory member should be declared public. This allows the user of the class to add the node to the tree.
3. Classes |
|
class | senf::console::GenericNode |
Config/console node tree base-class. More... |
|
class | senf::console::DirectoryNode |
Config/console tree directory node. More... |
|
class | senf::console::CommandNode |
Config/console tree command node. More... |
|
class | senf::console::ScopedDirectory< Owner > |
DirectoryNode member proxy. More... |