blob: 6d6547d7620fdf8e0451a69667c59df9ce27c88e [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#ifndef BUFFET_COMMANDS_COMMAND_QUEUE_H_
6#define BUFFET_COMMANDS_COMMAND_QUEUE_H_
7
8#include <map>
9#include <memory>
10#include <string>
11
Alex Vakulenko132617a2014-09-04 08:59:43 -070012#include <base/macros.h>
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070013
14#include "buffet/commands/command_instance.h"
15
16namespace buffet {
17
Alex Vakulenko515b42b2014-08-07 15:46:31 -070018class CommandDispachInterface;
19
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070020class CommandQueue final {
21 public:
22 CommandQueue() = default;
23
Alex Vakulenko515b42b2014-08-07 15:46:31 -070024 // Sets a command dispatch notifications for changes in command queue.
25 // |dispatch_interface| must outlive the CommandQueue object instance
26 // or be nullptr.
27 void SetCommandDispachInterface(CommandDispachInterface* dispatch_interface) {
28 dispatch_interface_ = dispatch_interface;
29 }
30
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070031 // Checks if the command queue is empty.
32 bool IsEmpty() const { return map_.empty(); }
33
34 // Returns the number of commands in the queue.
35 size_t GetCount() const { return map_.size(); }
36
37 // Adds a new command to the queue. Each command in the queue has a unique
Anton Muhin5191e812014-10-30 17:49:48 +040038 // ID that identifies that command instance in this queue.
39 // One shouldn't attempt to add a command with the same ID.
40 void Add(std::unique_ptr<CommandInstance> instance);
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070041
42 // Removes a command identified by |id| from the queue. Returns a unique
43 // pointer to the command instance if removed successfully, or an empty
44 // unique_ptr if the command with the given ID doesn't exist in the queue.
Alex Vakulenkofedc4872014-08-20 12:38:43 -070045 std::unique_ptr<CommandInstance> Remove(const std::string& id);
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070046
47 // Finds a command instance in the queue by the instance |id|. Returns
48 // nullptr if the command with the given |id| is not found. The returned
49 // pointer should not be persisted for a long period of time.
Alex Vakulenkofedc4872014-08-20 12:38:43 -070050 CommandInstance* Find(const std::string& id) const;
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070051
52 private:
53 // ID-to-CommandInstance map.
Alex Vakulenkofedc4872014-08-20 12:38:43 -070054 std::map<std::string, std::unique_ptr<CommandInstance>> map_;
Alex Vakulenko515b42b2014-08-07 15:46:31 -070055 // Callback interface for command dispatch, if provided.
56 CommandDispachInterface* dispatch_interface_ = nullptr;
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070057
58 DISALLOW_COPY_AND_ASSIGN(CommandQueue);
59};
60
61} // namespace buffet
62
63#endif // BUFFET_COMMANDS_COMMAND_QUEUE_H_