/* Code generated by IfcQuery EXPRESS generator, www.ifcquery.com */
#include <sstream>
#include <limits>

#include "ifcpp/model/AttributeObject.h"
#include "ifcpp/model/BuildingException.h"
#include "ifcpp/model/BuildingGuid.h"
#include "ifcpp/reader/ReaderUtil.h"
#include "ifcpp/writer/WriterUtil.h"
#include "ifcpp/IFC4/include/IfcGloballyUniqueId.h"
#include "ifcpp/IFC4/include/IfcLabel.h"
#include "ifcpp/IFC4/include/IfcLagTime.h"
#include "ifcpp/IFC4/include/IfcOwnerHistory.h"
#include "ifcpp/IFC4/include/IfcProcess.h"
#include "ifcpp/IFC4/include/IfcRelSequence.h"
#include "ifcpp/IFC4/include/IfcSequenceEnum.h"
#include "ifcpp/IFC4/include/IfcText.h"

// ENTITY IfcRelSequence 
IfcRelSequence::IfcRelSequence( int id ) { m_entity_id = id; }
IfcRelSequence::~IfcRelSequence() {}
shared_ptr<BuildingObject> IfcRelSequence::getDeepCopy( BuildingCopyOptions& options )
{
	shared_ptr<IfcRelSequence> copy_self( new IfcRelSequence() );
	if( m_GlobalId )
	{
		if( options.create_new_IfcGloballyUniqueId ) { copy_self->m_GlobalId = make_shared<IfcGloballyUniqueId>( createBase64Uuid<wchar_t>().data() ); }
		else { copy_self->m_GlobalId = dynamic_pointer_cast<IfcGloballyUniqueId>( m_GlobalId->getDeepCopy(options) ); }
	}
	if( m_OwnerHistory )
	{
		if( options.shallow_copy_IfcOwnerHistory ) { copy_self->m_OwnerHistory = m_OwnerHistory; }
		else { copy_self->m_OwnerHistory = dynamic_pointer_cast<IfcOwnerHistory>( m_OwnerHistory->getDeepCopy(options) ); }
	}
	if( m_Name ) { copy_self->m_Name = dynamic_pointer_cast<IfcLabel>( m_Name->getDeepCopy(options) ); }
	if( m_Description ) { copy_self->m_Description = dynamic_pointer_cast<IfcText>( m_Description->getDeepCopy(options) ); }
	if( m_RelatingProcess ) { copy_self->m_RelatingProcess = dynamic_pointer_cast<IfcProcess>( m_RelatingProcess->getDeepCopy(options) ); }
	if( m_RelatedProcess ) { copy_self->m_RelatedProcess = dynamic_pointer_cast<IfcProcess>( m_RelatedProcess->getDeepCopy(options) ); }
	if( m_TimeLag ) { copy_self->m_TimeLag = dynamic_pointer_cast<IfcLagTime>( m_TimeLag->getDeepCopy(options) ); }
	if( m_SequenceType ) { copy_self->m_SequenceType = dynamic_pointer_cast<IfcSequenceEnum>( m_SequenceType->getDeepCopy(options) ); }
	if( m_UserDefinedSequenceType ) { copy_self->m_UserDefinedSequenceType = dynamic_pointer_cast<IfcLabel>( m_UserDefinedSequenceType->getDeepCopy(options) ); }
	return copy_self;
}
void IfcRelSequence::getStepLine( std::stringstream& stream ) const
{
	stream << "#" << m_entity_id << "= IFCRELSEQUENCE" << "(";
	if( m_GlobalId ) { m_GlobalId->getStepParameter( stream ); } else { stream << "$"; }
	stream << ",";
	if( m_OwnerHistory ) { stream << "#" << m_OwnerHistory->m_entity_id; } else { stream << "$"; }
	stream << ",";
	if( m_Name ) { m_Name->getStepParameter( stream ); } else { stream << "$"; }
	stream << ",";
	if( m_Description ) { m_Description->getStepParameter( stream ); } else { stream << "$"; }
	stream << ",";
	if( m_RelatingProcess ) { stream << "#" << m_RelatingProcess->m_entity_id; } else { stream << "$"; }
	stream << ",";
	if( m_RelatedProcess ) { stream << "#" << m_RelatedProcess->m_entity_id; } else { stream << "$"; }
	stream << ",";
	if( m_TimeLag ) { stream << "#" << m_TimeLag->m_entity_id; } else { stream << "$"; }
	stream << ",";
	if( m_SequenceType ) { m_SequenceType->getStepParameter( stream ); } else { stream << "$"; }
	stream << ",";
	if( m_UserDefinedSequenceType ) { m_UserDefinedSequenceType->getStepParameter( stream ); } else { stream << "$"; }
	stream << ");";
}
void IfcRelSequence::getStepParameter( std::stringstream& stream, bool ) const { stream << "#" << m_entity_id; }
const std::wstring IfcRelSequence::toString() const { return L"IfcRelSequence"; }
void IfcRelSequence::readStepArguments( const std::vector<std::wstring>& args, const std::map<int,shared_ptr<BuildingEntity> >& map )
{
	const size_t num_args = args.size();
	if( num_args != 9 ){ std::stringstream err; err << "Wrong parameter count for entity IfcRelSequence, expecting 9, having " << num_args << ". Entity ID: " << m_entity_id << std::endl; throw BuildingException( err.str().c_str() ); }
	m_GlobalId = IfcGloballyUniqueId::createObjectFromSTEP( args[0], map );
	readEntityReference( args[1], m_OwnerHistory, map );
	m_Name = IfcLabel::createObjectFromSTEP( args[2], map );
	m_Description = IfcText::createObjectFromSTEP( args[3], map );
	readEntityReference( args[4], m_RelatingProcess, map );
	readEntityReference( args[5], m_RelatedProcess, map );
	readEntityReference( args[6], m_TimeLag, map );
	m_SequenceType = IfcSequenceEnum::createObjectFromSTEP( args[7], map );
	m_UserDefinedSequenceType = IfcLabel::createObjectFromSTEP( args[8], map );
}
void IfcRelSequence::getAttributes( std::vector<std::pair<std::string, shared_ptr<BuildingObject> > >& vec_attributes ) const
{
	IfcRelConnects::getAttributes( vec_attributes );
	vec_attributes.emplace_back( std::make_pair( "RelatingProcess", m_RelatingProcess ) );
	vec_attributes.emplace_back( std::make_pair( "RelatedProcess", m_RelatedProcess ) );
	vec_attributes.emplace_back( std::make_pair( "TimeLag", m_TimeLag ) );
	vec_attributes.emplace_back( std::make_pair( "SequenceType", m_SequenceType ) );
	vec_attributes.emplace_back( std::make_pair( "UserDefinedSequenceType", m_UserDefinedSequenceType ) );
}
void IfcRelSequence::getAttributesInverse( std::vector<std::pair<std::string, shared_ptr<BuildingObject> > >& vec_attributes_inverse ) const
{
	IfcRelConnects::getAttributesInverse( vec_attributes_inverse );
}
void IfcRelSequence::setInverseCounterparts( shared_ptr<BuildingEntity> ptr_self_entity )
{
	IfcRelConnects::setInverseCounterparts( ptr_self_entity );
	shared_ptr<IfcRelSequence> ptr_self = dynamic_pointer_cast<IfcRelSequence>( ptr_self_entity );
	if( !ptr_self ) { throw BuildingException( "IfcRelSequence::setInverseCounterparts: type mismatch" ); }
	if( m_RelatedProcess )
	{
		m_RelatedProcess->m_IsSuccessorFrom_inverse.emplace_back( ptr_self );
	}
	if( m_RelatingProcess )
	{
		m_RelatingProcess->m_IsPredecessorTo_inverse.emplace_back( ptr_self );
	}
}
void IfcRelSequence::unlinkFromInverseCounterparts()
{
	IfcRelConnects::unlinkFromInverseCounterparts();
	if( m_RelatedProcess )
	{
		std::vector<weak_ptr<IfcRelSequence> >& IsSuccessorFrom_inverse = m_RelatedProcess->m_IsSuccessorFrom_inverse;
		for( auto it_IsSuccessorFrom_inverse = IsSuccessorFrom_inverse.begin(); it_IsSuccessorFrom_inverse != IsSuccessorFrom_inverse.end(); )
		{
			weak_ptr<IfcRelSequence> self_candidate_weak = *it_IsSuccessorFrom_inverse;
			if( self_candidate_weak.expired() )
			{
				++it_IsSuccessorFrom_inverse;
				continue;
			}
			shared_ptr<IfcRelSequence> self_candidate( *it_IsSuccessorFrom_inverse );
			if( self_candidate.get() == this )
			{
				it_IsSuccessorFrom_inverse= IsSuccessorFrom_inverse.erase( it_IsSuccessorFrom_inverse );
			}
			else
			{
				++it_IsSuccessorFrom_inverse;
			}
		}
	}
	if( m_RelatingProcess )
	{
		std::vector<weak_ptr<IfcRelSequence> >& IsPredecessorTo_inverse = m_RelatingProcess->m_IsPredecessorTo_inverse;
		for( auto it_IsPredecessorTo_inverse = IsPredecessorTo_inverse.begin(); it_IsPredecessorTo_inverse != IsPredecessorTo_inverse.end(); )
		{
			weak_ptr<IfcRelSequence> self_candidate_weak = *it_IsPredecessorTo_inverse;
			if( self_candidate_weak.expired() )
			{
				++it_IsPredecessorTo_inverse;
				continue;
			}
			shared_ptr<IfcRelSequence> self_candidate( *it_IsPredecessorTo_inverse );
			if( self_candidate.get() == this )
			{
				it_IsPredecessorTo_inverse= IsPredecessorTo_inverse.erase( it_IsPredecessorTo_inverse );
			}
			else
			{
				++it_IsPredecessorTo_inverse;
			}
		}
	}
}
