Remove the ConfigTypeIterator class

refs #12448
This commit is contained in:
Gunnar Beutner 2016-08-16 13:25:36 +02:00
parent 9a455f0c3d
commit 2d551db9d8
5 changed files with 42 additions and 81 deletions

View 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);
return std::make_pair(
ConfigTypeIterator<ConfigObject>(type, 0),
ConfigTypeIterator<ConfigObject>(type, UINT_MAX)
);
boost::mutex::scoped_lock lock(m_Mutex);
return m_ObjectVector;
}
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();
}

View File

@ -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,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;
static std::vector<intrusive_ptr<ConfigObject> > GetObjectsHelper(Type *type);
};
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;
}
};
}
#endif /* CONFIGTYPE_H */

View 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)

View 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)

View 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&)