2012-04-18 15:22:25 +02:00
|
|
|
#include "i2-base.h"
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
|
|
|
* GetProperty
|
|
|
|
*
|
|
|
|
* Retrieves a value from the dictionary.
|
|
|
|
*
|
|
|
|
* @param key The key.
|
|
|
|
* @param value Pointer to the value.
|
|
|
|
* @returns true if the value was retrieved, false otherwise.
|
|
|
|
*/
|
2012-04-20 13:49:04 +02:00
|
|
|
bool Dictionary::GetProperty(string key, Variant *value) const
|
2012-04-18 15:22:25 +02:00
|
|
|
{
|
2012-04-20 13:49:04 +02:00
|
|
|
ConstDictionaryIterator i = m_Data.find(key);
|
2012-04-18 15:22:25 +02:00
|
|
|
|
|
|
|
if (i == m_Data.end())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*value = i->second;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
|
|
|
* SetProperty
|
|
|
|
*
|
|
|
|
* Sets a value in the dictionary.
|
|
|
|
*
|
|
|
|
* @param key The key.
|
|
|
|
* @param value The value.
|
|
|
|
*/
|
2012-04-20 13:49:04 +02:00
|
|
|
void Dictionary::SetProperty(string key, const Variant& value)
|
2012-04-18 15:22:25 +02:00
|
|
|
{
|
2012-04-20 13:49:04 +02:00
|
|
|
DictionaryIterator i = m_Data.find(key);
|
|
|
|
|
|
|
|
Variant oldValue;
|
|
|
|
if (i != m_Data.end()) {
|
|
|
|
oldValue = i->second;
|
|
|
|
m_Data.erase(i);
|
|
|
|
}
|
|
|
|
|
2012-04-18 15:22:25 +02:00
|
|
|
m_Data[key] = value;
|
2012-04-20 13:49:04 +02:00
|
|
|
|
2012-04-22 16:45:31 +02:00
|
|
|
PropertyChangedEventArgs dpce;
|
2012-04-20 13:49:04 +02:00
|
|
|
dpce.Source = shared_from_this();
|
|
|
|
dpce.Property = key;
|
|
|
|
dpce.OldValue = oldValue;
|
|
|
|
dpce.NewValue = value;
|
|
|
|
OnPropertyChanged(dpce);
|
2012-04-18 15:22:25 +02:00
|
|
|
}
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
|
|
|
* GetPropertyString
|
|
|
|
*
|
|
|
|
* Retrieves a value from the dictionary and converts it to a string.
|
|
|
|
*
|
|
|
|
* @param key The key.
|
|
|
|
* @param value Pointer to the value.
|
|
|
|
* @returns true if the value was retrieved, false otherwise.
|
|
|
|
*/
|
2012-04-20 13:49:04 +02:00
|
|
|
bool Dictionary::GetPropertyString(string key, string *value)
|
2012-04-18 15:22:25 +02:00
|
|
|
{
|
|
|
|
Variant data;
|
|
|
|
|
2012-04-20 13:49:04 +02:00
|
|
|
if (!GetProperty(key, &data))
|
2012-04-18 15:22:25 +02:00
|
|
|
return false;
|
|
|
|
|
2012-04-18 15:51:17 +02:00
|
|
|
*value = static_cast<string>(data);
|
2012-04-18 15:22:25 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
|
|
|
* SetPropertyString
|
|
|
|
*
|
|
|
|
* Sets a value in the dictionary.
|
|
|
|
*
|
|
|
|
* @param key The key.
|
|
|
|
* @param value The value.
|
|
|
|
*/
|
2012-04-20 13:49:04 +02:00
|
|
|
void Dictionary::SetPropertyString(string key, const string& value)
|
2012-04-18 15:22:25 +02:00
|
|
|
{
|
2012-04-20 13:49:04 +02:00
|
|
|
SetProperty(key, Variant(value));
|
2012-04-18 15:22:25 +02:00
|
|
|
}
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
|
|
|
* GetPropertyInteger
|
|
|
|
*
|
|
|
|
* Retrieves a value from the dictionary and converts it to an integer.
|
|
|
|
*
|
|
|
|
* @param key The key.
|
|
|
|
* @param value Pointer to the value.
|
|
|
|
* @returns true if the value was retrieved, false otherwise.
|
|
|
|
*/
|
2012-04-20 13:49:04 +02:00
|
|
|
bool Dictionary::GetPropertyInteger(string key, long *value)
|
2012-04-18 15:22:25 +02:00
|
|
|
{
|
|
|
|
Variant data;
|
|
|
|
|
2012-04-20 13:49:04 +02:00
|
|
|
if (!GetProperty(key, &data))
|
2012-04-18 15:22:25 +02:00
|
|
|
return false;
|
|
|
|
|
2012-04-20 13:49:04 +02:00
|
|
|
*value = static_cast<long>(data);
|
2012-04-18 15:22:25 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
|
|
|
* SetPropertyInteger
|
|
|
|
*
|
|
|
|
* Sets a value in the dictionary.
|
|
|
|
*
|
|
|
|
* @param key The key.
|
|
|
|
* @param value The value.
|
|
|
|
*/
|
2012-04-20 13:49:04 +02:00
|
|
|
void Dictionary::SetPropertyInteger(string key, long value)
|
2012-04-18 15:22:25 +02:00
|
|
|
{
|
2012-04-20 13:49:04 +02:00
|
|
|
SetProperty(key, Variant(value));
|
2012-04-18 15:22:25 +02:00
|
|
|
}
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
|
|
|
* GetPropertyDictionary
|
|
|
|
*
|
|
|
|
* Retrieves a value from the dictionary and converts it to a dictionary.
|
|
|
|
*
|
|
|
|
* @param key The key.
|
|
|
|
* @param value Pointer to the value.
|
|
|
|
* @returns true if the value was retrieved, false otherwise.
|
|
|
|
*/
|
2012-04-20 13:49:04 +02:00
|
|
|
bool Dictionary::GetPropertyDictionary(string key, Dictionary::Ptr *value)
|
2012-04-18 15:22:25 +02:00
|
|
|
{
|
|
|
|
Dictionary::Ptr dictionary;
|
|
|
|
Variant data;
|
|
|
|
|
2012-04-20 13:49:04 +02:00
|
|
|
if (!GetProperty(key, &data))
|
2012-04-18 15:22:25 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
dictionary = dynamic_pointer_cast<Dictionary>(data.GetObject());
|
|
|
|
|
|
|
|
if (dictionary == NULL)
|
2012-04-30 08:22:30 +02:00
|
|
|
throw InvalidArgumentException("Property is not a dictionary.");
|
2012-04-18 15:22:25 +02:00
|
|
|
|
|
|
|
*value = dictionary;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
|
|
|
* SetPropertyDictionary
|
|
|
|
*
|
|
|
|
* Sets a value in the dictionary.
|
|
|
|
*
|
|
|
|
* @param key The key.
|
|
|
|
* @param value The value.
|
|
|
|
*/
|
2012-04-20 13:49:04 +02:00
|
|
|
void Dictionary::SetPropertyDictionary(string key, const Dictionary::Ptr& value)
|
2012-04-18 15:22:25 +02:00
|
|
|
{
|
2012-04-20 13:49:04 +02:00
|
|
|
SetProperty(key, Variant(value));
|
2012-04-18 15:22:25 +02:00
|
|
|
}
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
|
|
|
* GetPropertyObject
|
|
|
|
*
|
|
|
|
* Retrieves a value from the dictionary and converts it to an object.
|
|
|
|
*
|
|
|
|
* @param key The key.
|
|
|
|
* @param value Pointer to the value.
|
|
|
|
* @returns true if the value was retrieved, false otherwise.
|
|
|
|
*/
|
2012-04-20 13:49:04 +02:00
|
|
|
bool Dictionary::GetPropertyObject(string key, Object::Ptr *value)
|
2012-04-18 15:22:25 +02:00
|
|
|
{
|
|
|
|
Variant data;
|
|
|
|
|
2012-04-20 13:49:04 +02:00
|
|
|
if (!GetProperty(key, &data))
|
2012-04-18 15:22:25 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
*value = data;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
|
|
|
* SetPropertyObject
|
|
|
|
*
|
|
|
|
* Sets a value in the dictionary.
|
|
|
|
*
|
|
|
|
* @param key The key.
|
|
|
|
* @param value The value.
|
|
|
|
*/
|
2012-04-20 13:49:04 +02:00
|
|
|
void Dictionary::SetPropertyObject(string key, const Object::Ptr& value)
|
2012-04-18 15:22:25 +02:00
|
|
|
{
|
2012-04-20 13:49:04 +02:00
|
|
|
SetProperty(key, Variant(value));
|
2012-04-18 15:22:25 +02:00
|
|
|
}
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
|
|
|
* Begin
|
|
|
|
*
|
|
|
|
* Returns an iterator to the beginning of the dictionary.
|
|
|
|
*
|
|
|
|
* @returns An iterator.
|
|
|
|
*/
|
2012-04-18 15:22:25 +02:00
|
|
|
DictionaryIterator Dictionary::Begin(void)
|
|
|
|
{
|
|
|
|
return m_Data.begin();
|
|
|
|
}
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
|
|
|
* End
|
|
|
|
*
|
|
|
|
* Returns an iterator to the end of the dictionary.
|
|
|
|
*
|
|
|
|
* @returns An iterator.
|
|
|
|
*/
|
2012-04-18 15:22:25 +02:00
|
|
|
DictionaryIterator Dictionary::End(void)
|
|
|
|
{
|
|
|
|
return m_Data.end();
|
|
|
|
}
|
2012-04-30 15:30:45 +02:00
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
|
|
|
* GetLength
|
|
|
|
*
|
|
|
|
* Returns the number of elements in the dictionary.
|
|
|
|
*
|
|
|
|
* @returns Number of elements.
|
|
|
|
*/
|
2012-04-30 15:30:45 +02:00
|
|
|
long Dictionary::GetLength(void) const
|
|
|
|
{
|
|
|
|
return m_Data.size();
|
|
|
|
}
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
|
|
|
* AddUnnamedProperty
|
|
|
|
*
|
|
|
|
* Adds an unnamed value to the dictionary.
|
|
|
|
*
|
|
|
|
* @param value The value.
|
|
|
|
*/
|
2012-04-30 15:30:45 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
|
|
|
* AddUnnamedPropertyString
|
|
|
|
*
|
|
|
|
* Adds an unnamed value to the dictionary.
|
|
|
|
*
|
|
|
|
* @param value The value.
|
|
|
|
*/
|
2012-04-30 15:30:45 +02:00
|
|
|
void Dictionary::AddUnnamedPropertyString(const string& value)
|
|
|
|
{
|
|
|
|
AddUnnamedProperty(Variant(value));
|
|
|
|
}
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
|
|
|
* AddUnnamedPropertyInteger
|
|
|
|
*
|
|
|
|
* Adds an unnamed value to the dictionary.
|
|
|
|
*
|
|
|
|
* @param value The value.
|
|
|
|
*/
|
2012-04-30 15:30:45 +02:00
|
|
|
void Dictionary::AddUnnamedPropertyInteger(long value)
|
|
|
|
{
|
|
|
|
AddUnnamedProperty(Variant(value));
|
|
|
|
}
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
|
|
|
* AddUnnamedPropertyDictionary
|
|
|
|
*
|
|
|
|
* Adds an unnamed value to the dictionary.
|
|
|
|
*
|
|
|
|
* @param value The value.
|
|
|
|
*/
|
2012-04-30 15:30:45 +02:00
|
|
|
void Dictionary::AddUnnamedPropertyDictionary(const Dictionary::Ptr& value)
|
|
|
|
{
|
|
|
|
AddUnnamedProperty(Variant(value));
|
|
|
|
}
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
|
|
|
* AddUnnamedPropertyObject
|
|
|
|
*
|
|
|
|
* Adds an unnamed value to the dictionary.
|
|
|
|
*
|
|
|
|
* @param value The value.
|
|
|
|
*/
|
2012-04-30 15:30:45 +02:00
|
|
|
void Dictionary::AddUnnamedPropertyObject(const Object::Ptr& value)
|
|
|
|
{
|
|
|
|
AddUnnamedProperty(Variant(value));
|
|
|
|
}
|