GenericObject.cpp

From Giona

(Difference between revisions)
 
Line 4: Line 4:
  * =================
  * =================
  * Implementation of a generic object
  * Implementation of a generic object
 +
* --------------------------------------------------------
  * Author: Rosario Marino
  * Author: Rosario Marino
  */
  */
Line 15: Line 16:
{
{
Debug::debug("GenericObject::GenericObject()");
Debug::debug("GenericObject::GenericObject()");
-
this->attributeList = new GenericList();
+
this->attributeList = new GenericAttributeList();
this->description = EMPTY_STRING;
this->description = EMPTY_STRING;
this->name = EMPTY_STRING;
this->name = EMPTY_STRING;
Line 38: Line 39:
if(name==NULL)
if(name==NULL)
{
{
-
Debug::debug("ERROR: ERROR_NULL_PARAMETER");
+
Debug::debugError("bool GenericObject::setName(STRING name)", "ERROR_NULL_PARAMETER", "parameter: name");
this->lastError=ERROR_NULL_PARAMETER;
this->lastError=ERROR_NULL_PARAMETER;
return false;
return false;
Line 44: Line 45:
if(strcmp(name, EMPTY_STRING)==0)
if(strcmp(name, EMPTY_STRING)==0)
{
{
-
Debug::debug("ERROR: ERROR_EMPTY_STRING");
+
Debug::debugError("bool GenericObject::setName(STRING name)", "ERROR_EMPTY_STRING", "parameter: name");;
this->lastError=ERROR_EMPTY_STRING;
this->lastError=ERROR_EMPTY_STRING;
return false;
return false;
Line 67: Line 68:
{
{
Debug::debug("int GenericObject::getAttributeNumber()");
Debug::debug("int GenericObject::getAttributeNumber()");
-
return this->attributeList->getLength();
+
return this->attributeList->getAttributeNumber();
}
}
Line 78: Line 79:
if(description==NULL)
if(description==NULL)
{
{
-
Debug::debug("ERROR: ERROR_NULL_PARAMETER");
+
Debug::debugError("bool GenericObject::setDescription(STRING description)", "ERROR_NULL_PARAMETER", "parameter: description");
this->lastError=ERROR_NULL_PARAMETER;
this->lastError=ERROR_NULL_PARAMETER;
return false;
return false;
Line 84: Line 85:
if(strcmp(description, EMPTY_STRING)==0)
if(strcmp(description, EMPTY_STRING)==0)
{
{
-
Debug::debug("ERROR: ERROR_EMPTY_STRING");
+
Debug::debugError("bool GenericObject::setDescription(STRING description)", "ERROR_EMPTY_STRING", "parameter: description");
this->lastError=ERROR_EMPTY_STRING;
this->lastError=ERROR_EMPTY_STRING;
return false;
return false;
Line 108: Line 109:
{
{
Debug::debug("STRING GenericObject::getAttributeValue(STRING name)");
Debug::debug("STRING GenericObject::getAttributeValue(STRING name)");
-
if(name==NULL)
+
STRING value=this->attributeList->getAttributeValue(name);
-
{
+
if(value==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();
+
this->lastError=ERROR_GENERIC_ATTRIBUTE_LIST_ERROR;
-
return value;
+
Debug::debugError("STRING GenericObject::getAttributeValue(STRING name)","ERROR_GENERIC_ATTRIBUTE_LIST_ERROR", "Try to run with debug error activated");
}
}
-
Debug::debug("ERROR: ERROR_ATTRIBUTE_NOT_FOUND");
+
return value;
-
this->lastError=ERROR_ATTRIBUTE_NOT_FOUND;
+
-
return NULL;
+
}
}
Line 139: Line 124:
{
{
Debug::debug("STRING GenericObject::getAttributeDescription(STRING name)");
Debug::debug("STRING GenericObject::getAttributeDescription(STRING name)");
-
if(name==NULL)
+
STRING description=this->attributeList->getAttributeDescription(name);
 +
if(description==NULL)
{
{
-
Debug::debug("ERROR: ERROR_NULL_PARAMETER");
+
this->lastError=ERROR_GENERIC_ATTRIBUTE_LIST_ERROR;
-
this->lastError=ERROR_NULL_PARAMETER;
+
Debug::debugError("STRING GenericObject::getAttributeDescription(STRING name)","ERROR_GENERIC_ATTRIBUTE_LIST_ERROR", "Try to run with debug error activated");
-
return NULL;
+
}
}
-
if(strcmp(name, EMPTY_STRING)==0)
+
return description;
 +
}
 +
 
 +
/*
 +
* Delete the attribute
 +
* Returns false in case of error
 +
*/
 +
bool GenericObject::deleteAttibute(STRING name)
 +
{
 +
if(!this->attributeList->deleteAttibute(name))
{
{
-
Debug::debug("ERROR: ERROR_EMPTY_STRING");
+
this->lastError=ERROR_GENERIC_ATTRIBUTE_LIST_ERROR;
-
this->lastError=ERROR_EMPTY_STRING;
+
Debug::debugError("bool GenericObject::deleteAttibute(STRING name)","ERROR_GENERIC_ATTRIBUTE_LIST_ERROR", "Try to run with debug error activated");
-
return NULL;
+
return false;
}
}
-
STRING value = EMPTY_STRING;
+
return true;
-
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
  * Sets the value of the attribute
Line 170: Line 154:
bool GenericObject::setAttributeValue(STRING name, STRING value)
bool GenericObject::setAttributeValue(STRING name, STRING value)
{
{
-
Debug::debug("void GenericObject::setAttributeValue(STRING name, STRING value)");
+
if(!this->attributeList->setAttributeValue(name,value))
-
if(name==NULL)
+
{
{
-
Debug::debug("ERROR: ERROR_NULL_PARAMETER - name");
+
this->lastError=ERROR_GENERIC_ATTRIBUTE_LIST_ERROR;
-
this->lastError=ERROR_NULL_PARAMETER;
+
Debug::debugError("bool GenericObject::setAttributeValue(STRING name, STRING value)","ERROR_GENERIC_ATTRIBUTE_LIST_ERROR", "Try to run with debug error activated");
-
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 false;
}
}
Line 226: Line 168:
  */
  */
bool GenericObject::setAttributeDescription(STRING name, STRING description)
bool GenericObject::setAttributeDescription(STRING name, STRING description)
-
{
+
{
-
Debug::debug("bool GenericObject::setAttributeDescription(STRING name, STRING description)");
+
if(!this->attributeList->setAttributeDescription(name,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");
+
this->lastError=ERROR_GENERIC_ATTRIBUTE_LIST_ERROR;
 +
Debug::debugError("bool GenericObject::setAttributeDescription(STRING name, STRING description)","ERROR_GENERIC_ATTRIBUTE_LIST_ERROR", "Try to run with debug error activated");
return false;
return false;
}
}
Line 261: Line 184:
  {
  {
Debug::debug("STRING* GenericObject::getAttributeNameList()");
Debug::debug("STRING* GenericObject::getAttributeNameList()");
-
  int i = 0;
+
  return this->attributeList->getAttributeNameList();
-
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;
 
-
}
 
/*
/*
Line 305: Line 195:
delete (attributeList);
delete (attributeList);
}
}
-
 
</pre>
</pre>
[[Il database Object Oriented Generico|BACK]]
[[Il database Object Oriented Generico|BACK]]
[[Category:Codice Sorgente]]
[[Category:Codice Sorgente]]

Current revision as of 16:28, 19 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 GenericAttributeList();
	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::debugError("bool GenericObject::setName(STRING name)", "ERROR_NULL_PARAMETER", "parameter: name");
		this->lastError=ERROR_NULL_PARAMETER;
		return false;
	}
	if(strcmp(name, EMPTY_STRING)==0)
	{
		Debug::debugError("bool GenericObject::setName(STRING name)", "ERROR_EMPTY_STRING", "parameter: name");;
		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->getAttributeNumber();
}

/*
 * Sets the description of the object
 */
bool GenericObject::setDescription(STRING description)
{
	Debug::debug("bool GenericObject::setDescription(STRING description)");
	if(description==NULL)
	{
		Debug::debugError("bool GenericObject::setDescription(STRING description)", "ERROR_NULL_PARAMETER", "parameter: description");
		this->lastError=ERROR_NULL_PARAMETER;
		return false;
	}
	if(strcmp(description, EMPTY_STRING)==0)
	{
		Debug::debugError("bool GenericObject::setDescription(STRING description)", "ERROR_EMPTY_STRING", "parameter: description");
		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)");
	STRING value=this->attributeList->getAttributeValue(name);
	if(value==NULL)
	{
		this->lastError=ERROR_GENERIC_ATTRIBUTE_LIST_ERROR;
		Debug::debugError("STRING GenericObject::getAttributeValue(STRING name)","ERROR_GENERIC_ATTRIBUTE_LIST_ERROR", "Try to run with debug error activated");
	}
	return value;
}

/*
 * Returns the description of the attribute. Returns NULL in case of error
 */
STRING GenericObject::getAttributeDescription(STRING name)
{
	Debug::debug("STRING GenericObject::getAttributeDescription(STRING name)");
	STRING description=this->attributeList->getAttributeDescription(name);
	if(description==NULL)
	{
		this->lastError=ERROR_GENERIC_ATTRIBUTE_LIST_ERROR;
		Debug::debugError("STRING GenericObject::getAttributeDescription(STRING name)","ERROR_GENERIC_ATTRIBUTE_LIST_ERROR", "Try to run with debug error activated");
	}
	return description;
}

/*
 * 	Delete the attribute
 * 	Returns false in case of error
 */
bool GenericObject::deleteAttibute(STRING name)
{
	if(!this->attributeList->deleteAttibute(name))
	{
		this->lastError=ERROR_GENERIC_ATTRIBUTE_LIST_ERROR;
		Debug::debugError("bool GenericObject::deleteAttibute(STRING name)","ERROR_GENERIC_ATTRIBUTE_LIST_ERROR", "Try to run with debug error activated");		
		return false;
	}
	return true;
}
/*
 * 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)
{
	if(!this->attributeList->setAttributeValue(name,value))
	{
		this->lastError=ERROR_GENERIC_ATTRIBUTE_LIST_ERROR;
		Debug::debugError("bool GenericObject::setAttributeValue(STRING name, STRING value)","ERROR_GENERIC_ATTRIBUTE_LIST_ERROR", "Try to run with debug error activated");		
		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)
{
	if(!this->attributeList->setAttributeDescription(name,description))
	{
		this->lastError=ERROR_GENERIC_ATTRIBUTE_LIST_ERROR;
		Debug::debugError("bool GenericObject::setAttributeDescription(STRING name, STRING description)","ERROR_GENERIC_ATTRIBUTE_LIST_ERROR", "Try to run with debug error activated");		
		return false;
	}
	return true;
 }

/*
 * Returns the array of all attribute names
 */
 STRING* GenericObject::getAttributeNameList()
 {
	Debug::debug("STRING* GenericObject::getAttributeNameList()");
 	return this->attributeList->getAttributeNameList();
 }

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

BACK

Personal tools