Merge pull request #9641 from Icinga/code-optimization-1

Some trivial code optimisations
This commit is contained in:
Julian Brost 2023-01-26 14:43:31 +01:00 committed by GitHub
commit 27af8829f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 25 additions and 22 deletions

View File

@ -15,21 +15,24 @@ boost::thread_specific_ptr<Loader::DeferredInitializerPriorityQueue>& Loader::Ge
void Loader::ExecuteDeferredInitializers()
{
if (!GetDeferredInitializers().get())
auto& initializers = GetDeferredInitializers();
if (!initializers.get())
return;
while (!GetDeferredInitializers().get()->empty()) {
DeferredInitializer initializer = GetDeferredInitializers().get()->top();
GetDeferredInitializers().get()->pop();
while (!initializers->empty()) {
DeferredInitializer initializer = initializers->top();
initializers->pop();
initializer();
}
}
void Loader::AddDeferredInitializer(const std::function<void()>& callback, InitializePriority priority)
{
if (!GetDeferredInitializers().get())
GetDeferredInitializers().reset(new Loader::DeferredInitializerPriorityQueue());
auto& initializers = GetDeferredInitializers();
if (!initializers.get()) {
initializers.reset(new Loader::DeferredInitializerPriorityQueue());
}
GetDeferredInitializers().get()->push(DeferredInitializer(callback, priority));
initializers->push(DeferredInitializer(callback, priority));
}

View File

@ -211,18 +211,18 @@ Value icinga::operator+(const Value& lhs, const Value& rhs)
return static_cast<double>(lhs) + static_cast<double>(rhs);
if ((lhs.IsString() || lhs.IsEmpty() || lhs.IsNumber()) && (rhs.IsString() || rhs.IsEmpty() || rhs.IsNumber()) && (!(lhs.IsEmpty() && rhs.IsEmpty()) || lhs.IsString() || rhs.IsString()))
return static_cast<String>(lhs) + static_cast<String>(rhs);
else if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
else if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
return static_cast<double>(lhs) + static_cast<double>(rhs);
else if (lhs.IsObjectType<DateTime>() && rhs.IsNumber())
return new DateTime(Convert::ToDateTimeValue(lhs) + rhs);
else if ((lhs.IsObjectType<Array>() || lhs.IsEmpty()) && (rhs.IsObjectType<Array>() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty())) {
else if ((lhs.IsObjectType<Array>() || lhs.IsEmpty()) && (rhs.IsObjectType<Array>() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty())) {
Array::Ptr result = new Array();
if (!lhs.IsEmpty())
static_cast<Array::Ptr>(lhs)->CopyTo(result);
if (!rhs.IsEmpty())
static_cast<Array::Ptr>(rhs)->CopyTo(result);
return result;
} else if ((lhs.IsObjectType<Dictionary>() || lhs.IsEmpty()) && (rhs.IsObjectType<Dictionary>() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty())) {
} else if ((lhs.IsObjectType<Dictionary>() || lhs.IsEmpty()) && (rhs.IsObjectType<Dictionary>() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty())) {
Dictionary::Ptr result = new Dictionary();
if (!lhs.IsEmpty())
static_cast<Dictionary::Ptr>(lhs)->CopyTo(result);
@ -262,7 +262,7 @@ Value icinga::operator-(const Value& lhs, const Value& rhs)
return new DateTime(Convert::ToDateTimeValue(lhs) - rhs);
else if (lhs.IsObjectType<DateTime>() && rhs.IsObjectType<DateTime>())
return Convert::ToDateTimeValue(lhs) - Convert::ToDateTimeValue(rhs);
else if ((lhs.IsObjectType<DateTime>() || lhs.IsEmpty()) && (rhs.IsObjectType<DateTime>() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
else if ((lhs.IsObjectType<DateTime>() || lhs.IsEmpty()) && (rhs.IsObjectType<DateTime>() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
return new DateTime(Convert::ToDateTimeValue(lhs) - Convert::ToDateTimeValue(rhs));
else if ((lhs.IsObjectType<Array>() || lhs.IsEmpty()) && (rhs.IsObjectType<Array>() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty())) {
if (lhs.IsEmpty())
@ -554,7 +554,7 @@ bool icinga::operator<(const Value& lhs, const Value& rhs)
return static_cast<String>(lhs) < static_cast<String>(rhs);
else if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
return static_cast<double>(lhs) < static_cast<double>(rhs);
else if ((lhs.IsObjectType<DateTime>() || lhs.IsEmpty()) && (rhs.IsObjectType<DateTime>() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
else if ((lhs.IsObjectType<DateTime>() || lhs.IsEmpty()) && (rhs.IsObjectType<DateTime>() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
return Convert::ToDateTimeValue(lhs) < Convert::ToDateTimeValue(rhs);
else if (lhs.IsObjectType<Array>() && rhs.IsObjectType<Array>()) {
Array::Ptr larr = lhs;
@ -607,7 +607,7 @@ bool icinga::operator>(const Value& lhs, const Value& rhs)
return static_cast<String>(lhs) > static_cast<String>(rhs);
else if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
return static_cast<double>(lhs) > static_cast<double>(rhs);
else if ((lhs.IsObjectType<DateTime>() || lhs.IsEmpty()) && (rhs.IsObjectType<DateTime>() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
else if ((lhs.IsObjectType<DateTime>() || lhs.IsEmpty()) && (rhs.IsObjectType<DateTime>() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
return Convert::ToDateTimeValue(lhs) > Convert::ToDateTimeValue(rhs);
else if (lhs.IsObjectType<Array>() && rhs.IsObjectType<Array>()) {
Array::Ptr larr = lhs;
@ -660,7 +660,7 @@ bool icinga::operator<=(const Value& lhs, const Value& rhs)
return static_cast<String>(lhs) <= static_cast<String>(rhs);
else if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
return static_cast<double>(lhs) <= static_cast<double>(rhs);
else if ((lhs.IsObjectType<DateTime>() || lhs.IsEmpty()) && (rhs.IsObjectType<DateTime>() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
else if ((lhs.IsObjectType<DateTime>() || lhs.IsEmpty()) && (rhs.IsObjectType<DateTime>() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
return Convert::ToDateTimeValue(lhs) <= Convert::ToDateTimeValue(rhs);
else
BOOST_THROW_EXCEPTION(std::invalid_argument("Operator <= cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));
@ -692,7 +692,7 @@ bool icinga::operator>=(const Value& lhs, const Value& rhs)
return static_cast<String>(lhs) >= static_cast<String>(rhs);
else if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
return static_cast<double>(lhs) >= static_cast<double>(rhs);
else if ((lhs.IsObjectType<DateTime>() || lhs.IsEmpty()) && (rhs.IsObjectType<DateTime>() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
else if ((lhs.IsObjectType<DateTime>() || lhs.IsEmpty()) && (rhs.IsObjectType<DateTime>() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
return Convert::ToDateTimeValue(lhs) >= Convert::ToDateTimeValue(rhs);
else
BOOST_THROW_EXCEPTION(std::invalid_argument("Operator >= cannot be applied to values of type '" + lhs.GetTypeName() + "' and '" + rhs.GetTypeName() + "'"));

View File

@ -148,7 +148,7 @@ public:
{
ConfigItem::Ptr item = ConfigItem::GetByTypeAndName(Type::GetByName(type), name);
if (!item || (item && item->IsAbstract()))
if (!item || item->IsAbstract())
return false;
return true;

View File

@ -22,7 +22,7 @@ public:
static thread_local CheckCommand::Ptr ExecuteOverride;
virtual void Execute(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr,
void Execute(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr,
const Dictionary::Ptr& resolvedMacros = nullptr,
bool useResolvedMacros = false);
};

View File

@ -22,7 +22,7 @@ public:
static thread_local EventCommand::Ptr ExecuteOverride;
virtual void Execute(const Checkable::Ptr& checkable,
void Execute(const Checkable::Ptr& checkable,
const Dictionary::Ptr& resolvedMacros = nullptr,
bool useResolvedMacros = false);
};

View File

@ -853,7 +853,7 @@ void ApiListener::SyncClient(const JsonRpcConnection::Ptr& aclient, const Endpoi
Zone::Ptr myZone = Zone::GetLocalZone();
auto parent (myZone->GetParent());
if (parent == eZone || !parent && eZone == myZone) {
if (parent == eZone || (!parent && eZone == myZone)) {
JsonRpcConnection::SendCertificateRequest(aclient, nullptr, String());
if (Utility::PathExists(ApiListener::GetCertificateRequestsDir())) {
@ -1325,7 +1325,7 @@ void ApiListener::OpenLogFile()
Utility::MkDirP(Utility::DirName(path), 0750);
auto *fp = new std::fstream(path.CStr(), std::fstream::out | std::ofstream::app);
std::unique_ptr<std::fstream> fp = std::make_unique<std::fstream>(path.CStr(), std::fstream::out | std::ofstream::app);
if (!fp->good()) {
Log(LogWarning, "ApiListener")
@ -1333,7 +1333,7 @@ void ApiListener::OpenLogFile()
return;
}
m_LogFile = new StdioStream(fp, true);
m_LogFile = new StdioStream(fp.release(), true);
m_LogMessageCount = 0;
SetLogMessageTimestamp(Utility::GetTime());
}

View File

@ -83,7 +83,7 @@ void ConfigObjectUtility::RepairPackage(const String& package)
String foundActiveStage;
for (fs::recursive_directory_iterator it(path); it != end; it++) {
for (fs::recursive_directory_iterator it(path); it != end; ++it) {
boost::system::error_code ec;
const fs::path d = *it;