<commit_before>/********************************************************************************
  $Header$

  purpose:
    Generic smart pointer class.

    SP is a "smart pointer" class with reference counting. You can
    copy smart pointers or pass them to subroutines.

    SSP is a "simple smart pointer" which does not support copying.
    It is useful for automatic object destruction only.

  notes:

  to do:

  author(s):
   - <NAME>, <EMAIL>

  modifications:
   09/May/2002 - <NAME>
     - first implementation
 ********************************************************************************
    LibVideoGfx - video processing library
    Copyright (C) 2002  <NAME>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 ********************************************************************************/

#ifndef LIBVIDEOGFX_UTILITY_SMARTPOINTER_HH
#define LIBVIDEOGFX_UTILITY_SMARTPOINTER_HH

#include <libvideogfx/error.hh>

namespace videogfx {

  template <class T> class SP
  {
  public:
    SP()
    {
      counter = NULL;
      pointer = NULL;
    }

    SP(T* p)
    {
      counter = new long;
      *counter = 1;

      pointer = p;
    }

    SP(const SP<T>& s)
    {
      if (s.counter) (*s.counter)++;

      counter = s.counter;
      pointer = s.pointer;
    }

    ~SP()
    {
      Decouple();
    }

    SP<T>& operator=(const SP<T>& s)
    {
      if (&s == this) return *this;  // without this, things go wrong because Decouple() sets 'counter' AND ALSO 's.counter' to NULL

      if (s.counter) (*(s.counter))++;
      Decouple();

      counter = s.counter;
      pointer = s.pointer;

      return *this;
    }

    SP<T>& operator=(T* p)
    {
      if (p==NULL)
	Decouple();
      else
	*this = SP<T>(p);

      return *this;
    }

    bool IsNULL() const { return counter==NULL; }
    T* operator->()  { AssertDescr(counter,"smart pointer is NULL"); return pointer; }
    operator T*()    { AssertDescr(counter,"smart pointer is NULL"); return pointer; }
    T& operator()()  { AssertDescr(counter,"smart pointer is NULL"); return *pointer; }

    void Decouple()
    {
      if (counter)
	{
	  (*counter)--;

	  if (*counter==0)
	    {
	      delete pointer;
	      delete counter;
	    }

	  pointer=NULL;
	  counter=NULL;
	}
    }

  private:
    long* counter;
    T*    pointer;<commit_msg>added const-dereferencing for smart-pointers<commit_after>/********************************************************************************
  $Header$

  purpose:
    Generic smart pointer class.

    SP is a "smart pointer" class with reference counting. You can
    copy smart pointers or pass them to subroutines.

    SSP is a "simple smart pointer" which does not support copying.
    It is useful for automatic object destruction only.

  notes:

  to do:

  author(s):
   - <NAME>, <EMAIL>

  modifications:
   09/May/2002 - <NAME>
     - first implementation
 ********************************************************************************
    LibVideoGfx - video processing library
    Copyright (C) 2002  <NAME>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 ********************************************************************************/

#ifndef LIBVIDEOGFX_UTILITY_SMARTPOINTER_HH
#define LIBVIDEOGFX_UTILITY_SMARTPOINTER_HH

#include <libvideogfx/error.hh>

namespace videogfx {

  template <class T> class SP
  {
  public:
    SP()
    {
      counter = NULL;
      pointer = NULL;
    }

    SP(T* p)
    {
      counter = new long;
      *counter = 1;

      pointer = p;
    }

    SP(const SP<T>& s)
    {
      if (s.counter) (*s.counter)++;

      counter = s.counter;
      pointer = s.pointer;
    }

    ~SP()
    {
      Decouple();
    }

    SP<T>& operator=(const SP<T>& s)
    {
      if (&s == this) return *this;  // without this, things go wrong because Decouple() sets 'counter' AND ALSO 's.counter' to NULL

      if (s.counter) (*(s.counter))++;
      Decouple();

      counter = s.counter;
      pointer = s.pointer;

      return *this;
    }

    SP<T>& operator=(T* p)
    {
      if (p==NULL)
	Decouple();
      else
	*this = SP<T>(p);

      return *this;
    }

    bool IsNULL() const { return counter==NULL; }
    T* operator->()  { AssertDescr(counter,"smart pointer is NULL"); return pointer; }
    operator T*()    { AssertDescr(counter,"smart pointer is NULL"); return pointer; }
    T& operator()()  { AssertDescr(counter,"smart pointer is NULL"); return *pointer; }

    const T* operator->() const { AssertDescr(counter,"smart pointer is NULL"); return pointer; }
    operator const T*()   const { AssertDescr(counter,"smart pointer is NULL"); return pointer; }
    const T& operator()() const { AssertDescr(counter,"smart pointer is NULL"); return *pointer; }

    void Decouple()
    {
      if (counter)
	{
	  (*counter)--;

	  if (*counter==0)
	    {
	      delete pointer;
	      delete counter;
	    }

	  pointer=NULL;
	  counter=NULL;
	}
    }

  private:
    long* counter;
    T*    pointer;
{'fraction_non_alphanumeric': 0.16972261401034322, 'fraction_numerical': 0.008776053910045448, 'mean_word_length': 2.8399518652226234, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3010203', 'n_tokens_mistral': 1911, 'n_tokens_neox': 1780, 'n_words': 654}