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

  Program:   Insight Segmentation & Registration Toolkit (ITK)
  Module:    itkSmartPointerTest.cxx
  Language:  C++
  Date:      $Date$
  Version:   $Revision$


Copyright (c) 2000 National Library of Medicine
All rights reserved.

See COPYRIGHT.txt for copyright details.

=========================================================================*/
#include <iostream>
#include "itkSmartPointer.h"

class itkTestObject
{
public:
  typedef itkSmartPointer<itkTestObject> Pointer;
  static itkTestObject::Pointer New();
  void Register()
    {
    std::cout << "Register " << *this << " count:" 
              << (m_ReferenceCount+1) << "  " << std::endl;
    m_ReferenceCount++;
    }
  void UnRegister()
    {
    std::cout << "UnRegister " << this << " count:" 
              << (m_ReferenceCount-1) << "  " << std::endl;
      
    m_ReferenceCount--;
    if ( m_ReferenceCount == 0 )
      {
      delete this;
      }
    }
    inline friend std::ostream &operator << (std::ostream &os, 
                                             itkTestObject const& o) 
    {
      os << "itkTestObject " << (void*)&o << " " << o.m_ReferenceCount; 
      return os;
    }
  
  std::ostream& Print(std::ostream& os) const
    {
    os << "itkTestObject " << (void*)this << " " << this->m_ReferenceCount; 
    return os;
    }   

protected:
  itkTestObject() 
    {
    m_ReferenceCount = 0;
    std::cout << "construct itkTestObject " << *this << std::endl; 
    }
  ~itkTestObject() 
    {
    std::cout << "destruct itkTestObject " << *this << std::endl; 
    }

private:
  unsigned int m_ReferenceCount;
};

itkTestObject::Pointer itkTestObject::New()
{
  return itkTestObject::Pointer(new itkTestObject);
}



int main()
{
  std::cout <<"second test" << std::endl;
  {
  itkTestObject::Pointer o1 = itkTestObject::New();
  std::cout << "o1 " << &o1 << std::endl;
  itkTestObject::Pointer o2 = itkTestObject::New();
  std::cout << "o2 " << &o2 << std::endl;
  itkTestObject::Pointer o3 = itkTestObject::New();
  std::cout << "o3 " << &o3 << std::endl;
  itkTestObject::Pointer o4 = itkTestObject::New();
  std::cout << "o4 " << &o4 << std::endl;
  
  o1 = o2;
  o2 = o3;
  o4 = o1;
  if ( o1 < o2 )
    {
    std::cout << "o1 is < o2 " << o1 << " " << o2 << std::endl;
    }<commit_msg>ENH: add a test for polymorphic use of smart pointers<commit_after>/*=========================================================================

  Program:   Insight Segmentation & Registration Toolkit (ITK)
  Module:    itkSmartPointerTest.cxx
  Language:  C++
  Date:      $Date$
  Version:   $Revision$


Copyright (c) 2000 National Library of Medicine
All rights reserved.

See COPYRIGHT.txt for copyright details.

=========================================================================*/
#include <iostream>
#include "itkSmartPointer.h"

class itkTestObject
{
public:
  typedef itkSmartPointer<itkTestObject> Pointer;
  static itkTestObject::Pointer New();
  virtual void Register()
    {
    std::cout << "Register " << *this << " count:" 
              << (m_ReferenceCount+1) << "  " << std::endl;
    m_ReferenceCount++;
    }
  virtual void UnRegister()
    {
    std::cout << "UnRegister " << this << " count:" 
              << (m_ReferenceCount-1) << "  " << std::endl;
      
    m_ReferenceCount--;
    if ( m_ReferenceCount == 0 )
      {
      delete this;
      }
    }
    inline friend std::ostream &operator << (std::ostream &os, 
                                             itkTestObject const& o) 
    {
      os << "itkTestObject " << (void*)&o << " " << o.m_ReferenceCount; 
      return os;
    }
  
  std::ostream& Print(std::ostream& os) const
    {
    os << "itkTestObject " << (void*)this << " " << this->m_ReferenceCount; 
    return os;
    }   

protected:
  itkTestObject() 
    {
    m_ReferenceCount = 0;
    std::cout << "construct itkTestObject " << *this << std::endl; 
    }
  ~itkTestObject() 
    {
    std::cout << "destruct itkTestObject " << *this << std::endl; 
    }

private:
  unsigned int m_ReferenceCount;
};

itkTestObject::Pointer itkTestObject::New()
{
  return itkTestObject::Pointer(new itkTestObject);
}

class itkTestObjectSubClass : public itkTestObject
{
public:
  typedef itkSmartPointer<itkTestObjectSubClass> Pointer;
  static itkTestObjectSubClass::Pointer New();
};


itkTestObjectSubClass::Pointer itkTestObjectSubClass::New()
{
  return itkTestObjectSubClass::Pointer(new itkTestObjectSubClass);
}


int main()
{
  itkTestObject::Pointer to = itkTestObjectSubClass::New();
  itkTestObjectSubClass::Pointer sc = dynamic_cast<itkTestObjectSubClass*>((itkTestObject*)to);
  
  std::cout <<"second test" << std::endl;
  {
  itkTestObject::Pointer o1 = itkTestObject::New();
  std::cout << "o1 " << &o1 << std::endl;
  itkTestObject::Pointer o2 = itkTestObject::New();
  std::cout << "o2 " << &o2 << std::endl;
  itkTestObject::Pointer o3 = itkTestObject::New();
  std::cout << "o3 " << &o3 << std::endl;
  itkTestObject::Pointer o4 = itkTestObject::New();
  std::cout << "o4 " << &o4 << std::endl;
  
  o1 = o2;
  o2 = o3;
  o4 = o1;
  if ( o1 < o2 )
    {
    std::cout << "o1 is < o2 " << o1 << " " << o2 << std::endl;
    }
{'fraction_non_alphanumeric': 0.20596051632498102, 'fraction_numerical': 0.012148823082763858, 'mean_word_length': 3.2017543859649122, 'pattern_counts': {'":': 0, '<': 197, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '122089', 'n_tokens_mistral': 1790, 'n_tokens_neox': 1715, 'n_words': 363}