Ejemplo n.º 1
0
//=================================================================================
bool Node::AttachToParent( Node* parent, Context& context, List::const_iterator* insertBeforeIter /*= 0*/ )
{
	context.ClearAllErrors();

	if( this->parent )
		return context.IssueError( "Cannot attach node to parent, because the given node already has a parent." );

	if( this == parent || IsDescendant( parent ) )
		return context.IssueError( "Trees cannot have cycles.  An attempt to create a cyclical relationship has been detected and prevented." );

	if( !parent->children )
		parent->children = new List();

	if( insertBeforeIter && *insertBeforeIter != parent->children->end() )
	{
		const Node* beforeNode = **insertBeforeIter;
		if( beforeNode->parent != parent )
			return context.IssueError( "Cannot insert given node before given iterator, because these do not share the same parent." );

		parent->children->insert( *insertBeforeIter, this );
	}
	else
		parent->children->push_back( this );

	this->parent = parent;

	// This unfortunately changes the time complexity of attachment from O(1) to O(n) in the depth of the tree.
	parent->InvalidateIndices();

	return true;
}
Ejemplo n.º 2
0
//=================================================================================
bool Node::WriteToFile( const std::string& file, Context& context, std::string* buffer /*= 0*/ ) const
{
	bool success = false;
	lua_State* L = 0;
	
	context.ClearAllErrors();

	try
	{
		// Create a new Lua state to hold the contents of the file.
		L = lua_newstate( &Allocate, 0 );
		if( !L )
			throw new Error( "Failed to create Lua state in preparation for writing to file \"%s\".", file.c_str() );

		// Create the table that represents this node.
		lua_newtable( L );
		if( !WriteToTable( L, context ) )
			throw new Error( "The node \"%s\" of type \"%s\", or one of its children, failed to write its node contents to a Lua table.", name.c_str(), GetType().c_str() );

		// Lastly, write the table out to disk.
		Writer writer( file, buffer );
		if( !writer.Write( L, context ) )
			throw new Error( "Failed to write Lua file \"%s\" for root node \"%s\" of type \"%s\".", file.c_str(), name.c_str(), GetType().c_str() );

		// Hurrah!
		success = true;
	}
	catch( Error* error )
	{
		context.IssueError( error );
	}

	// Be sure to clean up our mess before returning.
	if( L )
		lua_close( L );

	return success;
}
Ejemplo n.º 3
0
//=================================================================================
bool Node::DetachFromParent( Context& context, List::const_iterator* insertedBeforeIter /*= 0*/ )
{
	context.ClearAllErrors();

	if( !parent )
		return context.IssueError( "Cannot detach root node from its parent, because a root node doesn't have a parent." );

	if( !parent->children )
		return context.IssueError( "The tree is somehow corrupt, because a child's parent has no record of any of its children." );

	// This unfortunately changes the time complexity of detachment from O(1) to O(n) in the depth of the tree.
	parent->InvalidateIndices();

	// Remember where the child was located if the caller wants that.
	if( insertedBeforeIter && ChildLocation( *insertedBeforeIter ) )
		( *insertedBeforeIter )++;

	// Perform the removal.
	parent->children->remove( this );
	parent = 0;

	return true;
}