[icinga-checkins] icinga.org: icinga2/master: Remove the ConfigTypeIterator class

git at icinga.org git at icinga.org
Tue Aug 16 13:26:02 CEST 2016


Module: icinga2
Branch: master
Commit: 3da05ee14bcbb00e1c5548233db7adb5b052ebc7
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=3da05ee14bcbb00e1c5548233db7adb5b052ebc7

Author: Gunnar Beutner <gunnar.beutner at netways.de>
Date:   Tue Aug 16 13:25:36 2016 +0200

Remove the ConfigTypeIterator class

refs #12448

---

 lib/base/configtype.cpp        |   19 ++++++---
 lib/base/configtype.hpp        |   85 ++++++++++------------------------------
 lib/base/objectlock.hpp        |    3 +-
 lib/db_ido/dbconnection.cpp    |    8 ++--
 lib/livestatus/statustable.cpp |    4 +-
 5 files changed, 41 insertions(+), 78 deletions(-)

diff --git a/lib/base/configtype.cpp b/lib/base/configtype.cpp
index d43b5a0..6c41a5e 100644
--- a/lib/base/configtype.cpp
+++ b/lib/base/configtype.cpp
@@ -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();
 }
diff --git a/lib/base/configtype.hpp b/lib/base/configtype.hpp
index f9c7145..51c20ef 100644
--- a/lib/base/configtype.hpp
+++ b/lib/base/configtype.hpp
@@ -24,15 +24,13 @@
 #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,18 +41,28 @@ 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());
 	}
 
+	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:
 	template<typename T> friend class ConfigTypeIterator;
 
@@ -64,63 +72,10 @@ private:
 	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 */
diff --git a/lib/base/objectlock.hpp b/lib/base/objectlock.hpp
index 9368a25..b26fd1c 100644
--- a/lib/base/objectlock.hpp
+++ b/lib/base/objectlock.hpp
@@ -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)
diff --git a/lib/db_ido/dbconnection.cpp b/lib/db_ido/dbconnection.cpp
index f43cae9..f07ba70 100644
--- a/lib/db_ido/dbconnection.cpp
+++ b/lib/db_ido/dbconnection.cpp
@@ -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)
diff --git a/lib/livestatus/statustable.cpp b/lib/livestatus/statustable.cpp
index 26d6129..d4087b8 100644
--- a/lib/livestatus/statustable.cpp
+++ b/lib/livestatus/statustable.cpp
@@ -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&)



More information about the icinga-checkins mailing list