blob: d660a1de23a2b65f98c25e9f1ffe24fa373127de [file] [log] [blame]
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -07001// 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 Deymof6cbe322014-11-10 19:55:35 -08007#include "buffet/commands/command_dispatch_interface.h"
8
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -07009namespace buffet {
10
Anton Muhin5191e812014-10-30 17:49:48 +040011void CommandQueue::Add(std::unique_ptr<CommandInstance> instance) {
12 std::string id = instance->GetID();
13 LOG_IF(FATAL, id.empty()) << "Command has no ID";
Alex Vakulenkof6b38712014-09-03 16:23:38 -070014 instance->SetCommandQueue(this);
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070015 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 Vakulenko515b42b2014-08-07 15:46:31 -070018 if (dispatch_interface_)
Alex Vakulenkofedc4872014-08-20 12:38:43 -070019 dispatch_interface_->OnCommandAdded(pair.first->second.get());
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070020}
21
Alex Vakulenkofedc4872014-08-20 12:38:43 -070022std::unique_ptr<CommandInstance> CommandQueue::Remove(
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070023 const std::string& id) {
Alex Vakulenkofedc4872014-08-20 12:38:43 -070024 std::unique_ptr<CommandInstance> instance;
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070025 auto p = map_.find(id);
26 if (p != map_.end()) {
27 instance = std::move(p->second);
Alex Vakulenkof6b38712014-09-03 16:23:38 -070028 instance->SetCommandQueue(nullptr);
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070029 map_.erase(p);
Alex Vakulenko515b42b2014-08-07 15:46:31 -070030 if (dispatch_interface_)
Alex Vakulenkofedc4872014-08-20 12:38:43 -070031 dispatch_interface_->OnCommandRemoved(instance.get());
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070032 }
33 return instance;
34}
35
Alex Vakulenkofedc4872014-08-20 12:38:43 -070036CommandInstance* CommandQueue::Find(const std::string& id) const {
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070037 auto p = map_.find(id);
38 return (p != map_.end()) ? p->second.get() : nullptr;
39}
40
41} // namespace buffet