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 | #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 | |
| 16 | namespace buffet { |
| 17 | |
Alex Vakulenko | 515b42b | 2014-08-07 15:46:31 -0700 | [diff] [blame] | 18 | class CommandDispachInterface; |
| 19 | |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 20 | class CommandQueue final { |
| 21 | public: |
| 22 | CommandQueue() = default; |
| 23 | |
Alex Vakulenko | 515b42b | 2014-08-07 15:46:31 -0700 | [diff] [blame] | 24 | // 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 Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 31 | // 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 Vakulenko | 8dc69af | 2014-08-07 10:29:42 -0700 | [diff] [blame] | 38 | // 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 Vakulenko | fedc487 | 2014-08-20 12:38:43 -0700 | [diff] [blame] | 42 | std::string Add(std::unique_ptr<CommandInstance> instance); |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 43 | |
| 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 Vakulenko | fedc487 | 2014-08-20 12:38:43 -0700 | [diff] [blame] | 47 | std::unique_ptr<CommandInstance> Remove(const std::string& id); |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 48 | |
| 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 Vakulenko | fedc487 | 2014-08-20 12:38:43 -0700 | [diff] [blame] | 52 | CommandInstance* Find(const std::string& id) const; |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 53 | |
| 54 | private: |
| 55 | // ID-to-CommandInstance map. |
Alex Vakulenko | fedc487 | 2014-08-20 12:38:43 -0700 | [diff] [blame] | 56 | std::map<std::string, std::unique_ptr<CommandInstance>> map_; |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 57 | // Counter for generating unique command IDs. |
| 58 | int next_id_ = 0; |
Alex Vakulenko | 515b42b | 2014-08-07 15:46:31 -0700 | [diff] [blame] | 59 | // Callback interface for command dispatch, if provided. |
| 60 | CommandDispachInterface* dispatch_interface_ = nullptr; |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 61 | |
| 62 | DISALLOW_COPY_AND_ASSIGN(CommandQueue); |
| 63 | }; |
| 64 | |
| 65 | } // namespace buffet |
| 66 | |
| 67 | #endif // BUFFET_COMMANDS_COMMAND_QUEUE_H_ |