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 | |
Alex Vakulenko | 132617a | 2014-09-04 08:59:43 -0700 | [diff] [blame] | 12 | #include <base/macros.h> |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 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 |
Anton Muhin | 5191e81 | 2014-10-30 17:49:48 +0400 | [diff] [blame] | 38 | // 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 Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 41 | |
| 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 Vakulenko | fedc487 | 2014-08-20 12:38:43 -0700 | [diff] [blame] | 45 | std::unique_ptr<CommandInstance> Remove(const std::string& id); |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 46 | |
| 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 Vakulenko | fedc487 | 2014-08-20 12:38:43 -0700 | [diff] [blame] | 50 | CommandInstance* Find(const std::string& id) const; |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 51 | |
| 52 | private: |
| 53 | // ID-to-CommandInstance map. |
Alex Vakulenko | fedc487 | 2014-08-20 12:38:43 -0700 | [diff] [blame] | 54 | std::map<std::string, std::unique_ptr<CommandInstance>> map_; |
Alex Vakulenko | 515b42b | 2014-08-07 15:46:31 -0700 | [diff] [blame] | 55 | // Callback interface for command dispatch, if provided. |
| 56 | CommandDispachInterface* dispatch_interface_ = nullptr; |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 57 | |
| 58 | DISALLOW_COPY_AND_ASSIGN(CommandQueue); |
| 59 | }; |
| 60 | |
| 61 | } // namespace buffet |
| 62 | |
| 63 | #endif // BUFFET_COMMANDS_COMMAND_QUEUE_H_ |