From 7fccecacce55c447b710f689b50cf3a87850a886 Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Sat, 26 Sep 2009 04:19:57 +0200 Subject: [PATCH] WIP using the reverse index to find out the Command-ID this includes using a Command* as key within a std::map --- src/lib/util.hpp | 15 ++++++++++++- src/proc/control/command-registry.hpp | 32 ++++++++++++++++++--------- src/proc/control/command.cpp | 2 +- src/proc/control/command.hpp | 8 +++++++ 4 files changed, 45 insertions(+), 12 deletions(-) diff --git a/src/lib/util.hpp b/src/lib/util.hpp index a72f6c2b1..85203c727 100644 --- a/src/lib/util.hpp +++ b/src/lib/util.hpp @@ -99,7 +99,7 @@ namespace util { /** shortcut for containment test on a map */ template inline bool - contains (MAP& map, typename MAP::key_type& key) + contains (MAP& map, typename MAP::key_type const& key) { return map.find(key) != map.end(); } @@ -132,6 +132,19 @@ namespace util { return end != std::find(begin,end, val); } + /** fetch value from a Map, or return a default if not found */ + template + inline typename MAP::value_type + getValue_or_default (MAP& map, typename MAP::key_type const& key + , typename MAP::value_type defaultVal) + { + typename MAP::iterator pos = map.find (key); + if (pos != map.end()) + return pos->second; + else + return defaultVal; + } + /** shortcut for removing all copies of an Element * in any sequential collection */ template diff --git a/src/proc/control/command-registry.hpp b/src/proc/control/command-registry.hpp index 7e7129cf3..b6451f8b5 100644 --- a/src/proc/control/command-registry.hpp +++ b/src/proc/control/command-registry.hpp @@ -71,8 +71,23 @@ namespace control { // using std::ostream; using std::string; using util::contains; + using util::getValue_or_default; + /** + * Helper for building a std::map with + * Command* as keys. Defines the order + * by the address of the Command's + * implementation object. + */ + struct order_by_impl + { + bool + operator() (Command* pC1, Command* pC2) + { + return ( pC1 && pC2 && (*pC1 < *pC2)); + } + }; /** @@ -84,7 +99,7 @@ namespace control { { // using a hashtable to implement the index typedef unordered_map > CmdIndex; - typedef std::map< void*, Symbol> ReverseIndex; + typedef std::map ReverseIndex; CmdIndex index_; ReverseIndex ridx_; @@ -140,12 +155,8 @@ namespace control { queryIndex (Symbol cmdID) { Lock sync(this); - - if (contains (index_,cmdID)) - return index_[cmdID]; - else - return Command(); - } + return getValue_or_default (index_, cmdID, Command() ); + } //if not found /** search the command index for a definition @@ -155,14 +166,15 @@ namespace control { const char* findDefinition (Command const& cmdInstance) const { - UNIMPLEMENTED ("try to find a registration in the index for a given command instance"); - } +// Lock sync(this); ////////////////////////////////////TICKET #272 Lock should also be usable in const methods + return getValue_or_default (ridx_, &cmdInstance, 0 ); + } //used as Key size_t index_size() const { - UNIMPLEMENTED ("number of defs in the index"); + return index_.size(); } diff --git a/src/proc/control/command.cpp b/src/proc/control/command.cpp index 6902ea302..f70695c83 100644 --- a/src/proc/control/command.cpp +++ b/src/proc/control/command.cpp @@ -200,7 +200,7 @@ namespace control { - /** is this a valid command definition frame? especially + /** is this a valid command definition? especially.. * - the prototype command is initialised properly * - there is a command definition registered for our command ID * - and the registered command uses the same underlying command impl diff --git a/src/proc/control/command.hpp b/src/proc/control/command.hpp index d064daf1c..0c105d2a5 100644 --- a/src/proc/control/command.hpp +++ b/src/proc/control/command.hpp @@ -165,6 +165,7 @@ namespace control { operator string() const; friend bool operator== (Command const&, Command const&); + friend bool operator< (Command const&, Command const&); protected: @@ -212,6 +213,13 @@ namespace control { return ! (c1 == c2); } + /** allow for sets and associative containers */ + inline bool + operator< (Command const& c1, Command const& c2) + { + return ( c1 && c2 && (&c1.impl() < &c2.impl())); + } +