From 97fc5bb10be21781af3cb4d15d7c7b99143e10eb Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Mon, 9 Feb 2015 08:50:17 +0100 Subject: [PATCH] Implement additional attributes for the Host class refs #7564 --- lib/base/dynamicobject.cpp | 2 +- lib/base/dynamicobject.ti | 4 +-- lib/checker/checkercomponent.cpp | 2 +- lib/config/configitem.cpp | 4 +-- lib/icinga/dependency-apply.cpp | 2 +- lib/icinga/host.hpp | 6 ++--- lib/icinga/host.ti | 11 ++++++++ lib/icinga/notification-apply.cpp | 2 +- lib/icinga/scheduleddowntime-apply.cpp | 2 +- lib/icinga/service-apply.cpp | 2 +- lib/icinga/service.ti | 6 ++--- lib/remote/apilistener.cpp | 2 +- lib/remote/zone.cpp | 2 +- tools/mkclass/class_lexer.ll | 1 + tools/mkclass/class_parser.yy | 8 +++++- tools/mkclass/classcompiler.cpp | 37 +++++++++++++++++--------- tools/mkclass/classcompiler.hpp | 10 ++++--- 17 files changed, 68 insertions(+), 35 deletions(-) diff --git a/lib/base/dynamicobject.cpp b/lib/base/dynamicobject.cpp index 957ac7565..ff78cd3d1 100644 --- a/lib/base/dynamicobject.cpp +++ b/lib/base/dynamicobject.cpp @@ -53,7 +53,7 @@ DynamicObject::DynamicObject(void) DynamicType::Ptr DynamicObject::GetType(void) const { - return DynamicType::GetByName(GetTypeName()); + return DynamicType::GetByName(GetTypeNameV()); } DebugInfo DynamicObject::GetDebugInfo(void) const diff --git a/lib/base/dynamicobject.ti b/lib/base/dynamicobject.ti index fba2fb4c5..b541820ad 100644 --- a/lib/base/dynamicobject.ti +++ b/lib/base/dynamicobject.ti @@ -44,8 +44,8 @@ abstract class DynamicObject return m_ShortName; }}} }; - [config, internal, get_protected] String type (TypeName); - [config] String zone; + [config, internal, get_protected] String type (TypeNameV); + [config] String zone (ZoneName); [config, internal, get_protected] Array::Ptr templates; [get_protected] bool active; [get_protected] bool paused { diff --git a/lib/checker/checkercomponent.cpp b/lib/checker/checkercomponent.cpp index f91a5db2b..ca74315f8 100644 --- a/lib/checker/checkercomponent.cpp +++ b/lib/checker/checkercomponent.cpp @@ -258,7 +258,7 @@ void CheckerComponent::ObjectHandler(const DynamicObject::Ptr& object) Checkable::Ptr checkable = static_pointer_cast(object); - Zone::Ptr zone = Zone::GetByName(checkable->GetZone()); + Zone::Ptr zone = Zone::GetByName(checkable->GetZoneName()); bool same_zone = (!zone || Zone::GetLocalZone() == zone); { diff --git a/lib/config/configitem.cpp b/lib/config/configitem.cpp index 0bfcddf94..69e7e2d1b 100644 --- a/lib/config/configitem.cpp +++ b/lib/config/configitem.cpp @@ -160,8 +160,8 @@ DynamicObject::Ptr ConfigItem::Commit(bool discard) DynamicObject::Ptr dobj = static_pointer_cast(type->Instantiate()); dobj->SetDebugInfo(m_DebugInfo); - dobj->SetTypeName(m_Type); - dobj->SetZone(m_Zone); + dobj->SetTypeNameV(m_Type); + dobj->SetZoneName(m_Zone); dobj->SetName(m_Name); DebugHint debugHints; diff --git a/lib/icinga/dependency-apply.cpp b/lib/icinga/dependency-apply.cpp index a16c9c059..07ccf762a 100644 --- a/lib/icinga/dependency-apply.cpp +++ b/lib/icinga/dependency-apply.cpp @@ -66,7 +66,7 @@ bool Dependency::EvaluateApplyRuleInstance(const Checkable::Ptr& checkable, cons if (service) builder->AddExpression(new SetExpression(MakeIndexer(ScopeCurrent, "child_service_name"), OpSetLiteral, MakeLiteral(service->GetShortName()), di)); - String zone = checkable->GetZone(); + String zone = checkable->GetZoneName(); if (!zone.IsEmpty()) builder->AddExpression(new SetExpression(MakeIndexer(ScopeCurrent, "zone"), OpSetLiteral, MakeLiteral(zone), di)); diff --git a/lib/icinga/host.hpp b/lib/icinga/host.hpp index 96ecab05f..7ff78f6fd 100644 --- a/lib/icinga/host.hpp +++ b/lib/icinga/host.hpp @@ -51,9 +51,9 @@ public: static HostState CalculateState(ServiceState state); - HostState GetState(void) const; - HostState GetLastState(void) const; - HostState GetLastHardState(void) const; + virtual HostState GetState(void) const; + virtual HostState GetLastState(void) const; + virtual HostState GetLastHardState(void) const; double GetLastStateUp(void) const; double GetLastStateDown(void) const; diff --git a/lib/icinga/host.ti b/lib/icinga/host.ti index 310e60a8b..bf570f86c 100644 --- a/lib/icinga/host.ti +++ b/lib/icinga/host.ti @@ -36,6 +36,17 @@ class Host : Checkable [config] String address; [config] String address6; + + [enum, no_storage] HostState "state" { + get; + }; + [enum, no_storage] HostState last_state { + get; + }; + [enum, no_storage] HostState last_hard_state { + get; + }; + }; } diff --git a/lib/icinga/notification-apply.cpp b/lib/icinga/notification-apply.cpp index b6fe251b4..16a8f9d09 100644 --- a/lib/icinga/notification-apply.cpp +++ b/lib/icinga/notification-apply.cpp @@ -65,7 +65,7 @@ bool Notification::EvaluateApplyRuleInstance(const Checkable::Ptr& checkable, co if (service) builder->AddExpression(new SetExpression(MakeIndexer(ScopeCurrent, "service_name"), OpSetLiteral, MakeLiteral(service->GetShortName()), di)); - String zone = checkable->GetZone(); + String zone = checkable->GetZoneName(); if (!zone.IsEmpty()) builder->AddExpression(new SetExpression(MakeIndexer(ScopeCurrent, "zone"), OpSetLiteral, MakeLiteral(zone), di)); diff --git a/lib/icinga/scheduleddowntime-apply.cpp b/lib/icinga/scheduleddowntime-apply.cpp index 33336e852..9180ef477 100644 --- a/lib/icinga/scheduleddowntime-apply.cpp +++ b/lib/icinga/scheduleddowntime-apply.cpp @@ -64,7 +64,7 @@ bool ScheduledDowntime::EvaluateApplyRuleInstance(const Checkable::Ptr& checkabl if (service) builder->AddExpression(new SetExpression(MakeIndexer(ScopeCurrent, "service_name"), OpSetLiteral, MakeLiteral(service->GetShortName()), di)); - String zone = checkable->GetZone(); + String zone = checkable->GetZoneName(); if (!zone.IsEmpty()) builder->AddExpression(new SetExpression(MakeIndexer(ScopeCurrent, "zone"), OpSetLiteral, MakeLiteral(zone), di)); diff --git a/lib/icinga/service-apply.cpp b/lib/icinga/service-apply.cpp index 5c39e9cf0..8e28a5078 100644 --- a/lib/icinga/service-apply.cpp +++ b/lib/icinga/service-apply.cpp @@ -58,7 +58,7 @@ bool Service::EvaluateApplyRuleInstance(const Host::Ptr& host, const String& nam builder->AddExpression(new SetExpression(MakeIndexer(ScopeCurrent, "name"), OpSetLiteral, MakeLiteral(name), di)); - String zone = host->GetZone(); + String zone = host->GetZoneName(); if (!zone.IsEmpty()) builder->AddExpression(new SetExpression(MakeIndexer(ScopeCurrent, "zone"), OpSetLiteral, MakeLiteral(zone), di)); diff --git a/lib/icinga/service.ti b/lib/icinga/service.ti index bd21f718c..4533403f5 100644 --- a/lib/icinga/service.ti +++ b/lib/icinga/service.ti @@ -44,17 +44,17 @@ class Service : Checkable < ServiceNameComposer }}} }; [config] String host_name; - [enum] ServiceState "state" { + [enum, no_storage] ServiceState "state" { get {{{ return GetStateRaw(); }}} }; - [enum] ServiceState last_state { + [enum, no_storage] ServiceState last_state { get {{{ return GetLastStateRaw(); }}} }; - [enum] ServiceState last_hard_state { + [enum, no_storage] ServiceState last_hard_state { get {{{ return GetLastHardStateRaw(); }}} diff --git a/lib/remote/apilistener.cpp b/lib/remote/apilistener.cpp index 9c962dc64..d7956eecb 100644 --- a/lib/remote/apilistener.cpp +++ b/lib/remote/apilistener.cpp @@ -556,7 +556,7 @@ void ApiListener::SyncRelayMessage(const MessageOrigin& origin, const DynamicObj /* only relay the message to a) the same zone, b) the parent zone and c) direct child zones */ if (target_zone != my_zone && target_zone != my_zone->GetParent() && - secobj->GetZone() != target_zone->GetName()) { + secobj->GetZoneName() != target_zone->GetName()) { skippedEndpoints.push_back(endpoint); continue; } diff --git a/lib/remote/zone.cpp b/lib/remote/zone.cpp index 472b05710..194292cdb 100644 --- a/lib/remote/zone.cpp +++ b/lib/remote/zone.cpp @@ -60,7 +60,7 @@ bool Zone::CanAccessObject(const DynamicObject::Ptr& object) if (dynamic_pointer_cast(object)) object_zone = static_pointer_cast(object); else - object_zone = Zone::GetByName(object->GetZone()); + object_zone = Zone::GetByName(object->GetZoneName()); if (!object_zone) object_zone = Zone::GetLocalZone(); diff --git a/tools/mkclass/class_lexer.ll b/tools/mkclass/class_lexer.ll index 799a9924c..2d74276c3 100644 --- a/tools/mkclass/class_lexer.ll +++ b/tools/mkclass/class_lexer.ll @@ -141,6 +141,7 @@ get_protected { yylval->num = FAGetProtected; return T_FIELD_ATTRIBUTE; } set_protected { yylval->num = FASetProtected; return T_FIELD_ATTRIBUTE; } protected { yylval->num = FAGetProtected | FASetProtected; return T_FIELD_ATTRIBUTE; } internal { yylval->num = FAInternal; return T_FIELD_ATTRIBUTE; } +no_storage { yylval->num = FANoStorage; return T_FIELD_ATTRIBUTE; } default { yylval->num = FTDefault; return T_FIELD_ACCESSOR_TYPE; } get { yylval->num = FTGet; return T_FIELD_ACCESSOR_TYPE; } set { yylval->num = FTSet; return T_FIELD_ACCESSOR_TYPE; } diff --git a/tools/mkclass/class_parser.yy b/tools/mkclass/class_parser.yy index 5d064b42b..5f60d92b3 100644 --- a/tools/mkclass/class_parser.yy +++ b/tools/mkclass/class_parser.yy @@ -265,9 +265,11 @@ class_field: field_attribute_list identifier identifier alternative_name_specifi switch (it->Type) { case FTGet: field->GetAccessor = it->Accessor; + field->PureGetAccessor = it->Pure; break; case FTSet: field->SetAccessor = it->Accessor; + field->PureSetAccessor = it->Pure; break; case FTDefault: field->DefaultAccessor = it->Accessor; @@ -339,9 +341,13 @@ field_accessors: /* empty */ field_accessor: T_FIELD_ACCESSOR_TYPE T_STRING { - $$ = new FieldAccessor(static_cast($1), $2); + $$ = new FieldAccessor(static_cast($1), $2, false); std::free($2); } + | T_FIELD_ACCESSOR_TYPE ';' + { + $$ = new FieldAccessor(static_cast($1), "", true); + } ; identifier: T_IDENTIFIER diff --git a/tools/mkclass/classcompiler.cpp b/tools/mkclass/classcompiler.cpp index 9fa8de744..901a3835e 100644 --- a/tools/mkclass/classcompiler.cpp +++ b/tools/mkclass/classcompiler.cpp @@ -454,15 +454,21 @@ void ClassCompiler::HandleClass(const Klass& klass, const ClassDebugInfo&) prot = "public"; std::cout << prot << ":" << std::endl - << "\t" << it->Type << " Get" << it->GetFriendlyName() << "(void) const" << std::endl + << "\t" << "virtual " << it->Type << " Get" << it->GetFriendlyName() << "(void) const"; + + if (it->PureGetAccessor) { + std::cout << " = 0;" << std::endl; + } else { + std::cout << std::endl << "\t" << "{" << std::endl; - if (it->GetAccessor.empty()) - std::cout << "\t\t" << "return m_" << it->GetFriendlyName() << ";" << std::endl; - else - std::cout << it->GetAccessor << std::endl; + if (it->GetAccessor.empty() && !(it->Attributes & FANoStorage)) + std::cout << "\t\t" << "return m_" << it->GetFriendlyName() << ";" << std::endl; + else + std::cout << it->GetAccessor << std::endl; - std::cout << "\t" << "}" << std::endl << std::endl; + std::cout << "\t" << "}" << std::endl << std::endl; + } } /* setters */ @@ -484,15 +490,20 @@ void ClassCompiler::HandleClass(const Klass& klass, const ClassDebugInfo&) else std::cout << "const " << it->Type << "&"; - std::cout << " value)" << std::endl - << "\t" << "{" << std::endl; + std::cout << " value)" << std::endl; - if (it->SetAccessor.empty()) - std::cout << "\t\t" << "m_" << it->GetFriendlyName() << " = value;" << std::endl; - else - std::cout << it->SetAccessor << std::endl; + if (it->PureSetAccessor) { + std::cout << " = 0;" << std::endl; + } else { + std::cout << "\t" << "{" << std::endl; + + if (it->SetAccessor.empty() && !(it->Attributes & FANoStorage)) + std::cout << "\t\t" << "m_" << it->GetFriendlyName() << " = value;" << std::endl; + else + std::cout << it->SetAccessor << std::endl; - std::cout << "\t" << "}" << std::endl << std::endl; + std::cout << "\t" << "}" << std::endl << std::endl; + } } /* default */ diff --git a/tools/mkclass/classcompiler.hpp b/tools/mkclass/classcompiler.hpp index 04994b6d8..138ea40ea 100644 --- a/tools/mkclass/classcompiler.hpp +++ b/tools/mkclass/classcompiler.hpp @@ -48,9 +48,10 @@ struct FieldAccessor { FieldAccessorType Type; std::string Accessor; + bool Pure; - FieldAccessor(FieldAccessorType type, const std::string& accessor) - : Type(type), Accessor(accessor) + FieldAccessor(FieldAccessorType type, const std::string& accessor, bool pure) + : Type(type), Accessor(accessor), Pure(pure) { } }; @@ -61,7 +62,8 @@ enum FieldAttribute FAEnum = 4, FAGetProtected = 8, FASetProtected = 16, - FAInternal = 32 + FAInternal = 32, + FANoStorage = 64 }; struct Field @@ -71,7 +73,9 @@ struct Field std::string Name; std::string AlternativeName; std::string GetAccessor; + bool PureGetAccessor; std::string SetAccessor; + bool PureSetAccessor; std::string DefaultAccessor; std::string GetFriendlyName(void) const -- 2.40.0