GenericObject.cpp

From Giona

(Difference between revisions)
Line 184: Line 184:
</pre>
</pre>
 +
[[Il database Object Oriented Generico|BACK]]
[[Category:Codice Sorgente]]
[[Category:Codice Sorgente]]

Revision as of 12:18, 13 December 2007

/*
 * GenericObject.cpp
 * =================
 * 
 * Implementation of a generic object
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "bool.h"
#include "mytypes.h"
#include "GenericListNode.h"
#include "GenericList.h"
#include "GenericAttribute.h"
#include "GenericObject.h"

/*
 * Constructor
 */
GenericObject::GenericObject()
{
	this->attributeList = new GenericList();
	this->description = EMPTY_STRING;
	this->name = EMPTY_STRING;
	this->lastError = 0;
}

/*
 * Returns last error code
 */
int GenericObject::getLastError()
{
	return this->lastError;
}

/*
 * Sets the name of the object
 */
void GenericObject::setName(STRING name)
{
	this->name = name;
}

/*
 * Returns the name of the object
 */
 STRING GenericObject::getName()
 {
 	return this->name;
 }

/*
 * Returns the number of attributes
 */
int GenericObject::getAttributeNumber()
{
	return this->attributeList->getLength();
}

/*
 * Sets the description of the object
 */
void GenericObject::setDescription(STRING description)
{
	this->description = description;
}

/*
 * Returns the description of the object
 */
STRING GenericObject::getDescription()
{
	return this->description; 	
}

/*
 * Returns the value of the attribute
 */ 
STRING GenericObject::getAttributeValue(STRING name)
{
	STRING value = EMPTY_STRING;
	GenericAttribute* att = this->getAttribute(name);
	if(att!= NULL)
		value = att->getValue();
	return value;
}

/*
 * Returns the description of the attribute
 */
STRING GenericObject::getAttributeDescription(STRING description)
{
	STRING value = EMPTY_STRING;
	GenericAttribute* att = this->getAttribute(name);
	if(att!= NULL)
		value = att->getDescription();
	return value;
}

/*
 * Sets the value of the attribute
 * If the attribute with such name does not exists, a new one 
 * is created
 */
void GenericObject::setAttributeValue(STRING name, STRING value)
{
	GenericAttribute* att = this->getAttribute(name);
	if(att == NULL)
	{
		att = new GenericAttribute();
		att->setName(name);
		GenericListNode * node = new GenericListNode();
		node->setElem((genericListElemT)att);
		this->attributeList->addNode(node);
	}
	att->setValue(value);
}

/*
 * Sets the description of the attribute if it exists.
 * returns FALSE if the attribute does not exist
 */
 BOOL GenericObject::setAttributeDescription(STRING name, STRING description)
 {
 	GenericAttribute* att = this->getAttribute(name);
	if(att == NULL)
		return FALSE;
	att->setDescription(description);
	return TRUE;
 }

/*
 * Returns the array of all attribute names
 */
 STRING* GenericObject::getAttributeNameList()
 {
 	int i = 0;
 	int length = this->attributeList->getLength();
 	STRING* array= new STRING [length];
 	GenericListNode * node = this->attributeList->getHead();
 	while(node != NULL)
 	{
 		GenericAttribute * att = (GenericAttribute *) (node->getElem());
 		STRING name = att->getName();
 		array[i] = name;
 		i++;
 		node = node->getNext();
 	}
 	return array;
 }

/*
 * Private function
 * ===================
 * Returns the pointer of the attribute whose name is the one passed
 */
GenericAttribute* GenericObject::getAttribute(STRING name)
{
	GenericAttribute * att = NULL;
	GenericListNode* node = this->attributeList->getHead();
	BOOL found = FALSE;
	while(node != NULL)
	{
		att = (GenericAttribute*) node->getElem();
		STRING attName = att->getName();
		if(EQUAL_STRING(name, attName))
			return att;
		node = node->getNext();
	}
	return NULL;
}

/*
 * Destructor
 */
GenericObject::~GenericObject()
{
	delete (attributeList);
}


BACK

Personal tools