icinga2/base/dictionary.cpp

158 lines
2.8 KiB
C++

#include "i2-base.h"
using namespace icinga;
bool Dictionary::GetProperty(string key, Variant *value) const
{
ConstDictionaryIterator i = m_Data.find(key);
if (i == m_Data.end())
return false;
*value = i->second;
return true;
}
void Dictionary::SetProperty(string key, const Variant& value)
{
DictionaryIterator i = m_Data.find(key);
Variant oldValue;
if (i != m_Data.end()) {
oldValue = i->second;
m_Data.erase(i);
}
m_Data[key] = value;
PropertyChangedEventArgs dpce;
dpce.Source = shared_from_this();
dpce.Property = key;
dpce.OldValue = oldValue;
dpce.NewValue = value;
OnPropertyChanged(dpce);
}
bool Dictionary::GetPropertyString(string key, string *value)
{
Variant data;
if (!GetProperty(key, &data))
return false;
*value = static_cast<string>(data);
return true;
}
void Dictionary::SetPropertyString(string key, const string& value)
{
SetProperty(key, Variant(value));
}
bool Dictionary::GetPropertyInteger(string key, long *value)
{
Variant data;
if (!GetProperty(key, &data))
return false;
*value = static_cast<long>(data);
return true;
}
void Dictionary::SetPropertyInteger(string key, long value)
{
SetProperty(key, Variant(value));
}
bool Dictionary::GetPropertyDictionary(string key, Dictionary::Ptr *value)
{
Dictionary::Ptr dictionary;
Variant data;
if (!GetProperty(key, &data))
return false;
dictionary = dynamic_pointer_cast<Dictionary>(data.GetObject());
if (dictionary == NULL)
throw InvalidArgumentException("Property is not a dictionary.");
*value = dictionary;
return true;
}
void Dictionary::SetPropertyDictionary(string key, const Dictionary::Ptr& value)
{
SetProperty(key, Variant(value));
}
bool Dictionary::GetPropertyObject(string key, Object::Ptr *value)
{
Variant data;
if (!GetProperty(key, &data))
return false;
*value = data;
return true;
}
void Dictionary::SetPropertyObject(string key, const Object::Ptr& value)
{
SetProperty(key, Variant(value));
}
DictionaryIterator Dictionary::Begin(void)
{
return m_Data.begin();
}
DictionaryIterator Dictionary::End(void)
{
return m_Data.end();
}
long Dictionary::GetLength(void) const
{
return m_Data.size();
}
void Dictionary::AddUnnamedProperty(const Variant& value)
{
map<string, Variant>::const_iterator it;
string key;
do {
long index = GetLength();
stringstream s;
s << "_" << GetLength();
key = s.str();
it = m_Data.find(key);
} while (it != m_Data.end());
m_Data[key] = value;
}
void Dictionary::AddUnnamedPropertyString(const string& value)
{
AddUnnamedProperty(Variant(value));
}
void Dictionary::AddUnnamedPropertyInteger(long value)
{
AddUnnamedProperty(Variant(value));
}
void Dictionary::AddUnnamedPropertyDictionary(const Dictionary::Ptr& value)
{
AddUnnamedProperty(Variant(value));
}
void Dictionary::AddUnnamedPropertyObject(const Object::Ptr& value)
{
AddUnnamedProperty(Variant(value));
}