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

Program:   Medical Imaging & Interaction Toolkit
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 "mitkDataStorage.h"

#include "mitkDataTreeNode.h"
#include "mitkProperties.h"
#include "mitkNodePredicateBase.h"
#include "mitkNodePredicateProperty.h"
#include "mitkGroupTagProperty.h"
#include "itkCommand.h"


mitk::DataStorage::Pointer mitk::DataStorage::s_Instance = NULL;


mitk::DataStorage::DataStorage() 
: itk::Object(), m_ManageCompleteTree(true), m_DuringRemove(false) // true by default until all Reliver functionalities use the datastorage properly
{
  m_DataTree = NULL;
}


mitk::DataStorage::~DataStorage()
{
  if(m_DataTree.IsNotNull())
  {
    m_DataTree->RemoveObserver(m_DeleteInTreeObserverTag);
  }

  // workaround for bug #343: do another UnRegister in case we re-create a DataStorage 
  // which happens when a PlugIn is re-initialized within Chili
  if(s_Instance.IsNotNull())
  {
    s_Instance->m_DataTree->UnRegister();
  }

  m_DataTree = NULL; 
}


mitk::DataStorage* mitk::DataStorage::CreateInstance(mitk::DataTree* tree)
{
  // workaround for bug #343: do another UnRegister in case we re-create a DataStorage 
  // which happens when a PlugIn is re-initialized within Chili
  if(s_Instance.IsNotNull())
  {
    s_Instance->m_DataTree->UnRegister();
  }
  s_Instance = NULL;
  try
  {
    s_Instance = mitk::DataStorage::New();
    s_Instance->Initialize(tree);   // If no datastore created yet, and tree is NULL, then this will raise an exception, because the datastorage must be initialized with a datatree
    /// \todo additional register on tree needed to prevent crash in itkTimeStamp.cxx (see bug #343)
    tree->Register();
  }
  catch(...)
  {
    s_Instance = NULL;
    throw 1;  // insert exception handling here
  }
  return s_Instance;
}


mitk::DataStorage* mitk::DataStorage::GetInstance()
{
  if (DataStorage::s_Instance.IsNull())
   throw 1;  // insert exception handling here
  else  
    return s_Instance;
}<commit_msg>CHG (#1446): mitkDataStorage.cpp now has an itkGenericOutputMacro if GetInstance() is called and the instance is NULL. Additionally the return value is set to NULL. This will most probably cause problems in cases where the mitkDataStorage.cpp is destroyed before the functionalities are destroyed.<commit_after>/*=========================================================================

Program:   Medical Imaging & Interaction Toolkit
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 "mitkDataStorage.h"

#include "mitkDataTreeNode.h"
#include "mitkProperties.h"
#include "mitkNodePredicateBase.h"
#include "mitkNodePredicateProperty.h"
#include "mitkGroupTagProperty.h"
#include "itkCommand.h"


mitk::DataStorage::Pointer mitk::DataStorage::s_Instance = NULL;


mitk::DataStorage::DataStorage() 
: itk::Object(), m_ManageCompleteTree(true), m_DuringRemove(false) // true by default until all Reliver functionalities use the datastorage properly
{
  m_DataTree = NULL;
}


mitk::DataStorage::~DataStorage()
{
  if(m_DataTree.IsNotNull())
  {
    m_DataTree->RemoveObserver(m_DeleteInTreeObserverTag);
  }

  // workaround for bug #343: do another UnRegister in case we re-create a DataStorage 
  // which happens when a PlugIn is re-initialized within Chili
  if(s_Instance.IsNotNull())
  {
    s_Instance->m_DataTree->UnRegister();
  }

  m_DataTree = NULL; 
}


mitk::DataStorage* mitk::DataStorage::CreateInstance(mitk::DataTree* tree)
{
  // workaround for bug #343: do another UnRegister in case we re-create a DataStorage 
  // which happens when a PlugIn is re-initialized within Chili
  if(s_Instance.IsNotNull())
  {
    s_Instance->m_DataTree->UnRegister();
  }
  s_Instance = NULL;
  try
  {
    s_Instance = mitk::DataStorage::New();
    s_Instance->Initialize(tree);   // If no datastore created yet, and tree is NULL, then this will raise an exception, because the datastorage must be initialized with a datatree
    /// \todo additional register on tree needed to prevent crash in itkTimeStamp.cxx (see bug #343)
    tree->Register();
  }
  catch(...)
  {
    s_Instance = NULL;
    throw 1;  // insert exception handling here
  }
  return s_Instance;
}


mitk::DataStorage* mitk::DataStorage::GetInstance()
{
  if (DataStorage::s_Instance.IsNull())
  {
      itkGenericOutputMacro("Trying to use mitk::DataStorage::GetInstance() " 
        << "without an available singleton instance. Either no instance has "
        << "been created (use DataStorage::CreateInstance) or it has already "
        << "been destroyed.");
      return NULL;
  }
  else  
    return s_Instance;
}
{'fraction_non_alphanumeric': 0.1471059661620659, 'fraction_numerical': 0.004452359750667854, 'mean_word_length': 4.5936254980079685, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 17, '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': '3867715', 'n_tokens_mistral': 1670, 'n_tokens_neox': 1604, 'n_words': 500}