Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 1 | // Copyright 2014 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "buffet/commands/command_queue.h" |
| 6 | |
Alex Deymo | f6cbe32 | 2014-11-10 19:55:35 -0800 | [diff] [blame] | 7 | #include "buffet/commands/command_dispatch_interface.h" |
| 8 | |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 9 | namespace buffet { |
| 10 | |
Anton Muhin | 5191e81 | 2014-10-30 17:49:48 +0400 | [diff] [blame] | 11 | void CommandQueue::Add(std::unique_ptr<CommandInstance> instance) { |
| 12 | std::string id = instance->GetID(); |
| 13 | LOG_IF(FATAL, id.empty()) << "Command has no ID"; |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 14 | instance->SetCommandQueue(this); |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 15 | auto pair = map_.insert(std::make_pair(id, std::move(instance))); |
| 16 | LOG_IF(FATAL, !pair.second) << "Command with ID '" << id |
| 17 | << "' is already in the queue"; |
Alex Vakulenko | 515b42b | 2014-08-07 15:46:31 -0700 | [diff] [blame] | 18 | if (dispatch_interface_) |
Alex Vakulenko | fedc487 | 2014-08-20 12:38:43 -0700 | [diff] [blame] | 19 | dispatch_interface_->OnCommandAdded(pair.first->second.get()); |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 20 | } |
| 21 | |
Alex Vakulenko | fedc487 | 2014-08-20 12:38:43 -0700 | [diff] [blame] | 22 | std::unique_ptr<CommandInstance> CommandQueue::Remove( |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 23 | const std::string& id) { |
Alex Vakulenko | fedc487 | 2014-08-20 12:38:43 -0700 | [diff] [blame] | 24 | std::unique_ptr<CommandInstance> instance; |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 25 | auto p = map_.find(id); |
| 26 | if (p != map_.end()) { |
| 27 | instance = std::move(p->second); |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 28 | instance->SetCommandQueue(nullptr); |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 29 | map_.erase(p); |
Alex Vakulenko | 515b42b | 2014-08-07 15:46:31 -0700 | [diff] [blame] | 30 | if (dispatch_interface_) |
Alex Vakulenko | fedc487 | 2014-08-20 12:38:43 -0700 | [diff] [blame] | 31 | dispatch_interface_->OnCommandRemoved(instance.get()); |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 32 | } |
| 33 | return instance; |
| 34 | } |
| 35 | |
Alex Vakulenko | fedc487 | 2014-08-20 12:38:43 -0700 | [diff] [blame] | 36 | CommandInstance* CommandQueue::Find(const std::string& id) const { |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 37 | auto p = map_.find(id); |
| 38 | return (p != map_.end()) ? p->second.get() : nullptr; |
| 39 | } |
| 40 | |
| 41 | } // namespace buffet |