1 |
commit: 0f7c0723157eab83f7f0716c09764cc5004a63a3 |
2 |
Author: Mu Qiao <qiaomuf <AT> gentoo <DOT> org> |
3 |
AuthorDate: Tue Apr 12 01:43:13 2011 +0000 |
4 |
Commit: Petteri Räty <betelgeuse <AT> gentoo <DOT> org> |
5 |
CommitDate: Tue Apr 12 07:22:49 2011 +0000 |
6 |
URL: http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=0f7c0723 |
7 |
|
8 |
Support getting all values of an array |
9 |
|
10 |
--- |
11 |
src/core/interpreter.h | 17 +++++++++++++++-- |
12 |
src/core/symbols.hpp | 16 ++++++++++++++++ |
13 |
src/core/tests/interpreter_test.cpp | 13 +++++++++++++ |
14 |
src/core/tests/symbols_test.cpp | 24 ++++++++++++++++++++++++ |
15 |
4 files changed, 68 insertions(+), 2 deletions(-) |
16 |
|
17 |
diff --git a/src/core/interpreter.h b/src/core/interpreter.h |
18 |
index 43f816b..5c75bf0 100644 |
19 |
--- a/src/core/interpreter.h |
20 |
+++ b/src/core/interpreter.h |
21 |
@@ -361,11 +361,11 @@ public: |
22 |
return new_value; |
23 |
} |
24 |
|
25 |
- /// \brief resolve any variable |
26 |
+ /// \brief resolve string/int variable |
27 |
/// \param variable name |
28 |
/// \param array index, use index=0 if it's not an array |
29 |
/// \return the value of the variable, call default constructor if |
30 |
- // it's undefined |
31 |
+ /// it's undefined |
32 |
template <typename T> |
33 |
T resolve(const std::string& name, const unsigned index=0) |
34 |
{ |
35 |
@@ -375,6 +375,19 @@ public: |
36 |
return value->get_value<T>(index); |
37 |
} |
38 |
|
39 |
+ /// \brief resolve array variable |
40 |
+ /// \param variable name |
41 |
+ /// \param[out] vector that stores all array values |
42 |
+ template <typename T> |
43 |
+ void resolve_array(const std::string& name, std::vector<T>& values) |
44 |
+ { |
45 |
+ std::shared_ptr<variable> value = members.resolve(name); |
46 |
+ if(!value) |
47 |
+ return; |
48 |
+ |
49 |
+ value->get_all_values(values); |
50 |
+ } |
51 |
+ |
52 |
/// \brief check whether the value of the variable is null, return true |
53 |
/// if the variable is undefined |
54 |
/// \param variable name |
55 |
|
56 |
diff --git a/src/core/symbols.hpp b/src/core/symbols.hpp |
57 |
index 9bb0b98..5992a67 100644 |
58 |
--- a/src/core/symbols.hpp |
59 |
+++ b/src/core/symbols.hpp |
60 |
@@ -162,6 +162,22 @@ public: |
61 |
return boost::apply_visitor(visitor, iter->second); |
62 |
} |
63 |
|
64 |
+ /// \brief retrieve all values of the array |
65 |
+ /// \param[out] vector that stores all array values, values in the arrays will |
66 |
+ /// be cleared first |
67 |
+ template<typename T> |
68 |
+ void get_all_values(std::vector<T>& all_values) const |
69 |
+ { |
70 |
+ static converter<T> visitor; |
71 |
+ |
72 |
+ all_values.clear(); |
73 |
+ |
74 |
+ for(auto iter = value.begin(); iter != value.end(); ++iter) |
75 |
+ all_values.push_back( |
76 |
+ boost::apply_visitor(visitor, iter->second)); |
77 |
+ } |
78 |
+ |
79 |
+ |
80 |
/// \brief set the value of the variable, raise exception if it's readonly |
81 |
/// \param the new value to be set |
82 |
/// \param array index, use index=0 if it's not an array |
83 |
|
84 |
diff --git a/src/core/tests/interpreter_test.cpp b/src/core/tests/interpreter_test.cpp |
85 |
index 28f8d35..9bb561a 100644 |
86 |
--- a/src/core/tests/interpreter_test.cpp |
87 |
+++ b/src/core/tests/interpreter_test.cpp |
88 |
@@ -118,6 +118,19 @@ TEST(interpreter, set_array_value) |
89 |
EXPECT_STREQ("2", walker.resolve<string>("ro_array", 1).c_str()); |
90 |
} |
91 |
|
92 |
+TEST(interpreter, get_array_values) |
93 |
+{ |
94 |
+ interpreter walker; |
95 |
+ std::map<int, std::string> values = {{0, "1"}, {1, "2"}, {2, "3"}}; |
96 |
+ walker.define("array", values); |
97 |
+ |
98 |
+ std::vector<int> array_values; |
99 |
+ walker.resolve_array("array", array_values); |
100 |
+ EXPECT_EQ(1, array_values[0]); |
101 |
+ EXPECT_EQ(2, array_values[1]); |
102 |
+ EXPECT_EQ(3, array_values[2]); |
103 |
+} |
104 |
+ |
105 |
TEST(interperter, substring_expansion_exception) |
106 |
{ |
107 |
interpreter walker; |
108 |
|
109 |
diff --git a/src/core/tests/symbols_test.cpp b/src/core/tests/symbols_test.cpp |
110 |
index 1602f52..b45d286 100644 |
111 |
--- a/src/core/tests/symbols_test.cpp |
112 |
+++ b/src/core/tests/symbols_test.cpp |
113 |
@@ -94,6 +94,30 @@ TEST(symbol_test, array_variable) |
114 |
EXPECT_EQ(3, normal_array.get_value<int>(2)); |
115 |
} |
116 |
|
117 |
+TEST(symbol_test, get_all_values) |
118 |
+{ |
119 |
+ map<int, string> values = {{0, "1"}, {1, "2"}, {2, "3"}}; |
120 |
+ variable array("foo", values); |
121 |
+ vector<string> string_values; |
122 |
+ array.get_all_values(string_values); |
123 |
+ |
124 |
+ EXPECT_EQ(3, string_values.size()); |
125 |
+ EXPECT_STREQ("1", string_values[0].c_str()); |
126 |
+ EXPECT_STREQ("2", string_values[1].c_str()); |
127 |
+ EXPECT_STREQ("3", string_values[2].c_str()); |
128 |
+ |
129 |
+ variable a_string("foo", 10); |
130 |
+ a_string.get_all_values(string_values); |
131 |
+ EXPECT_EQ(1, string_values.size()); |
132 |
+ EXPECT_STREQ("10", string_values[0].c_str()); |
133 |
+ |
134 |
+ variable an_int("foo", 10); |
135 |
+ vector<int> int_values; |
136 |
+ an_int.get_all_values(int_values); |
137 |
+ EXPECT_EQ(1, int_values.size()); |
138 |
+ EXPECT_EQ(10, int_values[0]); |
139 |
+} |
140 |
+ |
141 |
TEST(symbol_test, is_null) |
142 |
{ |
143 |
variable var("foo", 10); |