blob: 4e2706b8459cb2ae0ac9474ac9b4547bee99ae19 [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
12#include <base/basictypes.h>
13
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
Alex Vakulenko8dc69af2014-08-07 10:29:42 -070038 // ID that identifies that command instance in this queue. This identifier
39 // has no relation to any GCD command identifiers or anything else. Just a
40 // unique key in this queue class.
41 // The ID string of the added command is returned by this method.
Alex Vakulenkofedc4872014-08-20 12:38:43 -070042 std::string Add(std::unique_ptr<CommandInstance> instance);
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070043
44 // Removes a command identified by |id| from the queue. Returns a unique
45 // pointer to the command instance if removed successfully, or an empty
46 // unique_ptr if the command with the given ID doesn't exist in the queue.
Alex Vakulenkofedc4872014-08-20 12:38:43 -070047 std::unique_ptr<CommandInstance> Remove(const std::string& id);
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070048
49 // Finds a command instance in the queue by the instance |id|. Returns
50 // nullptr if the command with the given |id| is not found. The returned
51 // pointer should not be persisted for a long period of time.
Alex Vakulenkofedc4872014-08-20 12:38:43 -070052 CommandInstance* Find(const std::string& id) const;
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070053
54 private:
55 // ID-to-CommandInstance map.
Alex Vakulenkofedc4872014-08-20 12:38:43 -070056 std::map<std::string, std::unique_ptr<CommandInstance>> map_;
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070057 // Counter for generating unique command IDs.
58 int next_id_ = 0;
Alex Vakulenko515b42b2014-08-07 15:46:31 -070059 // Callback interface for command dispatch, if provided.
60 CommandDispachInterface* dispatch_interface_ = nullptr;
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070061
62 DISALLOW_COPY_AND_ASSIGN(CommandQueue);
63};
64
65} // namespace buffet
66
67#endif // BUFFET_COMMANDS_COMMAND_QUEUE_H_