SettingsCommandDispatcher.hpp
1 #pragma once
2 #include <score/command/SettingsCommand.hpp>
3 #include <score/plugins/StringFactoryKey.hpp>
4 #include <score/tools/std/HashMap.hpp>
5 
6 #include <memory>
7 namespace score
8 {
10 {
11 public:
12  template <typename TheCommand, typename... Args>
13  void submit(Args&&... args)
14  {
15  static_assert(!TheCommand::is_deferred, "Don't use a deferred command");
16  auto it = commands.find(TheCommand::static_key());
17  if(it != commands.end())
18  {
19  static_cast<TheCommand&>(*it->second).update(std::forward<Args>(args)...);
20  it->second->redo();
21  }
22  else
23  {
24  auto cmd = std::make_unique<TheCommand>(std::forward<Args>(args)...);
25  cmd->redo();
26  commands.insert(std::make_pair(TheCommand::static_key(), std::move(cmd)));
27  }
28  }
29 
30  template <typename TheCommand, typename... Args>
31  void submitDeferredCommand(Args&&... args)
32  {
33  static_assert(TheCommand::is_deferred, "Use a deferred command");
34  auto it = deferred.find(TheCommand::static_key());
35  if(it != deferred.end())
36  {
37  static_cast<TheCommand&>(*it->second).update(std::forward<Args>(args)...);
38  }
39  else
40  {
41  auto cmd = std::make_unique<TheCommand>(std::forward<Args>(args)...);
42  deferred.insert(std::make_pair(TheCommand::static_key(), std::move(cmd)));
43  }
44  }
45 
46  void commit()
47  {
48  commands.clear();
49  for(auto& it : deferred)
50  {
51  it.second->redo();
52  }
53  deferred.clear();
54  }
55 
56  void rollback()
57  {
58  for(auto& it : commands)
59  {
60  it.second->undo();
61  }
62  commands.clear();
63  deferred.clear();
64  }
65 
66 private:
67  score::hash_map<CommandKey, std::unique_ptr<score::SettingsCommandBase>> commands;
68  score::hash_map<CommandKey, std::unique_ptr<score::SettingsCommandBase>> deferred;
69 };
70 }
Definition: SettingsCommandDispatcher.hpp:10
Base toolkit upon which the software is built.
Definition: Application.cpp:90