Lumiera  0.pre.03
»edit your freedom«
command-registry.hpp
Go to the documentation of this file.
1 /*
2  COMMAND-REGISTRY.hpp - steam-Command object registration and storage management
3 
4  Copyright (C)
5  2009, Hermann Vosseler <Ichthyostega@web.de>
6 
7   **Lumiera** is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by the
9   Free Software Foundation; either version 2 of the License, or (at your
10   option) any later version. See the file COPYING for further details.
11 
12 */
13 
14 
49 #ifndef CONTROL_COMMAND_REGISTRY_H
50 #define CONTROL_COMMAND_REGISTRY_H
51 
52 #include "lib/error.hpp"
53 #include "lib/depend.hpp"
54 #include "lib/sync.hpp"
55 #include "include/logging.h"
56 #include "lib/nocopy.hpp"
57 #include "lib/util.hpp"
58 
63 
64 #include <boost/functional/hash.hpp>
65 #include <unordered_map>
66 #include <memory>
67 #include <string>
68 #include <map>
69 
70 
71 
72 namespace steam {
73 namespace control {
74 
75  using boost::hash;
76  using std::shared_ptr;
77  using std::unordered_map;
80  using util::contains;
81  using std::string;
82  using std::map;
83 
84 
92  {
93  bool
94  operator() (const Command *pC1, const Command *pC2) const
95  {
96  return (!pC1 && pC2)
97  || ( pC1 && pC2 && (*pC1 < *pC2));
98  }
99  };
100 
101 
110  : public lib::Sync<>
112  {
113  // using a hashtable to implement the index
114  typedef unordered_map<Symbol, Command, hash<Symbol>> CmdIndex;
115  typedef map< const Command*, Symbol, order_by_impl> ReverseIndex;
116 
117  TypedAllocationManager allocator_;
118  CmdIndex index_;
119  ReverseIndex ridx_;
120 
121 
122  public:
124 
125 
126  ~CommandRegistry()
127  {
128  if (0 < index_size())
129  TRACE (command, "Shutting down Command system...");
131  ridx_.clear();
132  index_.clear();
133  }
134 
135 
136 
142  void
143  track (Symbol cmdID, Command const& commandHandle)
144  {
145  Lock sync{this};
146 
147  REQUIRE (commandHandle);
148  if (contains (index_,cmdID) || contains(ridx_, &commandHandle))
149  commandHandle.duplicate_detected(cmdID);
150 
151  Command& indexSlot = index_[cmdID];
152  indexSlot = commandHandle;
153  ridx_[&indexSlot] = cmdID;
154 
155  ENSURE (contains(ridx_, &indexSlot));
156  ENSURE (contains(index_, cmdID));
157  }
158 
159 
164  bool
165  remove (Symbol cmdID)
166  {
167  Lock sync{this};
168 
169  bool actually_remove = contains (index_,cmdID);
170  if (actually_remove)
171  {
172  ridx_.erase(& index_[cmdID]);
173  index_.erase(cmdID);
174  }
175  ENSURE (!contains (index_,cmdID));
176  return actually_remove;
177  }
178 
179 
188  Command
190  {
191  Lock sync{this};
192  return getValue_or_default (index_, cmdID, Command() );
193  } //if not found
194 
195 
200  Symbol
201  findDefinition (Command const& cmdInstance) const
202  {
203  Lock sync{this};
204  return getValue_or_default (ridx_, &cmdInstance, Symbol::BOTTOM );
205  } //used as Key
206 
207 
208  size_t
209  index_size() const
210  {
211  return index_.size();
212  }
213 
214 
215  size_t
216  instance_count() const
217  {
218  return allocator_.numSlots<CommandImpl>();
219  }
220 
221 
227  template< typename SIG_OPER
228  , typename SIG_CAPT
229  , typename SIG_UNDO
230  >
232  newCommandImpl (function<SIG_OPER>& operFunctor
233  ,function<SIG_CAPT>& captFunctor
234  ,function<SIG_UNDO>& undoFunctor)
235  {
236 
237  // derive the storage type necessary
238  // to hold the command arguments and UNDO memento
239  typedef typename UndoSignature<SIG_CAPT>::Memento Mem;
241 
242  shared_ptr<Arguments> pArg (allocator_.create<Arguments>());
243 
244  return allocator_.create<CommandImpl> (pArg, operFunctor,captFunctor,undoFunctor);
245  }
246 
247 
248 
249 
264  createCloneImpl (CommandImpl const& refObject);
265 
266 
267  };
268 
269 
270 }} // namespace steam::control
271 #endif
Facility for monitor object based locking.
Definition: sync.hpp:209
Helper for building a std::map with Command* as keys.
Abstract foundation for building custom allocation managers.
shared_ptr< CommandImpl > newCommandImpl(function< SIG_OPER > &operFunctor, function< SIG_CAPT > &captFunctor, function< SIG_UNDO > &undoFunctor)
set up a new command implementation frame
Any copy and copy construction prohibited.
Definition: nocopy.hpp:37
This header is for including and configuring NoBug.
Registry managing command implementation objects (Singleton).
Steam-Layer implementation namespace root.
Access point to singletons and other kinds of dependencies designated by type.
Definition: depend.hpp:280
Symbol findDefinition(Command const &cmdInstance) const
search the command index for a definition
Object Monitor based synchronisation.
Token or Atom with distinct identity.
Definition: symbol.hpp:117
Mix-Ins to allow or prohibit various degrees of copying and cloning.
Command queryIndex(Symbol cmdID)
query the command index by ID
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
Steam-Layer command frontend.
Singleton services and Dependency Injection.
Foundation for a custom allocation manager, tracking the created objects by smart-ptrs.
MAP::mapped_type getValue_or_default(MAP &map, typename MAP::key_type const &key, typename MAP::mapped_type defaultVal)
fetch value from a Map, or return a default if not found
Definition: util.hpp:275
Lumiera error handling (C++ interface).
Handle object representing a single Command instance to be used by client code.
Definition: command.hpp:115
Steam-Layer Command implementation.
void track(Symbol cmdID, Command const &commandHandle)
register a command (Frontend) under the given ID.
Metaprogramming helpers for deriving the precise function signatures necessary to implement a given c...
This is "the" top level CmdClosure implementation.
bool contains(SEQ const &cont, typename SEQ::const_reference val)
shortcut for brute-force containment test in any sequential container
Definition: util.hpp:255
static lib::Depend< CommandRegistry > instance
storage for the singleton factory used to access CommandRegistry
A passive container record holding the actual command arguments & UNDO state.