/* 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/IfcElement.h"
#include "ifcpp/IFC4/include/IfcFeatureElementSubtraction.h"
#include "ifcpp/IFC4/include/IfcGloballyUniqueId.h"
#include "ifcpp/IFC4/include/IfcLabel.h"
#include "ifcpp/IFC4/include/IfcOwnerHistory.h"
#include "ifcpp/IFC4/include/IfcRelVoidsElement.h"
#include "ifcpp/IFC4/include/IfcText.h"

// ENTITY IfcRelVoidsElement 
IfcRelVoidsElement::IfcRelVoidsElement( int id ) { m_entity_id = id; }
IfcRelVoidsElement::~IfcRelVoidsElement() {}
shared_ptr<BuildingObject> IfcRelVoidsElement::getDeepCopy( BuildingCopyOptions& options )
{
	shared_ptr<IfcRelVoidsElement> copy_self( new IfcRelVoidsElement() );
	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_RelatingBuildingElement ) { copy_self->m_RelatingBuildingElement = dynamic_pointer_cast<IfcElement>( m_RelatingBuildingElement->getDeepCopy(options) ); }
	if( m_RelatedOpeningElement ) { copy_self->m_RelatedOpeningElement = dynamic_pointer_cast<IfcFeatureElementSubtraction>( m_RelatedOpeningElement->getDeepCopy(options) ); }
	return copy_self;
}
void IfcRelVoidsElement::getStepLine( std::stringstream& stream ) const
{
	stream << "#" << m_entity_id << "= IFCRELVOIDSELEMENT" << "(";
	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_RelatingBuildingElement ) { stream << "#" << m_RelatingBuildingElement->m_entity_id; } else { stream << "$"; }
	stream << ",";
	if( m_RelatedOpeningElement ) { stream << "#" << m_RelatedOpeningElement->m_entity_id; } else { stream << "$"; }
	stream << ");";
}
void IfcRelVoidsElement::getStepParameter( std::stringstream& stream, bool ) const { stream << "#" << m_entity_id; }
const std::wstring IfcRelVoidsElement::toString() const { return L"IfcRelVoidsElement"; }
void IfcRelVoidsElement::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 != 6 ){ std::stringstream err; err << "Wrong parameter count for entity IfcRelVoidsElement, expecting 6, 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_RelatingBuildingElement, map );
	readEntityReference( args[5], m_RelatedOpeningElement, map );
}
void IfcRelVoidsElement::getAttributes( std::vector<std::pair<std::string, shared_ptr<BuildingObject> > >& vec_attributes ) const
{
	IfcRelDecomposes::getAttributes( vec_attributes );
	vec_attributes.emplace_back( std::make_pair( "RelatingBuildingElement", m_RelatingBuildingElement ) );
	vec_attributes.emplace_back( std::make_pair( "RelatedOpeningElement", m_RelatedOpeningElement ) );
}
void IfcRelVoidsElement::getAttributesInverse( std::vector<std::pair<std::string, shared_ptr<BuildingObject> > >& vec_attributes_inverse ) const
{
	IfcRelDecomposes::getAttributesInverse( vec_attributes_inverse );
}
void IfcRelVoidsElement::setInverseCounterparts( shared_ptr<BuildingEntity> ptr_self_entity )
{
	IfcRelDecomposes::setInverseCounterparts( ptr_self_entity );
	shared_ptr<IfcRelVoidsElement> ptr_self = dynamic_pointer_cast<IfcRelVoidsElement>( ptr_self_entity );
	if( !ptr_self ) { throw BuildingException( "IfcRelVoidsElement::setInverseCounterparts: type mismatch" ); }
	if( m_RelatedOpeningElement )
	{
		m_RelatedOpeningElement->m_VoidsElements_inverse = ptr_self;
	}
	if( m_RelatingBuildingElement )
	{
		m_RelatingBuildingElement->m_HasOpenings_inverse.emplace_back( ptr_self );
	}
}
void IfcRelVoidsElement::unlinkFromInverseCounterparts()
{
	IfcRelDecomposes::unlinkFromInverseCounterparts();
	if( m_RelatedOpeningElement )
	{
		if( !m_RelatedOpeningElement->m_VoidsElements_inverse.expired() )
		{
			shared_ptr<IfcRelVoidsElement> self_candidate( m_RelatedOpeningElement->m_VoidsElements_inverse );
			if( self_candidate.get() == this )
			{
				weak_ptr<IfcRelVoidsElement>& self_candidate_weak = m_RelatedOpeningElement->m_VoidsElements_inverse;
				self_candidate_weak.reset();
			}
		}
	}
	if( m_RelatingBuildingElement )
	{
		std::vector<weak_ptr<IfcRelVoidsElement> >& HasOpenings_inverse = m_RelatingBuildingElement->m_HasOpenings_inverse;
		for( auto it_HasOpenings_inverse = HasOpenings_inverse.begin(); it_HasOpenings_inverse != HasOpenings_inverse.end(); )
		{
			weak_ptr<IfcRelVoidsElement> self_candidate_weak = *it_HasOpenings_inverse;
			if( self_candidate_weak.expired() )
			{
				++it_HasOpenings_inverse;
				continue;
			}
			shared_ptr<IfcRelVoidsElement> self_candidate( *it_HasOpenings_inverse );
			if( self_candidate.get() == this )
			{
				it_HasOpenings_inverse= HasOpenings_inverse.erase( it_HasOpenings_inverse );
			}
			else
			{
				++it_HasOpenings_inverse;
			}
		}
	}
}
