My Project
commands_iterable.cpp
1 /*
2  * uuid-console - Microcontroller console shell
3  * Copyright 2022 Simon Arlott
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include <uuid/console.h>
20 
21 #include <memory>
22 #include <utility>
23 
24 namespace uuid {
25 
26 namespace console {
27 
29  auto range = commands_.equal_range(shell.context());
30  return AvailableCommands(shell, range.first, range.second);
31 }
32 
33 Commands::AvailableCommand::AvailableCommand(const Command &command) : command_(command) {
34  name_.reserve(command_.name_.size());
35  for (auto flash_name : command_.name_) {
36  name_.push_back(std::move(read_flash_string(flash_name)));
37  }
38 
39  arguments_.reserve(command_.arguments_.size());
40  for (auto flash_argument : command_.arguments_) {
41  arguments_.push_back(std::move(read_flash_string(flash_argument)));
42  }
43 }
44 
46  const command_iterator &begin, const command_iterator &end)
47  : shell_(shell), begin_(begin), end_(end) {
48  // Skip over unavailable commands at the beginning
49  while (begin_ != end_ && !shell_.has_flags(begin_->second.flags_, begin_->second.not_flags_)) {
50  ++begin_;
51  }
52 }
53 
55  return AvailableCommands::const_iterator(shell_, begin_, begin_, end_);
56 }
57 
59  return AvailableCommands::const_iterator(shell_, begin_, end_, end_);
60 }
61 
63  const command_iterator &begin, const command_iterator &command,
64  const command_iterator &end)
65  : shell_(std::move(shell)), begin_(begin), command_(command), end_(end) {
66  update();
67 }
68 
70  if (command_ != end_) {
71  available_command_ = std::make_shared<AvailableCommand>(command_->second);
72  } else {
73  available_command_.reset();
74  }
75 }
76 
78  do {
79  ++command_;
80  } while (command_ != end_ && !shell_.has_flags(command_->second.flags_, command_->second.not_flags_));
81 
82  update();
83  return *this;
84 }
85 
87  do {
88  --command_;
89  } while (command_ != begin_ && !shell_.has_flags(command_->second.flags_, command_->second.not_flags_));
90 
91  update();
92  return *this;
93 }
94 
95 } // namespace console
96 
97 } // namespace uuid
uuid::console::Commands::AvailableCommands::const_iterator::update
void update()
Update the available command from the current command iterator.
Definition: commands_iterable.cpp:69
uuid::console::Shell
Base class for a command shell.
Definition: console.h:772
uuid::console::Commands::AvailableCommands::const_iterator::operator--
const_iterator & operator--()
Pre-decrement the current iterator to the previous available command.
Definition: commands_iterable.cpp:86
uuid::console::Commands::AvailableCommands::command_iterator
std::multimap< unsigned int, Command >::const_iterator command_iterator
Definition: console.h:212
uuid::console::Commands::Command::name_
const flash_string_vector name_
Definition: console.h:707
uuid::console::Commands::AvailableCommand::command_
const Commands::Command & command_
Definition: console.h:197
uuid::console::Commands::AvailableCommands::const_iterator::const_iterator
const_iterator(const Shell &shell, const command_iterator &begin, const command_iterator &command, const command_iterator &end)
Create an iterator describing an available command in a shell with the current flags.
Definition: commands_iterable.cpp:62
uuid::console::Commands::AvailableCommand::AvailableCommand
AvailableCommand(const Command &command)
Construct a new command that is available for execution on a Shell.
Definition: commands_iterable.cpp:33
uuid::console::Commands::AvailableCommands
Available commands in the shell's context with the current flags.
Definition: console.h:210
uuid::console::Shell::context
unsigned int context() const
Get the context at the top of the stack.
Definition: console.h:979
uuid::console::Commands::AvailableCommands::cbegin
const_iterator cbegin() const
Returns an iterator to the first command.
Definition: commands_iterable.cpp:54
uuid::console::Commands::AvailableCommands::AvailableCommands
AvailableCommands(const Shell &shell, const command_iterator &begin, const command_iterator &end)
Create an iterable object describing available commands in a shell with the current flags.
Definition: commands_iterable.cpp:45
uuid::console::Commands::AvailableCommands::cend
const_iterator cend() const
Returns an iterator to the element following the last command.
Definition: commands_iterable.cpp:58
uuid::console::Commands::Command
Command for execution on a Shell.
Definition: console.h:662
uuid::console::Commands::Command::arguments_
const flash_string_vector arguments_
Definition: console.h:708
uuid::console::Shell::has_flags
bool has_flags(unsigned int flags, unsigned int not_flags=0) const
Check if the current flags include all of the specified flags and none of the specified not_flags.
Definition: console.h:1031
uuid::read_flash_string
std::string read_flash_string(const __FlashStringHelper *flash_str)
Read a string from flash and convert it to a std::string.
Definition: read_flash_string.cpp:27
uuid::console::Commands::AvailableCommands::end_
command_iterator end_
Definition: console.h:374
uuid
Common utilities.
Definition: get_uptime_ms.cpp:28
uuid::console::Commands::AvailableCommands::shell_
const Shell & shell_
Definition: console.h:372
uuid::console::Commands::AvailableCommands::const_iterator
Iterator of available commands for execution on a Shell.
Definition: console.h:219
uuid::console::Commands::AvailableCommand::name_
std::vector< std::string > name_
Definition: console.h:198
uuid::console::Commands::AvailableCommand::arguments_
std::vector< std::string > arguments_
Definition: console.h:199
uuid::console::Commands::available_commands
AvailableCommands available_commands(const Shell &shell) const
Get the available commands in the current context and with the current flags.
Definition: commands_iterable.cpp:28
uuid::console::Commands::AvailableCommands::begin_
command_iterator begin_
Definition: console.h:373
uuid::console::Commands::commands_
std::multimap< unsigned int, Command > commands_
Definition: console.h:762
uuid::console::Commands::AvailableCommands::const_iterator::operator++
const_iterator & operator++()
Pre-increment the current iterator to the next available command.
Definition: commands_iterable.cpp:77