// __author__ = Juan Carlos De Abreu (jcabreur@gmail.com)

#include "DataKey.h"
#include "../../App.h"

BEGIN_TEG_NAMESPACE();

// DataKey constructor.
DataKey::DataKey() {
	for (int i = 0; i < 256; ++i) keyFunction[i] = NULL;
}

// DataKey destructor.
DataKey::~DataKey() {
	release();
}

void DataKey::release(void) {
	// Release each function.
	for (int i = 0; i < 256; ++i) keyFunction[i] = NULL;
}

void DataKey::update(void) {
	for (int i = 0; i < 256; ++i) {
		if (KEY[i]) {
			// If the key has a related function then apply it.
			if (keyFunction[i] != NULL)
				keyFunction[i]->resolve(std::map<std::string, SmartPointer<IData>> ());
		}
	}
}

void DataKey::addKeyFunc(const int& key, SmartPointer<DataCallFunction> func) {
	// Check if is a valid key
	assert(key >= 0 && key <= 256);

	// Update the function related to the key.
	keyFunction[key] = func;
}

END_TEG_NAMESPACE();
