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