<commit_before>/* -*- mode: c++; indent-tabs-mode: nil; tab-width: 2 -*-
 *
 *  $Id: rchandle.h,v 1.1 2006/10/09 07:08:02 <NAME> Exp $
 *
 *  Copyright 2006-2007 FLWOR Foundation.  Apache License.
 *	Author: <NAME>
 *
 */

#ifndef XQP_RCHANDLE_H
#define XQP_RCHANDLE_H

#include <string>
#include <sstream>
#include <iostream>

#include "util/hashfun.h"

namespace xqp {

/*******************************************************************************
  
  Base class for reference counted objects

  Any class wishing to take advantage of automatic reference counting
  must inherit from this class.

  rcobject encapsulates the reference count, as well as functions
  for incrementing and decrementing the count. It also contains code
  for destroying a value when it is no longer in use, i.e., when its
  reference count becomes 0.
********************************************************************************/
class rcobject
{
private:
  int refCount;

public:
  rcobject() :	refCount(0) { }

  rcobject(const rcobject& rhs) : 	refCount(0) { }

  virtual ~rcobject() { }

  virtual void free()      { delete this; }

	int getRefCount() const  { return refCount; }

  void addReference()      { ++(this->refCount); }

  void removeReference()   { if (--refCount == 0) this->free(); }

	rcobject& operator=(const rcobject&) { return *this; }
};


/*__________________________________________________________________________  
|                                                                           |
|  rchandle = r(ef)c(ount)handle                                            |
|                                                                           |
|  Template class for smart pointers-to-T objects. T must support the       |
|  rcobject interface, typically by inheriting from rcobject								| 
|___________________________________________________________________________|*/

template<class T>
class rchandle
{
private:
  T  * p;      // dumb pointer this object emulates

  void init(); // common initialization

public:	// ctor, dtor
  rchandle(T* realPtr = 0);
  rchandle(rchandle const& rhs);
  ~rchandle();

public:
  T *get_ptr () const { return p; }
  bool isNull () const { return p==NULL; }
  template <class otherT> rchandle<otherT> cast () const
  { return rchandle<otherT> (static_cast<otherT *> (p)); }

public:	// operator overloading
  rchandle& operator=(rchandle const& rhs);
  T* operator->() const; 
  T& operator*() const;  
  operator T* () { return get_ptr (); }
  operator const T * () { return get_ptr (); }
	bool operator==(rchandle const& h) const;
	bool operator!=(rchandle const& h) const;
	bool operator==(T const* pp) const;
	bool operator!=(T const* pp) const;
  int  operator<(const rchandle& h) const;
  unsigned long hash() const;

public:
	std::string debug() const;
}; 


template<class T>
inline rchandle<T>::rchandle(T* realPtr) : p(realPtr) { init(); }

template<class T>
inline rchandle<T>::rchandle(rchandle const& rhs) : p(rhs.p) { init(); }

template<class T>
inline rchandle<T>::~rchandle() 
{
  if (p)<commit_msg>Added implicit conversion for casting between rchandles<commit_after>/* -*- mode: c++; indent-tabs-mode: nil; tab-width: 2 -*-
 *
 *  $Id: rchandle.h,v 1.1 2006/10/09 07:08:02 <NAME> Exp $
 *
 *  Copyright 2006-2007 FLWOR Foundation.  Apache License.
 *	Author: <NAME>
 *
 */

#ifndef XQP_RCHANDLE_H
#define XQP_RCHANDLE_H

#include <string>
#include <sstream>
#include <iostream>

#include "util/hashfun.h"

namespace xqp {

/*******************************************************************************
  
  Base class for reference counted objects

  Any class wishing to take advantage of automatic reference counting
  must inherit from this class.

  rcobject encapsulates the reference count, as well as functions
  for incrementing and decrementing the count. It also contains code
  for destroying a value when it is no longer in use, i.e., when its
  reference count becomes 0.
********************************************************************************/
class rcobject
{
private:
  int refCount;

public:
  rcobject() :	refCount(0) { }

  rcobject(const rcobject& rhs) : 	refCount(0) { }

  virtual ~rcobject() { }

  virtual void free()      { delete this; }

	int getRefCount() const  { return refCount; }

  void addReference()      { ++(this->refCount); }

  void removeReference()   { if (--refCount == 0) this->free(); }

	rcobject& operator=(const rcobject&) { return *this; }
};


/*__________________________________________________________________________  
|                                                                           |
|  rchandle = r(ef)c(ount)handle                                            |
|                                                                           |
|  Template class for smart pointers-to-T objects. T must support the       |
|  rcobject interface, typically by inheriting from rcobject								| 
|___________________________________________________________________________|*/

template<class T>
class rchandle
{
private:
  T  * p;      // dumb pointer this object emulates

  void init(); // common initialization

public:	// ctor, dtor
  rchandle(T* realPtr = 0);
  rchandle(rchandle const& rhs);
  ~rchandle();

public:
  T *get_ptr () const { return p; }
  bool isNull () const { return p==NULL; }
  template <class otherT> rchandle<otherT> cast () const
  { return rchandle<otherT> (static_cast<otherT *> (p)); }

public:	// operator overloading
  rchandle& operator=(rchandle const& rhs);
  T* operator->() const; 
  T& operator*() const;  
  operator T* () { return get_ptr (); }
  operator const T * () { return get_ptr (); }
  template <class otherT> operator rchandle<otherT> () { return cast<otherT> (); }
	bool operator==(rchandle const& h) const;
	bool operator!=(rchandle const& h) const;
	bool operator==(T const* pp) const;
	bool operator!=(T const* pp) const;
  int  operator<(const rchandle& h) const;
  unsigned long hash() const;

public:
	std::string debug() const;
}; 


template<class T>
inline rchandle<T>::rchandle(T* realPtr) : p(realPtr) { init(); }

template<class T>
inline rchandle<T>::rchandle(rchandle const& rhs) : p(rhs.p) { init(); }

template<class T>
inline rchandle<T>::~rchandle() 
{
  if (p)
{'fraction_non_alphanumeric': 0.16573793981850024, 'fraction_numerical': 0.009552619009711829, 'mean_word_length': 3.1713147410358564, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5190541', 'n_tokens_mistral': 1971, 'n_tokens_neox': 1863, 'n_words': 560}