// Utility to release all old OMObjects from the given container.
AAFRESULT ImplAAFRefContainerValue::ReleaseAllObjects(OMReferenceContainer *pContainerProperty)
{
  ASSERTU(pContainerProperty && usesReferenceCounting());

  OMReferenceContainerIterator* containerIter = pContainerProperty->createIterator();
  if (NULL == containerIter)
    return AAFRESULT_NOMEMORY;
    
  AAFRESULT result = AAFRESULT_SUCCESS;
  while (AAFRESULT_SUCCEEDED(result) && (containerIter->before() || containerIter->valid()))
  {
    if (++(*containerIter))
    {
      OMObject *object = containerIter->currentObject();
      ImplAAFStorable *obj = dynamic_cast<ImplAAFStorable*>(object);
      ASSERTU(NULL != obj);
      if (NULL == obj)
      {
        result = AAFRESULT_INVALID_OBJ;
      }
      else
      {
        obj->ReleaseReference();
      }
    }
  }

  delete containerIter;
  containerIter = NULL;
  
  return AAFRESULT_SUCCESS;
}
//
// WriteTo
//
AAFRESULT STDMETHODCALLTYPE ImplAAFRefArrayValue::WriteTo(
  OMProperty* pOmProp)
{
  AAFRESULT result = AAFRESULT_SUCCESS;
  OMObject* object;
  ImplAAFStorable* obj;
  if (NULL == property())
  {
    // First empty the current property.
    OMContainerProperty * pNewContainerProperty = dynamic_cast<OMContainerProperty *>(pOmProp);
    if (NULL == pNewContainerProperty)
      return AAFRESULT_INVALID_OBJ;
    OMReferenceContainer *pNewReferenceContainer = pNewContainerProperty->referenceContainer();
    if (NULL == pNewReferenceContainer)
      return AAFRESULT_INVALID_OBJ;
    
    if (usesReferenceCounting())
    {
      result = ReleaseAllObjects(pNewReferenceContainer);
      if (AAFRESULT_FAILED(result))
        return result;
    }
      
    // Make sure that there are no elements in the new property container.
    pNewReferenceContainer->removeAllObjects();
    
    // Install the new "empty" container property.
    SetProperty(pOmProp);

    OMReferenceContainerIterator* tempIterator = _tempStorableVector->createIterator();
    if (NULL == tempIterator)
    {
      result = AAFRESULT_NOMEMORY;
    }
    else
    {
      while (AAFRESULT_SUCCEEDED(result) && (tempIterator->before() || tempIterator->valid()))
      {
        if (++(*tempIterator))
        {
          object = tempIterator->currentObject();
          obj = dynamic_cast<ImplAAFStorable*>(object);
          ASSERTU(NULL != obj);
          if (NULL == obj)
          {
            result = AAFRESULT_INVALID_OBJ;
          }
          else
          {
//            if (usesReferenceCounting())
//            {
//              obj->detach();
//            }
            
            // Add the element from the temporary property container to the property.
            result = InsertObject(obj); // reference counts if necessary...
            
            if (AAFRESULT_SUCCEEDED(result))
            {
              if (usesReferenceCounting())
              {
                obj->ReleaseReference();
              }
            }
          }
        }
      }
   
      delete tempIterator;
      tempIterator = NULL;
    }
    
    // Cleanup the temporary property now the the the pOmProp has been created and initialized.
		delete _tempStorableVector;
		_tempStorableVector = NULL;
    
    if (AAFRESULT_FAILED(result))
      return result;
  }

  return (ImplAAFRefContainerValue::WriteTo(pOmProp));
}