public inbox for gentoo-commits@lists.gentoo.org
 help / color / mirror / Atom feed
From: "Petteri Räty" <betelgeuse@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Subject: [gentoo-commits] proj/libbash:master commit in: src/core/, src/core/tests/
Date: Tue, 12 Apr 2011 18:29:21 +0000 (UTC)	[thread overview]
Message-ID: <0f7c0723157eab83f7f0716c09764cc5004a63a3.betelgeuse@gentoo> (raw)

commit:     0f7c0723157eab83f7f0716c09764cc5004a63a3
Author:     Mu Qiao <qiaomuf <AT> gentoo <DOT> org>
AuthorDate: Tue Apr 12 01:43:13 2011 +0000
Commit:     Petteri Räty <betelgeuse <AT> gentoo <DOT> org>
CommitDate: Tue Apr 12 07:22:49 2011 +0000
URL:        http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=0f7c0723

Support getting all values of an array

---
 src/core/interpreter.h              |   17 +++++++++++++++--
 src/core/symbols.hpp                |   16 ++++++++++++++++
 src/core/tests/interpreter_test.cpp |   13 +++++++++++++
 src/core/tests/symbols_test.cpp     |   24 ++++++++++++++++++++++++
 4 files changed, 68 insertions(+), 2 deletions(-)

diff --git a/src/core/interpreter.h b/src/core/interpreter.h
index 43f816b..5c75bf0 100644
--- a/src/core/interpreter.h
+++ b/src/core/interpreter.h
@@ -361,11 +361,11 @@ public:
     return new_value;
   }
 
-  /// \brief resolve any variable
+  /// \brief resolve string/int variable
   /// \param variable name
   /// \param array index, use index=0 if it's not an array
   /// \return the value of the variable, call default constructor if
-  //          it's undefined
+  ///         it's undefined
   template <typename T>
   T resolve(const std::string& name, const unsigned index=0)
   {
@@ -375,6 +375,19 @@ public:
     return value->get_value<T>(index);
   }
 
+  /// \brief resolve array variable
+  /// \param variable name
+  /// \param[out] vector that stores all array values
+  template <typename T>
+  void resolve_array(const std::string& name, std::vector<T>& values)
+  {
+    std::shared_ptr<variable> value = members.resolve(name);
+    if(!value)
+      return;
+
+    value->get_all_values(values);
+  }
+
   /// \brief check whether the value of the variable is null, return true
   ///        if the variable is undefined
   /// \param variable name

diff --git a/src/core/symbols.hpp b/src/core/symbols.hpp
index 9bb0b98..5992a67 100644
--- a/src/core/symbols.hpp
+++ b/src/core/symbols.hpp
@@ -162,6 +162,22 @@ public:
     return boost::apply_visitor(visitor, iter->second);
   }
 
+  /// \brief retrieve all values of the array
+  /// \param[out] vector that stores all array values, values in the arrays will
+  ///             be cleared first
+  template<typename T>
+  void get_all_values(std::vector<T>& all_values) const
+  {
+    static converter<T> visitor;
+
+    all_values.clear();
+
+    for(auto iter = value.begin(); iter != value.end(); ++iter)
+        all_values.push_back(
+                boost::apply_visitor(visitor, iter->second));
+  }
+
+
   /// \brief set the value of the variable, raise exception if it's readonly
   /// \param the new value to be set
   /// \param array index, use index=0 if it's not an array

diff --git a/src/core/tests/interpreter_test.cpp b/src/core/tests/interpreter_test.cpp
index 28f8d35..9bb561a 100644
--- a/src/core/tests/interpreter_test.cpp
+++ b/src/core/tests/interpreter_test.cpp
@@ -118,6 +118,19 @@ TEST(interpreter, set_array_value)
   EXPECT_STREQ("2", walker.resolve<string>("ro_array", 1).c_str());
 }
 
+TEST(interpreter, get_array_values)
+{
+  interpreter walker;
+  std::map<int, std::string> values = {{0, "1"}, {1, "2"}, {2, "3"}};
+  walker.define("array", values);
+
+  std::vector<int> array_values;
+  walker.resolve_array("array", array_values);
+  EXPECT_EQ(1, array_values[0]);
+  EXPECT_EQ(2, array_values[1]);
+  EXPECT_EQ(3, array_values[2]);
+}
+
 TEST(interperter, substring_expansion_exception)
 {
   interpreter walker;

diff --git a/src/core/tests/symbols_test.cpp b/src/core/tests/symbols_test.cpp
index 1602f52..b45d286 100644
--- a/src/core/tests/symbols_test.cpp
+++ b/src/core/tests/symbols_test.cpp
@@ -94,6 +94,30 @@ TEST(symbol_test, array_variable)
   EXPECT_EQ(3, normal_array.get_value<int>(2));
 }
 
+TEST(symbol_test, get_all_values)
+{
+  map<int, string> values = {{0, "1"}, {1, "2"}, {2, "3"}};
+  variable array("foo", values);
+  vector<string> string_values;
+  array.get_all_values(string_values);
+
+  EXPECT_EQ(3, string_values.size());
+  EXPECT_STREQ("1", string_values[0].c_str());
+  EXPECT_STREQ("2", string_values[1].c_str());
+  EXPECT_STREQ("3", string_values[2].c_str());
+
+  variable a_string("foo", 10);
+  a_string.get_all_values(string_values);
+  EXPECT_EQ(1, string_values.size());
+  EXPECT_STREQ("10", string_values[0].c_str());
+
+  variable an_int("foo", 10);
+  vector<int> int_values;
+  an_int.get_all_values(int_values);
+  EXPECT_EQ(1, int_values.size());
+  EXPECT_EQ(10, int_values[0]);
+}
+
 TEST(symbol_test, is_null)
 {
   variable var("foo", 10);



             reply	other threads:[~2011-04-12 18:29 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-04-12 18:29 Petteri Räty [this message]
  -- strict thread matches above, loose matches on Subject: below --
2011-05-27 23:03 [gentoo-commits] proj/libbash:master commit in: src/core/, src/core/tests/ Petteri Räty
2011-05-25 19:42 Petteri Räty
2011-05-23 14:34 Petteri Räty
2011-05-23 14:34 Petteri Räty
2011-05-08 13:07 Petteri Räty
2011-04-27 15:11 Petteri Räty
2011-04-14  4:50 Petteri Räty
2011-04-14  4:50 Petteri Räty
2011-04-12 18:29 Petteri Räty
2011-04-12 18:29 Petteri Räty
2011-04-06  7:43 Petteri Räty
2011-04-04 16:09 Petteri Räty
2011-04-04 15:52 Petteri Räty
2011-04-04 15:52 Petteri Räty
2011-03-30 12:48 Petteri Räty

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=0f7c0723157eab83f7f0716c09764cc5004a63a3.betelgeuse@gentoo \
    --to=betelgeuse@gentoo.org \
    --cc=gentoo-commits@lists.gentoo.org \
    --cc=gentoo-dev@lists.gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox