Re: how to pass smart pointes to function.

From: lokb (lokeshbabu_ms_at_rediffmail.com)
Date: 07/08/04


Date: Thu, 08 Jul 2004 16:38:47 -0400

Hi,
This is what ObjVar is doing
#ifndef SPTR_HPP
#define SPTR_HPP

#include <string>

using namespace std;

template <class T> class Counted;

template <class T>
class ObjVar
{
public:
        ObjVar()
          {
            m_pCounted = 0;
          }

        ObjVar(T* pT)
          {
            m_pCounted = new Counted<T>(pT);
            m_pCounted->GetRef();
          }

        ~ObjVar()
          {
            UnBind();
          }

        ObjVar(const ObjVar<T>& rVar)
          {

            m_pCounted = rVar.m_pCounted;
            if (!Null())
              m_pCounted->GetRef();
          }

        ObjVar<T>& operator=(const ObjVar<T>& rVar)
          {

            if (!rVar.Null())
              rVar.m_pCounted->GetRef();
            UnBind();
            m_pCounted = rVar.m_pCounted;
            return *this;
          }

        ObjVar<T>& operator=(Counted<T>* pCounted)
          {
            if (!rVar.Null())
              rVar.m_pCounted->GetRef();
            UnBind();
            m_pCounted = pCounted;
            return *this;
          }

        T* operator->()
          {
            if (!Null())
              return m_pCounted->my_pT;

          }

        T& operator*()
          {
            if (!Null())
              return *m_pCounted->my_pT;
          }

        const T* operator->() const
          {
            if (Null())
              throw return;
            return m_pCounted->my_pT;

          }

        bool Null() const
          {
            return m_pCounted == 0;
          }

        void SetNull()
          {
            UnBind();
          }

 private:
        void UnBind()
          {
            if (!Null() && m_pCounted->FreeRef() == 0)
              delete m_pCounted;
            m_pCounted = 0;
          }

        Counted<T>* m_pCounted;

};
template <class T> class Counted
{
        friend class ObjVar<T>;
private:
        Counted(T* pT) : Count(0), my_pT(pT)
          {

          }

        ~Counted()
          {
            if (Count == 0)
              delete my_pT;
          }

        unsigned GetRef()
          {
            return ++Count;
          }

        unsigned FreeRef()
          {
            if (Count!=0)
              return --Count;
          }

        T* const my_pT;
        unsigned Count;
};

The IELStruct name was misleading sorry abt that.. its actually
IndexStructure and is defined as

typedef ObjVar<IndexStruct> IndexStruct_sptr;

let me know if u require more information
Thanks,
Lokesh



Relevant Pages

  • Re: dllexport vs. template + inheritance
    ... - Why does the compiler fully instantiate the template, ... #ifndef DllTemplateImpl_H ...
    (microsoft.public.vc.language)
  • Member template function specialization in a template class
    ... specialization of class member functions. ... template ... #ifndef MY_CLASS_H ...
    (comp.lang.cpp)
  • Strange template problem.
    ... The following program does not compile. ... using namespace std; ... template class Foo ...
    (comp.lang.cpp)
  • Re: Help for me:LNK2019
    ... using namespace std; ... void my_new_handler ... i.e. you have to put the full implementation in the header file. ... "To be accessible from other compilation units, a template definition must ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Template specialization
    ... I certainly had to do this to get your code to compile on my ... > namespace std or namespaces within namespace std unless otherwise ... > A program may add template specializations for any standard library ...
    (comp.lang.cpp)