Whole Tomato Software Forums
Whole Tomato Software Forums
Main Site | Profile | Register | Active Topics | Members | Search | FAQ
User name:
Password:
Save Password
Forgot your password?

 All Forums
 Visual Assist
 Feature Requests
 Auto generated Copy Ctor and Assignment Operator
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Vertexwahn
Senior Member

35 Posts

Posted - Jun 14 2011 :  9:08:04 PM  Show Profile  Reply with Quote
The getter/setter generation is great. I think something similar should be done for Copy Constructors and Assignment Operators. The idea: Click left on a class -> Select Generator Shallow Copy Constructor -> a Copy Ctor is automatically generated - similar assignment operators should be generated

accord
Whole Tomato Software

United Kingdom
3287 Posts

Posted - Jun 15 2011 :  5:28:58 PM  Show Profile  Reply with Quote
We are considering implementing these at some point:

case=3945

Thank you for the feedback.
Go to Top of Page

Vertexwahn
Senior Member

35 Posts

Posted - Jun 15 2011 :  6:07:08 PM  Show Profile  Reply with Quote
To clarify things I want to give a short example how I imagine that the generator should work:

Consider this code:

class Mesh : public BaseMesh
{
public:
	//! Default constructor.
	Mesh();

	//! Virtual destructor.
	virtual ~Mesh();

	std::vector<mesh_vertex> vertices;
	std::vector<std::uint32_t> indices;

protected:
	int id;
	Transform m_Transform;
private:
};


After generate Ctor/Assignment:

class Mesh : public BaseMesh
{
public:
	//! Default constructor.
	Mesh();

	//! Virtual destructor.
	virtual ~Mesh();

	std::vector<mesh_vertex> vertices;
	std::vector<std::uint32_t> indices;

	//! Assignement Operator
	//! param[in] source	Object that should be assigned to this one
	Mesh& operator=( const Mesh& source )
	{
		// copy members from base class EntityObject
		EntityObject::operator=(source);

		// copy additional member from this class
		vertices = source.vertices;
		indices = source.indices;
		MaterialID = source.MaterialID;
		m_Transform = source.m_Transform;

		return *this;
	}

	//! Ctor
	//!  param[in] source	Object that should be copied
	Mesh( const Mesh &source ) : EntityObject(source)
	{
		vertices = source.vertices;
		indices = source.indices;
		MaterialID = source.MaterialID;
		m_Transform = source.m_Transform;
	}

protected:
	int id;
	Transform m_Transform;
private:
};


Note that the assignment operator calls the assignment operator of its base class - the copy ctor does something similar

If this should be too complicated to implement just skip it - but if it's possible take care of it


BTW: Is there a public timeline when things get implemented into VAssistX - case=3945 doesn't tell me if takes just a week or a few month or it will get implemented when the hell gets frozen

Edited by - Vertexwahn on Jun 15 2011 6:11:39 PM
Go to Top of Page

accord
Whole Tomato Software

United Kingdom
3287 Posts

Posted - Jun 15 2011 :  6:17:17 PM  Show Profile  Reply with Quote
I have added a comment to the case about your example here. The feature request is about the same idea, but you went into the details which can be useful if we decide to implement this at some point.

Thank you again.

Edited by - accord on Jun 15 2011 6:17:54 PM
Go to Top of Page

Vertexwahn
Senior Member

35 Posts

Posted - Jun 15 2011 :  6:42:09 PM  Show Profile  Reply with Quote
This code is redundant in the assignment and CopyCtor:


	vertices = source.vertices;
	indices = source.indices;
	MaterialID = source.MaterialID;
	m_Transform = source.m_Transform;


maybe there should be a private shallowSubclassCopy() method provided that does prevent code duplication:

Mesh::Mesh( const Mesh &source ) : 
	BaseMesh(source)
{
	shallowSubclassCopy(source);
}

Mesh& Mesh::operator=( const Mesh& source )
{
	// copy members from base class EntityObject
	EntityObject::operator=(source);
	
	// copy additional member from this class
	shallowSubclassCopy(source);

	return *this;
}

void shallowSubclassCopy( const Mesh &source )
{
	vertices = source.vertices;
	indices = source.indices;
	MaterialID = source.MaterialID;
	m_Transform = source.m_Transform;
}


maybe you can make this optional with a checkbox:
[x] Create private shallowSubclassCopy Method to prevent code duplication


It is also common to use "copy swap" to implement the assignment operator - this works like this:

class string
{
    public:
 
	// ...
 
	string &string::operator=(const string &source)
	{
                string temp( source ); // first make a copy of the source object
	        
                // now swap the internals of the copy with this object
                std::swap( temp.str, str );

                // just do the same for other internals members
                std::swap( temp.m_ID, ID);
                std::swap( temp.m_Transform, m_Transform);
                // and so on...

                return *this;
	        // destructor of temp is called - str pointer of tmp is deallocated
	}
 
	// ..
 
    private:
	char* str;
};


maybe the user can choose with a radio control which variant should generated:
( ) Memberwise Copy
(*) Copy Swap

Edited by - Vertexwahn on Jun 15 2011 8:48:54 PM
Go to Top of Page

accord
Whole Tomato Software

United Kingdom
3287 Posts

Posted - Jun 16 2011 :  10:33:09 PM  Show Profile  Reply with Quote
Thank you for the additional thoughts. However, in the first iteration we may do a rather simple version so user have the basic source for copying the values, thus long and boring part of the task can we avoided.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
© 2023 Whole Tomato Software, LLC Go To Top Of Page
Snitz Forums 2000