From: Gunnar Beutner Date: Mon, 16 Jul 2012 20:00:50 +0000 (+0200) Subject: Use BOOST_FOREACH for most for loops. X-Git-Tag: v0.0.1~218 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e2bf3bd56b9f188b260df547b4299ff681c8d95a;p=icinga2 Use BOOST_FOREACH for most for loops. --- diff --git a/base/application.cpp b/base/application.cpp index 253d9d3e4..fe17a1dda 100644 --- a/base/application.cpp +++ b/base/application.cpp @@ -61,9 +61,9 @@ Application::~Application(void) m_ShuttingDown = true; /* stop all components */ - for (map::iterator i = m_Components.begin(); - i != m_Components.end(); i++) { - i->second->Stop(); + Component::Ptr component; + BOOST_FOREACH(tie(tuples::ignore, component), m_Components) { + component->Stop(); } m_Components.clear(); @@ -243,8 +243,8 @@ string Application::GetExePath(void) const boost::algorithm::split(paths, pathEnv, boost::is_any_of(":")); bool foundPath = false; - for (vector::iterator it = paths.begin(); it != paths.end(); it++) { - string pathTest = *it + "/" + argv0; + BOOST_FOREACH(string& path, paths) { + string pathTest = path + "/" + argv0; if (access(pathTest.c_str(), X_OK) == 0) { executablePath = pathTest; diff --git a/base/dictionary.h b/base/dictionary.h index 28736d013..919a60870 100644 --- a/base/dictionary.h +++ b/base/dictionary.h @@ -34,7 +34,6 @@ public: typedef shared_ptr Ptr; typedef weak_ptr WeakPtr; - typedef map::const_iterator ConstIterator; typedef map::iterator Iterator; /** @@ -47,7 +46,7 @@ public: template bool Get(const string& key, T *value) const { - ConstIterator i = m_Data.find(key); + map::const_iterator i = m_Data.find(key); if (i == m_Data.end()) return false; @@ -110,6 +109,33 @@ private: map m_Data; }; +inline Dictionary::Iterator range_begin(Dictionary::Ptr x) +{ + return x->Begin(); +} + +inline Dictionary::Iterator range_end(Dictionary::Ptr x) +{ + return x->End(); +} + +} + +namespace boost +{ + +template<> +struct range_mutable_iterator +{ + typedef icinga::Dictionary::Iterator type; +}; + +template<> +struct range_const_iterator +{ + typedef icinga::Dictionary::Iterator type; +}; + } #endif /* DICTIONARY_H */ diff --git a/base/event.cpp b/base/event.cpp index d3692f60e..83ac4f015 100644 --- a/base/event.cpp +++ b/base/event.cpp @@ -44,9 +44,9 @@ void Event::ProcessEvents(const system_time& wait_until) events.swap(m_Events); } - vector::iterator it; - for (it = events.begin(); it != events.end(); it++) - it->m_Callback(); + BOOST_FOREACH(const Event& ev, events) { + ev.m_Callback(); + } } void Event::Post(const function& callback) diff --git a/base/i2-base.h b/base/i2-base.h index e953777cf..94db9f57a 100644 --- a/base/i2-base.h +++ b/base/i2-base.h @@ -122,6 +122,8 @@ using std::type_info; #include #include #include +#include +#include using boost::shared_ptr; using boost::weak_ptr; @@ -134,6 +136,9 @@ using boost::thread_group; using boost::mutex; using boost::condition_variable; using boost::system_time; +using boost::tie; + +namespace tuples = boost::tuples; #if defined(__APPLE__) && defined(__MACH__) # pragma GCC diagnostic ignored "-Wdeprecated-declarations" diff --git a/base/logger.cpp b/base/logger.cpp index 591fe7952..7b177af5a 100644 --- a/base/logger.cpp +++ b/base/logger.cpp @@ -93,10 +93,7 @@ LogSeverity Logger::GetMinSeverity(void) const */ void Logger::ForwardLogEntry(const LogEntry& entry) { - set::iterator it; - for (it = m_Loggers.begin(); it != m_Loggers.end(); it++) { - Logger::Ptr logger = *it; - + BOOST_FOREACH(const Logger::Ptr& logger, m_Loggers) { if (entry.Severity >= logger->GetMinSeverity()) logger->ProcessLogEntry(entry); } diff --git a/base/objectmap.h b/base/objectmap.h index df9e14da0..b275fc2ce 100644 --- a/base/objectmap.h +++ b/base/objectmap.h @@ -47,8 +47,9 @@ public: m_Parent->OnObjectCommitted.connect(boost::bind(&ObjectMap::ObjectCommittedHandler, this, _2)); m_Parent->OnObjectRemoved.connect(boost::bind(&ObjectMap::ObjectRemovedHandler, this, _2)); - for (typename ObjectSet::Iterator it = m_Parent->Begin(); it != m_Parent->End(); it++) - AddObject(*it); + BOOST_FOREACH(const TValue& object, m_Parent) { + AddObject(object); + } } Range GetRange(TKey key) @@ -60,8 +61,8 @@ public: { Range range = GetRange(key); - for (Iterator it = range.first; it != range.second; it++) { - callback(GetSelf(), *it); + BOOST_FOREACH(const TValue& object, range) { + callback(GetSelf(), object); } } @@ -83,16 +84,16 @@ private: { TKey key; if (!m_KeyGetter(object, &key)) - return; + return; - pair range = GetRange(key); + pair range = GetRange(key); - for (Iterator i = range.first; i != range.second; i++) { - if (i->second == object) { - m_Objects.erase(i); - break; - } - } + for (Iterator i = range.first; i != range.second; i++) { + if (i->second == object) { + m_Objects.erase(i); + break; + } + } } void CheckObject(const TValue& object) @@ -117,6 +118,35 @@ private: } }; +template +typename ObjectMap::Iterator range_begin(typename ObjectMap::Ptr x) +{ + return x->Begin(); +} + +template +typename ObjectSet::Iterator range_end(typename ObjectMap::Ptr x) +{ + return x->End(); +} + +} + +namespace boost +{ + +template +struct range_mutable_iterator > > +{ + typedef typename shared_ptr >::Iterator type; +}; + +template +struct range_const_iterator > > +{ + typedef typename shared_ptr > type; +}; + } #endif /* OBJECTMAP_H */ diff --git a/base/objectset.h b/base/objectset.h index 8441428f9..26cba0870 100644 --- a/base/objectset.h +++ b/base/objectset.h @@ -47,9 +47,10 @@ public: m_Parent->OnObjectCommitted.connect(boost::bind(&ObjectSet::ObjectAddedOrCommittedHandler, this, _2)); m_Parent->OnObjectRemoved.connect(boost::bind(&ObjectSet::ObjectRemovedHandler, this, _2)); - for (ObjectSet::Iterator it = m_Parent->Begin(); it != m_Parent->End(); it++) - CheckObject(*it); - } + BOOST_FOREACH(const TValue& object, m_Parent) { + CheckObject(object); + } + } } void AddObject(const TValue& object) @@ -102,8 +103,8 @@ public: void ForeachObject(function callback) { - for (Iterator it = Begin(); it != End(); it++) { - callback(GetSelf(), *it); + BOOST_FOREACH(const TValue& object, m_Objects) { + callback(GetSelf(), object); } } @@ -124,6 +125,35 @@ private: } }; +template +typename ObjectSet::Iterator range_begin(shared_ptr > x) +{ + return x->Begin(); +} + +template +typename ObjectSet::Iterator range_end(shared_ptr > x) +{ + return x->End(); +} + +} + +namespace boost +{ + +template +struct range_mutable_iterator > > +{ + typedef typename icinga::ObjectSet::Iterator type; +}; + +template +struct range_const_iterator > > +{ + typedef typename icinga::ObjectSet::Iterator type; +}; + } #endif /* OBJECTSET_H */ diff --git a/base/process.cpp b/base/process.cpp index cc641f80d..4b24e3881 100644 --- a/base/process.cpp +++ b/base/process.cpp @@ -66,11 +66,12 @@ void Process::WorkerThreadProc(void) FD_ZERO(&readfds); - for (it = tasks.begin(); it != tasks.end(); it++) { - if (it->first > nfds) - nfds = it->first; + int fd; + BOOST_FOREACH(tie(fd, tuples::ignore), tasks); + if (fd > nfds) + nfds = fd; - FD_SET(it->first, &readfds); + FD_SET(fd, &readfds); } timeval tv; diff --git a/cib/cib.vcxproj b/cib/cib.vcxproj index 50c9fdbc7..3b5f858f8 100644 --- a/cib/cib.vcxproj +++ b/cib/cib.vcxproj @@ -90,6 +90,7 @@ + @@ -106,6 +107,7 @@ + diff --git a/cib/cib.vcxproj.filters b/cib/cib.vcxproj.filters index 049b0d688..81e391f32 100644 --- a/cib/cib.vcxproj.filters +++ b/cib/cib.vcxproj.filters @@ -44,6 +44,9 @@ Headerdateien + + Headerdateien + @@ -79,5 +82,8 @@ Quelldateien + + Quelldateien + \ No newline at end of file diff --git a/cib/host.cpp b/cib/host.cpp index 2b59cc98f..9b697c082 100644 --- a/cib/host.cpp +++ b/cib/host.cpp @@ -69,9 +69,9 @@ set Host::GetParents(void) const if (GetProperty("dependencies", &dependencies)) { dependencies = Service::ResolveDependencies(*this, dependencies); - Dictionary::Iterator it; - for (it = dependencies->Begin(); it != dependencies->End(); it++) { - Service service = Service::GetByName(it->second); + string dependency; + BOOST_FOREACH(tie(tuples::ignore, dependency), dependencies) { + Service service = Service::GetByName(dependency); string parent = service.GetHost().GetName(); @@ -99,9 +99,9 @@ bool Host::IsReachable(void) const if (GetProperty("dependencies", &dependencies)) { dependencies = Service::ResolveDependencies(*this, dependencies); - Dictionary::Iterator it; - for (it = dependencies->Begin(); it != dependencies->End(); it++) { - Service service = Service::GetByName(it->second); + string dependency; + BOOST_FOREACH(tie(tuples::ignore, dependency), dependencies) { + Service service = Service::GetByName(dependency); if (!service.IsReachable() || (service.GetState() != StateOK && service.GetState() != StateWarning)) { @@ -119,9 +119,10 @@ bool Host::IsUp(void) const if (GetProperty("hostchecks", &hostchecks)) { hostchecks = Service::ResolveDependencies(*this, hostchecks); - Dictionary::Iterator it; - for (it = hostchecks->Begin(); it != hostchecks->End(); it++) { - Service service = Service::GetByName(it->second); + string hostcheck; + BOOST_FOREACH(tie(tuples::ignore, hostcheck), hostchecks) { + Service service = Service::GetByName(hostcheck); + if (service.GetState() != StateOK && service.GetState() != StateWarning) { return false; } diff --git a/cib/macroprocessor.cpp b/cib/macroprocessor.cpp index de5a25228..4bd4aeb2f 100644 --- a/cib/macroprocessor.cpp +++ b/cib/macroprocessor.cpp @@ -38,10 +38,8 @@ string MacroProcessor::ResolveMacros(const string& str, const vector::const_iterator it; - for (it = macroDicts.begin(); it != macroDicts.end(); it++) { - Dictionary::Ptr macros = *it; - if (macros && macros->Get(name, &value)) { + BOOST_FOREACH(const Dictionary::Ptr& macroDict, macroDicts) { + if (macroDict && macroDict->Get(name, &value)) { resolved = true; break; } diff --git a/cib/nagioschecktask.cpp b/cib/nagioschecktask.cpp index c8493df6d..6be960c70 100644 --- a/cib/nagioschecktask.cpp +++ b/cib/nagioschecktask.cpp @@ -110,10 +110,7 @@ void NagiosCheckTask::ProcessCheckOutput(CheckResult& result, const string& outp vector lines; boost::algorithm::split(lines, output, is_any_of("\r\n")); - vector::iterator it; - for (it = lines.begin(); it != lines.end(); it++) { - const string& line = *it; - + BOOST_FOREACH (const string& line, lines) { string::size_type delim = line.find('|'); if (!text.empty()) diff --git a/cib/nullchecktask.cpp b/cib/nullchecktask.cpp index f0ca1b9a0..479bbc95e 100644 --- a/cib/nullchecktask.cpp +++ b/cib/nullchecktask.cpp @@ -39,8 +39,8 @@ void NullCheckTask::ScriptFunc(const ScriptTask::Ptr& task, const vectorFinishResult(cr.GetDictionary()); } -void NullCheckTask::Register(void) +void NullCheckTask::Register(void) { - ScriptFunction::Ptr func = boost::make_shared(&NullCheckTask::ScriptFunc); - ScriptFunction::Register("native::NullCheck", func); + ScriptFunction::Ptr func = boost::make_shared(&NullCheckTask::ScriptFunc); + ScriptFunction::Register("native::NullCheck", func); } diff --git a/cib/service.cpp b/cib/service.cpp index 8092e024c..33773203e 100644 --- a/cib/service.cpp +++ b/cib/service.cpp @@ -117,14 +117,14 @@ void Service::GetDependenciesRecursive(const Dictionary::Ptr& result) const { if (!dependencies) return; - Dictionary::Iterator it; - for (it = dependencies->Begin(); it != dependencies->End(); it++) { - if (result->Contains(it->second)) + string dependency; + BOOST_FOREACH(tie(tuples::ignore, dependency), dependencies) { + if (result->Contains(dependency)) continue; - result->Set(it->second, it->second); + result->Set(dependency, dependency); - Service service = Service::GetByName(it->second); + Service service = Service::GetByName(dependency); service.GetDependenciesRecursive(result); } } @@ -148,9 +148,9 @@ bool Service::IsReachable(void) const Dictionary::Ptr dependencies = boost::make_shared(); GetDependenciesRecursive(dependencies); - Dictionary::Iterator it; - for (it = dependencies->Begin(); it != dependencies->End(); it++) { - Service service = Service::GetByName(it->second); + string dependency; + BOOST_FOREACH(tie(tuples::ignore, dependency), dependencies) { + Service service = Service::GetByName(dependency); /* ignore ourselves */ if (service.GetName() == GetName()) @@ -379,10 +379,8 @@ bool Service::IsAllowedChecker(const string& checker) const if (!checkers) return true; - Dictionary::Iterator it; - for (it = checkers->Begin(); it != checkers->End(); it++) { - string pattern = it->second; - + string pattern; + BOOST_FOREACH(tie(tuples::ignore, pattern), checkers) { if (Utility::Match(pattern, checker)) return true; } @@ -397,14 +395,14 @@ Dictionary::Ptr Service::ResolveDependencies(Host host, const Dictionary::Ptr& d Dictionary::Ptr result = boost::make_shared(); - Dictionary::Iterator it; - for (it = dependencies->Begin(); it != dependencies->End(); it++) { + string dependency; + BOOST_FOREACH(tie(tuples::ignore, dependency), dependencies) { string name; - if (services && services->Contains(it->first)) - name = host.GetName() + "-" + it->first; + if (services && services->Contains(dependency)) + name = host.GetName() + "-" + dependency; else - name = it->first; + name = dependency; result->Set(name, name); } diff --git a/components/cibsync/cibsynccomponent.cpp b/components/cibsync/cibsynccomponent.cpp index 7c0d27c47..9ff939603 100644 --- a/components/cibsync/cibsynccomponent.cpp +++ b/components/cibsync/cibsynccomponent.cpp @@ -171,9 +171,7 @@ void CIBSyncComponent::FetchObjectsHandler(const Endpoint::Ptr& sender) { ConfigObject::Set::Ptr allObjects = ConfigObject::GetAllObjects(); - for (ConfigObject::Set::Iterator ci = allObjects->Begin(); ci != allObjects->End(); ci++) { - ConfigObject::Ptr object = *ci; - + BOOST_FOREACH(const ConfigObject::Ptr& object, allObjects) { if (!ShouldReplicateObject(object)) continue; diff --git a/components/compat/compatcomponent.cpp b/components/compat/compatcomponent.cpp index 37473b6f8..0d759368f 100644 --- a/components/compat/compatcomponent.cpp +++ b/components/compat/compatcomponent.cpp @@ -148,7 +148,7 @@ void CompatComponent::DumpServiceStatus(ofstream& fp, Service service) state = StateUnknown; fp << "servicestatus {" << "\n" - << "\t" << "host_name=" << service.GetHost().GetName() << "\n" + << "\t" << "host_name=" << service.GetHost().GetName() << "\n" << "\t" << "service_description=" << service.GetAlias() << "\n" << "\t" << "check_interval=" << service.GetCheckInterval() / 60.0 << "\n" << "\t" << "retry_interval=" << service.GetRetryInterval() / 60.0 << "\n" @@ -233,31 +233,34 @@ void CompatComponent::StatusTimerHandler(void) ConfigObject::TMap::Range range; range = ConfigObject::GetObjects("host"); - ConfigObject::TMap::Iterator it; - for (it = range.first; it != range.second; it++) { - Host host = it->second; + ConfigObject::Ptr object; + BOOST_FOREACH(tie(tuples::ignore, object), range) { + Host host = object; Dictionary::Ptr dict; - Dictionary::Iterator dt; - dict = host.GetGroups(); if (dict) { - for (dt = dict->Begin(); dt != dict->End(); dt++) - hostgroups[dt->second].push_back(host.GetName()); + string hostgroup; + BOOST_FOREACH(tie(tuples::ignore, hostgroup), dict) { + hostgroups[hostgroup].push_back(host.GetName()); + } } DumpHostStatus(statusfp, host); DumpHostObject(objectfp, host); } - map >::iterator hgt; - for (hgt = hostgroups.begin(); hgt != hostgroups.end(); hgt++) { + pair > hgt; + BOOST_FOREACH(hgt, hostgroups) { + const string& name = hgt.first; + const vector& hosts = hgt.second; + objectfp << "define hostgroup {" << "\n" - << "\t" << "hostgroup_name" << "\t" << hgt->first << "\n"; + << "\t" << "hostgroup_name" << "\t" << name << "\n"; - if (HostGroup::Exists(hgt->first)) { - HostGroup hg = HostGroup::GetByName(hgt->first); + if (HostGroup::Exists(name)) { + HostGroup hg = HostGroup::GetByName(name); objectfp << "\t" << "alias" << "\t" << hg.GetAlias() << "\n" << "\t" << "notes_url" << "\t" << hg.GetNotesUrl() << "\n" << "\t" << "action_url" << "\t" << hg.GetActionUrl() << "\n"; @@ -265,7 +268,7 @@ void CompatComponent::StatusTimerHandler(void) objectfp << "\t" << "members" << "\t"; - DumpStringList(objectfp, hgt->second); + DumpStringList(objectfp, hosts); objectfp << "\n" << "}" << "\n"; @@ -275,30 +278,34 @@ void CompatComponent::StatusTimerHandler(void) map > servicegroups; - for (it = range.first; it != range.second; it++) { - Service service = it->second; + BOOST_FOREACH(tie(tuples::ignore, object), range) { + Service service = object; Dictionary::Ptr dict; - Dictionary::Iterator dt; dict = service.GetGroups(); if (dict) { - for (dt = dict->Begin(); dt != dict->End(); dt++) - servicegroups[dt->second].push_back(service); + string servicegroup; + BOOST_FOREACH(tie(tuples::ignore, servicegroup), dict) { + servicegroups[servicegroup].push_back(service); + } } DumpServiceStatus(statusfp, service); DumpServiceObject(objectfp, service); } - map >::iterator sgt; - for (sgt = servicegroups.begin(); sgt != servicegroups.end(); sgt++) { + pair > sgt; + BOOST_FOREACH(sgt, servicegroups) { + const string& name = sgt.first; + const vector& services = sgt.second; + objectfp << "define servicegroup {" << "\n" - << "\t" << "servicegroup_name" << "\t" << sgt->first << "\n"; + << "\t" << "servicegroup_name" << "\t" << name << "\n"; - if (ServiceGroup::Exists(sgt->first)) { - ServiceGroup sg = ServiceGroup::GetByName(sgt->first); + if (ServiceGroup::Exists(name)) { + ServiceGroup sg = ServiceGroup::GetByName(name); objectfp << "\t" << "alias" << "\t" << sg.GetAlias() << "\n" << "\t" << "notes_url" << "\t" << sg.GetNotesUrl() << "\n" << "\t" << "action_url" << "\t" << sg.GetActionUrl() << "\n"; @@ -309,9 +316,9 @@ void CompatComponent::StatusTimerHandler(void) vector sglist; vector::iterator vt; - for (vt = sgt->second.begin(); vt != sgt->second.end(); vt++) { - sglist.push_back(vt->GetHost().GetName()); - sglist.push_back(vt->GetAlias()); + BOOST_FOREACH(const Service& service, services) { + sglist.push_back(service.GetHost().GetName()); + sglist.push_back(service.GetAlias()); } DumpStringList(objectfp, sglist); diff --git a/components/configfile/configfilecomponent.cpp b/components/configfile/configfilecomponent.cpp index e0d753f48..6f9a2f718 100644 --- a/components/configfile/configfilecomponent.cpp +++ b/components/configfile/configfilecomponent.cpp @@ -40,9 +40,7 @@ void ConfigFileComponent::Start(void) Logger::Write(LogInformation, "configfile", "Executing config items..."); - vector::iterator it; - for (it = configItems.begin(); it != configItems.end(); it++) { - ConfigItem::Ptr item = *it; + BOOST_FOREACH(const ConfigItem::Ptr& item, configItems) { item->Commit(); } } diff --git a/components/convenience/conveniencecomponent.cpp b/components/convenience/conveniencecomponent.cpp index 8c7632fc7..afcfcea92 100644 --- a/components/convenience/conveniencecomponent.cpp +++ b/components/convenience/conveniencecomponent.cpp @@ -111,11 +111,9 @@ void ConvenienceComponent::HostCommittedHandler(const ConfigItem::Ptr& item) host->GetProperty("services", &serviceDescs); if (serviceDescs) { - Dictionary::Iterator it; - for (it = serviceDescs->Begin(); it != serviceDescs->End(); it++) { - string svcname = it->first; - Variant svcdesc = it->second; - + string svcname; + Variant svcdesc; + BOOST_FOREACH(tie(svcname, svcdesc), serviceDescs) { stringstream namebuf; namebuf << item->GetName() << "-" << svcname; string name = namebuf.str(); @@ -152,10 +150,8 @@ void ConvenienceComponent::HostCommittedHandler(const ConfigItem::Ptr& item) } if (oldServices) { - Dictionary::Iterator it; - for (it = oldServices->Begin(); it != oldServices->End(); it++) { - ConfigItem::Ptr service = it->second; - + ConfigItem::Ptr service; + BOOST_FOREACH(tie(tuples::ignore, service), oldServices) { if (!newServices->Contains(service->GetName())) service->Unregister(); } @@ -180,9 +176,8 @@ void ConvenienceComponent::HostRemovedHandler(const ConfigItem::Ptr& item) if (!services) return; - Dictionary::Iterator it; - for (it = services->Begin(); it != services->End(); it++) { - ConfigItem::Ptr service = it->second; + ConfigItem::Ptr service; + BOOST_FOREACH(tie(tuples::ignore, service), services) { service->Unregister(); } } diff --git a/components/delegation/delegationcomponent.cpp b/components/delegation/delegationcomponent.cpp index fc4ac880f..d061e0759 100644 --- a/components/delegation/delegationcomponent.cpp +++ b/components/delegation/delegationcomponent.cpp @@ -161,9 +161,8 @@ void DelegationComponent::SessionEstablishedHandler(const Endpoint::Ptr& endpoin return; /* locally clear checker for all services that previously belonged to this endpoint */ - ConfigObject::Set::Iterator it; - for (it = m_AllServices->Begin(); it != m_AllServices->End(); it++) { - Service service = *it; + BOOST_FOREACH(const ConfigObject::Ptr& object, m_AllServices) { + Service service = object; if (service.GetChecker() == endpoint->GetIdentity()) service.SetChecker(""); @@ -184,9 +183,8 @@ void DelegationComponent::DelegationTimerHandler(void) vector services; /* build "checker -> service count" histogram */ - ConfigObject::Set::Iterator it; - for (it = m_AllServices->Begin(); it != m_AllServices->End(); it++) { - Service service = *it; + BOOST_FOREACH(const ConfigObject::Ptr& object, m_AllServices) { + Service service = object; services.push_back(service); @@ -207,10 +205,7 @@ void DelegationComponent::DelegationTimerHandler(void) int delegated = 0; /* re-assign services */ - vector::iterator sit; - for (sit = services.begin(); sit != services.end(); sit++) { - Service service = *sit; - + BOOST_FOREACH(Service& service, services) { string checker = service.GetChecker(); Endpoint::Ptr oldEndpoint; @@ -229,8 +224,9 @@ void DelegationComponent::DelegationTimerHandler(void) msgbuf << "Service: " << service.GetName() << ", candidates: " << candidates.size(); Logger::Write(LogDebug, "delegation", msgbuf.str()); - for (cit = candidates.begin(); cit != candidates.end(); cit++) - avg_services += histogram[*cit]; + BOOST_FOREACH(const Endpoint::Ptr& candidate, candidates) { + avg_services += histogram[candidate]; + } avg_services /= candidates.size(); overflow_tolerance = candidates.size() * 2; @@ -253,15 +249,13 @@ void DelegationComponent::DelegationTimerHandler(void) } /* find a new checker for the service */ - for (cit = candidates.begin(); cit != candidates.end(); cit++) { - Endpoint::Ptr newEndpoint = *cit; - + BOOST_FOREACH(const Endpoint::Ptr& candidate, candidates) { /* does this checker already have too many services */ - if (histogram[newEndpoint] > avg_services) + if (histogram[candidate] > avg_services) continue; - service.SetChecker(newEndpoint->GetIdentity()); - histogram[newEndpoint]++; + service.SetChecker(candidate->GetIdentity()); + histogram[candidate]++; delegated++; @@ -271,29 +265,30 @@ void DelegationComponent::DelegationTimerHandler(void) assert(candidates.size() == 0 || !service.GetChecker().empty()); } - map::iterator hit; - for (hit = histogram.begin(); hit != histogram.end(); hit++) { + Endpoint::Ptr endpoint; + int count; + BOOST_FOREACH(tie(endpoint, count), histogram) { stringstream msgbuf; - msgbuf << "histogram: " << hit->first->GetIdentity() << " - " << hit->second; + msgbuf << "histogram: " << endpoint->GetIdentity() << " - " << count; Logger::Write(LogInformation, "delegation", msgbuf.str()); } if (delegated > 0) { if (need_clear) { - map::iterator hit; - for (hit = histogram.begin(); hit != histogram.end(); hit++) { - ClearServices(hit->first); + Endpoint::Ptr endpoint; + BOOST_FOREACH(tie(endpoint, tuples::ignore), histogram) { + ClearServices(endpoint); } } - for (sit = services.begin(); sit != services.end(); sit++) { - string checker = sit->GetChecker(); + BOOST_FOREACH(Service& service, services) { + string checker = service.GetChecker(); Endpoint::Ptr endpoint = EndpointManager::GetInstance()->GetEndpointByIdentity(checker); if (!endpoint) continue; - AssignService(endpoint, *sit); + AssignService(endpoint, service); } } diff --git a/components/discovery/discoverycomponent.cpp b/components/discovery/discoverycomponent.cpp index 2b7102e3b..29811a2ae 100644 --- a/components/discovery/discoverycomponent.cpp +++ b/components/discovery/discoverycomponent.cpp @@ -168,12 +168,13 @@ void DiscoveryComponent::NewEndpointHandler(const Endpoint::Ptr& endpoint) // register published/subscribed topics for this endpoint ComponentDiscoveryInfo::Ptr info = ic->second; - set::iterator it; - for (it = info->Publications.begin(); it != info->Publications.end(); it++) - endpoint->RegisterPublication(*it); + BOOST_FOREACH(string publication, info->Publications) { + endpoint->RegisterPublication(publication); + } - for (it = info->Subscriptions.begin(); it != info->Subscriptions.end(); it++) - endpoint->RegisterSubscription(*it); + BOOST_FOREACH(string subscription, info->Subscriptions) { + endpoint->RegisterSubscription(subscription); + } FinishDiscoverySetup(endpoint); } @@ -299,15 +300,17 @@ void DiscoveryComponent::SendDiscoveryMessage(const string& method, const string } set::iterator i; - MessagePart subscriptions; - for (i = info->Subscriptions.begin(); i != info->Subscriptions.end(); i++) - subscriptions.Add(*i); + Dictionary::Ptr subscriptions = boost::make_shared(); + BOOST_FOREACH(string subscription, info->Subscriptions) { + subscriptions->Add(subscription); + } params.SetSubscriptions(subscriptions); - MessagePart publications; - for (i = info->Publications.begin(); i != info->Publications.end(); i++) - publications.Add(*i); + Dictionary::Ptr publications = boost::make_shared(); + BOOST_FOREACH(string publication, info->Publications) { + publications->Add(publication); + } params.SetPublications(publications); @@ -324,15 +327,15 @@ bool DiscoveryComponent::HasMessagePermission(const Dictionary::Ptr& roles, cons ConfigObject::TMap::Range range = ConfigObject::GetObjects("role"); - for (ConfigObject::TMap::Iterator ip = range.first; ip != range.second; ip++) { - ConfigObject::Ptr role = ip->second; - + ConfigObject::Ptr role; + BOOST_FOREACH(tie(tuples::ignore, role), range) { Dictionary::Ptr permissions; if (!role->GetProperty(messageType, &permissions)) continue; - for (Dictionary::Iterator is = permissions->Begin(); is != permissions->End(); is++) { - if (Utility::Match(is->second, message)) + string permission; + BOOST_FOREACH(tie(tuples::ignore, permission), permissions) { + if (Utility::Match(permission, message)) return true; } } @@ -373,26 +376,26 @@ void DiscoveryComponent::ProcessDiscoveryMessage(const string& identity, const D Endpoint::Ptr endpoint = EndpointManager::GetInstance()->GetEndpointByIdentity(identity); - MessagePart publications; + Dictionary::Ptr publications; if (message.GetPublications(&publications)) { - Dictionary::Iterator i; - for (i = publications.Begin(); i != publications.End(); i++) { - if (trusted || HasMessagePermission(roles, "publications", i->second)) { - info->Publications.insert(i->second); + string publication; + BOOST_FOREACH(tie(tuples::ignore, publication), publications) { + if (trusted || HasMessagePermission(roles, "publications", publication)) { + info->Publications.insert(publication); if (endpoint) - endpoint->RegisterPublication(i->second); + endpoint->RegisterPublication(publication); } } } - MessagePart subscriptions; + Dictionary::Ptr subscriptions; if (message.GetSubscriptions(&subscriptions)) { - Dictionary::Iterator i; - for (i = subscriptions.Begin(); i != subscriptions.End(); i++) { - if (trusted || HasMessagePermission(roles, "subscriptions", i->second)) { - info->Subscriptions.insert(i->second); + string subscription; + BOOST_FOREACH(tie(tuples::ignore, subscription), subscriptions) { + if (trusted || HasMessagePermission(roles, "subscriptions", subscription)) { + info->Subscriptions.insert(subscription); if (endpoint) - endpoint->RegisterSubscription(i->second); + endpoint->RegisterSubscription(subscription); } } } @@ -456,9 +459,8 @@ void DiscoveryComponent::DiscoveryTimerHandler(void) /* check whether we have to reconnect to one of our upstream endpoints */ ConfigObject::TMap::Range range = ConfigObject::GetObjects("endpoint"); - for (ConfigObject::TMap::Iterator it = range.first; it != range.second; it++) { - ConfigObject::Ptr object = it->second; - + ConfigObject::Ptr object; + BOOST_FOREACH(tie(tuples::ignore, object), range) { /* Check if we're already connected to this endpoint. */ if (endpointManager->GetEndpointByIdentity(object->GetName())) continue; @@ -472,8 +474,8 @@ void DiscoveryComponent::DiscoveryTimerHandler(void) map::iterator curr, i; for (i = m_Components.begin(); i != m_Components.end(); ) { - string identity = i->first; - ComponentDiscoveryInfo::Ptr info = i->second; + const string& identity = i->first; + const ComponentDiscoveryInfo::Ptr& info = i->second; curr = i; i++; diff --git a/components/discovery/discoverymessage.cpp b/components/discovery/discoverymessage.cpp index ccc0b7840..354cf01df 100644 --- a/components/discovery/discoverymessage.cpp +++ b/components/discovery/discoverymessage.cpp @@ -59,22 +59,22 @@ void DiscoveryMessage::SetService(const string& value) Set("service", value); } -bool DiscoveryMessage::GetSubscriptions(MessagePart *value) const +bool DiscoveryMessage::GetSubscriptions(Dictionary::Ptr *value) const { return Get("subscriptions", value); } -void DiscoveryMessage::SetSubscriptions(MessagePart value) +void DiscoveryMessage::SetSubscriptions(const Dictionary::Ptr& value) { Set("subscriptions", value); } -bool DiscoveryMessage::GetPublications(MessagePart *value) const +bool DiscoveryMessage::GetPublications(Dictionary::Ptr *value) const { return Get("publications", value); } -void DiscoveryMessage::SetPublications(MessagePart value) +void DiscoveryMessage::SetPublications(const Dictionary::Ptr& value) { Set("publications", value); } diff --git a/components/discovery/discoverymessage.h b/components/discovery/discoverymessage.h index cb304b03d..9cce48a90 100644 --- a/components/discovery/discoverymessage.h +++ b/components/discovery/discoverymessage.h @@ -41,11 +41,11 @@ public: bool GetService(string *value) const; void SetService(const string& value); - bool GetSubscriptions(MessagePart *value) const; - void SetSubscriptions(MessagePart value); + bool GetSubscriptions(Dictionary::Ptr *value) const; + void SetSubscriptions(const Dictionary::Ptr& value); - bool GetPublications(MessagePart *value) const; - void SetPublications(MessagePart value); + bool GetPublications(Dictionary::Ptr *value) const; + void SetPublications(const Dictionary::Ptr& value); }; } diff --git a/dyn/configcompiler.cpp b/dyn/configcompiler.cpp index e951ff6ef..ec1013676 100644 --- a/dyn/configcompiler.cpp +++ b/dyn/configcompiler.cpp @@ -47,7 +47,7 @@ void *ConfigCompiler::GetScanner(void) const vector ConfigCompiler::GetResult(void) const { - return m_Result; + return m_Result; } string ConfigCompiler::GetPath(void) const diff --git a/dyn/configitem.cpp b/dyn/configitem.cpp index faea417ed..3d7295e4d 100644 --- a/dyn/configitem.cpp +++ b/dyn/configitem.cpp @@ -56,13 +56,12 @@ vector ConfigItem::GetParents(void) const void ConfigItem::CalculateProperties(Dictionary::Ptr dictionary) const { - vector::const_iterator it; - for (it = m_Parents.begin(); it != m_Parents.end(); it++) { - ConfigItem::Ptr parent = ConfigItem::GetObject(GetType(), *it); + BOOST_FOREACH(const string& name, m_Parents) { + ConfigItem::Ptr parent = ConfigItem::GetObject(GetType(), name); if (!parent) { stringstream message; - message << "Parent object '" << *it << "' does not exist (" << m_DebugInfo << ")"; + message << "Parent object '" << name << "' does not exist (" << m_DebugInfo << ")"; throw domain_error(message.str()); } @@ -76,12 +75,12 @@ ConfigItem::Set::Ptr ConfigItem::GetAllObjects(void) { static ObjectSet::Ptr allObjects; - if (!allObjects) { - allObjects = boost::make_shared >(); - allObjects->Start(); - } + if (!allObjects) { + allObjects = boost::make_shared >(); + allObjects->Start(); + } - return allObjects; + return allObjects; } bool ConfigItem::GetTypeAndName(const ConfigItem::Ptr& object, pair *key) diff --git a/dyn/expression.cpp b/dyn/expression.cpp index 9dc567646..3f720133d 100644 --- a/dyn/expression.cpp +++ b/dyn/expression.cpp @@ -81,9 +81,10 @@ void Expression::Execute(const Dictionary::Ptr& dictionary) const if (valueExprl) { valueExprl->Execute(dict); } else if (valueDict) { - Dictionary::Iterator it; - for (it = valueDict->Begin(); it != valueDict->End(); it++) { - dict->Set(it->first, it->second); + string key; + Variant value; + BOOST_FOREACH(tie(key, value), valueDict) { + dict->Set(key, value); } } else { stringstream message; diff --git a/dyn/expressionlist.cpp b/dyn/expressionlist.cpp index c9d504875..41cf019ea 100644 --- a/dyn/expressionlist.cpp +++ b/dyn/expressionlist.cpp @@ -21,10 +21,6 @@ using namespace icinga; -ExpressionList::ExpressionList(void) -{ -} - void ExpressionList::AddExpression(const Expression& expression) { m_Expressions.push_back(expression); @@ -37,9 +33,7 @@ size_t ExpressionList::GetLength(void) const void ExpressionList::Execute(const Dictionary::Ptr& dictionary) const { - vector::const_iterator it; - - for (it = m_Expressions.begin(); it != m_Expressions.end(); it++) { - it->Execute(dictionary); + BOOST_FOREACH(const Expression& expression, m_Expressions) { + expression.Execute(dictionary); } } diff --git a/dyn/expressionlist.h b/dyn/expressionlist.h index 527ec9e0c..d0364e550 100644 --- a/dyn/expressionlist.h +++ b/dyn/expressionlist.h @@ -29,17 +29,12 @@ public: typedef shared_ptr Ptr; typedef weak_ptr WeakPtr; - ExpressionList(void); -// ExpressionList(Dictionary::Ptr serializedDictionary); - void AddExpression(const Expression& expression); void Execute(const Dictionary::Ptr& dictionary) const; size_t GetLength(void) const; -// Dictionary::Ptr Serialize(void); - private: vector m_Expressions; }; diff --git a/icinga/endpointmanager.cpp b/icinga/endpointmanager.cpp index 40b9c6886..272819e35 100644 --- a/icinga/endpointmanager.cpp +++ b/icinga/endpointmanager.cpp @@ -240,10 +240,8 @@ void EndpointManager::SendAnycastMessage(Endpoint::Ptr sender, throw invalid_argument("Message is missing the 'method' property."); vector candidates; - for (map::iterator i = m_Endpoints.begin(); i != m_Endpoints.end(); i++) - { - Endpoint::Ptr endpoint = i->second; - + Endpoint::Ptr endpoint; + BOOST_FOREACH(tie(tuples::ignore, endpoint), m_Endpoints) { /* don't forward messages between non-local endpoints */ if (!sender->IsLocal() && !endpoint->IsLocal()) continue; @@ -277,11 +275,8 @@ void EndpointManager::SendMulticastMessage(Endpoint::Ptr sender, if (!message.GetMethod(&method)) throw invalid_argument("Message is missing the 'method' property."); - map::iterator i; - for (i = m_Endpoints.begin(); i != m_Endpoints.end(); i++) - { - Endpoint::Ptr recipient = i->second; - + Endpoint::Ptr recipient; + BOOST_FOREACH(tie(tuples::ignore, recipient), m_Endpoints) { /* don't forward messages back to the sender */ if (sender == recipient) continue; diff --git a/jsonrpc/jsonrpcclient.cpp b/jsonrpc/jsonrpcclient.cpp index 1fda13274..6d0871b22 100644 --- a/jsonrpc/jsonrpcclient.cpp +++ b/jsonrpc/jsonrpcclient.cpp @@ -48,16 +48,11 @@ void JsonRpcClient::SendMessage(const MessagePart& message) */ void JsonRpcClient::DataAvailableHandler(void) { - for (;;) { - string jsonString; - MessagePart message; - - if (!Netstring::ReadStringFromIOQueue(this, &jsonString)) - return; + string jsonString; + while (Netstring::ReadStringFromIOQueue(this, &jsonString)) { try { - message = MessagePart(jsonString); - OnNewMessage(GetSelf(), message); + OnNewMessage(GetSelf(), MessagePart(jsonString)); } catch (const exception& ex) { Logger::Write(LogCritical, "jsonrpc", "Exception while processing message from JSON-RPC client: " + string(ex.what())); }