From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from pigeon.gentoo.org ([208.92.234.80] helo=lists.gentoo.org) by finch.gentoo.org with esmtp (Exim 4.60) (envelope-from ) id 1Q6m5e-0003Gu-5y for garchives@archives.gentoo.org; Mon, 04 Apr 2011 15:53:18 +0000 Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 589021C06F; Mon, 4 Apr 2011 15:52:41 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) by pigeon.gentoo.org (Postfix) with ESMTP id 168351C06A for ; Mon, 4 Apr 2011 15:52:41 +0000 (UTC) Received: from pelican.gentoo.org (unknown [66.219.59.40]) (using TLSv1 with cipher ADH-CAMELLIA256-SHA (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 51F9F1B4065 for ; Mon, 4 Apr 2011 15:52:40 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by pelican.gentoo.org (Postfix) with ESMTP id A951280065 for ; Mon, 4 Apr 2011 15:52:39 +0000 (UTC) From: "Petteri Räty" To: gentoo-commits@lists.gentoo.org Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Petteri Räty" Message-ID: <8b4e14c2089780faa5e5d705821491648cfc926e.betelgeuse@gentoo> Subject: [gentoo-commits] proj/libbash:master commit in: src/core/, src/core/tests/ X-VCS-Repository: proj/libbash X-VCS-Files: src/core/interpreter.h src/core/symbols.hpp src/core/tests/interpreter_test.cpp src/core/tests/symbols_test.cpp X-VCS-Directories: src/core/ src/core/tests/ X-VCS-Committer: betelgeuse X-VCS-Committer-Name: Petteri Räty X-VCS-Revision: 8b4e14c2089780faa5e5d705821491648cfc926e Date: Mon, 4 Apr 2011 15:52:39 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: quoted-printable X-Archives-Salt: X-Archives-Hash: 228f96aa8cb06a947eaefa8af1b24369 commit: 8b4e14c2089780faa5e5d705821491648cfc926e Author: Mu Qiao gentoo org> AuthorDate: Mon Apr 4 05:16:55 2011 +0000 Commit: Petteri R=C3=A4ty gentoo org> CommitDate: Mon Apr 4 09:49:41 2011 +0000 URL: http://git.overlays.gentoo.org/gitweb/?p=3Dproj/libbash.git;a= =3Dcommit;h=3D8b4e14c2 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(value)->get_value(); } =20 + /// \brief check whether the value of the variable is null, return tru= e + /// 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 value =3D members.resolve(name); + if(value) + return std::static_pointer_cast(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 void define(const std::string& name, const T& value, - bool readonly=3Dfalse) + bool readonly=3Dfalse, + bool is_null=3Dfalse) { std::shared_ptr 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; =20 + /// \var private::null_value + /// \brief whether the symbol is null=20 + bool null_value; + public: template - variable(const std::string& name, T v, bool ro=3Dfalse) - : symbol(name), value(v), readonly(ro){} + variable(const std::string& name, + T v, + bool ro=3Dfalse, + bool is_null=3Dfalse) + : symbol(name), value(v), readonly(ro), null_value(is_null){} =20 /// \brief retrieve actual value of the symbol /// \return the value of the symbol @@ -157,13 +164,22 @@ public: =20 /// \brief set the value of the symbol, raise exception if it's readon= ly /// \param the new value to be set + /// \param whether to set the variable to null value, default is false template - void set_value(T new_value) + void set_value(T new_value, bool is_null=3Dfalse) { if(readonly) throw interpreter_exception(get_name() + " is readonly variable"); + null_value =3D is_null; value =3D 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; + } }; =20 /// diff --git a/src/core/tests/interpreter_test.cpp b/src/core/tests/interpr= eter_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("undefined").c_str()); } =20 +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_tes= t.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()); } =20 +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;