[asterisk-scf-commits] asterisk-scf/release/ice-util-cpp.git branch "master" updated.

Commits to the Asterisk SCF project code repositories asterisk-scf-commits at lists.digium.com
Thu Aug 11 17:03:36 CDT 2011


branch "master" has been updated
       via  3deea66aefa19e940ca6a5ec0649a0c722a15e23 (commit)
      from  a5fe978beeaeff3362834fbf4a9210106988f6d8 (commit)

Summary of changes:
 include/AsteriskSCF/Collections/HandleSet.h        |   72 ++++++++++++
 .../AsteriskSCF/Collections/{Set.h => LoggedSet.h} |  121 +++++---------------
 include/AsteriskSCF/Collections/ProxySet.h         |   78 +++++++++++++
 test/Collections/TestCollections.cpp               |   23 +++-
 4 files changed, 196 insertions(+), 98 deletions(-)
 create mode 100644 include/AsteriskSCF/Collections/HandleSet.h
 rename include/AsteriskSCF/Collections/{Set.h => LoggedSet.h} (66%)
 create mode 100644 include/AsteriskSCF/Collections/ProxySet.h


- Log -----------------------------------------------------------------
commit 3deea66aefa19e940ca6a5ec0649a0c722a15e23
Author: Ken Hunt <ken.hunt at digium.com>
Date:   Thu Aug 11 17:00:36 2011 -0500

    - Incorporates suggest to rename Set.
    - Moved each template class defined in Set.h into its own file.
    - Incorporated suggestion to support "set" semantics on the interface rather than a mix os "set" and "hashtable".

diff --git a/include/AsteriskSCF/Collections/HandleSet.h b/include/AsteriskSCF/Collections/HandleSet.h
new file mode 100644
index 0000000..04772d9
--- /dev/null
+++ b/include/AsteriskSCF/Collections/HandleSet.h
@@ -0,0 +1,72 @@
+/*
+ * Asterisk SCF -- An open-source communications framework.
+ *
+ * Copyright (C) 2010-2011, Digium, Inc.
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk SCF project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE.txt file
+ * at the top of the source tree.
+ */
+#pragma once
+
+#include <vector>
+
+#include <boost/shared_ptr.hpp>
+#include <boost/thread/locks.hpp>
+
+#include <Ice/Ice.h>
+
+#include <AsteriskSCF/System/ExceptionsIf.h>
+#include <AsteriskSCF/logger.h>
+#include <AsteriskSCF/Helpers/ProxyHelper.h>
+#include <AsteriskSCF/Collections/LoggedSet.h>
+
+namespace AsteriskSCF
+{
+namespace Collections
+{
+
+/**
+ * Maintains a set of Handle objects of type S.
+ * The type S is must be derived from Ice::Handle. 
+ * For further details, refer to the LoggedSet definition. 
+ * @see LoggedSet
+ */
+ template <typename S>
+ class HandleSet : public LoggedSet<S, std::string, AsteriskSCF::System::V1::NullHandleException>
+ {
+ public:
+    HandleSet(const AsteriskSCF::System::Logging::Logger& logger,
+              const std::string& loggingLabel,
+              bool doesThrow = true)
+                  : LoggedSet<S, std::string, AsteriskSCF::System::V1::NullHandleException>
+                       (logger,
+                        loggingLabel,
+                        doesThrow)
+    {
+    }
+
+    HandleSet(const std::string& loggingLabel,
+              bool doesThrow = true)
+                 : LoggedSet<S, std::string, AsteriskSCF::System::V1::NullHandleException>
+                      (loggingLabel,
+                       doesThrow)
+               
+    {
+    }
+
+protected:
+     virtual std::string getIdentity(const S & item)
+     {
+         return item->ice_id();
+     }
+ };
+
+} // end namespace Collections
+} // end namespace AsteriskSCF
diff --git a/include/AsteriskSCF/Collections/Set.h b/include/AsteriskSCF/Collections/LoggedSet.h
similarity index 66%
rename from include/AsteriskSCF/Collections/Set.h
rename to include/AsteriskSCF/Collections/LoggedSet.h
index 78f7f21..c43111d 100644
--- a/include/AsteriskSCF/Collections/Set.h
+++ b/include/AsteriskSCF/Collections/LoggedSet.h
@@ -33,32 +33,33 @@ namespace Collections
 
 /**
  * Maintains a set of type T. 
- * The items are keyed on a type K.
- * The set maintains at most one item of a given id. 
- * The id for an item is obtained via an overridable member function
- * getIdentity(). 
+ * - The items are keyed on a type K.
+ * - The set maintains at most one item of a given key. 
+ * - The key for an item must be obtainable from the item itself. The
+ *    getIdentity(item) operation (a required override) allows flexibility
+ *    in how the identity is obtained. 
+ * - Operations on the set are made internally thread safe. 
+ * - The set will log (at Warning level) calls that don't complete in normal
+ *   fashion. (ex: attempt to add a null object, attempt to acquire a non-existent member, etc.)
+ * - Execeptions are optionally thrown based on argument to constructor. 
  */
 template <typename T, typename K, typename NullItemException>
-class Set
+class LoggedSet
 {
 public:
-    Set(const Ice::ObjectAdapterPtr& adapter,
-        const AsteriskSCF::System::Logging::Logger& logger,
+    LoggedSet(const AsteriskSCF::System::Logging::Logger& logger,
         const std::string& loggingLabel,
         bool doesThrow = true)
               : mLogger(logger),
                 mLoggingLabel(loggingLabel),
-                mAdapter(adapter),
                 mDoesThrow(doesThrow)
     {
     }
 
-    Set(const Ice::ObjectAdapterPtr& adapter,
-        const std::string& loggingLabel,
+    LoggedSet(const std::string& loggingLabel,
         bool doesThrow = true)
              : mLogger(AsteriskSCF::System::Logging::getLoggerFactory().getLogger("AsteriskSCF.Collections")),
                mLoggingLabel(loggingLabel),
-               mAdapter(adapter),
                mDoesThrow(doesThrow)
                
     {
@@ -78,7 +79,7 @@ public:
         boost::unique_lock<boost::shared_mutex> lock(mLock);
         if (item == 0)
         {
-            mLogger(AsteriskSCF::System::Logging::Warning) << "Attempting to add a null member for " << mLoggingLabel;
+            mLogger(AsteriskSCF::System::Logging::Warning) << "Attempt to add a null member for " << mLoggingLabel << " rejected.";
             if (mDoesThrow)
             {
                throw NullItemException();
@@ -106,7 +107,7 @@ public:
         {
             if ((*i) == 0)
             {
-                mLogger(AsteriskSCF::System::Logging::Warning) << "Attempting to add a null member for " << mLoggingLabel;
+                mLogger(AsteriskSCF::System::Logging::Warning) << "Attempt to add a null member for " << mLoggingLabel << " rejected.";
                 continue;
             }
 
@@ -146,7 +147,7 @@ public:
 
         for(typename std::vector<K>::const_iterator s = idsNotFound.begin(); s != idsNotFound.end(); ++s)
         {
-            mLogger(AsteriskSCF::System::Logging::Warning) << "Attempted to remove non-existent member " << (*s);
+            mLogger(AsteriskSCF::System::Logging::Warning) << "Attempted to remove non-existent member " << (*s) << " from " << mLoggingLabel;
         }
     }
 
@@ -165,7 +166,7 @@ public:
             return;
         }
 
-        mLogger(AsteriskSCF::System::Logging::Warning) << "Attempted to remove non-existent member " << id;
+        mLogger(AsteriskSCF::System::Logging::Warning) << "Attempted to remove non-existent member " << id << " from " << mLoggingLabel;
         if (mDoesThrow)
         {
             throw AsteriskSCF::System::V1::UnknownObject(id);
@@ -220,6 +221,8 @@ public:
             return pair->second;
         }
 
+        mLogger(AsteriskSCF::System::Logging::Warning) << "Attempted to get a non-existent member " << key << " from " << mLoggingLabel;
+       
         if (mDoesThrow)
         {
             throw AsteriskSCF::System::V1::UnknownObject(key);
@@ -227,101 +230,35 @@ public:
         return 0;
     }
 
-    bool has(K key)
+    bool has(const T& item)
     {
         boost::unique_lock<boost::shared_mutex> lock(mLock);
 
-        typename std::map<K, T>::const_iterator search = mMap.find(key);
+        typename std::map<K, T>::const_iterator search = mMap.find(getIdentity(item));
         return (search != mMap.end());
     }
 
-    typedef boost::shared_ptr< Set<T, K, NullItemException> > SetPtr;
+    bool empty()
+    {
+        boost::unique_lock<boost::shared_mutex> lock(mLock);
+        return mMap.empty();
+    }
+
+    // A shared pointer to this set type. 
+    typedef boost::shared_ptr< LoggedSet<T, K, NullItemException> > SetPtr;
 
 protected:
 
+    // Overridable operation that determines how the key is obtained from an item. 
     virtual K getIdentity(const T & item) = 0;
 
     AsteriskSCF::System::Logging::Logger mLogger;
     std::string mLoggingLabel;
-    Ice::ObjectAdapterPtr mAdapter;
     boost::shared_mutex mLock;
     std::map<K, T> mMap;
     bool mDoesThrow;
 };
 
-/**
- * Maintains a set of handles of type S.
- * The type S is assumed to be derived from Ice::Handle. 
- */
- template <typename S>
- class HandleSet : public Set<S, std::string, AsteriskSCF::System::V1::NullHandleException>
- {
- public:
-    HandleSet(const Ice::ObjectAdapterPtr& adapter,
-              const AsteriskSCF::System::Logging::Logger& logger,
-              const std::string& loggingLabel,
-              bool doesThrow = true)
-                  : Set<S, std::string, AsteriskSCF::System::V1::NullHandleException>(adapter,
-                        logger,
-                        loggingLabel,
-                        doesThrow)
-    {
-    }
-
-    HandleSet(const Ice::ObjectAdapterPtr& adapter,
-              const std::string& loggingLabel,
-              bool doesThrow = true)
-                 : Set<S, std::string, AsteriskSCF::System::V1::NullHandleException>(adapter,
-                       loggingLabel,
-                       doesThrow)
-               
-    {
-    }
-
-protected:
-     virtual std::string getIdentity(const S & item)
-     {
-         return item->ice_id();
-     }
- };
-
-/**
-* Maintains a set of proxies of type S. 
-* The type S is assumed to be derived from Ice::Proxy. 
-*/
-template <typename S>
-class ProxySet : public Set<S, std::string, AsteriskSCF::System::V1::NullProxyException>
-{
-public:
-    ProxySet(const Ice::ObjectAdapterPtr& adapter,
-             const AsteriskSCF::System::Logging::Logger& logger,
-             const std::string& loggingLabel,
-             bool doesThrow = true)
-                : Set<S, std::string, AsteriskSCF::System::V1::NullProxyException>(adapter,
-                      logger,
-                      loggingLabel,
-                      doesThrow)
-    {
-    }
-
-    ProxySet(const Ice::ObjectAdapterPtr& adapter,
-             const std::string& loggingLabel,
-             bool doesThrow = true)
-                : Set<S, std::string, AsteriskSCF::System::V1::NullProxyException>(adapter,
-                      loggingLabel,
-                      doesThrow)
-               
-    {
-    }
-
-protected:
-    virtual std::string getIdentity(const S & item)
-    {
-        // Note: In referencing a member of a base template class, gcc requires the "this" 
-        // to know where to look for the member variable.
-        return this->mAdapter->getCommunicator()->identityToString(item->ice_getIdentity());
-    }
-};
 
 } // end namespace Collections
 } // end namespace AsteriskSCF
diff --git a/include/AsteriskSCF/Collections/ProxySet.h b/include/AsteriskSCF/Collections/ProxySet.h
new file mode 100644
index 0000000..8f41b6e
--- /dev/null
+++ b/include/AsteriskSCF/Collections/ProxySet.h
@@ -0,0 +1,78 @@
+/*
+ * Asterisk SCF -- An open-source communications framework.
+ *
+ * Copyright (C) 2010-2011, Digium, Inc.
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk SCF project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE.txt file
+ * at the top of the source tree.
+ */
+#pragma once
+
+#include <vector>
+
+#include <boost/shared_ptr.hpp>
+#include <boost/thread/locks.hpp>
+
+#include <Ice/Ice.h>
+
+#include <AsteriskSCF/System/ExceptionsIf.h>
+#include <AsteriskSCF/logger.h>
+#include <AsteriskSCF/Helpers/ProxyHelper.h>
+#include <AsteriskSCF/Collections/LoggedSet.h>
+
+namespace AsteriskSCF
+{
+namespace Collections
+{
+
+/**
+* Maintains a set of Ice proxies of type S. 
+* The type S must be derived from Ice::Proxy. 
+ * For further details, refer to the LoggedSet definition. 
+* @see LoggedSet
+*/
+template <typename S>
+class ProxySet : public LoggedSet<S, std::string, AsteriskSCF::System::V1::NullProxyException>
+{
+public:
+    ProxySet(const Ice::ObjectAdapterPtr& adapter,
+             const AsteriskSCF::System::Logging::Logger& logger,
+             const std::string& loggingLabel,
+             bool doesThrow = true)
+                : LoggedSet<S, std::string, AsteriskSCF::System::V1::NullProxyException>(
+                      logger,
+                      loggingLabel,
+                      doesThrow),
+                  mAdapter(adapter)
+    {
+    }
+
+    ProxySet(const Ice::ObjectAdapterPtr& adapter,
+             const std::string& loggingLabel,
+             bool doesThrow = true)
+                : LoggedSet<S, std::string, AsteriskSCF::System::V1::NullProxyException>(
+                            loggingLabel,
+                            doesThrow),
+                  mAdapter(adapter)
+               
+    {
+    }
+
+protected:
+    virtual std::string getIdentity(const S & item)
+    {
+        return mAdapter->getCommunicator()->identityToString(item->ice_getIdentity());
+    }
+
+    Ice::ObjectAdapterPtr mAdapter;
+};
+
+} // end namespace Collections
+} // end namespace AsteriskSCF
diff --git a/test/Collections/TestCollections.cpp b/test/Collections/TestCollections.cpp
index fb54ce9..ac1ccf7 100644
--- a/test/Collections/TestCollections.cpp
+++ b/test/Collections/TestCollections.cpp
@@ -24,7 +24,8 @@
 #include <AsteriskSCF/Testing/IceBoxBoostTest.h>
 
 #include <TestCollectionsIf.h>
-#include <AsteriskSCF/Collections/Set.h>
+#include <AsteriskSCF/Collections/HandleSet.h>
+#include <AsteriskSCF/Collections/ProxySet.h>
 
 using namespace std;
 using namespace AsteriskSCF::Collections;
@@ -189,8 +190,9 @@ BOOST_AUTO_TEST_CASE(ProxySetTest)
 
     BOOST_CHECK(caught == false);
     BOOST_CHECK(results.size() == 0);
+    BOOST_CHECK(proxySet.empty() == true);
 
-    BOOST_CHECK(proxySet.has("Something") == false);
+    BOOST_CHECK(proxySet.has(global.prx2) == false);
 
     ///////////////////////////////////////////////////////
     // Now add some content added.
@@ -203,10 +205,12 @@ BOOST_AUTO_TEST_CASE(ProxySetTest)
 
     proxySet.add(multiple);
 
-    BOOST_CHECK(proxySet.has( IceBoxTestEnv.adapter->getCommunicator()->identityToString(global.prx1->ice_getIdentity())) == true);
+    BOOST_CHECK(proxySet.has( global.prx1) == true);
 
     BOOST_CHECK(proxySet.size() == 3);
 
+    BOOST_CHECK(proxySet.has(global.prx2) == true);
+
     proxySet.remove(global.prx2);
 
     BOOST_CHECK(proxySet.size() == 2);
@@ -214,6 +218,8 @@ BOOST_AUTO_TEST_CASE(ProxySetTest)
     results = proxySet.getAll();
     BOOST_CHECK(results.size() == 2);
 
+    BOOST_CHECK(proxySet.has(global.prx2) == false);
+
     SampleListenerPrx prx = proxySet.get(IceBoxTestEnv.adapter->getCommunicator()->identityToString(global.prx1->ice_getIdentity()));
     BOOST_CHECK(prx->ice_getIdentity() == global.prx1->ice_getIdentity());
 
@@ -237,7 +243,7 @@ BOOST_AUTO_TEST_CASE(ProxySetTest)
  */
 BOOST_AUTO_TEST_CASE(HandleSetTest)
 {
-    HandleSet<BaseValuePtr> handleSet(IceBoxTestEnv.adapter, lg, "Values Set");
+    HandleSet<BaseValuePtr> handleSet(lg, "Values Set");
 
     /////////////////////////////////////////////////////////////
     // Try accessing the empty set first. 
@@ -288,7 +294,9 @@ BOOST_AUTO_TEST_CASE(HandleSetTest)
     BOOST_CHECK(caught == false);
     BOOST_CHECK(results.size() == 0);
 
-    BOOST_CHECK(handleSet.has("Something") == false);
+    BOOST_CHECK(handleSet.has(global.ptr1) == false);
+
+    BOOST_CHECK(handleSet.empty() == true);
 
     ///////////////////////////////////////////////////////
     // Now add some content added.
@@ -299,15 +307,18 @@ BOOST_AUTO_TEST_CASE(HandleSetTest)
     multiple.push_back(global.ptr2);
     multiple.push_back(global.ptr3);
 
+    BOOST_CHECK(handleSet.empty() == false);
+
     handleSet.add(multiple);
 
-    BOOST_CHECK(handleSet.has(global.ptr1->ice_id()) == true);
+    BOOST_CHECK(handleSet.has(global.ptr2) == true);
 
     BOOST_CHECK(handleSet.size() == 3);
 
     handleSet.remove(global.ptr2);
 
     BOOST_CHECK(handleSet.size() == 2);
+    BOOST_CHECK(handleSet.has(global.ptr2) == false);
 
     results = handleSet.getAll();
     BOOST_CHECK(results.size() == 2);

-----------------------------------------------------------------------


-- 
asterisk-scf/release/ice-util-cpp.git



More information about the asterisk-scf-commits mailing list