Remove the ConfigTypeIterator class
authorGunnar Beutner <gunnar.beutner@netways.de>
Tue, 16 Aug 2016 11:25:36 +0000 (13:25 +0200)
committerGunnar Beutner <gunnar.beutner@netways.de>
Tue, 16 Aug 2016 11:26:24 +0000 (13:26 +0200)
refs #12448

lib/base/configtype.cpp
lib/base/configtype.hpp
lib/base/objectlock.hpp
lib/db_ido/dbconnection.cpp
lib/livestatus/statustable.cpp

index d43b5a07dff90e3598595deffa2847ac0d9a0218..6c41a5e3de31b334adeabbe460cd59087fac7493 100644 (file)
@@ -75,12 +75,19 @@ void ConfigType::UnregisterObject(const ConfigObject::Ptr& object)
        }
 }
 
-std::pair<ConfigTypeIterator<ConfigObject>, ConfigTypeIterator<ConfigObject> > ConfigType::GetObjects(void)
+std::vector<ConfigObject::Ptr> ConfigType::GetObjects(void) const
 {
-       Type::Ptr type = dynamic_cast<Type *>(this);
+       boost::mutex::scoped_lock lock(m_Mutex);
+       return m_ObjectVector;
+}
 
-       return std::make_pair(
-           ConfigTypeIterator<ConfigObject>(type, 0),
-           ConfigTypeIterator<ConfigObject>(type, UINT_MAX)
-       );
+std::vector<ConfigObject::Ptr> ConfigType::GetObjectsHelper(Type *type)
+{
+       return static_cast<TypeImpl<ConfigObject> *>(type)->GetObjects();
+}
+
+int ConfigType::GetObjectCount(void) const
+{
+       boost::mutex::scoped_lock lock(m_Mutex);
+       return m_ObjectVector.size();
 }
index f9c714588ee49ec279cebe2c5e8f19f78cc69460..2b2af6e6b05250b7d727fd83157fc269fe0d962b 100644 (file)
 #include "base/object.hpp"
 #include "base/type.hpp"
 #include "base/dictionary.hpp"
+#include <boost/foreach.hpp>
 
 namespace icinga
 {
 
 class ConfigObject;
 
-template<typename T>
-class ConfigTypeIterator;
-
 class I2_BASE_API ConfigType
 {
 public:
@@ -43,84 +41,39 @@ public:
        void RegisterObject(const intrusive_ptr<ConfigObject>& object);
        void UnregisterObject(const intrusive_ptr<ConfigObject>& object);
 
-       std::pair<ConfigTypeIterator<ConfigObject>, ConfigTypeIterator<ConfigObject> > GetObjects(void);
+       std::vector<intrusive_ptr<ConfigObject> > GetObjects(void) const;
 
        template<typename T>
-       static std::pair<ConfigTypeIterator<T>, ConfigTypeIterator<T> > GetObjectsByType(void)
+       static TypeImpl<T> *Get(void)
        {
-               Type::Ptr type = T::TypeInstance;
-               return std::make_pair(
-                   ConfigTypeIterator<T>(type, 0),
-                   ConfigTypeIterator<T>(type, UINT_MAX)
-               );
+               typedef TypeImpl<T> ObjType;
+               return static_cast<ObjType *>(T::TypeInstance.get());
        }
 
-private:
-       template<typename T> friend class ConfigTypeIterator;
+       template<typename T>
+       static std::vector<intrusive_ptr<T> > GetObjectsByType(void)
+       {
+               std::vector<intrusive_ptr<ConfigObject> > objects = GetObjectsHelper(T::TypeInstance.get());
+               std::vector<intrusive_ptr<T> > result;
+               BOOST_FOREACH(const intrusive_ptr<ConfigObject>& object, objects) {
+                       result.push_back(static_pointer_cast<T>(object));
+               }
+               return result;
+       }
+
+       int GetObjectCount(void) const;
 
+private:
        typedef std::map<String, intrusive_ptr<ConfigObject> > ObjectMap;
        typedef std::vector<intrusive_ptr<ConfigObject> > ObjectVector;
 
        mutable boost::mutex m_Mutex;
        ObjectMap m_ObjectMap;
        ObjectVector m_ObjectVector;
-};
-
-template<typename T>
-class ConfigTypeIterator : public boost::iterator_facade<ConfigTypeIterator<T>, const intrusive_ptr<T>, boost::forward_traversal_tag>
-{
-public:
-       ConfigTypeIterator(const Type::Ptr& type, int index)
-               : m_Type(type), m_ConfigType(dynamic_cast<ConfigType *>(type.get())), m_Index(index)
-       {
-               ASSERT(m_ConfigType);
-       }
-
-private:
-       friend class boost::iterator_core_access;
-
-       Type::Ptr m_Type;
-       ConfigType *m_ConfigType;
-       ConfigType::ObjectVector::size_type m_Index;
-       mutable intrusive_ptr<T> m_Current;
-
-       void increment(void)
-       {
-               m_Index++;
-       }
-
-       void decrement(void)
-       {
-               m_Index--;
-       }
-
-       void advance(int n)
-       {
-               m_Index += n;
-       }
-
-       bool equal(const ConfigTypeIterator<T>& other) const
-       {
-               ASSERT(other.m_Type == m_Type);
-
-               {
-                       boost::mutex::scoped_lock lock(m_ConfigType->m_Mutex);
 
-                       if ((other.m_Index == UINT_MAX || other.m_Index >= other.m_ConfigType->m_ObjectVector.size()) &&
-                           (m_Index == UINT_MAX || m_Index >= m_ConfigType->m_ObjectVector.size()))
-                               return true;
-               }
-
-               return (other.m_Index == m_Index);
-       }
-
-       const intrusive_ptr<T>& dereference(void) const
-       {
-               boost::mutex::scoped_lock lock(m_ConfigType->m_Mutex);
-               m_Current = static_pointer_cast<T>(*(m_ConfigType->m_ObjectVector.begin() + m_Index));
-               return m_Current;
-       }
+       static std::vector<intrusive_ptr<ConfigObject> > GetObjectsHelper(Type *type);
 };
+
 }
 
 #endif /* CONFIGTYPE_H */
index 9368a25bccd05da329b9252938cd8d446e28593b..b26fd1c59ff13e8a35e7cc3fcbe19b67231f3c06 100644 (file)
@@ -31,7 +31,8 @@ namespace icinga
 /**
  * A scoped lock for Objects.
  */
-struct I2_BASE_API ObjectLock {
+struct I2_BASE_API ObjectLock
+{
 public:
        inline ObjectLock(void)
                : m_Object(NULL), m_Locked(false)
index f43cae9547af748c1da069161f90f332fe44ffbf..f07ba70c5a236910f8f4e7ca9d79902ae072882d 100644 (file)
@@ -234,10 +234,10 @@ void DbConnection::UpdateProgramStatus(void)
        query3.WhereCriteria->Set("instance_id", 0);  /* DbConnection class fills in real ID */
        DbObject::OnQuery(query3);
 
-       InsertRuntimeVariable("total_services", std::distance(ConfigType::GetObjectsByType<Service>().first, ConfigType::GetObjectsByType<Service>().second));
-       InsertRuntimeVariable("total_scheduled_services", std::distance(ConfigType::GetObjectsByType<Service>().first, ConfigType::GetObjectsByType<Service>().second));
-       InsertRuntimeVariable("total_hosts", std::distance(ConfigType::GetObjectsByType<Host>().first, ConfigType::GetObjectsByType<Host>().second));
-       InsertRuntimeVariable("total_scheduled_hosts", std::distance(ConfigType::GetObjectsByType<Host>().first, ConfigType::GetObjectsByType<Host>().second));
+       InsertRuntimeVariable("total_services", ConfigType::Get<Service>()->GetObjectCount());
+       InsertRuntimeVariable("total_scheduled_services", ConfigType::Get<Service>()->GetObjectCount());
+       InsertRuntimeVariable("total_hosts", ConfigType::Get<Host>()->GetObjectCount());
+       InsertRuntimeVariable("total_scheduled_hosts", ConfigType::Get<Host>()->GetObjectCount());
 }
 
 void DbConnection::CleanUpHandler(void)
index 26d61290af2e2e1370911a768d3da323dfa38c86..d4087b8adbfd4924e2231b94b3421c0ded72048f 100644 (file)
@@ -207,12 +207,12 @@ Value StatusTable::ProgramStartAccessor(const Value&)
 
 Value StatusTable::NumHostsAccessor(const Value&)
 {
-       return std::distance(ConfigType::GetObjectsByType<Host>().first, ConfigType::GetObjectsByType<Host>().second);
+       return ConfigType::Get<Host>()->GetObjectCount();
 }
 
 Value StatusTable::NumServicesAccessor(const Value&)
 {
-       return std::distance(ConfigType::GetObjectsByType<Service>().first, ConfigType::GetObjectsByType<Service>().second);
+       return ConfigType::Get<Service>()->GetObjectCount();
 }
 
 Value StatusTable::ProgramVersionAccessor(const Value&)