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: Mon,  4 Apr 2011 15:52:39 +0000 (UTC)	[thread overview]
Message-ID: <8b4e14c2089780faa5e5d705821491648cfc926e.betelgeuse@gentoo> (raw)

commit:     8b4e14c2089780faa5e5d705821491648cfc926e
Author:     Mu Qiao <qiaomuf <AT> gentoo <DOT> org>
AuthorDate: Mon Apr  4 05:16:55 2011 +0000
Commit:     Petteri Räty <betelgeuse <AT> gentoo <DOT> org>
CommitDate: Mon Apr  4 09:49:41 2011 +0000
URL:        http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=8b4e14c2

Add a member indicating whether a variable is null

---
 src/core/interpreter.h              |   19 +++++++++++++++++--
 src/core/symbols.hpp                |   22 +++++++++++++++++++---
 src/core/tests/interpreter_test.cpp |    9 +++++++++
 src/core/tests/symbols_test.cpp     |    9 +++++++++
 4 files changed, 54 insertions(+), 5 deletions(-)

diff --git a/src/core/interpreter.h b/src/core/interpreter.h
index ef95caa..858916e 100644
--- a/src/core/interpreter.h
+++ b/src/core/interpreter.h
@@ -358,6 +358,19 @@ public:
     return std::static_pointer_cast<variable>(value)->get_value<T>();
   }
 
+  /// \brief check whether the value of the variable is null, return true
+  ///        if the variable is undefined
+  /// \param variable name
+  /// \return whether the value of the variable is null
+  bool is_null(const std::string& name)
+  {
+    std::shared_ptr<symbol> value = members.resolve(name);
+    if(value)
+      return std::static_pointer_cast<variable>(value)->is_null();
+    else
+      return true;
+  }
+
   /// \brief update the variable value, raise interpreter_exception if
   ///        it's readonly, will define the variable if it doesn't exist
   /// \param variable name
@@ -378,13 +391,15 @@ public:
   /// \param the name of the variable
   /// \param the value of the variable
   /// \param whether it's readonly, default is false
+  /// \param whether it's null, default is false
   template <typename T>
   void define(const std::string& name,
               const T& value,
-              bool readonly=false)
+              bool readonly=false,
+              bool is_null=false)
   {
     std::shared_ptr<variable> target(
-        new variable(name, value, readonly));
+        new variable(name, value, readonly, is_null));
     members.define(target);
   }
 };

diff --git a/src/core/symbols.hpp b/src/core/symbols.hpp
index 53b699e..a4ae514 100644
--- a/src/core/symbols.hpp
+++ b/src/core/symbols.hpp
@@ -141,10 +141,17 @@ class variable: public symbol
   /// \brief whether the symbol is readonly
   bool readonly;
 
+  /// \var private::null_value
+  /// \brief whether the symbol is null 
+  bool null_value;
+
 public:
   template <typename T>
-  variable(const std::string& name, T v, bool ro=false)
-    : symbol(name), value(v), readonly(ro){}
+  variable(const std::string& name,
+           T v,
+           bool ro=false,
+           bool is_null=false)
+    : symbol(name), value(v), readonly(ro), null_value(is_null){}
 
   /// \brief retrieve actual value of the symbol
   /// \return the value of the symbol
@@ -157,13 +164,22 @@ public:
 
   /// \brief set the value of the symbol, raise exception if it's readonly
   /// \param the new value to be set
+  /// \param whether to set the variable to null value, default is false
   template <typename T>
-  void set_value(T new_value)
+  void set_value(T new_value, bool is_null=false)
   {
     if(readonly)
       throw interpreter_exception(get_name() + " is readonly variable");
+    null_value = is_null;
     value = new_value;
   }
+
+  /// \brief check whether the value of the variable is null
+  /// \return whether the value of the variable is null
+  bool is_null() const
+  {
+    return null_value;
+  }
 };
 
 ///

diff --git a/src/core/tests/interpreter_test.cpp b/src/core/tests/interpreter_test.cpp
index e5976e5..c3f7888 100644
--- a/src/core/tests/interpreter_test.cpp
+++ b/src/core/tests/interpreter_test.cpp
@@ -44,6 +44,15 @@ TEST(interpreter, define_resolve_string)
   EXPECT_STREQ("", walker.resolve<string>("undefined").c_str());
 }
 
+TEST(interpreter, is_null)
+{
+  interpreter walker;
+  walker.define("foo", "hello");
+  EXPECT_FALSE(walker.is_null("foo"));
+  walker.define("foo", "hello", false, true);
+  EXPECT_TRUE(walker.is_null("foo"));
+}
+
 TEST(interpreter, set_int_value)
 {
   interpreter walker;

diff --git a/src/core/tests/symbols_test.cpp b/src/core/tests/symbols_test.cpp
index 891e9cb..7620be0 100644
--- a/src/core/tests/symbols_test.cpp
+++ b/src/core/tests/symbols_test.cpp
@@ -65,6 +65,15 @@ TEST(symbol_test, string_variable)
   EXPECT_EQ(123, int_string.get_value<int>());
 }
 
+TEST(symbol_test, is_null)
+{
+  variable var("foo", 10);
+  EXPECT_FALSE(var.is_null());
+  var.set_value("bar", true);
+  EXPECT_TRUE(var.is_null());
+  EXPECT_TRUE(variable("foo", "", false, true).is_null());
+}
+
 TEST(scope_test, define_resolve)
 {
   scope members;



             reply	other threads:[~2011-04-04 15:53 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-04-04 15:52 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-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-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=8b4e14c2089780faa5e5d705821491648cfc926e.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