From 754b79afb2f69b84557373b43390edcbe2854d7e Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Thu, 11 Jun 2009 20:29:53 -0400 Subject: [PATCH] Enhancement - Record::has - Implemented Record::has which guarantees no autovivification of attributes. --- src/Record.cpp | 10 ++++++++++ src/Record.h | 1 + src/tests/record.t.cpp | 6 +++++- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Record.cpp b/src/Record.cpp index 4e36386e1..76e24edb6 100644 --- a/src/Record.cpp +++ b/src/Record.cpp @@ -116,6 +116,16 @@ void Record::parse (const std::string& input) "Record not recognized as format 4"); } +//////////////////////////////////////////////////////////////////////////////// +bool Record::has (const std::string& name) const +{ + Record::const_iterator i = this->find (name); + if (i != this->end ()) + return true; + + return false; +} + //////////////////////////////////////////////////////////////////////////////// std::vector Record::all () { diff --git a/src/Record.h b/src/Record.h index 5fb6380d2..d2698cd3c 100644 --- a/src/Record.h +++ b/src/Record.h @@ -43,6 +43,7 @@ public: std::string composeCSV (); void parse (const std::string&); + bool has (const std::string&) const; std::vector all (); const std::string get (const std::string&) const; int get_int (const std::string&) const; diff --git a/src/tests/record.t.cpp b/src/tests/record.t.cpp index 9fc368f33..a4bbf069f 100644 --- a/src/tests/record.t.cpp +++ b/src/tests/record.t.cpp @@ -35,7 +35,7 @@ Context context; //////////////////////////////////////////////////////////////////////////////// int main (int argc, char** argv) { - UnitTest t (17); + UnitTest t (19); // (blank) bool good = true; @@ -85,6 +85,10 @@ int main (int argc, char** argv) record.set ("name", "value"); t.is (record.composeF4 (), "[name:\"value\"]\n", "Record::set"); + // Record::has + t.ok (record.has ("name"), "Record::has"); + t.notok (record.has ("woof"), "Record::has not"); + // Record::get_int record.set ("one", 1); t.is (record.composeF4 (), "[name:\"value\" one:\"1\"]\n", "Record::set");