From 8dc434a141f0fb8684fbcaae34a5ddc1d268c51a Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Fri, 25 Sep 2009 16:36:56 +0200 Subject: [PATCH] CommandRegistry implementation (...) --- src/proc/control/command-registry.hpp | 34 ++++++++++++++++++--------- src/proc/control/command.cpp | 24 +++++++++++-------- src/proc/control/command.hpp | 5 ++-- 3 files changed, 40 insertions(+), 23 deletions(-) diff --git a/src/proc/control/command-registry.hpp b/src/proc/control/command-registry.hpp index 4f0abfe56..7e7129cf3 100644 --- a/src/proc/control/command-registry.hpp +++ b/src/proc/control/command-registry.hpp @@ -43,6 +43,7 @@ #include "lib/sync.hpp" #include "lib/format.hpp" #include "include/logging.h" +#include "lib/util.hpp" //#include "lib/bool-checkable.hpp" #include "proc/control/command.hpp" @@ -51,6 +52,7 @@ #include "proc/control/typed-allocation-manager.hpp" //#include "proc/control/memento-tie.hpp" +#include #include #include #include @@ -62,10 +64,13 @@ namespace control { + using boost::hash; using boost::noncopyable; using std::tr1::shared_ptr; + using std::tr1::unordered_map; // using std::ostream; using std::string; + using util::contains; @@ -78,8 +83,8 @@ namespace control { , noncopyable { // using a hashtable to implement the index - typedef std::tr1::unordered_map CmdIndex; - typedef std::map ReverseIndex; + typedef unordered_map > CmdIndex; + typedef std::map< void*, Symbol> ReverseIndex; CmdIndex index_; ReverseIndex ridx_; @@ -93,11 +98,11 @@ namespace control { /** register a command (Frontend) under the given ID * @return either the new command, or an already existing * command registered under the given ID */ - Command + Command& track (Symbol cmdID, Command const& commandHandle) { Lock sync(this); - UNIMPLEMENTED ("place a commandHandle into the command index, or return the command already registered there"); + Command& indexSlot (index_[cmdID]); if (!indexSlot) { @@ -105,7 +110,6 @@ namespace control { ridx_[&indexSlot] = cmdID; } return indexSlot; - ///////////////////////////TODO possible to return a const& ?? } @@ -117,7 +121,15 @@ namespace control { remove (Symbol cmdID) { Lock sync(this); - UNIMPLEMENTED ("de-register a command definition."); + + bool actually_remove = contains (index_,cmdID); + if (actually_remove) + { + ridx_.erase(& index_[cmdID]); + index_.erase(cmdID); + } + ENSURE (!contains (index_,cmdID)); + return actually_remove; } @@ -128,11 +140,11 @@ namespace control { queryIndex (Symbol cmdID) { Lock sync(this); - UNIMPLEMENTED ("retrieve the command registered under the given ID, maybe return an »empty« command"); - // if index.contains(cmdID) - // return index[cmdID] - // else - return Command(); + + if (contains (index_,cmdID)) + return index_[cmdID]; + else + return Command(); } diff --git a/src/proc/control/command.cpp b/src/proc/control/command.cpp index 1c294131d..602fd9e2a 100644 --- a/src/proc/control/command.cpp +++ b/src/proc/control/command.cpp @@ -105,7 +105,7 @@ namespace control { } - Command + Command const& Command::fetchDef (Symbol cmdID) { CommandRegistry& registry = CommandRegistry::instance(); @@ -150,18 +150,22 @@ namespace control { Command - Command::storeDef (Symbol newCmdID) + Command::storeDef (Symbol newCmdID) const { CommandRegistry& registry = CommandRegistry::instance(); + + if (!registry.queryIndex (newCmdID)) + { + Command cloneDefinition; + cloneDefinition.activate (registry.createCloneImpl(impl())); + + Command& registeredCloneDef = registry.track (newCmdID, cloneDefinition); + if (cloneDefinition == registeredCloneDef) + return registeredCloneDef; + } + static format fmt("Unable to store %s as new command. ID \"%s\" is already in use"); - - if (registry.queryIndex (newCmdID)) - throw error::Logic (str (fmt % *this % newCmdID), LUMIERA_ERROR_DUPLICATE_COMMAND); - - Command cloneDefinition; - cloneDefinition.activate (registry.createCloneImpl(impl())); - - return registry.track (newCmdID, cloneDefinition); + throw error::Logic (str (fmt % *this % newCmdID), LUMIERA_ERROR_DUPLICATE_COMMAND); } diff --git a/src/proc/control/command.hpp b/src/proc/control/command.hpp index 130cea17b..269a53425 100644 --- a/src/proc/control/command.hpp +++ b/src/proc/control/command.hpp @@ -116,7 +116,8 @@ namespace control { static Command get (FuncPtr func); static bool remove (Symbol cmdID); - Command storeDef (Symbol newCmdID); + /** create a clone definition */ + Command storeDef (Symbol newCmdID) const; Command() { } ///< undefined command ~Command(); @@ -165,7 +166,7 @@ namespace control { protected: - static Command fetchDef (Symbol cmdID); + static Command const& fetchDef (Symbol cmdID); friend class CommandDef;