From 2d551db9d848ca75df9bdd6713f6264affe3244e Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Tue, 16 Aug 2016 13:25:36 +0200 Subject: [PATCH] Remove the ConfigTypeIterator class refs #12448 --- lib/base/configtype.cpp | 21 +++++--- lib/base/configtype.hpp | 87 ++++++++-------------------------- lib/base/objectlock.hpp | 3 +- lib/db_ido/dbconnection.cpp | 8 ++-- lib/livestatus/statustable.cpp | 4 +- 5 files changed, 42 insertions(+), 81 deletions(-) diff --git a/lib/base/configtype.cpp b/lib/base/configtype.cpp index d43b5a07d..6c41a5e3d 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 > ConfigType::GetObjects(void) +std::vector ConfigType::GetObjects(void) const { - Type::Ptr type = dynamic_cast(this); - - return std::make_pair( - ConfigTypeIterator(type, 0), - ConfigTypeIterator(type, UINT_MAX) - ); + boost::mutex::scoped_lock lock(m_Mutex); + return m_ObjectVector; +} + +std::vector ConfigType::GetObjectsHelper(Type *type) +{ + return static_cast *>(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 f9c714588..2b2af6e6b 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 namespace icinga { class ConfigObject; -template -class ConfigTypeIterator; - class I2_BASE_API ConfigType { public: @@ -43,84 +41,39 @@ public: void RegisterObject(const intrusive_ptr& object); void UnregisterObject(const intrusive_ptr& object); - std::pair, ConfigTypeIterator > GetObjects(void); + std::vector > GetObjects(void) const; template - static std::pair, ConfigTypeIterator > GetObjectsByType(void) + static TypeImpl *Get(void) { - Type::Ptr type = T::TypeInstance; - return std::make_pair( - ConfigTypeIterator(type, 0), - ConfigTypeIterator(type, UINT_MAX) - ); + typedef TypeImpl ObjType; + return static_cast(T::TypeInstance.get()); } -private: - template friend class ConfigTypeIterator; + template + static std::vector > GetObjectsByType(void) + { + std::vector > objects = GetObjectsHelper(T::TypeInstance.get()); + std::vector > result; + BOOST_FOREACH(const intrusive_ptr& object, objects) { + result.push_back(static_pointer_cast(object)); + } + return result; + } + int GetObjectCount(void) const; + +private: typedef std::map > ObjectMap; typedef std::vector > ObjectVector; mutable boost::mutex m_Mutex; ObjectMap m_ObjectMap; ObjectVector m_ObjectVector; + + static std::vector > GetObjectsHelper(Type *type); }; -template -class ConfigTypeIterator : public boost::iterator_facade, const intrusive_ptr, boost::forward_traversal_tag> -{ -public: - ConfigTypeIterator(const Type::Ptr& type, int index) - : m_Type(type), m_ConfigType(dynamic_cast(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 m_Current; - - void increment(void) - { - m_Index++; - } - - void decrement(void) - { - m_Index--; - } - - void advance(int n) - { - m_Index += n; - } - - bool equal(const ConfigTypeIterator& 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& dereference(void) const - { - boost::mutex::scoped_lock lock(m_ConfigType->m_Mutex); - m_Current = static_pointer_cast(*(m_ConfigType->m_ObjectVector.begin() + m_Index)); - return m_Current; - } -}; } #endif /* CONFIGTYPE_H */ diff --git a/lib/base/objectlock.hpp b/lib/base/objectlock.hpp index 9368a25bc..b26fd1c59 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 f43cae954..f07ba70c5 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().first, ConfigType::GetObjectsByType().second)); - InsertRuntimeVariable("total_scheduled_services", std::distance(ConfigType::GetObjectsByType().first, ConfigType::GetObjectsByType().second)); - InsertRuntimeVariable("total_hosts", std::distance(ConfigType::GetObjectsByType().first, ConfigType::GetObjectsByType().second)); - InsertRuntimeVariable("total_scheduled_hosts", std::distance(ConfigType::GetObjectsByType().first, ConfigType::GetObjectsByType().second)); + InsertRuntimeVariable("total_services", ConfigType::Get()->GetObjectCount()); + InsertRuntimeVariable("total_scheduled_services", ConfigType::Get()->GetObjectCount()); + InsertRuntimeVariable("total_hosts", ConfigType::Get()->GetObjectCount()); + InsertRuntimeVariable("total_scheduled_hosts", ConfigType::Get()->GetObjectCount()); } void DbConnection::CleanUpHandler(void) diff --git a/lib/livestatus/statustable.cpp b/lib/livestatus/statustable.cpp index 26d61290a..d4087b8ad 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().first, ConfigType::GetObjectsByType().second); + return ConfigType::Get()->GetObjectCount(); } Value StatusTable::NumServicesAccessor(const Value&) { - return std::distance(ConfigType::GetObjectsByType().first, ConfigType::GetObjectsByType().second); + return ConfigType::Get()->GetObjectCount(); } Value StatusTable::ProgramVersionAccessor(const Value&)