| Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef SIMPLE_PERF_COMMAND_H_ |
| 18 | #define SIMPLE_PERF_COMMAND_H_ |
| 19 | |
| Yabin Cui | f79f07e | 2015-06-01 11:21:37 -0700 | [diff] [blame] | 20 | #include <functional> |
| 21 | #include <memory> |
| Yabin Cui | acf04b2 | 2018-04-18 13:10:40 -0700 | [diff] [blame] | 22 | #include <limits> |
| Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 23 | #include <string> |
| 24 | #include <vector> |
| 25 | |
| Yabin Cui | acf04b2 | 2018-04-18 13:10:40 -0700 | [diff] [blame] | 26 | #include <android-base/logging.h> |
| Elliott Hughes | 66dd09e | 2015-12-04 14:00:57 -0800 | [diff] [blame] | 27 | #include <android-base/macros.h> |
| Yabin Cui | acf04b2 | 2018-04-18 13:10:40 -0700 | [diff] [blame] | 28 | #include <android-base/parseint.h> |
| Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 29 | |
| 30 | class Command { |
| 31 | public: |
| 32 | Command(const std::string& name, const std::string& short_help_string, |
| 33 | const std::string& long_help_string) |
| 34 | : name_(name), short_help_string_(short_help_string), long_help_string_(long_help_string) { |
| Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | virtual ~Command() { |
| 38 | } |
| 39 | |
| 40 | const std::string& Name() const { |
| 41 | return name_; |
| 42 | } |
| 43 | |
| 44 | const std::string& ShortHelpString() const { |
| 45 | return short_help_string_; |
| 46 | } |
| 47 | |
| 48 | const std::string LongHelpString() const { |
| 49 | return long_help_string_; |
| 50 | } |
| 51 | |
| 52 | virtual bool Run(const std::vector<std::string>& args) = 0; |
| 53 | |
| Yabin Cui | acf04b2 | 2018-04-18 13:10:40 -0700 | [diff] [blame] | 54 | template <typename T> |
| 55 | bool GetUintOption(const std::vector<std::string>& args, size_t* pi, T* value, uint64_t min = 0, |
| 56 | uint64_t max = std::numeric_limits<T>::max(), bool allow_suffixes = false) { |
| 57 | if (!NextArgumentOrError(args, pi)) { |
| 58 | return false; |
| 59 | } |
| 60 | uint64_t tmp_value; |
| 61 | if (!android::base::ParseUint(args[*pi], &tmp_value, max, allow_suffixes) || tmp_value < min) { |
| 62 | LOG(ERROR) << "Invalid argument for option " << args[*pi - 1] << ": " << args[*pi]; |
| 63 | return false; |
| 64 | } |
| 65 | *value = static_cast<T>(tmp_value); |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | bool GetDoubleOption(const std::vector<std::string>& args, size_t* pi, double* value, |
| 70 | double min = 0, double max = std::numeric_limits<double>::max()); |
| 71 | |
| Yabin Cui | f79f07e | 2015-06-01 11:21:37 -0700 | [diff] [blame] | 72 | protected: |
| 73 | bool NextArgumentOrError(const std::vector<std::string>& args, size_t* pi); |
| 74 | void ReportUnknownOption(const std::vector<std::string>& args, size_t i); |
| Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 75 | |
| 76 | private: |
| 77 | const std::string name_; |
| 78 | const std::string short_help_string_; |
| 79 | const std::string long_help_string_; |
| 80 | |
| Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 81 | DISALLOW_COPY_AND_ASSIGN(Command); |
| 82 | }; |
| 83 | |
| Yabin Cui | f79f07e | 2015-06-01 11:21:37 -0700 | [diff] [blame] | 84 | void RegisterCommand(const std::string& cmd_name, |
| Yabin Cui | 3e4c595 | 2016-07-26 15:03:27 -0700 | [diff] [blame] | 85 | const std::function<std::unique_ptr<Command>(void)>& callback); |
| Yabin Cui | f79f07e | 2015-06-01 11:21:37 -0700 | [diff] [blame] | 86 | void UnRegisterCommand(const std::string& cmd_name); |
| 87 | std::unique_ptr<Command> CreateCommandInstance(const std::string& cmd_name); |
| 88 | const std::vector<std::string> GetAllCommandNames(); |
| Yabin Cui | 616b3a0 | 2017-07-14 15:59:56 -0700 | [diff] [blame] | 89 | bool RunSimpleperfCmd(int argc, char** argv); |
| Yabin Cui | f79f07e | 2015-06-01 11:21:37 -0700 | [diff] [blame] | 90 | |
| Yabin Cui | 67d3abd | 2015-04-16 15:26:31 -0700 | [diff] [blame] | 91 | #endif // SIMPLE_PERF_COMMAND_H_ |