/* 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/IfcCoordinateOperation.h"
#include "ifcpp/IFC4/include/IfcCoordinateReferenceSystem.h"
#include "ifcpp/IFC4/include/IfcCoordinateReferenceSystemSelect.h"
#include "ifcpp/IFC4/include/IfcGeometricRepresentationContext.h"

// ENTITY IfcCoordinateOperation 
IfcCoordinateOperation::IfcCoordinateOperation( int id ) { m_entity_id = id; }
IfcCoordinateOperation::~IfcCoordinateOperation() {}
shared_ptr<BuildingObject> IfcCoordinateOperation::getDeepCopy( BuildingCopyOptions& options )
{
	shared_ptr<IfcCoordinateOperation> copy_self( new IfcCoordinateOperation() );
	if( m_SourceCRS ) { copy_self->m_SourceCRS = dynamic_pointer_cast<IfcCoordinateReferenceSystemSelect>( m_SourceCRS->getDeepCopy(options) ); }
	if( m_TargetCRS ) { copy_self->m_TargetCRS = dynamic_pointer_cast<IfcCoordinateReferenceSystem>( m_TargetCRS->getDeepCopy(options) ); }
	return copy_self;
}
void IfcCoordinateOperation::getStepLine( std::stringstream& stream ) const
{
	stream << "#" << m_entity_id << "= IFCCOORDINATEOPERATION" << "(";
	if( m_SourceCRS ) { m_SourceCRS->getStepParameter( stream, true ); } else { stream << "$" ; }
	stream << ",";
	if( m_TargetCRS ) { stream << "#" << m_TargetCRS->m_entity_id; } else { stream << "$"; }
	stream << ");";
}
void IfcCoordinateOperation::getStepParameter( std::stringstream& stream, bool ) const { stream << "#" << m_entity_id; }
const std::wstring IfcCoordinateOperation::toString() const { return L"IfcCoordinateOperation"; }
void IfcCoordinateOperation::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 != 2 ){ std::stringstream err; err << "Wrong parameter count for entity IfcCoordinateOperation, expecting 2, having " << num_args << ". Entity ID: " << m_entity_id << std::endl; throw BuildingException( err.str().c_str() ); }
	m_SourceCRS = IfcCoordinateReferenceSystemSelect::createObjectFromSTEP( args[0], map );
	readEntityReference( args[1], m_TargetCRS, map );
}
void IfcCoordinateOperation::getAttributes( std::vector<std::pair<std::string, shared_ptr<BuildingObject> > >& vec_attributes ) const
{
	vec_attributes.emplace_back( std::make_pair( "SourceCRS", m_SourceCRS ) );
	vec_attributes.emplace_back( std::make_pair( "TargetCRS", m_TargetCRS ) );
}
void IfcCoordinateOperation::getAttributesInverse( std::vector<std::pair<std::string, shared_ptr<BuildingObject> > >& vec_attributes_inverse ) const
{
}
void IfcCoordinateOperation::setInverseCounterparts( shared_ptr<BuildingEntity> ptr_self_entity )
{
	shared_ptr<IfcCoordinateOperation> ptr_self = dynamic_pointer_cast<IfcCoordinateOperation>( ptr_self_entity );
	if( !ptr_self ) { throw BuildingException( "IfcCoordinateOperation::setInverseCounterparts: type mismatch" ); }
	shared_ptr<IfcCoordinateReferenceSystem>  SourceCRS_IfcCoordinateReferenceSystem = dynamic_pointer_cast<IfcCoordinateReferenceSystem>( m_SourceCRS );
	if( SourceCRS_IfcCoordinateReferenceSystem )
	{
		SourceCRS_IfcCoordinateReferenceSystem->m_HasCoordinateOperation_inverse.emplace_back( ptr_self );
	}
	shared_ptr<IfcGeometricRepresentationContext>  SourceCRS_IfcGeometricRepresentationContext = dynamic_pointer_cast<IfcGeometricRepresentationContext>( m_SourceCRS );
	if( SourceCRS_IfcGeometricRepresentationContext )
	{
		SourceCRS_IfcGeometricRepresentationContext->m_HasCoordinateOperation_inverse.emplace_back( ptr_self );
	}
}
void IfcCoordinateOperation::unlinkFromInverseCounterparts()
{
	shared_ptr<IfcCoordinateReferenceSystem>  SourceCRS_IfcCoordinateReferenceSystem = dynamic_pointer_cast<IfcCoordinateReferenceSystem>( m_SourceCRS );
	if( SourceCRS_IfcCoordinateReferenceSystem )
	{
		std::vector<weak_ptr<IfcCoordinateOperation> >& HasCoordinateOperation_inverse = SourceCRS_IfcCoordinateReferenceSystem->m_HasCoordinateOperation_inverse;
		for( auto it_HasCoordinateOperation_inverse = HasCoordinateOperation_inverse.begin(); it_HasCoordinateOperation_inverse != HasCoordinateOperation_inverse.end(); )
		{
			weak_ptr<IfcCoordinateOperation> self_candidate_weak = *it_HasCoordinateOperation_inverse;
			if( self_candidate_weak.expired() )
			{
				++it_HasCoordinateOperation_inverse;
				continue;
			}
			shared_ptr<IfcCoordinateOperation> self_candidate( *it_HasCoordinateOperation_inverse );
			if( self_candidate.get() == this )
			{
				it_HasCoordinateOperation_inverse= HasCoordinateOperation_inverse.erase( it_HasCoordinateOperation_inverse );
			}
			else
			{
				++it_HasCoordinateOperation_inverse;
			}
		}
	}
	shared_ptr<IfcGeometricRepresentationContext>  SourceCRS_IfcGeometricRepresentationContext = dynamic_pointer_cast<IfcGeometricRepresentationContext>( m_SourceCRS );
	if( SourceCRS_IfcGeometricRepresentationContext )
	{
		std::vector<weak_ptr<IfcCoordinateOperation> >& HasCoordinateOperation_inverse = SourceCRS_IfcGeometricRepresentationContext->m_HasCoordinateOperation_inverse;
		for( auto it_HasCoordinateOperation_inverse = HasCoordinateOperation_inverse.begin(); it_HasCoordinateOperation_inverse != HasCoordinateOperation_inverse.end(); )
		{
			weak_ptr<IfcCoordinateOperation> self_candidate_weak = *it_HasCoordinateOperation_inverse;
			if( self_candidate_weak.expired() )
			{
				++it_HasCoordinateOperation_inverse;
				continue;
			}
			shared_ptr<IfcCoordinateOperation> self_candidate( *it_HasCoordinateOperation_inverse );
			if( self_candidate.get() == this )
			{
				it_HasCoordinateOperation_inverse= HasCoordinateOperation_inverse.erase( it_HasCoordinateOperation_inverse );
			}
			else
			{
				++it_HasCoordinateOperation_inverse;
			}
		}
	}
}
