misc: Replace enable_if<>::type with enable_if_t<>.
[gem5.git] / src / base / match.cc
1 /*
2 * Copyright (c) 2019 The Regents of the University of California
3 * Copyright (c) 2004-2005 The Regents of The University of Michigan
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met: redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer;
10 * redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution;
13 * neither the name of the copyright holders nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #include "base/match.hh"
31
32 #include "base/str.hh"
33
34 using namespace std;
35
36 ObjectMatch::ObjectMatch()
37 {
38 }
39
40 ObjectMatch::ObjectMatch(const string &expr)
41 {
42 setExpression(expr);
43 }
44
45 void
46 ObjectMatch::add(const ObjectMatch &other)
47 {
48 tokens.insert(tokens.end(), other.tokens.begin(), other.tokens.end());
49 }
50
51 void
52 ObjectMatch::setExpression(const string &expr)
53 {
54 tokens.resize(1);
55 tokenize(tokens[0], expr, '.');
56 }
57
58 void
59 ObjectMatch::setExpression(const vector<string> &expr)
60 {
61 if (expr.empty()) {
62 tokens.resize(0);
63 } else {
64 tokens.resize(expr.size());
65 for (vector<string>::size_type i = 0; i < expr.size(); ++i)
66 tokenize(tokens[i], expr[i], '.');
67 }
68 }
69
70 /**
71 * @todo this should probably be changed to just use regular
72 * expression code
73 */
74 bool
75 ObjectMatch::domatch(const string &name) const
76 {
77 vector<string> name_tokens;
78 tokenize(name_tokens, name, '.');
79 int ntsize = name_tokens.size();
80
81 int num_expr = tokens.size();
82 for (int i = 0; i < num_expr; ++i) {
83 const vector<string> &token = tokens[i];
84 int jstop = token.size();
85
86 bool match = true;
87 for (int j = 0; j < jstop; ++j) {
88 if (j >= ntsize)
89 break;
90
91 const string &var = token[j];
92 if (var != "*" && var != name_tokens[j]) {
93 match = false;
94 break;
95 }
96 }
97
98 if (match)
99 return true;
100 }
101
102 return false;
103 }
104
105 std::vector<std::vector<std::string> >
106 ObjectMatch::getExpressions()
107 {
108 std::vector<std::vector<std::string> > to_return;
109 for (const auto &expression: tokens) {
110 std::vector<std::string> to_add;
111 to_add.insert(to_add.end(), expression.begin(), expression.end());
112 to_return.push_back(to_add);
113 }
114
115 return to_return;
116 }
117