Skip to content

Commit

Permalink
Implement modified attribute methods for DynamicObject
Browse files Browse the repository at this point in the history
refs #9081
refs #9093
  • Loading branch information
Michael Friedrich committed Aug 8, 2015
1 parent 8f3396a commit bde5535
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 6 deletions.
14 changes: 9 additions & 5 deletions lib/base/CMakeLists.txt
Expand Up @@ -23,11 +23,15 @@ mkclass_target(streamlogger.ti streamlogger.tcpp streamlogger.thpp)
mkclass_target(sysloglogger.ti sysloglogger.tcpp sysloglogger.thpp)

set(base_SOURCES
application.cpp application.thpp application-version.cpp array.cpp array-script.cpp boolean.cpp boolean-script.cpp console.cpp context.cpp
convert.cpp debuginfo.cpp dictionary.cpp dictionary-script.cpp dynamicobject.cpp dynamicobject.thpp dynamictype.cpp
exception.cpp fifo.cpp filelogger.cpp filelogger.thpp initialize.cpp json.cpp json-script.cpp loader.cpp logger.cpp logger.thpp math-script.cpp
netstring.cpp networkstream.cpp number.cpp number-script.cpp object.cpp object-script.cpp primitivetype.cpp process.cpp
ringbuffer.cpp scriptframe.cpp function.cpp function-script.cpp functionwrapper.cpp scriptglobal.cpp
application.cpp application.thpp application-version.cpp array.cpp
array-script.cpp boolean.cpp boolean-script.cpp console.cpp context.cpp
convert.cpp debuginfo.cpp dictionary.cpp dictionary-script.cpp
dynamicobject.cpp dynamicobject.thpp dynamicobject-script.cpp dynamictype.cpp
exception.cpp fifo.cpp filelogger.cpp filelogger.thpp initialize.cpp json.cpp
json-script.cpp loader.cpp logger.cpp logger.thpp math-script.cpp
netstring.cpp networkstream.cpp number.cpp number-script.cpp object.cpp
object-script.cpp primitivetype.cpp process.cpp ringbuffer.cpp scriptframe.cpp
function.cpp function-script.cpp functionwrapper.cpp scriptglobal.cpp
scriptutils.cpp serializer.cpp socket.cpp socketevents.cpp stacktrace.cpp
statsfunction.cpp stdiostream.cpp stream.cpp streamlogger.cpp streamlogger.thpp string.cpp string-script.cpp
sysloglogger.cpp sysloglogger.thpp tcpsocket.cpp thinmutex.cpp threadpool.cpp timer.cpp
Expand Down
35 changes: 34 additions & 1 deletion lib/base/dynamicobject.cpp
Expand Up @@ -41,7 +41,7 @@

using namespace icinga;

REGISTER_TYPE(DynamicObject);
REGISTER_TYPE_WITH_PROTOTYPE(DynamicObject, DynamicObject::GetPrototype());

boost::signals2::signal<void (const DynamicObject::Ptr&)> DynamicObject::OnStarted;
boost::signals2::signal<void (const DynamicObject::Ptr&)> DynamicObject::OnStopped;
Expand Down Expand Up @@ -99,6 +99,39 @@ void DynamicObject::ClearExtension(const String& key)
extensions->Remove(key);
}

void DynamicObject::ModifyAttribute(const String& attr, const Value& value)
{
Dictionary::Ptr original_attributes = GetOriginalAttributes();

if (!original_attributes) {
original_attributes = new Dictionary();
SetOriginalAttributes(original_attributes);
}

int field = GetReflectionType()->GetFieldId(attr);

Value attrVal = GetField(field);

if (!original_attributes->Contains(attr))
original_attributes->Set(attr, attrVal);

SetField(field, value);
//TODO: validation, vars.os
}

void DynamicObject::RestoreAttribute(const String& attr)
{
Dictionary::Ptr original_attributes = GetOriginalAttributes();

if (!original_attributes || !original_attributes->Contains(attr))
return;

Value attrVal = original_attributes->Get(attr);

SetField(GetReflectionType()->GetFieldId(attr), attrVal);
original_attributes->Remove(attr);
}

void DynamicObject::Register(void)
{
ASSERT(!OwnsLock());
Expand Down
5 changes: 5 additions & 0 deletions lib/base/dynamicobject.hpp
Expand Up @@ -57,6 +57,9 @@ class I2_BASE_API DynamicObject : public ObjectImpl<DynamicObject>
Value GetExtension(const String& key);
void ClearExtension(const String& key);

void ModifyAttribute(const String& attr, const Value& value);
void RestoreAttribute(const String& attr);

void Register(void);

void Activate(void);
Expand Down Expand Up @@ -86,6 +89,8 @@ class I2_BASE_API DynamicObject : public ObjectImpl<DynamicObject>
static void RestoreObjects(const String& filename, int attributeTypes = FAState);
static void StopObjects(void);

static Object::Ptr GetPrototype(void);

protected:
explicit DynamicObject(void);

Expand Down
1 change: 1 addition & 0 deletions lib/base/dynamicobject.ti
Expand Up @@ -83,6 +83,7 @@ abstract class DynamicObject : DynamicObjectBase
[protected] Dictionary::Ptr extensions;

[protected] bool state_loaded;
Dictionary::Ptr original_attributes;
};

}
14 changes: 14 additions & 0 deletions lib/base/type.hpp
Expand Up @@ -137,6 +137,20 @@ class TypeImpl
} } \
DEFINE_TYPE_INSTANCE(type)

#define REGISTER_TYPE_WITH_PROTOTYPE(type, prototype) \
namespace { namespace UNIQUE_NAME(rt) { \
void RegisterType ## type(void) \
{ \
icinga::Type::Ptr t = new TypeImpl<type>(); \
t->SetPrototype(prototype); \
type::TypeInstance = t; \
icinga::Type::Register(t); \
} \
\
INITIALIZE_ONCE_WITH_PRIORITY(RegisterType ## type, 10); \
} } \
DEFINE_TYPE_INSTANCE(type)

#define DEFINE_TYPE_INSTANCE(type) \
Type::Ptr type::TypeInstance

Expand Down

0 comments on commit bde5535

Please sign in to comment.