CommandRegistry implementation (...)

This commit is contained in:
Fischlurch 2009-09-25 16:36:56 +02:00
parent 109cc4701c
commit 8dc434a141
3 changed files with 40 additions and 23 deletions

View file

@ -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 <boost/functional/hash.hpp>
#include <boost/noncopyable.hpp>
#include <tr1/unordered_map>
#include <tr1/memory>
@ -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<Symbol, Command> CmdIndex;
typedef std::map<void*, Symbol> ReverseIndex;
typedef unordered_map<Symbol, Command, hash<Symbol> > 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();
}

View file

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

View file

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