Use ReflectionType instead of dynamic casts

This commit is contained in:
Jean Flach 2018-11-30 14:57:51 +01:00 committed by Michael Friedrich
parent 5cfd3c1ab0
commit dc5dc46364
1 changed files with 38 additions and 25 deletions

View File

@ -221,17 +221,20 @@ void RedisWriter::PrepareObject(const ConfigObject::Ptr& object, Dictionary::Ptr
attributes->Set("zone", ObjectsZone->GetName()); attributes->Set("zone", ObjectsZone->GetName());
} }
Endpoint::Ptr endpoint = dynamic_pointer_cast<Endpoint>(object); Type::Ptr type = object->GetReflectionType();
if (endpoint) {
if (type == Endpoint::TypeInstance) {
Endpoint::Ptr endpoint = static_pointer_cast<Endpoint>(object);
checksums->Set("properties_checksum", HashValue(attributes)); checksums->Set("properties_checksum", HashValue(attributes));
return; return;
} }
Zone::Ptr zone = dynamic_pointer_cast<Zone>(object); if (type == Zone::TypeInstance) {
if (zone) { Zone::Ptr zone = static_pointer_cast<Zone>(object);
attributes->Set("is_global", zone->GetGlobal());
attributes->Set("is_global", zone->GetGlobal());
checksums->Set("properties_checksum", HashValue(attributes)); checksums->Set("properties_checksum", HashValue(attributes));
Array::Ptr endpoints = new Array(); Array::Ptr endpoints = new Array();
@ -254,8 +257,9 @@ void RedisWriter::PrepareObject(const ConfigObject::Ptr& object, Dictionary::Ptr
return; return;
} }
Checkable::Ptr checkable = dynamic_pointer_cast<Checkable>(object); if (type == Host::TypeInstance || type == Service::TypeInstance) {
if (checkable) { Checkable::Ptr checkable = static_pointer_cast<Checkable>(object);
attributes->Set("checkcommand", checkable->GetCheckCommand()->GetName()); attributes->Set("checkcommand", checkable->GetCheckCommand()->GetName());
attributes->Set("max_check_attempts", checkable->GetMaxCheckAttempts()); attributes->Set("max_check_attempts", checkable->GetMaxCheckAttempts());
attributes->Set("check_timeout", checkable->GetCheckTimeout()); attributes->Set("check_timeout", checkable->GetCheckTimeout());
@ -344,8 +348,9 @@ void RedisWriter::PrepareObject(const ConfigObject::Ptr& object, Dictionary::Ptr
return; return;
} }
User::Ptr user = dynamic_pointer_cast<User>(object); if (type == User::TypeInstance) {
if (user) { User::Ptr user = static_pointer_cast<User>(object);
attributes->Set("display_name", user->GetDisplayName()); attributes->Set("display_name", user->GetDisplayName());
attributes->Set("email", user->GetEmail()); attributes->Set("email", user->GetEmail());
attributes->Set("pager", user->GetPager()); attributes->Set("pager", user->GetPager());
@ -380,8 +385,9 @@ void RedisWriter::PrepareObject(const ConfigObject::Ptr& object, Dictionary::Ptr
return; return;
} }
TimePeriod::Ptr timeperiod = dynamic_pointer_cast<TimePeriod>(object); if (type == TimePeriod::TypeInstance) {
if (timeperiod) { TimePeriod::Ptr timeperiod = static_pointer_cast<TimePeriod>(object);
attributes->Set("display_name", timeperiod->GetDisplayName()); attributes->Set("display_name", timeperiod->GetDisplayName());
attributes->Set("prefer_includes", timeperiod->GetPreferIncludes()); attributes->Set("prefer_includes", timeperiod->GetPreferIncludes());
@ -433,8 +439,9 @@ void RedisWriter::PrepareObject(const ConfigObject::Ptr& object, Dictionary::Ptr
return; return;
} }
Notification::Ptr notification = dynamic_pointer_cast<Notification>(object); if (type == Notification::TypeInstance) {
if (notification) { Notification::Ptr notification = static_pointer_cast<Notification>(object);
Host::Ptr host; Host::Ptr host;
Service::Ptr service; Service::Ptr service;
std::set<User::Ptr> users = notification->GetUsers(); std::set<User::Ptr> users = notification->GetUsers();
@ -487,8 +494,9 @@ void RedisWriter::PrepareObject(const ConfigObject::Ptr& object, Dictionary::Ptr
return; return;
} }
Comment::Ptr comment = dynamic_pointer_cast<Comment>(object); if (type == Comment::TypeInstance) {
if (comment) { Comment::Ptr comment = static_pointer_cast<Comment>(object);
attributes->Set("author", comment->GetAuthor()); attributes->Set("author", comment->GetAuthor());
attributes->Set("text", comment->GetText()); attributes->Set("text", comment->GetText());
attributes->Set("entry_type", comment->GetEntryType()); attributes->Set("entry_type", comment->GetEntryType());
@ -508,8 +516,9 @@ void RedisWriter::PrepareObject(const ConfigObject::Ptr& object, Dictionary::Ptr
return; return;
} }
Downtime::Ptr downtime = dynamic_pointer_cast<Downtime>(object); if (type == Downtime::TypeInstance) {
if (downtime) { Downtime::Ptr downtime = static_pointer_cast<Downtime>(object);
attributes->Set("author", downtime->GetAuthor()); attributes->Set("author", downtime->GetAuthor());
attributes->Set("comment", downtime->GetComment()); attributes->Set("comment", downtime->GetComment());
attributes->Set("entry_time", downtime->GetEntryTime()); attributes->Set("entry_time", downtime->GetEntryTime());
@ -534,8 +543,9 @@ void RedisWriter::PrepareObject(const ConfigObject::Ptr& object, Dictionary::Ptr
return; return;
} }
UserGroup::Ptr userGroup = dynamic_pointer_cast<UserGroup>(object); if (type == UserGroup::TypeInstance) {
if (userGroup) { UserGroup::Ptr userGroup = static_pointer_cast<UserGroup>(object);
attributes->Set("display_name", userGroup->GetDisplayName()); attributes->Set("display_name", userGroup->GetDisplayName());
checksums->Set("properties_checksum", HashValue(attributes)); checksums->Set("properties_checksum", HashValue(attributes));
@ -543,8 +553,9 @@ void RedisWriter::PrepareObject(const ConfigObject::Ptr& object, Dictionary::Ptr
return; return;
} }
HostGroup::Ptr hostGroup = dynamic_pointer_cast<HostGroup>(object); if (type == HostGroup::TypeInstance) {
if (hostGroup) { HostGroup::Ptr hostGroup = static_pointer_cast<HostGroup>(object);
attributes->Set("display_name", hostGroup->GetDisplayName()); attributes->Set("display_name", hostGroup->GetDisplayName());
checksums->Set("properties_checksum", HashValue(attributes)); checksums->Set("properties_checksum", HashValue(attributes));
@ -552,8 +563,9 @@ void RedisWriter::PrepareObject(const ConfigObject::Ptr& object, Dictionary::Ptr
return; return;
} }
ServiceGroup::Ptr serviceGroup = dynamic_pointer_cast<ServiceGroup>(object); if (type == ServiceGroup::TypeInstance) {
if (serviceGroup) { ServiceGroup::Ptr serviceGroup = static_pointer_cast<ServiceGroup>(object);
attributes->Set("display_name", serviceGroup->GetDisplayName()); attributes->Set("display_name", serviceGroup->GetDisplayName());
checksums->Set("properties_checksum", HashValue(attributes)); checksums->Set("properties_checksum", HashValue(attributes));
@ -561,8 +573,9 @@ void RedisWriter::PrepareObject(const ConfigObject::Ptr& object, Dictionary::Ptr
return; return;
} }
Command::Ptr command = dynamic_pointer_cast<Command>(object); if (type == CheckCommand::TypeInstance || type == NotificationCommand::TypeInstance || type == EventCommand::TypeInstance) {
if (command) { Command::Ptr command = static_pointer_cast<Command>(object);
if (dynamic_pointer_cast<CheckCommand>(object)) if (dynamic_pointer_cast<CheckCommand>(object))
attributes->Set("type", "CheckCommand"); attributes->Set("type", "CheckCommand");
else if (dynamic_pointer_cast<EventCommand>(object)) else if (dynamic_pointer_cast<EventCommand>(object))