• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #ifndef PANDA_VERIFIER_DEBUG_OPTIONS_METHOD_OPTIONS_CONFIG_H_
17 #define PANDA_VERIFIER_DEBUG_OPTIONS_METHOD_OPTIONS_CONFIG_H_
18 
19 #include "method_options.h"
20 
21 #include <regex>
22 
23 namespace panda::verifier {
24 
25 class MethodOptionsConfig {
26 public:
NewOptions(const PandaString & name)27     MethodOptions &NewOptions(const PandaString &name)
28     {
29         return Config.emplace(name, name).first->second;
30     }
GetOptions(const PandaString & name)31     const MethodOptions &GetOptions(const PandaString &name) const
32     {
33         ASSERT(IsOptionsPresent(name));
34         return Config.at(name);
35     }
IsOptionsPresent(const PandaString & name)36     bool IsOptionsPresent(const PandaString &name) const
37     {
38         return Config.count(name) > 0;
39     }
40     const MethodOptions &operator[](const PandaString &method_name) const
41     {
42         for (const auto &g : MethodGroups) {
43             const auto &regex = g.first;
44             if (std::regex_match(method_name, regex)) {
45                 return g.second;
46             }
47         }
48         return GetOptions("default");
49     }
AddOptionsForGroup(const PandaString & group_regex,const PandaString & options_name)50     bool AddOptionsForGroup(const PandaString &group_regex, const PandaString &options_name)
51     {
52         if (!IsOptionsPresent(options_name)) {
53             return false;
54         }
55         MethodGroups.emplace_back(
56             std::regex {group_regex, std::regex_constants::basic | std::regex_constants::optimize |
57                                          std::regex_constants::nosubs | std::regex_constants::icase},
58             GetOptions(options_name));
59         return true;
60     }
61 
62 private:
63     PandaUnorderedMap<PandaString, MethodOptions> Config;
64     PandaVector<std::pair<std::regex, const MethodOptions &>> MethodGroups;
65 };
66 
67 }  // namespace panda::verifier
68 
69 #endif  //! PANDA_VERIFIER_DEBUG_OPTIONS_METHOD_OPTIONS_CONFIG_H_
70