GenericObject.cpp

From Giona

(Difference between revisions)
Line 3: Line 3:
  * GenericObject.cpp
  * GenericObject.cpp
  * =================
  * =================
-
*
 
  * Implementation of a generic object
  * Implementation of a generic object
 +
* Author: Rosario Marino
  */
  */
-
#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"
#include "GenericObject.h"
Line 23: Line 14:
GenericObject::GenericObject()
GenericObject::GenericObject()
{
{
 +
Debug::debug("GenericObject::GenericObject()");
this->attributeList = new GenericList();
this->attributeList = new GenericList();
this->description = EMPTY_STRING;
this->description = EMPTY_STRING;
this->name = EMPTY_STRING;
this->name = EMPTY_STRING;
-
this->lastError = 0;
+
this->lastError = ERROR_NO;
}
}
Line 34: Line 26:
int GenericObject::getLastError()
int GenericObject::getLastError()
{
{
 +
Debug::debug("int GenericObject::getLastError()");
return this->lastError;
return this->lastError;
}
}
Line 40: Line 33:
  * Sets the name of the object
  * Sets the name of the object
  */
  */
-
void GenericObject::setName(STRING name)
+
bool GenericObject::setName(STRING name)
{
{
 +
Debug::debug("bool GenericObject::setName(STRING name)");
 +
if(name==NULL)
 +
{
 +
Debug::debug("ERROR: ERROR_NULL_PARAMETER");
 +
this->lastError=ERROR_NULL_PARAMETER;
 +
return false;
 +
}
 +
if(strcmp(name, EMPTY_STRING)==0)
 +
{
 +
Debug::debug("ERROR: ERROR_EMPTY_STRING");
 +
this->lastError=ERROR_EMPTY_STRING;
 +
return false;
 +
}
this->name = name;
this->name = name;
 +
return true;
}
}
Line 50: Line 57:
  STRING GenericObject::getName()
  STRING GenericObject::getName()
  {
  {
 +
Debug::debug("STRING GenericObject::getName()");
  return this->name;
  return this->name;
  }
  }
Line 58: Line 66:
int GenericObject::getAttributeNumber()
int GenericObject::getAttributeNumber()
{
{
 +
Debug::debug("int GenericObject::getAttributeNumber()");
return this->attributeList->getLength();
return this->attributeList->getLength();
}
}
Line 64: Line 73:
  * Sets the description of the object
  * Sets the description of the object
  */
  */
-
void GenericObject::setDescription(STRING description)
+
bool GenericObject::setDescription(STRING description)
{
{
 +
Debug::debug("bool GenericObject::setDescription(STRING description)");
 +
if(description==NULL)
 +
{
 +
Debug::debug("ERROR: ERROR_NULL_PARAMETER");
 +
this->lastError=ERROR_NULL_PARAMETER;
 +
return false;
 +
}
 +
if(strcmp(description, EMPTY_STRING)==0)
 +
{
 +
Debug::debug("ERROR: ERROR_EMPTY_STRING");
 +
this->lastError=ERROR_EMPTY_STRING;
 +
return false;
 +
}
this->description = description;
this->description = description;
 +
return true;
}
}
Line 74: Line 97:
STRING GenericObject::getDescription()
STRING GenericObject::getDescription()
{
{
 +
Debug::debug("STRING GenericObject::getDescription()");
return this->description;
return this->description;
}
}
/*
/*
-
  * Returns the value of the attribute
+
  * Returns the value of the attribute.
 +
* Returns NULL in case of error
  */  
  */  
STRING GenericObject::getAttributeValue(STRING name)
STRING GenericObject::getAttributeValue(STRING name)
{
{
 +
Debug::debug("STRING GenericObject::getAttributeValue(STRING name)");
 +
if(name==NULL)
 +
{
 +
Debug::debug("ERROR: ERROR_NULL_PARAMETER");
 +
this->lastError=ERROR_NULL_PARAMETER;
 +
return NULL;
 +
}
 +
if(strcmp(name, EMPTY_STRING)==0)
 +
{
 +
Debug::debug("ERROR: ERROR_EMPTY_STRING");
 +
this->lastError=ERROR_EMPTY_STRING;
 +
return NULL;
 +
}
 +
STRING value = EMPTY_STRING;
STRING value = EMPTY_STRING;
GenericAttribute* att = this->getAttribute(name);
GenericAttribute* att = this->getAttribute(name);
if(att!= NULL)
if(att!= NULL)
 +
{
value = att->getValue();
value = att->getValue();
-
return value;
+
return value;
 +
}
 +
Debug::debug("ERROR: ERROR_ATTRIBUTE_NOT_FOUND");
 +
this->lastError=ERROR_ATTRIBUTE_NOT_FOUND;
 +
return NULL;
}
}
/*
/*
-
  * Returns the description of the attribute
+
  * Returns the description of the attribute. Returns NULL in case of error
  */
  */
-
STRING GenericObject::getAttributeDescription(STRING description)
+
STRING GenericObject::getAttributeDescription(STRING name)
{
{
 +
Debug::debug("STRING GenericObject::getAttributeDescription(STRING name)");
 +
if(name==NULL)
 +
{
 +
Debug::debug("ERROR: ERROR_NULL_PARAMETER");
 +
this->lastError=ERROR_NULL_PARAMETER;
 +
return NULL;
 +
}
 +
if(strcmp(name, EMPTY_STRING)==0)
 +
{
 +
Debug::debug("ERROR: ERROR_EMPTY_STRING");
 +
this->lastError=ERROR_EMPTY_STRING;
 +
return NULL;
 +
}
STRING value = EMPTY_STRING;
STRING value = EMPTY_STRING;
GenericAttribute* att = this->getAttribute(name);
GenericAttribute* att = this->getAttribute(name);
if(att!= NULL)
if(att!= NULL)
 +
{
value = att->getDescription();
value = att->getDescription();
-
return value;
+
return value;
 +
}
 +
Debug::debug("ERROR: ERROR_ATTRIBUTE_NOT_FOUND");
 +
this->lastError=ERROR_ATTRIBUTE_NOT_FOUND;
 +
return NULL;
}
}
Line 104: Line 166:
  * Sets the value of the attribute
  * Sets the value of the attribute
  * If the attribute with such name does not exists, a new one  
  * If the attribute with such name does not exists, a new one  
-
  * is created
+
  * is created. Returns false in case of error.
  */
  */
-
void GenericObject::setAttributeValue(STRING name, STRING value)
+
bool GenericObject::setAttributeValue(STRING name, STRING value)
{
{
 +
Debug::debug("void GenericObject::setAttributeValue(STRING name, STRING value)");
 +
if(name==NULL)
 +
{
 +
Debug::debug("ERROR: ERROR_NULL_PARAMETER - name");
 +
this->lastError=ERROR_NULL_PARAMETER;
 +
return false;
 +
}
 +
if(strcmp(name, EMPTY_STRING)==0)
 +
{
 +
Debug::debug("ERROR: ERROR_EMPTY_STRING - name");
 +
this->lastError=ERROR_EMPTY_STRING;
 +
return false;
 +
}
 +
if(value==NULL)
 +
{
 +
Debug::debug("ERROR: ERROR_NULL_PARAMETER - value");
 +
this->lastError=ERROR_NULL_PARAMETER;
 +
return false;
 +
}
 +
if(strcmp(value, EMPTY_STRING)==0)
 +
{
 +
Debug::debug("ERROR: ERROR_EMPTY_STRING - value");
 +
this->lastError=ERROR_EMPTY_STRING;
 +
return false;
 +
}
 +
GenericAttribute* att = this->getAttribute(name);
GenericAttribute* att = this->getAttribute(name);
if(att == NULL)
if(att == NULL)
{
{
att = new GenericAttribute();
att = new GenericAttribute();
-
att->setName(name);
+
if (!att->setName(name))
 +
{
 +
Debug::debug("ERROR in setting attribute name");
 +
return false;
 +
}
GenericListNode * node = new GenericListNode();
GenericListNode * node = new GenericListNode();
node->setElem((genericListElemT)att);
node->setElem((genericListElemT)att);
-
this->attributeList->addNode(node);
+
if(!this->attributeList->addNode(node))
 +
{
 +
Debug::debug("ERROR in adding attribute in attribute list");
 +
return false;
 +
}
 +
}
 +
if(!att->setValue(value))
 +
{
 +
Debug::debug("ERROR in setting attribute value");
 +
return false;
}
}
-
att->setValue(value);
+
return true;
}
}
Line 124: Line 225:
  * returns FALSE if the attribute does not exist
  * returns FALSE if the attribute does not exist
  */
  */
-
BOOL GenericObject::setAttributeDescription(STRING name, STRING description)
+
bool GenericObject::setAttributeDescription(STRING name, STRING description)
  {
  {
 +
Debug::debug("bool GenericObject::setAttributeDescription(STRING name, STRING description)");
 +
if(name==NULL)
 +
{
 +
Debug::debug("ERROR: ERROR_NULL_PARAMETER - name");
 +
this->lastError=ERROR_NULL_PARAMETER;
 +
return false;
 +
}
 +
if(strcmp(name, EMPTY_STRING)==0)
 +
{
 +
Debug::debug("ERROR: ERROR_EMPTY_STRING - name");
 +
this->lastError=ERROR_EMPTY_STRING;
 +
return false;
 +
}
  GenericAttribute* att = this->getAttribute(name);
  GenericAttribute* att = this->getAttribute(name);
if(att == NULL)
if(att == NULL)
-
return FALSE;
+
{
-
att->setDescription(description);
+
Debug::debug("ERROR: ERROR_ATTRIBUTE_NOT_FOUND");
-
return TRUE;
+
this->lastError=ERROR_ATTRIBUTE_NOT_FOUND;
 +
return false;
 +
}
 +
if (!att->setDescription(description))
 +
{
 +
Debug::debug("ERROR in setting attribute description");
 +
return false;
 +
}
 +
return true;
  }
  }
Line 138: Line 260:
  STRING* GenericObject::getAttributeNameList()
  STRING* GenericObject::getAttributeNameList()
  {
  {
 +
Debug::debug("STRING* GenericObject::getAttributeNameList()");
  int i = 0;
  int i = 0;
  int length = this->attributeList->getLength();
  int length = this->attributeList->getLength();
Line 162: Line 285:
GenericAttribute * att = NULL;
GenericAttribute * att = NULL;
GenericListNode* node = this->attributeList->getHead();
GenericListNode* node = this->attributeList->getHead();
-
BOOL found = FALSE;
+
bool found = false;
while(node != NULL)
while(node != NULL)
{
{
att = (GenericAttribute*) node->getElem();
att = (GenericAttribute*) node->getElem();
STRING attName = att->getName();
STRING attName = att->getName();
-
if(EQUAL_STRING(name, attName))
+
if(strcmp(name, attName)==0)
return att;
return att;
node = node->getNext();
node = node->getNext();
Line 179: Line 302:
GenericObject::~GenericObject()
GenericObject::~GenericObject()
{
{
 +
Debug::debug("GenericObject::~GenericObject()");
delete (attributeList);
delete (attributeList);
}
}
-
 
</pre>
</pre>
[[Il database Object Oriented Generico|BACK]]
[[Il database Object Oriented Generico|BACK]]
[[Category:Codice Sorgente]]
[[Category:Codice Sorgente]]

Revision as of 16:58, 13 December 2007

/*
 * GenericObject.cpp
 * =================
 * Implementation of a generic object
 * Author: Rosario Marino
 */

#include "GenericObject.h"

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

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

/*
 * Sets the name of the object
 */
bool GenericObject::setName(STRING name)
{
	Debug::debug("bool GenericObject::setName(STRING name)");
	if(name==NULL)
	{
		Debug::debug("ERROR: ERROR_NULL_PARAMETER");
		this->lastError=ERROR_NULL_PARAMETER;
		return false;
	}
	if(strcmp(name, EMPTY_STRING)==0)
	{
		Debug::debug("ERROR: ERROR_EMPTY_STRING");
		this->lastError=ERROR_EMPTY_STRING;
		return false;
	}
	this->name = name;
	return true;
}

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

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

/*
 * Sets the description of the object
 */
bool GenericObject::setDescription(STRING description)
{
	Debug::debug("bool GenericObject::setDescription(STRING description)");
	if(description==NULL)
	{
		Debug::debug("ERROR: ERROR_NULL_PARAMETER");
		this->lastError=ERROR_NULL_PARAMETER;
		return false;
	}
	if(strcmp(description, EMPTY_STRING)==0)
	{
		Debug::debug("ERROR: ERROR_EMPTY_STRING");
		this->lastError=ERROR_EMPTY_STRING;
		return false;
	}
	this->description = description;
	return true;
}

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

/*
 * Returns the value of the attribute. 
 * Returns NULL in case of error
 */ 
STRING GenericObject::getAttributeValue(STRING name)
{
	Debug::debug("STRING GenericObject::getAttributeValue(STRING name)");
	if(name==NULL)
	{
		Debug::debug("ERROR: ERROR_NULL_PARAMETER");
		this->lastError=ERROR_NULL_PARAMETER;
		return NULL;
	}
	if(strcmp(name, EMPTY_STRING)==0)
	{
		Debug::debug("ERROR: ERROR_EMPTY_STRING");
		this->lastError=ERROR_EMPTY_STRING;
		return NULL;
	}
	
	STRING value = EMPTY_STRING;
	GenericAttribute* att = this->getAttribute(name);
	if(att!= NULL)
	{
		value = att->getValue();
		return value;
	}
	Debug::debug("ERROR: ERROR_ATTRIBUTE_NOT_FOUND");
	this->lastError=ERROR_ATTRIBUTE_NOT_FOUND;
	return NULL;
}

/*
 * Returns the description of the attribute. Returns NULL in case of error
 */
STRING GenericObject::getAttributeDescription(STRING name)
{
	Debug::debug("STRING GenericObject::getAttributeDescription(STRING name)");
	if(name==NULL)
	{
		Debug::debug("ERROR: ERROR_NULL_PARAMETER");
		this->lastError=ERROR_NULL_PARAMETER;
		return NULL;
	}
	if(strcmp(name, EMPTY_STRING)==0)
	{
		Debug::debug("ERROR: ERROR_EMPTY_STRING");
		this->lastError=ERROR_EMPTY_STRING;
		return NULL;
	}
	STRING value = EMPTY_STRING;
	GenericAttribute* att = this->getAttribute(name);
	if(att!= NULL)
	{
		value = att->getDescription();
		return value;
	}
	Debug::debug("ERROR: ERROR_ATTRIBUTE_NOT_FOUND");
	this->lastError=ERROR_ATTRIBUTE_NOT_FOUND;
	return NULL;
}

/*
 * Sets the value of the attribute
 * If the attribute with such name does not exists, a new one 
 * is created. Returns false in case of error.
 */
bool GenericObject::setAttributeValue(STRING name, STRING value)
{
	Debug::debug("void GenericObject::setAttributeValue(STRING name, STRING value)");
	if(name==NULL)
	{
		Debug::debug("ERROR: ERROR_NULL_PARAMETER - name");
		this->lastError=ERROR_NULL_PARAMETER;
		return false;
	}
	if(strcmp(name, EMPTY_STRING)==0)
	{
		Debug::debug("ERROR: ERROR_EMPTY_STRING - name");
		this->lastError=ERROR_EMPTY_STRING;
		return false;
	}
	if(value==NULL)
	{
		Debug::debug("ERROR: ERROR_NULL_PARAMETER - value");
		this->lastError=ERROR_NULL_PARAMETER;
		return false;
	}
	if(strcmp(value, EMPTY_STRING)==0)
	{
		Debug::debug("ERROR: ERROR_EMPTY_STRING - value");
		this->lastError=ERROR_EMPTY_STRING;
		return false;
	}

	GenericAttribute* att = this->getAttribute(name);
	if(att == NULL)
	{
		att = new GenericAttribute();
		if (!att->setName(name))
		{
			Debug::debug("ERROR in setting attribute name");
			return false;
		}
		GenericListNode * node = new GenericListNode();
		node->setElem((genericListElemT)att);
		if(!this->attributeList->addNode(node))
		{
			Debug::debug("ERROR in adding attribute in attribute list");
			return false;			
		}
	}
	if(!att->setValue(value))
	{
		Debug::debug("ERROR in setting attribute value");
		return false;
	}
	return true;
}

/*
 * Sets the description of the attribute if it exists.
 * returns FALSE if the attribute does not exist
 */
bool GenericObject::setAttributeDescription(STRING name, STRING description)
 {
	Debug::debug("bool GenericObject::setAttributeDescription(STRING name, STRING description)");
	if(name==NULL)
	{
		Debug::debug("ERROR: ERROR_NULL_PARAMETER - name");
		this->lastError=ERROR_NULL_PARAMETER;
		return false;
	}
	if(strcmp(name, EMPTY_STRING)==0)
	{
		Debug::debug("ERROR: ERROR_EMPTY_STRING - name");
		this->lastError=ERROR_EMPTY_STRING;
		return false;
	}
 	GenericAttribute* att = this->getAttribute(name);
	if(att == NULL)
	{
		Debug::debug("ERROR: ERROR_ATTRIBUTE_NOT_FOUND");
		this->lastError=ERROR_ATTRIBUTE_NOT_FOUND;
		return false;
	}
	if (!att->setDescription(description))
	{
		Debug::debug("ERROR in setting attribute description");
		return false;
	}
	return true;
 }

/*
 * Returns the array of all attribute names
 */
 STRING* GenericObject::getAttributeNameList()
 {
	Debug::debug("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(strcmp(name, attName)==0)
			return att;
		node = node->getNext();
	}
	return NULL;
}

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

BACK

Personal tools