void ComponentCollection::CopyTo(const Reflect::ElementPtr& destination) { __super::CopyTo( destination ); ComponentCollection* destCollection = Reflect::ObjectCast< ComponentCollection >( destination ); if ( destCollection ) { // Remove all attributes, we're going to bring them over manually destCollection->Clear(); // For each component in this component collection Reflect::Registry* registry = Reflect::Registry::GetInstance(); M_Component::const_iterator attrItr = m_Components.begin(); M_Component::const_iterator attrEnd = m_Components.end(); for ( ; attrItr != attrEnd; ++attrItr ) { // Create a new copy of the component and try to add it to the destination const ComponentPtr& attrib = attrItr->second; ComponentPtr destAttrib = Reflect::AssertCast< ComponentBase >( registry->CreateInstance( attrib->GetClass() ) ); if ( !CopyComponentTo( *destCollection, destAttrib, attrib ) ) { // Component could not be added to the destination collection, check sibling classes const std::set<tstring>& derived = ( registry->GetClass( attrib->GetClass()->m_Base ) )->m_Derived; std::set<tstring>::const_iterator derivedItr = derived.begin(); std::set<tstring>::const_iterator derivedEnd = derived.end(); for ( ; derivedItr != derivedEnd; ++derivedItr ) { const Reflect::Class* currentType = Reflect::Registry::GetInstance()->GetClass(*derivedItr); if ( currentType->m_TypeID != attrib->GetType() ) { destAttrib = Reflect::AssertCast< ComponentBase >( registry->CreateInstance( currentType ) ); if ( destAttrib.ReferencesObject() ) { if ( CopyComponentTo( *destCollection, destAttrib, attrib ) ) { break; } } } } } } } }
void ComponentCollection::CopyTo(Reflect::Object* object) { Base::CopyTo( object ); ComponentCollection* collection = Reflect::SafeCast< ComponentCollection >( object ); if ( collection ) { // Remove all attributes, we're going to bring them over manually collection->Clear(); // For each component in this component collection Reflect::Registry* registry = Reflect::Registry::GetInstance(); M_Component::const_iterator attrItr = m_Components.begin(); M_Component::const_iterator attrEnd = m_Components.end(); for ( ; attrItr != attrEnd; ++attrItr ) { // Create a new copy of the component and try to add it to the destination const ComponentPtr& attrib = attrItr->second; ComponentPtr destAttrib = Reflect::AssertCast< ComponentBase >( registry->CreateInstance( attrib->GetMetaClass() ) ); if ( !CopyComponentTo( *collection, destAttrib, attrib ) ) { // Component could not be added to the destination collection, check sibling classes for ( const Composite* sibling = attrib->GetMetaClass()->m_Base->m_FirstDerived; sibling; sibling = sibling->m_NextSibling ) { if ( sibling != attrib->GetMetaClass() ) { destAttrib = Reflect::AssertCast< ComponentBase >( registry->CreateInstance( Reflect::ReflectionCast< const MetaClass >( sibling ) ) ); if ( destAttrib.ReferencesObject() ) { if ( CopyComponentTo( *collection, destAttrib, attrib ) ) { break; } } } } } } } }
bool ComponentCollection::CopyComponentTo( ComponentCollection& destCollection, const ComponentPtr& destAttrib, const ComponentPtr& srcAttrib ) { bool inserted = false; Reflect::Registry* registry = Reflect::Registry::GetInstance(); tstring unused; // If there is already an component in the destination slot, or the // component is not in the destination, but is allowed to be... if ( destCollection.ValidateComponent( destAttrib, unused ) ) { // Component can be added to the destination collection, so do it! srcAttrib->CopyTo( destAttrib ); destCollection.SetComponent( destAttrib, false ); inserted = true; } else { ComponentPtr existing = destCollection.GetComponent( destAttrib->GetSlot() ); if ( existing.ReferencesObject() ) { destCollection.RemoveComponent( existing->GetSlot() ); if ( destCollection.ValidateComponent( destAttrib, unused ) ) { srcAttrib->CopyTo( destAttrib ); destCollection.SetComponent( destAttrib, false ); inserted = true; } else { destCollection.SetComponent( existing, false ); } } } return inserted; }