1/* 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15// Autogenerated file -- DO NOT EDIT! 16 17#ifndef PANDA_<%= Common::module.name.upcase %>_OPTIONS_GEN_H_ 18#define PANDA_<%= Common::module.name.upcase %>_OPTIONS_GEN_H_ 19 20#include "utils/pandargs.h" 21 22#include <optional> 23#include <string> 24#include <unordered_set> 25#include <vector> 26 27// NOLINTBEGIN(readability-identifier-naming) 28 29namespace <%= Common::module.namespace %> { 30 31class Options { 32public: 33 class Error { 34 public: 35 explicit Error(std::string msg) : msg_(std::move(msg)) {} 36 37 std::string GetMessage() { 38 return msg_; 39 } 40 private: 41 std::string msg_; 42 }; 43 44 explicit Options(const std::string &exe_path) : exe_dir_(GetExeDir(exe_path)) {} 45 46 void AddOptions(panda::PandArgParser *parser) { 47 parser->Add(&version); 48% Common::options.each do |op| 49% next if op.sub_option? 50% next if op.deprecated? 51 parser->Add(&<%= op.field_name %>); 52% end 53 } 54 55 bool IsVersion() const { 56 return version.GetValue(); 57 } 58 59 void SetVersion(bool value) { 60 version.SetValue(value); 61 } 62 63 bool WasSetVersion() { 64 return version.WasSet(); 65 } 66 67% Common::options.each do |op| 68% if !op.lang_specific? && !op.has_lang_suboptions? 69 <%= op.type %> <%= op.getter_name %>([[maybe_unused]] std::string_view lang = "") const { 70% if op.deprecated? 71 std::cerr << "WARNING: Option '<%= op.name %>' is deprecated and should not be used\n"; 72% end 73 return <%= op.field_name %>.GetValue(); 74 } 75 76 void <%= op.setter_name %>(<%= op.type %> value) { 77% if op.deprecated? 78 std::cerr << "WARNING: Option '<%= op.name %>' is deprecated and should not be used\n"; 79% end 80 <%= op.field_name %>.SetValue(<%= op.type == 'std::string' || op.type == 'arg_list_t' ? 'std::move(value)' : 'value' %>); 81 } 82 83 bool WasSet<%= op.camel_name %>([[maybe_unused]] std::string_view lang = "") const { 84% if op.deprecated? 85 std::cerr << "WARNING: Option '<%= op.name %>' is deprecated and should not be used\n"; 86% end 87 return <%= op.field_name %>.WasSet(); 88 } 89% end 90% if op.has_lang_suboptions? 91 <%= op.type %> <%= op.getter_name %>(std::string_view lang) const { 92% if op.deprecated? 93 std::cerr << "WARNING: Option '<%= op.name %>' is deprecated and should not be used\n"; 94% end 95% op.lang.each do |lang| 96 if (lang == "<%= "#{lang}" %>") { 97 return WasSet<%= op.lang_camel_name(lang) %>() ? <%= op.lang_field_name(lang) %>.GetValue() : <%= op.field_name %>.GetValue(); 98 } 99% end 100 return <%= op.field_name %>.GetValue(); 101 } 102 103 void <%= op.setter_name %>(<%= op.type %> value) { 104% if op.deprecated? 105 std::cerr << "WARNING: Option '<%= op.name %>' is deprecated and should not be used\n"; 106% end 107 <%= op.field_name %>.SetValue(<%= op.type == 'std::string' || op.type == 'arg_list_t' ? 'std::move(value)' : 'value' %>); 108 } 109 110 bool WasSet<%= op.camel_name %>(std::string_view lang) const { 111% if op.deprecated? 112 std::cerr << "WARNING: Option '<%= op.name %>' is deprecated and should not be used\n"; 113% end 114% op.lang.each do |lang| 115 if (lang == "<%= "#{lang}" %>") { 116 return <%= op.lang_field_name(lang) %>.WasSet(); 117 } 118% end 119 return <%= op.field_name %>.WasSet(); 120 } 121% end 122% end 123// NOLINTNEXTLINE(readability-function-size) 124 std::optional<Error> Validate() const { 125% Common::options.each do |op| 126% next unless defined? op.possible_values 127% is_string = op.type == 'std::string' || op.type == 'arg_list_t' 128% possible_values = op.possible_values.map { |e| is_string ? Common::to_raw(e) : e }.join(', ') 129 { 130 std::unordered_set<<%= is_string ? "std::string" : op.type %>> possible_values{<%= possible_values %>}; 131% if op.type != 'arg_list_t' 132 std::vector<<%= op.type %>> values{<%= op.field_name %>.GetValue()}; 133% else 134 const auto &values = <%= op.field_name %>.GetValue(); 135% end 136 for (const auto &value : values) { 137 if (possible_values.find(value) == possible_values.cend()) { 138 return Error("argument --<%= op.name %>: invalid value: '" + <%= is_string ? "value" : "std::to_string(value)" %> + \ 139 R"('. Possible values: <%= op.possible_values %>)"); 140 } 141 } 142 } 143% end 144 return {}; 145 } 146 147private: 148% Common::options.each do |op| 149% if op.lang_specific? 150 bool WasSet<%= op.camel_name %>() const { 151% if op.deprecated? 152 std::cerr << "WARNING: Option '<%= op.name %>' is deprecated and should not be used\n"; 153% end 154 return <%= op.field_name %>.WasSet(); 155 } 156% end 157% end 158 159 static std::string GetExeDir(const std::string &exe_path) { 160 auto pos = exe_path.find_last_of('/'); 161 return exe_path.substr(0, pos); 162 } 163 164% Common::options.each do |op| 165% next unless op.need_default_constant 166 static constexpr <%= op.type %> <%= op.default_constant_name %> = <%= op.default %>; 167 168% end 169 std::string exe_dir_; 170 panda::PandArg<bool> version{"version", false, R"(Ark version, file format version and minimum supported file format version)"}; 171% Common::options.each do |op| 172% if defined? op.delimiter 173 panda::PandArg<<%= op.type %>> <%= op.field_name %>{"<%= op.name %>", <%= op.default_value %>, <%= op.full_description %>, "<%= op.delimiter %>"}; 174% elsif defined? op.range 175% min, max = op.range.match('(\d+)\D*(\d+)').captures; 176% if op.default.to_i > max.to_i || op.default.to_i < min.to_i 177% abort "FAILED: Default value of argument " + op.name + " has out of range default parameter" 178% end 179 panda::PandArg<<%= op.type %>> <%= op.field_name %>{"<%= op.name %>", <%= op.default_value %>, <%= op.full_description %>, <%= min.to_i %>, <%= max.to_i %>}; 180% elsif op.has_sub_options 181 panda::PandArgCompound <%= op.field_name %>{"<%= op.name %>", <%= op.full_description %>, {<%= op.sub_options.map{|o| '&' + o.field_name}.join(', ') %>}}; 182% else 183 panda::PandArg<<%= op.type %>> <%= op.field_name %>{"<%= op.name %>", <%= op.default_value %>, <%= op.full_description %>}; 184% end 185% end 186}; 187 188} // namespace <%= Common::module.namespace %> 189 190// NOLINTEND(readability-identifier-naming) 191#endif // PANDA_<%= Common::module.name.upcase %>_OPTIONS_GEN_H_ 192