Browse Source

[3073] Add empty() member function to Element for element lists.

It has been asserted that using empty() is more efficient than using
size() > 0 due to the possible extra work size() has to do. So provide
an empty() function, which matches the underlying Boost implementation.
Kean Johnston 11 years ago
parent
commit
2d83a3a525

+ 5 - 0
src/lib/cc/data.cc

@@ -145,6 +145,11 @@ Element::size() const {
     isc_throw(TypeError, "size() called on a non-list Element");
 }
 
+bool
+Element::empty() const {
+    isc_throw(TypeError, "empty() called on a non-list Element");
+}
+
 ConstElementPtr
 Element::get(const std::string&) const {
     isc_throw(TypeError, "get(string) called on a non-map Element");

+ 4 - 0
src/lib/cc/data.h

@@ -210,6 +210,9 @@ public:
 
     /// Returns the number of elements in the list.
     virtual size_t size() const;
+
+    /// Return true if there are no elements in the list.
+    virtual bool empty() const;
     //@}
 
 
@@ -479,6 +482,7 @@ public:
     void remove(int i) { l.erase(l.begin() + i); };
     void toJSON(std::ostream& ss) const;
     size_t size() const { return (l.size()); }
+    bool empty() const { return (l.empty()); }
     bool equals(const Element& other) const;
 };
 

+ 1 - 1
src/lib/cc/session.cc

@@ -535,7 +535,7 @@ Session::reply(ConstElementPtr envelope, ConstElementPtr newmsg) {
 
 bool
 Session::hasQueuedMsgs() const {
-    return (impl_->queue_->size() > 0);
+    return (!impl_->queue_->empty());
 }
 
 void

+ 6 - 0
src/lib/cc/tests/data_unittests.cc

@@ -418,6 +418,7 @@ TEST(Element, create_and_value_throws) {
     EXPECT_THROW(el->add(el), TypeError);
     EXPECT_THROW(el->remove(1), TypeError);
     EXPECT_THROW(el->size(), TypeError);
+    EXPECT_THROW(el->empty(), TypeError);
     EXPECT_THROW(el->get("foo"), TypeError);
     EXPECT_THROW(el->set("foo", el), TypeError);
     EXPECT_THROW(el->remove("foo"), TypeError);
@@ -441,6 +442,7 @@ TEST(Element, create_and_value_throws) {
     EXPECT_THROW(el->add(el), TypeError);
     EXPECT_THROW(el->remove(1), TypeError);
     EXPECT_THROW(el->size(), TypeError);
+    EXPECT_THROW(el->empty(), TypeError);
     EXPECT_THROW(el->get("foo"), TypeError);
     EXPECT_THROW(el->set("foo", el), TypeError);
     EXPECT_THROW(el->remove("foo"), TypeError);
@@ -464,6 +466,7 @@ TEST(Element, create_and_value_throws) {
     EXPECT_THROW(el->add(el), TypeError);
     EXPECT_THROW(el->remove(1), TypeError);
     EXPECT_THROW(el->size(), TypeError);
+    EXPECT_THROW(el->empty(), TypeError);
     EXPECT_THROW(el->get("foo"), TypeError);
     EXPECT_THROW(el->set("foo", el), TypeError);
     EXPECT_THROW(el->remove("foo"), TypeError);
@@ -487,6 +490,7 @@ TEST(Element, create_and_value_throws) {
     EXPECT_THROW(el->add(el), TypeError);
     EXPECT_THROW(el->remove(1), TypeError);
     EXPECT_THROW(el->size(), TypeError);
+    EXPECT_THROW(el->empty(), TypeError);
     EXPECT_THROW(el->get("foo"), TypeError);
     EXPECT_THROW(el->set("foo", el), TypeError);
     EXPECT_THROW(el->remove("foo"), TypeError);
@@ -497,8 +501,10 @@ TEST(Element, create_and_value_throws) {
     testGetValueList<ConstElementPtr>();
 
     el = Element::createList();
+    EXPECT_TRUE(el->empty());
     v.push_back(Element::create(1));
     EXPECT_TRUE(el->setValue(v));
+    EXPECT_FALSE(el->empty());
     EXPECT_EQ("[ 1 ]", el->str());
 
     testGetValueMap<ElementPtr>();

+ 1 - 1
src/lib/config/ccsession.cc

@@ -144,7 +144,7 @@ parseCommand(ConstElementPtr& arg, ConstElementPtr command) {
         command->contains(isc::cc::CC_PAYLOAD_COMMAND)) {
         ConstElementPtr cmd = command->get(isc::cc::CC_PAYLOAD_COMMAND);
         if (cmd->getType() == Element::list &&
-            cmd->size() > 0 &&
+            !cmd->empty() &&
             cmd->get(0)->getType() == Element::string) {
             if (cmd->size() > 1) {
                 arg = cmd->get(1);

+ 4 - 4
src/lib/config/tests/fake_session.cc

@@ -104,7 +104,7 @@ FakeSession::recvmsg(ConstElementPtr& msg, bool nonblock, int) {
     //cout << "[XX] client asks for message " << endl;
     if (messages_ &&
         messages_->getType() == Element::list &&
-        messages_->size() > 0) {
+        !messages_->empty()) {
         msg = messages_->get(0);
         messages_->remove(0);
     } else {
@@ -127,7 +127,7 @@ FakeSession::recvmsg(ConstElementPtr& env, ConstElementPtr& msg, bool nonblock,
     env = ElementPtr();
     if (messages_ &&
         messages_->getType() == Element::list &&
-        messages_->size() > 0) {
+        !messages_->empty()) {
         // do we need initial message to have env[group] and [to] too?
         msg = messages_->get(0);
         messages_->remove(0);
@@ -210,13 +210,13 @@ FakeSession::reply(ConstElementPtr envelope, ConstElementPtr newmsg) {
 
 bool
 FakeSession::hasQueuedMsgs() const {
-    return (msg_queue_ && msg_queue_->size() > 0);
+    return (msg_queue_ && !msg_queue_->empty());
 }
 
 ConstElementPtr
 FakeSession::getFirstMessage(std::string& group, std::string& to) const {
     ConstElementPtr el;
-    if (msg_queue_ && msg_queue_->size() > 0) {
+    if (msg_queue_ && !msg_queue_->empty()) {
         el = msg_queue_->get(0);
         msg_queue_->remove(0);
         group = el->get(0)->stringValue();

+ 4 - 0
src/lib/datasrc/cache_config.cc

@@ -106,6 +106,10 @@ CacheConfig::CacheConfig(const std::string& datasrc_type,
         }
 
         const ConstElementPtr zones = datasrc_conf.get("cache-zones");
+        if (zones->empty()) {
+            return;
+        }
+
         for (size_t i = 0; i < zones->size(); ++i) {
             const dns::Name zone_name(zones->get(i)->stringValue());
             if (!zone_config_.insert(Zones::value_type(zone_name,

+ 5 - 0
src/lib/datasrc/client_list.cc

@@ -83,6 +83,11 @@ ConfigurableClientList::configure(const ConstElementPtr& config,
     if (!config) {
         isc_throw(isc::BadValue, "NULL configuration passed");
     }
+
+    if (config->empty()) {
+        return;
+    }
+
     // TODO: Implement recycling from the old configuration.
     size_t i(0); // Outside of the try to be able to access it in the catch
     try {

+ 2 - 1
src/lib/server_common/portconfig.cc

@@ -37,7 +37,8 @@ parseAddresses(isc::data::ConstElementPtr addresses,
 {
     AddressList result;
     if (addresses) {
-        if (addresses->getType() == Element::list) {
+        if (addresses->getType() == Element::list &&
+            !addresses->empty() ) {
             for (size_t i(0); i < addresses->size(); ++ i) {
                 ConstElementPtr addrPair(addresses->get(i));
                 ConstElementPtr addr(addrPair->get("address"));