[rkward-cvs] rkward/rkward/core rcontainerobject.cpp,1.24,1.25 rcontainerobject.h,1.19,1.20 rkvariable.cpp,1.28,1.29 rkvariable.h,1.17,1.18 robject.cpp,1.17,1.18 robject.h,1.19,1.20

Thomas Friedrichsmeier tfry at users.sourceforge.net
Fri Dec 9 14:05:17 UTC 2005


Update of /cvsroot/rkward/rkward/rkward/core
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv7653/core

Modified Files:
	rcontainerobject.cpp rcontainerobject.h rkvariable.cpp 
	rkvariable.h robject.cpp robject.h 
Log Message:
Cleaning up duplicate code in RObject classes. Should also fix a small memory leak. More duplications will be removed later

Index: robject.h
===================================================================
RCS file: /cvsroot/rkward/rkward/rkward/core/robject.h,v
retrieving revision 1.19
retrieving revision 1.20
diff -C2 -d -r1.19 -r1.20
*** robject.h	20 Sep 2004 17:42:41 -0000	1.19
--- robject.h	9 Dec 2005 14:05:14 -0000	1.20
***************
*** 32,35 ****
--- 32,37 ----
  Base class for representations of objects in the R-workspace. RObject is never used directly (contains pure virtual functions).
  
+ TODO: information about dimensionality and classes should be moved to RObject instead of its derived classes (virtual functions).
+ 	- after that, several other portions of the code should be updated (e.g. RKComponentPropertyRObjects)
  @author Thomas Friedrichsmeier
  */
***************
*** 37,43 ****
  class RObject : public RCommandReceiver {
  public:
! 	RObject(RContainerObject *parent, const QString &name);
  
! 	virtual ~RObject();
  
  	enum RObjectType { DataFrame=1, Matrix=2, Array=4, List=8, Container=16, Variable=32, Workspace=64, Function=128, HasMetaObject=256 };
--- 39,45 ----
  class RObject : public RCommandReceiver {
  public:
! 	RObject (RContainerObject *parent, const QString &name);
  
! 	virtual ~RObject ();
  
  	enum RObjectType { DataFrame=1, Matrix=2, Array=4, List=8, Container=16, Variable=32, Workspace=64, Function=128, HasMetaObject=256 };
***************
*** 57,64 ****
  	bool isVariable () { return (type & Variable); };
  	bool hasMetaObject () { return (type & HasMetaObject); };
! 	
  	void rename (const QString &new_short_name);
  	void remove (bool removed_in_workspace);
  
  /** A map of objects accessible by their short name. Used in RContainerObject. Defined here for technical reasons. */
  	typedef QMap<QString, RObject*> RObjectMap;
--- 59,73 ----
  	bool isVariable () { return (type & Variable); };
  	bool hasMetaObject () { return (type & HasMetaObject); };
! 
  	void rename (const QString &new_short_name);
  	void remove (bool removed_in_workspace);
  
+ 	int numClasses () { return num_classes; };
+ 	QString getClassName (int index) { return classname[index]; };
+ 	QString makeClassString (const QString &sep);
+ /** @param class_name the name of the class to check for
+ @returns true, if the object has (among others) the given class, false otherwise */
+ 	bool inherits (const QString &class_name);
+ 
  /** A map of objects accessible by their short name. Used in RContainerObject. Defined here for technical reasons. */
  	typedef QMap<QString, RObject*> RObjectMap;
***************
*** 107,110 ****
--- 116,121 ----
  	QString name;
  	int type;
+ 	int num_classes;
+ 	QString *classname;
  
  /** fetches the meta data from the backend */
***************
*** 117,120 ****
--- 128,135 ----
  	
  	void rCommandDone (RCommand *command);
+ /** handles updating class names from an update class command given as argument (common functionality between RContainerObject and RKVariable
+ @param command The command. Make sure it really is a command to update classes *before* calling this function!
+ @returns true if the classes changed, false if no change resulted */
+ 	bool handleUpdateClassCommand (RCommand *command);
  	
  /** an instance of this struct is created, when the object is opened for editing. For one thing, it keeps track of which editor(s) are working on the object.

Index: rcontainerobject.cpp
===================================================================
RCS file: /cvsroot/rkward/rkward/rkward/core/rcontainerobject.cpp,v
retrieving revision 1.24
retrieving revision 1.25
diff -C2 -d -r1.24 -r1.25
*** rcontainerobject.cpp	29 Sep 2005 16:02:50 -0000	1.24
--- rcontainerobject.cpp	9 Dec 2005 14:05:14 -0000	1.25
***************
*** 34,40 ****
  RContainerObject::RContainerObject (RContainerObject *parent, const QString &name) : RObject (parent, name) {
  	RK_TRACE (OBJECTS);
- 	classname = 0;
  	dimension = 0;
! 	num_classes = num_dimensions = 0;
  	num_children_updating = 0;
  }
--- 34,39 ----
  RContainerObject::RContainerObject (RContainerObject *parent, const QString &name) : RObject (parent, name) {
  	RK_TRACE (OBJECTS);
  	dimension = 0;
! 	num_dimensions = 0;
  	num_children_updating = 0;
  }
***************
*** 63,67 ****
  	bool properties_changed = false;
  	if (command->getFlags () == CLASSIFY_COMMAND) {
! 		// WARNING: This code is (mostly) duplicated in RContainerObject!
  		if (!command->intVectorLength ()) {
  			RK_ASSERT (false);
--- 62,66 ----
  	bool properties_changed = false;
  	if (command->getFlags () == CLASSIFY_COMMAND) {
! 		// WARNING: This code is (mostly) duplicated in RKVariable!
  		if (!command->intVectorLength ()) {
  			RK_ASSERT (false);
***************
*** 128,141 ****
  		
  	} else if (command->getFlags () == UPDATE_CLASS_COMMAND) {
! 		if (num_classes != command->stringVectorLength ()) {
! 			num_classes = command->stringVectorLength ();
! 			delete classname;
! 			classname = new QString [num_classes];
! 			properties_changed = true;
! 		}
! 		for (int cn=0; cn < num_classes; ++cn) {
! 			if (classname[cn] != command->getStringVector ()[cn]) properties_changed = true;
! 			classname[cn] = command->getStringVector ()[cn];
! 		}
  		if (properties_changed) RKGlobals::tracker ()->objectMetaChanged (this);
  	}
--- 127,131 ----
  		
  	} else if (command->getFlags () == UPDATE_CLASS_COMMAND) {
! 		if (handleUpdateClassCommand (command)) properties_changed = true;
  		if (properties_changed) RKGlobals::tracker ()->objectMetaChanged (this);
  	}
***************
*** 183,198 ****
  }
  
- QString RContainerObject::makeClassString (const QString &sep) {
- 	RK_TRACE (OBJECTS);
- 	QString ret;
- 	for (int i=0; i < num_classes; ++i) {
- 		ret.append (classname[i]);
- 		if (i < (num_classes - 1)) {
- 			ret.append (sep);
- 		}
- 	}
- 	return ret;
- }
- 
  void RContainerObject::writeChildMetaData (RCommandChain *chain) {
  	RK_TRACE (OBJECTS);
--- 173,176 ----

Index: rcontainerobject.h
===================================================================
RCS file: /cvsroot/rkward/rkward/rkward/core/rcontainerobject.h,v
retrieving revision 1.19
retrieving revision 1.20
diff -C2 -d -r1.19 -r1.20
*** rcontainerobject.h	28 Apr 2005 21:23:11 -0000	1.19
--- rcontainerobject.h	9 Dec 2005 14:05:14 -0000	1.20
***************
*** 35,42 ****
      ~RContainerObject();
  
- 	int numClasses () { return num_classes; };
- 	QString getClassName (int index) { return classname[index]; };
- 	QString makeClassString (const QString &sep);
- 	
  	void writeChildMetaData (RCommandChain *chain);
  	
--- 35,38 ----
***************
*** 79,84 ****
  	void checkRemovedChildren (char **current_children, int current_child_count);
  
- 	int num_classes;
- 	QString *classname;
  	int num_dimensions;
  	int *dimension;
--- 75,78 ----

Index: rkvariable.cpp
===================================================================
RCS file: /cvsroot/rkward/rkward/rkward/core/rkvariable.cpp,v
retrieving revision 1.28
retrieving revision 1.29
diff -C2 -d -r1.28 -r1.29
*** rkvariable.cpp	29 Sep 2005 16:02:50 -0000	1.28
--- rkvariable.cpp	9 Dec 2005 14:05:14 -0000	1.29
***************
*** 43,48 ****
  	var_type = Unknown;
  	length = 0;
- 	num_classes = 0 ;
- 	classname = 0 ;
  }
  
--- 43,46 ----
***************
*** 217,231 ****
  		setSyncing (true);
  	} else if (command->getFlags () == UPDATE_CLASS_COMMAND) {
! 		if (num_classes != command->stringVectorLength ()) {
! // TODO: clean deletion of classnames. need to valgrind one day, anyway
! 			num_classes = command->stringVectorLength ();
! 			delete classname;
! 			classname = new QString [num_classes];
! 			properties_changed = true;
! 		}
! 		for (int cn=0; cn < num_classes; ++cn) {
! 			if (classname[cn] != command->getStringVector ()[cn]) properties_changed = true;
! 			classname[cn] = command->getStringVector ()[cn];
! 		}
  		if (properties_changed) RKGlobals::tracker ()->objectMetaChanged (this);
  
--- 215,219 ----
  		setSyncing (true);
  	} else if (command->getFlags () == UPDATE_CLASS_COMMAND) {
! 		if (handleUpdateClassCommand (command)) properties_changed = true;
  		if (properties_changed) RKGlobals::tracker ()->objectMetaChanged (this);
  
***************
*** 234,250 ****
  }
  
- QString RKVariable::makeClassString (const QString &sep)
- {
- 	RK_TRACE (OBJECTS);
- 	QString ret;
- 	for (int i=0; i < num_classes; ++i) {
- 		ret.append (classname[i]);
- 		if (i < (num_classes - 1)) {
- 			ret.append (sep);
- 		}
- 	}
- 	return ret;
- 
- }
  
  ////////////////////// BEGIN: data-handling //////////////////////////////
--- 222,225 ----

Index: robject.cpp
===================================================================
RCS file: /cvsroot/rkward/rkward/rkward/core/robject.cpp,v
retrieving revision 1.17
retrieving revision 1.18
diff -C2 -d -r1.17 -r1.18
*** robject.cpp	29 Sep 2005 16:02:50 -0000	1.17
--- robject.cpp	9 Dec 2005 14:05:14 -0000	1.18
***************
*** 29,32 ****
--- 29,33 ----
  
  #define GET_META_COMMAND 1001
+ #define UPDATE_CLASS_COMMAND 1002
  
  RObject::RObject (RContainerObject *parent, const QString &name) {
***************
*** 38,45 ****
--- 39,50 ----
  	meta_map = 0;
  	data = 0;
+ 	num_classes = 0;
+ 	classname = 0;
  }
  
  RObject::~RObject () {
  	RK_TRACE (OBJECTS);
+ 
+ 	delete[] classname;
  	
  	if (data) discardEditData ();
***************
*** 117,120 ****
--- 122,148 ----
  }
  
+ QString RObject::makeClassString (const QString &sep) {
+ 	RK_TRACE (OBJECTS);
+ 	QString ret;
+ 	for (int i=0; i < num_classes; ++i) {
+ 		ret.append (classname[i]);
+ 		if (i < (num_classes - 1)) {
+ 			ret.append (sep);
+ 		}
+ 	}
+ 	return ret;
+ }
+ 
+ bool RObject::inherits (const QString &class_name) {
+ 	RK_TRACE (OBJECTS);
+ 
+ 	for (int i=0; i < num_classes; ++i) {
+ 		if (classname[i] == class_name) {
+ 			return true;
+ 		}
+ 	}
+ 	return false;
+ }
+ 
  QString RObject::makeChildName (const QString &short_child_name) {
  	RK_TRACE (OBJECTS);
***************
*** 184,187 ****
--- 212,234 ----
  }
  
+ bool RObject::handleUpdateClassCommand (RCommand *command) {
+ 	RK_TRACE (OBJECTS);
+ 
+ 	bool change = false;
+ 
+ 	if (num_classes != command->stringVectorLength ()) {
+ 		num_classes = command->stringVectorLength ();
+ 		delete[] classname;
+ 		classname = new QString [num_classes];
+ 		change = true;
+ 	}
+ 	for (int cn=0; cn < num_classes; ++cn) {
+ 		if (classname[cn] != command->getStringVector ()[cn]) change = true;
+ 		classname[cn] = command->getStringVector ()[cn];
+ 	}
+ 
+ 	return change;
+ }
+ 
  void RObject::rename (const QString &new_short_name) {
  	RK_TRACE (OBJECTS);

Index: rkvariable.h
===================================================================
RCS file: /cvsroot/rkward/rkward/rkward/core/rkvariable.h,v
retrieving revision 1.17
retrieving revision 1.18
diff -C2 -d -r1.17 -r1.18
*** rkvariable.h	2 Apr 2005 08:48:09 -0000	1.17
--- rkvariable.h	9 Dec 2005 14:05:14 -0000	1.18
***************
*** 27,31 ****
  RKVariables are so far the only type of object that is really editable (data.frames are just a bundle of RKVariables). Therefore, for most practical purposes, the RKVariable represents a column in a table.
  
! TODO: acutally, for now, the data is always given to the backend as strings. Change that!
  TODO: there should be "chunks" of column-data. This should be done at the level of rows, i.e. across columns. After all, if a row gets added/removed in one column, all other columns of the same table will also be affected.
  TODO: which functions should do syncing by themselves, which should not? Or should all set... ()-functions have an extra parameter for this?
--- 27,31 ----
  RKVariables are so far the only type of object that is really editable (data.frames are just a bundle of RKVariables). Therefore, for most practical purposes, the RKVariable represents a column in a table.
  
! TODO: actually, for now, the data is always given to the backend as strings. Change that!
  TODO: there should be "chunks" of column-data. This should be done at the level of rows, i.e. across columns. After all, if a row gets added/removed in one column, all other columns of the same table will also be affected.
  TODO: which functions should do syncing by themselves, which should not? Or should all set... ()-functions have an extra parameter for this?
***************
*** 40,44 ****
  	~RKVariable ();
  
! /** The VarType in String representatio */
  	QString getVarTypeString ();
  /** The VarType of this variable. Note: This is only the preferred VarType. In R the variable may be stored differently, if it contains illegal values (in that
--- 40,44 ----
  	~RKVariable ();
  
! /** The VarType in String representation */
  	QString getVarTypeString ();
  /** The VarType of this variable. Note: This is only the preferred VarType. In R the variable may be stored differently, if it contains illegal values (in that
***************
*** 58,64 ****
  /** reimplemented from RObject to also store value labels/factor levels (and in the future probably futher info) */
  	void writeMetaData (RCommandChain *chain);
- protected:
- 	int num_classes;
- 	QString *classname;
  friend class RContainerObject;
  	int length;
--- 58,61 ----
***************
*** 156,160 ****
  /** returns alignment to use for this variable */
  	CellAlign getAlignment ();
-     QString makeClassString (const QString &sep);
  protected:
  /** Extended from RObject::EditData to actually contain data. */
--- 153,156 ----





More information about the rkward-tracker mailing list