WIP using the reverse index to find out the Command-ID

this includes using a Command* as key within a std::map
This commit is contained in:
Fischlurch 2009-09-26 04:19:57 +02:00
parent 4c9af94e1d
commit 7fccecacce
4 changed files with 45 additions and 12 deletions

View file

@ -99,7 +99,7 @@ namespace util {
/** shortcut for containment test on a map */
template <typename MAP>
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 <typename MAP>
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 <typename SEQ>

View file

@ -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<Symbol, Command, hash<Symbol> > CmdIndex;
typedef std::map< void*, Symbol> ReverseIndex;
typedef std::map<Command*, Symbol, order_by_impl> 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();
}

View file

@ -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

View file

@ -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()));
}