<commit_before>/*=========================================================================

Program:   Medical Imaging & Interaction Toolkit
Module:    $RCSfile$
Language:  C++
Date:      $Date$
Version:   $Revision$

Copyright (c) German Cancer Research Center, Division of Medical and
Biological Informatics. All rights reserved.
See MITKCopyright.txt or http://www.mitk.org/copyright.html for details.

This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.  See the above copyright notices for more information.

=========================================================================*/


#include "mitkBaseData.h"
#include "mitkBaseProcess.h"
#include <mitkXMLWriter.h>
#include <mitkXMLReader.h>
#include <itkObjectFactoryBase.h>
#include <mitkTimeSlicedGeometry.h>

#define MITK_WEAKPOINTER_PROBLEM_WORKAROUND_ENABLED

const std::string mitk::BaseData::XML_NODE_NAME = "data";

//##ModelId=3E3FE04202B9
mitk::BaseData::BaseData() : m_SmartSourcePointer(NULL), m_SourceOutputIndexDuplicate(0), m_Unregistering(false), m_CalculatingExternalReferenceCount(false), m_ExternalReferenceCount(-1)
{
  m_Geometry3D = Geometry3D::New();
  m_PropertyList = PropertyList::New();
  
}

//##ModelId=3E3FE042031D
mitk::BaseData::~BaseData() 
{
  m_SmartSourcePointer = NULL;
}

//##ModelId=3DCBE2BA0139
const mitk::Geometry3D* mitk::BaseData::GetUpdatedGeometry()
{
  SetRequestedRegionToLargestPossibleRegion();

  UpdateOutputInformation();

  return m_Geometry3D.GetPointer();
}

mitk::Geometry3D* mitk::BaseData::GetGeometry() const
{ 
  return m_Geometry3D.GetPointer();
}

void mitk::BaseData::SetGeometry(Geometry3D* aGeometry3D)
{
  m_Geometry3D = aGeometry3D;
}

itk::SmartPointerForwardReference<mitk::BaseProcess> mitk::BaseData::GetSource() const
{
  return m_SmartSourcePointer.GetPointer();
}

//##ModelId=3E8600DB02DC
int mitk::BaseData::GetExternalReferenceCount() const
{
  if(m_CalculatingExternalReferenceCount==false) //this is only needed because a smart-pointer to m_Outputs (private!!) must be created by calling GetOutputs.
  {
    m_CalculatingExternalReferenceCount = true;

    m_ExternalReferenceCount = -1;

    int realReferenceCount = GetReferenceCount();

    if(GetSource()==NULL) 
    {
      m_ExternalReferenceCount = realReferenceCount;
      m_CalculatingExternalReferenceCount = false;
      return m_ExternalReferenceCount;
    }

    mitk::BaseProcess::DataObjectPointerArray outputs = m_SmartSourcePointer->GetOutputs();

    unsigned int idx;
    for (idx = 0; idx < outputs.size(); ++idx)
    {
      //references of outputs that are not referenced from someone else (reference additional to the reference from this BaseProcess object) are interpreted as non-existent 
      if(outputs[idx]==this)
        --realReferenceCount;
    }
    m_ExternalReferenceCount = realReferenceCount;
    if(m_ExternalReferenceCount<0)
      m_ExternalReferenceCount=0;
    m_CalculatingExternalReferenceCount = false;
  }
  else
    return -1;
  return m_ExternalReferenceCount;
}

//##ModelId=3E8600DB0188
void mitk::BaseData::UnRegister() const
{
#ifdef MITK_WEAKPOINTER_PROBLEM_WORKAROUND_ENABLED
  if(GetReferenceCount()>1)
  {
    Superclass::UnRegister();
    if((m_Unregistering==false) && (m_SmartSourcePointer.IsNotNull()))
    {
      m_Unregistering=true;
      if(m_SmartSourcePointer->GetExternalReferenceCount()==0)<commit_msg>ENH: enforce freeing of source (related to weak-pointer-problem)<commit_after>/*=========================================================================

Program:   Medical Imaging & Interaction Toolkit
Module:    $RCSfile$
Language:  C++
Date:      $Date$
Version:   $Revision$

Copyright (c) German Cancer Research Center, Division of Medical and
Biological Informatics. All rights reserved.
See MITKCopyright.txt or http://www.mitk.org/copyright.html for details.

This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.  See the above copyright notices for more information.

=========================================================================*/


#include "mitkBaseData.h"
#include "mitkBaseProcess.h"
#include <mitkXMLWriter.h>
#include <mitkXMLReader.h>
#include <itkObjectFactoryBase.h>
#include <mitkTimeSlicedGeometry.h>

#define MITK_WEAKPOINTER_PROBLEM_WORKAROUND_ENABLED

const std::string mitk::BaseData::XML_NODE_NAME = "data";

//##ModelId=3E3FE04202B9
mitk::BaseData::BaseData() : m_SmartSourcePointer(NULL), m_SourceOutputIndexDuplicate(0), m_Unregistering(false), m_CalculatingExternalReferenceCount(false), m_ExternalReferenceCount(-1)
{
  m_Geometry3D = Geometry3D::New();
  m_PropertyList = PropertyList::New();
  
}

//##ModelId=3E3FE042031D
mitk::BaseData::~BaseData() 
{
  m_SmartSourcePointer = NULL;
}

//##ModelId=3DCBE2BA0139
const mitk::Geometry3D* mitk::BaseData::GetUpdatedGeometry()
{
  SetRequestedRegionToLargestPossibleRegion();

  UpdateOutputInformation();

  return m_Geometry3D.GetPointer();
}

mitk::Geometry3D* mitk::BaseData::GetGeometry() const
{ 
  return m_Geometry3D.GetPointer();
}

void mitk::BaseData::SetGeometry(Geometry3D* aGeometry3D)
{
  m_Geometry3D = aGeometry3D;
}

itk::SmartPointerForwardReference<mitk::BaseProcess> mitk::BaseData::GetSource() const
{
  return m_SmartSourcePointer.GetPointer();
}

//##ModelId=3E8600DB02DC
int mitk::BaseData::GetExternalReferenceCount() const
{
  if(m_CalculatingExternalReferenceCount==false) //this is only needed because a smart-pointer to m_Outputs (private!!) must be created by calling GetOutputs.
  {
    m_CalculatingExternalReferenceCount = true;

    m_ExternalReferenceCount = -1;

    int realReferenceCount = GetReferenceCount();

    if(GetSource()==NULL) 
    {
      m_ExternalReferenceCount = realReferenceCount;
      m_CalculatingExternalReferenceCount = false;
      return m_ExternalReferenceCount;
    }

    mitk::BaseProcess::DataObjectPointerArray outputs = m_SmartSourcePointer->GetOutputs();

    unsigned int idx;
    for (idx = 0; idx < outputs.size(); ++idx)
    {
      //references of outputs that are not referenced from someone else (reference additional to the reference from this BaseProcess object) are interpreted as non-existent 
      if(outputs[idx]==this)
        --realReferenceCount;
    }
    m_ExternalReferenceCount = realReferenceCount;
    if(m_ExternalReferenceCount<0)
      m_ExternalReferenceCount=0;
    m_CalculatingExternalReferenceCount = false;
  }
  else
    return -1;
  return m_ExternalReferenceCount;
}

//##ModelId=3E8600DB0188
void mitk::BaseData::UnRegister() const
{
#ifdef MITK_WEAKPOINTER_PROBLEM_WORKAROUND_ENABLED
  if(GetReferenceCount()>1)
  {
    Superclass::UnRegister();
    if((m_Unregistering==false) && (m_SmartSourcePointer.IsNotNull()))
    {
      m_Unregistering=true;
      if((GetSource==NULL) || (m_SmartSourcePointer->GetExternalReferenceCount()==0))
{'fraction_non_alphanumeric': 0.1432052752293578, 'fraction_numerical': 0.016341743119266054, 'mean_word_length': 5.786964980544747, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3381119', 'n_tokens_mistral': 2213, 'n_tokens_neox': 2105, 'n_words': 435}