Destructor for const object

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Virendra Verma

    Destructor for const object

    This sounds weird, but I am looking for separate behaviors for
    destruction of a const and non-const object.

    I am trying to develop a smart/auto pointer class for writing objects
    to disk implicitly. The destructor of this class saves the object to
    the disk if it is dirty. The problem comes in the following scenario
    when a function returns an uncommitted pointer class because same
    copies will be committed as two separate objects on disk. For example,

    DbPtr< T > some_function( )
    {
    .....
    DbPtr<T> pN; // uncommitted copy
    return pN; // pN will be committed in the destructor but the
    returned
    // copy is not. I could define an assignment operator
    // for DbPtr<T> as below but the commit operation
    // on const object has problems as it has to modifies
    // object
    }

    tempalte<class T>
    class DbPtr : public some_base {
    public:
    ~DbPtr<T>( ) { commit(); } // I need to recognize here if I am
    destroying
    // a const object to bypass commit.
    void commit( );
    void operator=( const DbPtr<T>& rP ) { *this = rP; }
    void operator=( DbPtr<T>& rP ) { rP.commit(); *this = rP; }
    }

    Thanks in advance.
    -- Virendr
  • Kevin Goodsell

    #2
    Re: Destructor for const object

    Virendra Verma wrote:
    [color=blue]
    > This sounds weird, but I am looking for separate behaviors for
    > destruction of a const and non-const object.
    >
    > I am trying to develop a smart/auto pointer class for writing objects
    > to disk implicitly. The destructor of this class saves the object to
    > the disk if it is dirty.[/color]

    I don't see what 'const' has to do with this.
    [color=blue]
    > The problem comes in the following scenario
    > when a function returns an uncommitted pointer class because same
    > copies will be committed as two separate objects on disk. For example,
    >
    > DbPtr< T > some_function( )
    > {
    > .....
    > DbPtr<T> pN; // uncommitted copy
    > return pN; // pN will be committed in the destructor but the
    > returned
    > // copy is not. I could define an assignment operator
    > // for DbPtr<T> as below but the commit operation
    > // on const object has problems as it has to modifies
    > // object
    > }
    >
    > tempalte<class T>
    > class DbPtr : public some_base {
    > public:
    > ~DbPtr<T>( ) { commit(); } // I need to recognize here if I am
    > destroying
    > // a const object to bypass commit.[/color]

    You seem to have a lot of <T> that I'm pretty sure you don't need.

    Again, 'const' does not seem relevant. Your example doesn't include any
    'const' objects, and this approach (if it was possible) does not seem
    likely to solve your problem.
    [color=blue]
    > void commit( );[/color]

    "Commit" is probably a non-modifying operation, so this should be

    void commit() const;

    Yes, you should do this even if it has to modify members of the object.
    As long as the observable state of the object does not change (that is,
    users of the class can't see a difference), the operation should be
    const. Use 'mutable' members if you need to. This is why they exist.
    [color=blue]
    > void operator=( const DbPtr<T>& rP ) { *this = rP; }
    > void operator=( DbPtr<T>& rP ) { rP.commit(); *this = rP; }[/color]

    I also don't understand what you hope to accomplish with the assignment
    operators. Your example doesn't use them, either. And it looks like they
    will infinitely recurse.
    [color=blue]
    > }
    >[/color]

    It sounds like you want to commit when the final copy is destroyed
    (maybe -- it's not completely clear). In that case, you probably want a
    reference-counting scheme. const objects have nothing to do with it, and
    assignment operators are only related in the sense that they need to
    update reference counts, and generally perform some action when a
    reference count reaches 0.

    Or maybe you want a 'commit_on_dest roy' flag in your class. When an
    instance is copied, you could set it in one copy and clear it in the
    other. But you have to remember to do this in the assignment operator
    *and* the copy constructor.

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.

    Comment

    • tom_usenet

      #3
      Re: Destructor for const object

      On 12 Apr 2004 08:35:33 -0700, virenbeena@hotm ail.com (Virendra Verma)
      wrote:
      [color=blue]
      >This sounds weird, but I am looking for separate behaviors for
      >destruction of a const and non-const object.
      >
      >I am trying to develop a smart/auto pointer class for writing objects
      >to disk implicitly. The destructor of this class saves the object to
      >the disk if it is dirty. The problem comes in the following scenario
      >when a function returns an uncommitted pointer class because same
      >copies will be committed as two separate objects on disk. For example,
      >
      >DbPtr< T > some_function( )
      >{
      > .....
      > DbPtr<T> pN; // uncommitted copy
      > return pN; // pN will be committed in the destructor but the
      >returned
      > // copy is not. I could define an assignment operator
      > // for DbPtr<T> as below but the commit operation
      > // on const object has problems as it has to modifies
      > // object
      >}[/color]

      There are no const objects in the example above. Do you mean temporary
      objects? Presumably the commit only happens when the last reference
      goes out of scope?
      [color=blue]
      >
      >tempalte<cla ss T>
      >class DbPtr : public some_base {
      >public:
      > ~DbPtr<T>( ) { commit(); } // I need to recognize here if I am
      >destroying
      > // a const object to bypass commit.[/color]

      Do you mean whether you are destroying the last reference to an
      object? Just use reference counting...
      [color=blue]
      > void commit( );
      > void operator=( const DbPtr<T>& rP ) { *this = rP; }
      > void operator=( DbPtr<T>& rP ) { rP.commit(); *this = rP; }
      >}
      >
      >Thanks in advance.[/color]

      If you need different behaviour for const DbPtrs, create a partial
      specialization:

      template <class T>
      class DbPtr<T const> : public some_base
      {
      //specialization for const
      };

      Tom
      --
      C++ FAQ: http://www.parashift.com/c++-faq-lite/
      C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

      Comment

      • Nick Hounsome

        #4
        Re: Destructor for const object


        "Virendra Verma" <virenbeena@hot mail.com> wrote in message
        news:30ee7e04.0 404120735.15a90 573@posting.goo gle.com...[color=blue]
        > This sounds weird, but I am looking for separate behaviors for
        > destruction of a const and non-const object.
        >
        > I am trying to develop a smart/auto pointer class for writing objects
        > to disk implicitly. The destructor of this class saves the object to
        > the disk if it is dirty. The problem comes in the following scenario
        > when a function returns an uncommitted pointer class because same
        > copies will be committed as two separate objects on disk. For example,
        >
        > DbPtr< T > some_function( )
        > {
        > .....
        > DbPtr<T> pN; // uncommitted copy
        > return pN; // pN will be committed in the destructor but the
        > returned
        > // copy is not. I could define an assignment operator
        > // for DbPtr<T> as below but the commit operation
        > // on const object has problems as it has to modifies
        > // object
        > }
        >
        > tempalte<class T>
        > class DbPtr : public some_base {
        > public:
        > ~DbPtr<T>( ) { commit(); } // I need to recognize here if I am
        > destroying
        > // a const object to bypass commit.
        > void commit( );
        > void operator=( const DbPtr<T>& rP ) { *this = rP; }
        > void operator=( DbPtr<T>& rP ) { rP.commit(); *this = rP; }
        > }
        >
        > Thanks in advance.
        > -- Virendr[/color]

        I don't see what you are trying to do with DbPtr but I suspect that you
        should be using a reference counting smart pointer to a real class.
        These can be passed around as in your function example without commiting.
        When the reference count reaches 0 the smart pointer commits the object it
        points to and then
        deletes it.
        One advantage of separating the commit from the object itself is that you
        can then use the object
        both inside and outside the database.
        Another approach that will work better with exceptions is a separate
        transaction class .


        Comment

        • Virendra Verma

          #5
          Re: Destructor for const object

          Thanks both of you.

          I believe I have to use a reference counted class. The problem with
          the above scenario was that I was using class variables for DbPtr
          class which get duplicated before returning and thus committing both
          copies. Here is the code that does the trick. DbBasePtr class is only
          a single copy and keeps track commit status. If the copy is already
          committed, second commit will simply be ignored.

          I have another question. In my DbAutoPtr<T> class below, which
          operator gets called for the rvalue operand, a->? Is there an implicit
          or elegant (without creating or typecasting to a const object) way of
          "const T& operator->( ) const" version to be called as the rvalue is
          not being modified?

          struct C {
          int x;
          };

          struct A {
          int y;
          };


          DbStore db;
          DbAutoPtr< C> p(db, 0, sizeof( C ) );
          DbAutoPtr< A > a( db, 0, sizeof(A) );

          p->x = a->y;


          =============== ====
          // DbAutPtr.h - auto pinter

          #ifndef _DBAUTPTR_H_
          #define _DBAUTPTR_H_

          #include "syTypes.h"
          #include "DbDefs.h"
          #include "DbBasPtr.h "
          #include "DbRefCnt.h "

          class DbStore;

          //////////////////////////////////////////////////////////////////////////
          // DbAutoPtr< T > class
          //////////////////////////////////////////////////////////////////////////
          template< class T >
          class DbAutoPtr {
          public:
          DbAutoPtr( );
          DbAutoPtr( DbStore& rStore, fsize_type nAdr );
          DbAutoPtr( DbStore& rStore, fsize_type nAdr, size_type nSize );
          ~DbAutoPtr( );

          // Operators
          T* operator->( );
          const T* operator->( ) const;
          DbAutoPtr< T >& operator=( const DbBasePtr& rP );

          T& operator*( );
          const T& operator*( ) const;

          bool isNull( ) const;

          fsize_type address( );
          fsize_type address( ) const;
          size_type size( ) const;
          void setAddress( fsize_type nAdr );
          void setSize( size_type nSize );
          bool loadData( );
          bool writeData( );

          const DbBasePtr * basePtr( ) const;

          // protected:
          const T * pointer( ) const;
          T * pointer( );

          protected:
          DbRefCount< DbBasePtr > _oData; // Object data
          };

          //////////////////////////////////////////////////////////////////////////
          template<class T>
          inline DbAutoPtr<T>::D bAutoPtr( )
          //////////////////////////////////////////////////////////////////////////
          {
          }

          //////////////////////////////////////////////////////////////////////////
          template<class T>
          inline DbAutoPtr<T>::D bAutoPtr( DbStore& rStore, fsize_type nAdr )
          //////////////////////////////////////////////////////////////////////////
          {
          _oData = DbBasePtr( rStore, nAdr, sizeof( T ) );
          _oData->loadData( );
          }

          //////////////////////////////////////////////////////////////////////////
          template<class T>
          inline DbAutoPtr<T>::D bAutoPtr( DbStore& rStore, fsize_type nAdr,
          size_type nSize )
          //////////////////////////////////////////////////////////////////////////
          {
          _oData = DbBasePtr( rStore, nAdr, nSize );
          _oData->loadData( );
          }

          //////////////////////////////////////////////////////////////////////////
          template<class T>
          inline DbAutoPtr<T>::~ DbAutoPtr( )
          //////////////////////////////////////////////////////////////////////////
          {
          if( !_oData.isNull( ) )
          _oData->writeData( T::Clsid() );
          }

          //////////////////////////////////////////////////////////////////////////
          template<class T>
          inline DbAutoPtr< T >& DbAutoPtr<T>::o perator=( const DbBasePtr& rP )
          //////////////////////////////////////////////////////////////////////////
          {
          if( _oData.isNull() ) {
          _oData = rP;
          _oData->loadData( );
          }
          return *this;
          }

          //////////////////////////////////////////////////////////////////////////
          template<class T>
          inline const DbBasePtr * DbAutoPtr<T>::b asePtr( ) const
          //////////////////////////////////////////////////////////////////////////
          {
          return _oData.pointer( );
          }

          //////////////////////////////////////////////////////////////////////////
          template<class T>
          inline T* DbAutoPtr<T>::p ointer( )
          //////////////////////////////////////////////////////////////////////////
          {
          return (T*) _oData.pointer( )->dataPointer( );
          }

          //////////////////////////////////////////////////////////////////////////
          template<class T>
          inline const T* DbAutoPtr<T>::p ointer( ) const
          //////////////////////////////////////////////////////////////////////////
          {
          return (const T*) _oData.pointer( )->dataPointer( );
          }

          //////////////////////////////////////////////////////////////////////////
          template<class T>
          inline const T* DbAutoPtr<T>::o perator->() const
          //////////////////////////////////////////////////////////////////////////
          {
          return (const T*) pointer( );
          }

          //////////////////////////////////////////////////////////////////////////
          template<class T>
          inline T* DbAutoPtr<T>::o perator->()
          //////////////////////////////////////////////////////////////////////////
          {
          return (T*) pointer();
          }

          //////////////////////////////////////////////////////////////////////////
          template<class T>
          inline const T& DbAutoPtr<T>::o perator*() const
          //////////////////////////////////////////////////////////////////////////
          {
          V_ASSERT( pointer( ) );
          return *((const T*) pointer( ));
          }

          //////////////////////////////////////////////////////////////////////////
          template<class T>
          inline T& DbAutoPtr<T>::o perator*()
          //////////////////////////////////////////////////////////////////////////
          {
          V_ASSERT( pointer( ) );
          return *((T*) pointer( ));
          }

          //////////////////////////////////////////////////////////////////////////
          template<class T>
          inline bool DbAutoPtr<T>::i sNull( ) const
          //////////////////////////////////////////////////////////////////////////
          {
          return _oData.isNull( ) || (0 == _oData->size( ) );
          }

          //////////////////////////////////////////////////////////////////////////
          template<class T>
          inline fsize_type DbAutoPtr<T>::a ddress( ) const
          //////////////////////////////////////////////////////////////////////////
          {
          return _oData->address( );
          }

          //////////////////////////////////////////////////////////////////////////
          template<class T>
          inline size_type DbAutoPtr<T>::s ize( ) const
          //////////////////////////////////////////////////////////////////////////
          {
          return _oData->size( );
          }

          //////////////////////////////////////////////////////////////////////////
          template<class T>
          inline fsize_type DbAutoPtr<T>::a ddress( )
          //////////////////////////////////////////////////////////////////////////
          {
          return _oData->address( T::Clsid() );
          }

          //////////////////////////////////////////////////////////////////////////
          template<class T>
          inline void DbAutoPtr<T>::s etAddress( fsize_type nAdr )
          //////////////////////////////////////////////////////////////////////////
          {
          _oData->setAddress( nAdr );
          }

          //////////////////////////////////////////////////////////////////////////
          template<class T>
          inline void DbAutoPtr<T>::s etSize( size_type nSize )
          //////////////////////////////////////////////////////////////////////////
          {
          _oData->setSize( nSize );
          }

          //////////////////////////////////////////////////////////////////////////
          template<class T>
          inline bool DbAutoPtr<T>::l oadData( )
          //////////////////////////////////////////////////////////////////////////
          {
          return _oData->loadData( );
          }

          //////////////////////////////////////////////////////////////////////////
          template<class T>
          inline bool DbAutoPtr<T>::w riteData( )
          //////////////////////////////////////////////////////////////////////////
          {
          return _oData->writeData( T::Clsid() );
          }

          #endif
          ==========
          // DbRefCount.h - compact reference counted class

          #ifndef _REFCOUNT_H_
          #define _REFCOUNT_H_

          #include <stdlib.h>
          #include "syTypes.h"

          ///////////////////////////////////////////////////////////////////////////////
          // DbRefCount class
          ///////////////////////////////////////////////////////////////////////////////
          template< class T >
          class DbRefCount {
          public:
          DbRefCount( );
          DbRefCount( const T& t );
          DbRefCount( const DbRefCount& rPtr );
          ~DbRefCount( );

          T* operator->( );
          const T* operator->( ) const;
          T& operator*( );
          const T& operator*( ) const;
          DbRefCount& operator=( const T& t );
          DbRefCount& operator=( const DbRefCount& rPtr );
          bool operator==( const DbRefCount& rPtr ) const;
          bool isNull( ) const;

          // protected:
          const T * pointer( ) const;
          T * pointer( );
          void copyFrom( const DbRefCount& rSrc);
          void release( );
          void * alloc( size_t nSize );

          protected:
          int * _pPtr;
          };

          ///////////////////////////////////////////////////////////////////////////////
          template< class T >
          inline DbRefCount< T >::DbRefCount ( ) : _pPtr( NULLPTR )
          ///////////////////////////////////////////////////////////////////////////////
          {
          }

          ///////////////////////////////////////////////////////////////////////////////
          template< class T >
          inline DbRefCount< T >::DbRefCount ( const T& t ) : _pPtr( NULLPTR )
          ///////////////////////////////////////////////////////////////////////////////
          {
          T * p = new( alloc( sizeof(T) ) ) T( t );
          }

          ///////////////////////////////////////////////////////////////////////////////
          template< class T >
          inline DbRefCount< T >::DbRefCount ( const DbRefCount< T >& rPtr )
          ///////////////////////////////////////////////////////////////////////////////
          {
          copyFrom( rPtr );
          }

          ///////////////////////////////////////////////////////////////////////////////
          template< class T >
          inline DbRefCount< T >::~DbRefCoun t( )
          ///////////////////////////////////////////////////////////////////////////////
          {
          release( );
          }

          //////////////////////////////////////////////////////////////////////////
          template<class T>
          inline const T* DbRefCount<T>:: operator->() const
          //////////////////////////////////////////////////////////////////////////
          {
          return pointer( );
          }

          //////////////////////////////////////////////////////////////////////////
          template<class T>
          inline T* DbRefCount<T>:: operator->()
          //////////////////////////////////////////////////////////////////////////
          {
          return pointer( );
          }

          //////////////////////////////////////////////////////////////////////////
          template<class T>
          inline const T& DbRefCount<T>:: operator*() const
          //////////////////////////////////////////////////////////////////////////
          {
          V_ASSERT( pointer( ) );
          return *pointer( );
          }

          //////////////////////////////////////////////////////////////////////////
          template<class T>
          inline T& DbRefCount<T>:: operator*()
          //////////////////////////////////////////////////////////////////////////
          {
          V_ASSERT( pointer( ) );
          return *pointer( );
          }

          ///////////////////////////////////////////////////////////////////////////////
          template<class T>
          inline const T * DbRefCount< T >::pointer( ) const
          ///////////////////////////////////////////////////////////////////////////////
          {
          V_ASSERT( _pPtr );
          return (const T *) (_pPtr + 1);
          }

          ///////////////////////////////////////////////////////////////////////////////
          template<class T>
          inline T * DbRefCount< T >::pointer( )
          ///////////////////////////////////////////////////////////////////////////////
          {
          return _pPtr ? (T *) (_pPtr + 1) : NULLPTR;
          }

          ///////////////////////////////////////////////////////////////////////////////
          template<class T>
          inline DbRefCount< T >& DbRefCount< T >::operator =( const T& t )
          ///////////////////////////////////////////////////////////////////////////////
          {
          if( NULLPTR == _pPtr )
          new( alloc( sizeof(T) ) ) T( t );
          return *this;
          }

          ///////////////////////////////////////////////////////////////////////////////
          template<class T>
          inline DbRefCount< T >& DbRefCount< T >::operator=( const DbRefCount<
          T >& rPtr )
          ///////////////////////////////////////////////////////////////////////////////
          {
          if( &rPtr != this ) {
          release( );
          copyFrom( rPtr );
          }
          return *this;
          }

          ///////////////////////////////////////////////////////////////////////////////
          template<class T>
          inline bool DbRefCount< T >::operator== ( const DbRefCount< T >& rRhs )
          const
          ///////////////////////////////////////////////////////////////////////////////
          {
          return _pPtr == rRhs._pPtr;
          }

          ///////////////////////////////////////////////////////////////////////////////
          template<class T>
          inline bool DbRefCount< T >::isNull( ) const
          ///////////////////////////////////////////////////////////////////////////////
          {
          return _pPtr == NULLPTR;
          }

          ///////////////////////////////////////////////////////////////////////////////
          template<class T>
          inline void DbRefCount< T >::release( )
          ///////////////////////////////////////////////////////////////////////////////
          {
          if( _pPtr ) {
          (*_pPtr)--;
          if( *_pPtr == 0 ) {
          T * tp = pointer( );
          tp->~T( );
          ::free( _pPtr );
          _pPtr = NULLPTR;
          }
          }
          }

          ///////////////////////////////////////////////////////////////////////////////
          template<class T>
          inline void DbRefCount< T >::copyFrom( const DbRefCount< T >& rSrc )
          ///////////////////////////////////////////////////////////////////////////////
          {
          _pPtr = rSrc._pPtr;
          if( _pPtr ) (*_pPtr)++;
          }

          ///////////////////////////////////////////////////////////////////////////////
          template<class T>
          inline void * DbRefCount< T >::alloc( size_t nSize )
          ///////////////////////////////////////////////////////////////////////////////
          {
          int * pPtr = (int *) ::realloc( _pPtr, nSize + sizeof(int) );
          if( NULLPTR == _pPtr ) {
          *pPtr = 1;
          }
          _pPtr = pPtr;
          return _pPtr + 1;
          }

          #endif
          =======
          // DbBasPtr.h

          #ifndef _DBBASPTR_H_
          #define _DBBASPTR_H_

          #include "syTypes.h"
          #include "DbDefs.h"
          #include "DbObjRef.h "

          class DbStore;

          ///////////////////////////////////////////////////////////////////////////////
          // DbBasePtr
          ///////////////////////////////////////////////////////////////////////////////
          class DbBasePtr : public DbObjRef {
          public:
          DbBasePtr( DbStore& rStore, fsize_type nAdr, size_type nSize );
          ~DbBasePtr( );

          const void * dataPointer( ) const;
          void * dataPointer( );
          fsize_type address( clsid_type nClsid );
          fsize_type address( ) const;

          // protected:
          bool loadData( );
          bool writeData( clsid_type nClsid );
          void * alloc( size_type nSize );

          protected:
          DbStore * _pStore;
          void * _pData; // Object data
          };

          ///////////////////////////////////////////////////////////////////////////////
          inline DbBasePtr::DbBa sePtr( DbStore& rStore, fsize_type nAdr,
          size_type nSize )
          : DbObjRef( nAdr, nSize ), _pStore( &rStore ),
          _pData( NULLPTR )
          ///////////////////////////////////////////////////////////////////////////////
          {
          }

          ///////////////////////////////////////////////////////////////////////////////
          inline DbBasePtr::~DbB asePtr( )
          ///////////////////////////////////////////////////////////////////////////////
          {
          }

          ///////////////////////////////////////////////////////////////////////////////
          inline const void * DbBasePtr::data Pointer( ) const
          ///////////////////////////////////////////////////////////////////////////////
          {
          return _pData;
          }

          ///////////////////////////////////////////////////////////////////////////////
          inline void * DbBasePtr::data Pointer( )
          ///////////////////////////////////////////////////////////////////////////////
          {
          setDirty( );
          return _pData;
          }

          //////////////////////////////////////////////////////////////////////////
          inline fsize_type DbBasePtr::addr ess( ) const
          //////////////////////////////////////////////////////////////////////////
          {
          return DbObjRef::addre ss( );
          }

          //////////////////////////////////////////////////////////////////////////
          inline fsize_type DbBasePtr::addr ess( clsid_type nClsid )
          //////////////////////////////////////////////////////////////////////////
          {
          writeData( nClsid );
          return DbObjRef::addre ss( );
          }

          #endif

          Comment

          • Nick Hounsome

            #6
            Re: Destructor for const object


            "Virendra Verma" <virenbeena@hot mail.com> wrote in message
            news:30ee7e04.0 404131108.1df58 d18@posting.goo gle.com...[color=blue]
            > Thanks both of you.
            >
            > I believe I have to use a reference counted class. The problem with
            > the above scenario was that I was using class variables for DbPtr
            > class which get duplicated before returning and thus committing both
            > copies. Here is the code that does the trick. DbBasePtr class is only
            > a single copy and keeps track commit status. If the copy is already
            > committed, second commit will simply be ignored.
            >[/color]

            It is still not clear what you are trying to do but I believe that it is
            still
            totally wrong.

            1. For the DbAutoPtr you cannot have the reference count in the pointer.
            It must either be in the object pointed to or on the heap.
            2. DbAutoPtr ctor should take a T* because it is supposed to point to a T.
            3. The pointer should ONLY do the owning/commit. Use a separate mechanism to
            create new instances or find them initially in the db.
            4. The sizeof suggests an extremely dodgy implementation. I suggest that you
            should be allocating
            these things with a custom new: as in DbAutoPtr<X> xp = new(db) X;
            5. Your DbRefCount isn't a reference count. I think I see what you are
            trying to do but you can
            do this more cleanly by storing the reference count separately - the ref
            count should not be
            in the DB unless you have multiple concurrent cliemts with locking.

            [color=blue]
            > I have another question. In my DbAutoPtr<T> class below, which
            > operator gets called for the rvalue operand, a->? Is there an implicit
            > or elegant (without creating or typecasting to a const object) way of
            > "const T& operator->( ) const" version to be called as the rvalue is
            > not being modified?[/color]

            1. What's wrong with const T*?
            2. -> operates on pointers not references.
            [color=blue]
            >
            > struct C {
            > int x;
            > };
            >
            > struct A {
            > int y;
            > };
            >
            >
            > DbStore db;
            > DbAutoPtr< C> p(db, 0, sizeof( C ) );
            > DbAutoPtr< A > a( db, 0, sizeof(A) );
            >
            > p->x = a->y;
            >
            >
            > =============== ====
            > // DbAutPtr.h - auto pinter
            >
            > #ifndef _DBAUTPTR_H_
            > #define _DBAUTPTR_H_
            >
            > #include "syTypes.h"
            > #include "DbDefs.h"
            > #include "DbBasPtr.h "
            > #include "DbRefCnt.h "
            >
            > class DbStore;
            >
            > //////////////////////////////////////////////////////////////////////////
            > // DbAutoPtr< T > class
            > //////////////////////////////////////////////////////////////////////////
            > template< class T >
            > class DbAutoPtr {
            > public:
            > DbAutoPtr( );
            > DbAutoPtr( DbStore& rStore, fsize_type nAdr );
            > DbAutoPtr( DbStore& rStore, fsize_type nAdr, size_type nSize );
            > ~DbAutoPtr( );
            >
            > // Operators
            > T* operator->( );
            > const T* operator->( ) const;
            > DbAutoPtr< T >& operator=( const DbBasePtr& rP );
            >
            > T& operator*( );
            > const T& operator*( ) const;
            >
            > bool isNull( ) const;[/color]

            unnecessary and unnatural - it should work like an ordinary ptr
            [color=blue]
            >
            > fsize_type address( );
            > fsize_type address( ) const;
            > size_type size( ) const;
            > void setAddress( fsize_type nAdr );
            > void setSize( size_type nSize );
            > bool loadData( );
            > bool writeData( );[/color]

            All this stuff above belongs somewhere else.
            [color=blue]
            >
            > const DbBasePtr * basePtr( ) const;[/color]

            ?
            [color=blue]
            >
            > // protected:
            > const T * pointer( ) const;
            > T * pointer( );
            >
            > protected:
            > DbRefCount< DbBasePtr > _oData; // Object data[/color]

            How does this work then?
            [color=blue]
            > };
            >
            > //////////////////////////////////////////////////////////////////////////
            > template<class T>
            > inline DbAutoPtr<T>::D bAutoPtr( )
            > //////////////////////////////////////////////////////////////////////////
            > {
            > }
            >
            > //////////////////////////////////////////////////////////////////////////
            > template<class T>
            > inline DbAutoPtr<T>::D bAutoPtr( DbStore& rStore, fsize_type nAdr )
            > //////////////////////////////////////////////////////////////////////////
            > {
            > _oData = DbBasePtr( rStore, nAdr, sizeof( T ) );
            > _oData->loadData( );
            > }
            >
            > //////////////////////////////////////////////////////////////////////////
            > template<class T>
            > inline DbAutoPtr<T>::D bAutoPtr( DbStore& rStore, fsize_type nAdr,
            > size_type nSize )
            > //////////////////////////////////////////////////////////////////////////
            > {
            > _oData = DbBasePtr( rStore, nAdr, nSize );
            > _oData->loadData( );
            > }
            >
            > //////////////////////////////////////////////////////////////////////////
            > template<class T>
            > inline DbAutoPtr<T>::~ DbAutoPtr( )
            > //////////////////////////////////////////////////////////////////////////
            > {
            > if( !_oData.isNull( ) )
            > _oData->writeData( T::Clsid() );
            > }
            >
            > //////////////////////////////////////////////////////////////////////////
            > template<class T>
            > inline DbAutoPtr< T >& DbAutoPtr<T>::o perator=( const DbBasePtr& rP )
            > //////////////////////////////////////////////////////////////////////////
            > {
            > if( _oData.isNull() ) {
            > _oData = rP;
            > _oData->loadData( );
            > }
            > return *this;
            > }
            >
            > //////////////////////////////////////////////////////////////////////////
            > template<class T>
            > inline const DbBasePtr * DbAutoPtr<T>::b asePtr( ) const
            > //////////////////////////////////////////////////////////////////////////
            > {
            > return _oData.pointer( );
            > }
            >
            > //////////////////////////////////////////////////////////////////////////
            > template<class T>
            > inline T* DbAutoPtr<T>::p ointer( )
            > //////////////////////////////////////////////////////////////////////////
            > {
            > return (T*) _oData.pointer( )->dataPointer( );
            > }
            >
            > //////////////////////////////////////////////////////////////////////////
            > template<class T>
            > inline const T* DbAutoPtr<T>::p ointer( ) const
            > //////////////////////////////////////////////////////////////////////////
            > {
            > return (const T*) _oData.pointer( )->dataPointer( );
            > }
            >
            > //////////////////////////////////////////////////////////////////////////
            > template<class T>
            > inline const T* DbAutoPtr<T>::o perator->() const
            > //////////////////////////////////////////////////////////////////////////
            > {
            > return (const T*) pointer( );
            > }
            >
            > //////////////////////////////////////////////////////////////////////////
            > template<class T>
            > inline T* DbAutoPtr<T>::o perator->()
            > //////////////////////////////////////////////////////////////////////////
            > {
            > return (T*) pointer();
            > }
            >
            > //////////////////////////////////////////////////////////////////////////
            > template<class T>
            > inline const T& DbAutoPtr<T>::o perator*() const
            > //////////////////////////////////////////////////////////////////////////
            > {
            > V_ASSERT( pointer( ) );
            > return *((const T*) pointer( ));
            > }
            >
            > //////////////////////////////////////////////////////////////////////////
            > template<class T>
            > inline T& DbAutoPtr<T>::o perator*()
            > //////////////////////////////////////////////////////////////////////////
            > {
            > V_ASSERT( pointer( ) );
            > return *((T*) pointer( ));
            > }
            >
            > //////////////////////////////////////////////////////////////////////////
            > template<class T>
            > inline bool DbAutoPtr<T>::i sNull( ) const
            > //////////////////////////////////////////////////////////////////////////
            > {
            > return _oData.isNull( ) || (0 == _oData->size( ) );
            > }
            >
            > //////////////////////////////////////////////////////////////////////////
            > template<class T>
            > inline fsize_type DbAutoPtr<T>::a ddress( ) const
            > //////////////////////////////////////////////////////////////////////////
            > {
            > return _oData->address( );
            > }
            >
            > //////////////////////////////////////////////////////////////////////////
            > template<class T>
            > inline size_type DbAutoPtr<T>::s ize( ) const
            > //////////////////////////////////////////////////////////////////////////
            > {
            > return _oData->size( );
            > }
            >
            > //////////////////////////////////////////////////////////////////////////
            > template<class T>
            > inline fsize_type DbAutoPtr<T>::a ddress( )
            > //////////////////////////////////////////////////////////////////////////
            > {
            > return _oData->address( T::Clsid() );
            > }
            >
            > //////////////////////////////////////////////////////////////////////////
            > template<class T>
            > inline void DbAutoPtr<T>::s etAddress( fsize_type nAdr )
            > //////////////////////////////////////////////////////////////////////////
            > {
            > _oData->setAddress( nAdr );
            > }
            >
            > //////////////////////////////////////////////////////////////////////////
            > template<class T>
            > inline void DbAutoPtr<T>::s etSize( size_type nSize )
            > //////////////////////////////////////////////////////////////////////////
            > {
            > _oData->setSize( nSize );
            > }
            >
            > //////////////////////////////////////////////////////////////////////////
            > template<class T>
            > inline bool DbAutoPtr<T>::l oadData( )
            > //////////////////////////////////////////////////////////////////////////
            > {
            > return _oData->loadData( );
            > }
            >
            > //////////////////////////////////////////////////////////////////////////
            > template<class T>
            > inline bool DbAutoPtr<T>::w riteData( )
            > //////////////////////////////////////////////////////////////////////////
            > {
            > return _oData->writeData( T::Clsid() );
            > }
            >
            > #endif
            > ==========
            > // DbRefCount.h - compact reference counted class
            >
            > #ifndef _REFCOUNT_H_
            > #define _REFCOUNT_H_
            >
            > #include <stdlib.h>
            > #include "syTypes.h"
            >
            >[/color]
            ////////////////////////////////////////////////////////////////////////////
            ///[color=blue]
            > // DbRefCount class
            >[/color]
            ////////////////////////////////////////////////////////////////////////////
            ///[color=blue]
            > template< class T >
            > class DbRefCount {
            > public:
            > DbRefCount( );
            > DbRefCount( const T& t );
            > DbRefCount( const DbRefCount& rPtr );
            > ~DbRefCount( );
            >
            > T* operator->( );
            > const T* operator->( ) const;
            > T& operator*( );
            > const T& operator*( ) const;
            > DbRefCount& operator=( const T& t );
            > DbRefCount& operator=( const DbRefCount& rPtr );
            > bool operator==( const DbRefCount& rPtr ) const;
            > bool isNull( ) const;
            >
            > // protected:
            > const T * pointer( ) const;
            > T * pointer( );
            > void copyFrom( const DbRefCount& rSrc);
            > void release( );
            > void * alloc( size_t nSize );
            >
            > protected:
            > int * _pPtr;
            > };
            >
            >[/color]
            ////////////////////////////////////////////////////////////////////////////
            ///[color=blue]
            > template< class T >
            > inline DbRefCount< T >::DbRefCount ( ) : _pPtr( NULLPTR )
            >[/color]
            ////////////////////////////////////////////////////////////////////////////
            ///[color=blue]
            > {
            > }
            >
            >[/color]
            ////////////////////////////////////////////////////////////////////////////
            ///[color=blue]
            > template< class T >
            > inline DbRefCount< T >::DbRefCount ( const T& t ) : _pPtr( NULLPTR )
            >[/color]
            ////////////////////////////////////////////////////////////////////////////
            ///[color=blue]
            > {
            > T * p = new( alloc( sizeof(T) ) ) T( t );
            > }
            >
            >[/color]
            ////////////////////////////////////////////////////////////////////////////
            ///[color=blue]
            > template< class T >
            > inline DbRefCount< T >::DbRefCount ( const DbRefCount< T >& rPtr )
            >[/color]
            ////////////////////////////////////////////////////////////////////////////
            ///[color=blue]
            > {
            > copyFrom( rPtr );
            > }
            >
            >[/color]
            ////////////////////////////////////////////////////////////////////////////
            ///[color=blue]
            > template< class T >
            > inline DbRefCount< T >::~DbRefCoun t( )
            >[/color]
            ////////////////////////////////////////////////////////////////////////////
            ///[color=blue]
            > {
            > release( );
            > }
            >
            > //////////////////////////////////////////////////////////////////////////
            > template<class T>
            > inline const T* DbRefCount<T>:: operator->() const
            > //////////////////////////////////////////////////////////////////////////
            > {
            > return pointer( );
            > }
            >
            > //////////////////////////////////////////////////////////////////////////
            > template<class T>
            > inline T* DbRefCount<T>:: operator->()
            > //////////////////////////////////////////////////////////////////////////
            > {
            > return pointer( );
            > }
            >
            > //////////////////////////////////////////////////////////////////////////
            > template<class T>
            > inline const T& DbRefCount<T>:: operator*() const
            > //////////////////////////////////////////////////////////////////////////
            > {
            > V_ASSERT( pointer( ) );
            > return *pointer( );
            > }
            >
            > //////////////////////////////////////////////////////////////////////////
            > template<class T>
            > inline T& DbRefCount<T>:: operator*()
            > //////////////////////////////////////////////////////////////////////////
            > {
            > V_ASSERT( pointer( ) );
            > return *pointer( );
            > }
            >
            >[/color]
            ////////////////////////////////////////////////////////////////////////////
            ///[color=blue]
            > template<class T>
            > inline const T * DbRefCount< T >::pointer( ) const
            >[/color]
            ////////////////////////////////////////////////////////////////////////////
            ///[color=blue]
            > {
            > V_ASSERT( _pPtr );
            > return (const T *) (_pPtr + 1);
            > }
            >
            >[/color]
            ////////////////////////////////////////////////////////////////////////////
            ///[color=blue]
            > template<class T>
            > inline T * DbRefCount< T >::pointer( )
            >[/color]
            ////////////////////////////////////////////////////////////////////////////
            ///[color=blue]
            > {
            > return _pPtr ? (T *) (_pPtr + 1) : NULLPTR;
            > }
            >
            >[/color]
            ////////////////////////////////////////////////////////////////////////////
            ///[color=blue]
            > template<class T>
            > inline DbRefCount< T >& DbRefCount< T >::operator =( const T& t )
            >[/color]
            ////////////////////////////////////////////////////////////////////////////
            ///[color=blue]
            > {
            > if( NULLPTR == _pPtr )
            > new( alloc( sizeof(T) ) ) T( t );
            > return *this;
            > }
            >
            >[/color]
            ////////////////////////////////////////////////////////////////////////////
            ///[color=blue]
            > template<class T>
            > inline DbRefCount< T >& DbRefCount< T >::operator=( const DbRefCount<
            > T >& rPtr )
            >[/color]
            ////////////////////////////////////////////////////////////////////////////
            ///[color=blue]
            > {
            > if( &rPtr != this ) {
            > release( );
            > copyFrom( rPtr );
            > }
            > return *this;
            > }
            >
            >[/color]
            ////////////////////////////////////////////////////////////////////////////
            ///[color=blue]
            > template<class T>
            > inline bool DbRefCount< T >::operator== ( const DbRefCount< T >& rRhs )
            > const
            >[/color]
            ////////////////////////////////////////////////////////////////////////////
            ///[color=blue]
            > {
            > return _pPtr == rRhs._pPtr;
            > }
            >
            >[/color]
            ////////////////////////////////////////////////////////////////////////////
            ///[color=blue]
            > template<class T>
            > inline bool DbRefCount< T >::isNull( ) const
            >[/color]
            ////////////////////////////////////////////////////////////////////////////
            ///[color=blue]
            > {
            > return _pPtr == NULLPTR;
            > }
            >
            >[/color]
            ////////////////////////////////////////////////////////////////////////////
            ///[color=blue]
            > template<class T>
            > inline void DbRefCount< T >::release( )
            >[/color]
            ////////////////////////////////////////////////////////////////////////////
            ///[color=blue]
            > {
            > if( _pPtr ) {
            > (*_pPtr)--;
            > if( *_pPtr == 0 ) {
            > T * tp = pointer( );
            > tp->~T( );
            > ::free( _pPtr );
            > _pPtr = NULLPTR;
            > }
            > }
            > }
            >
            >[/color]
            ////////////////////////////////////////////////////////////////////////////
            ///[color=blue]
            > template<class T>
            > inline void DbRefCount< T >::copyFrom( const DbRefCount< T >& rSrc )
            >[/color]
            ////////////////////////////////////////////////////////////////////////////
            ///[color=blue]
            > {
            > _pPtr = rSrc._pPtr;
            > if( _pPtr ) (*_pPtr)++;
            > }
            >
            >[/color]
            ////////////////////////////////////////////////////////////////////////////
            ///[color=blue]
            > template<class T>
            > inline void * DbRefCount< T >::alloc( size_t nSize )
            >[/color]
            ////////////////////////////////////////////////////////////////////////////
            ///[color=blue]
            > {
            > int * pPtr = (int *) ::realloc( _pPtr, nSize + sizeof(int) );
            > if( NULLPTR == _pPtr ) {
            > *pPtr = 1;
            > }
            > _pPtr = pPtr;
            > return _pPtr + 1;
            > }
            >
            > #endif
            > =======
            > // DbBasPtr.h
            >
            > #ifndef _DBBASPTR_H_
            > #define _DBBASPTR_H_
            >
            > #include "syTypes.h"
            > #include "DbDefs.h"
            > #include "DbObjRef.h "
            >
            > class DbStore;
            >
            >[/color]
            ////////////////////////////////////////////////////////////////////////////
            ///[color=blue]
            > // DbBasePtr
            >[/color]
            ////////////////////////////////////////////////////////////////////////////
            ///[color=blue]
            > class DbBasePtr : public DbObjRef {
            > public:
            > DbBasePtr( DbStore& rStore, fsize_type nAdr, size_type nSize );
            > ~DbBasePtr( );
            >
            > const void * dataPointer( ) const;
            > void * dataPointer( );
            > fsize_type address( clsid_type nClsid );
            > fsize_type address( ) const;
            >
            > // protected:
            > bool loadData( );
            > bool writeData( clsid_type nClsid );
            > void * alloc( size_type nSize );
            >
            > protected:
            > DbStore * _pStore;
            > void * _pData; // Object data
            > };
            >
            >[/color]
            ////////////////////////////////////////////////////////////////////////////
            ///[color=blue]
            > inline DbBasePtr::DbBa sePtr( DbStore& rStore, fsize_type nAdr,
            > size_type nSize )
            > : DbObjRef( nAdr, nSize ), _pStore( &rStore ),
            > _pData( NULLPTR )
            >[/color]
            ////////////////////////////////////////////////////////////////////////////
            ///[color=blue]
            > {
            > }
            >
            >[/color]
            ////////////////////////////////////////////////////////////////////////////
            ///[color=blue]
            > inline DbBasePtr::~DbB asePtr( )
            >[/color]
            ////////////////////////////////////////////////////////////////////////////
            ///[color=blue]
            > {
            > }
            >
            >[/color]
            ////////////////////////////////////////////////////////////////////////////
            ///[color=blue]
            > inline const void * DbBasePtr::data Pointer( ) const
            >[/color]
            ////////////////////////////////////////////////////////////////////////////
            ///[color=blue]
            > {
            > return _pData;
            > }
            >
            >[/color]
            ////////////////////////////////////////////////////////////////////////////
            ///[color=blue]
            > inline void * DbBasePtr::data Pointer( )
            >[/color]
            ////////////////////////////////////////////////////////////////////////////
            ///[color=blue]
            > {
            > setDirty( );
            > return _pData;
            > }
            >
            > //////////////////////////////////////////////////////////////////////////
            > inline fsize_type DbBasePtr::addr ess( ) const
            > //////////////////////////////////////////////////////////////////////////
            > {
            > return DbObjRef::addre ss( );
            > }
            >
            > //////////////////////////////////////////////////////////////////////////
            > inline fsize_type DbBasePtr::addr ess( clsid_type nClsid )
            > //////////////////////////////////////////////////////////////////////////
            > {
            > writeData( nClsid );
            > return DbObjRef::addre ss( );
            > }
            >
            > #endif[/color]


            Comment

            • Virendra Verma

              #7
              Re: Destructor for const object

              "Nick Hounsome" <nh002@blueyond er.co.uk> wrote in message news:<FJ5fc.121 $og6.111@news-binary.blueyond er.co.uk>...[color=blue]
              > "Virendra Verma" <virenbeena@hot mail.com> wrote in message
              > news:30ee7e04.0 404131108.1df58 d18@posting.goo gle.com...
              > I believe I have to use a reference counted class. The problem with[color=green]
              > > the above scenario was that I was using class variables for DbPtr
              > > class which get duplicated before returning and thus committing both
              > > copies. Here is the code that does the trick. DbBasePtr class is only
              > > a single copy and keeps track commit status. If the copy is already
              > > committed, second commit will simply be ignored.
              > >[/color]
              >
              > It is still not clear what you are trying to do but I believe that it is
              > still
              > totally wrong.[/color]

              Sorry, for lack of clarification.

              The DbAutoPtr<T> class is only for disk i/o of objects. The objects on
              disk is a chunk of bytes whose address, size and class ids are
              maintained elsewhere. If program knows these object references, it
              will automatically load the blob into memory and construct the object
              of right type. These disk objects do not have polymorphic behavior.
              That is another public implementation on top of this interface. May be
              I should rename this class to DskAutoPtr. At a lower level is
              implemented recovery mechanism. All this class does is load objects
              into memory, maintain status if the object has become dirty via
              pointer operators and commit them on destruction whenever object is
              deleted. BTW, this class is private to database implementor and it
              needs to be extremely efficient.
              [color=blue]
              >
              > 1. For the DbAutoPtr you cannot have the reference count in the pointer.
              > It must either be in the object pointed to or on the heap.[/color]

              Why not? The reference count is part of DbBasePtr class which
              maintains object parameters such as its disk address, size and a
              pointer to memory holding disk data. When data is read from disk, it
              is a clean copy. Once you modify/extend the data it becomes dirty and
              the object may be relocated to different disk space.
              [color=blue]
              > 2. DbAutoPtr ctor should take a T* because it is supposed to point to a T.[/color]
              Nop because these objects need to be on a special heap so that I can
              apply recovery mechanism (rollback or commit).
              [color=blue]
              > 3. The pointer should ONLY do the owning/commit. Use a separate mechanism to
              > create new instances or find them initially in the db.[/color]
              In my case, the pointer also manages the memory for reasons mentioned
              in 2) above.
              [color=blue]
              > 4. The sizeof suggests an extremely dodgy implementation. I suggest that you
              > should be allocating
              > these things with a custom new: as in DbAutoPtr<X> xp = new(db) X;[/color]
              Yes, it is. For compactness and efficiency, disk objects need to be
              contigious. For a B++ index node, for example, the size of object is
              class size plus the size of entries in the node. Once you reference
              this object via DbAutoPtr class, entire index node is loaded and
              mapped to the index node class. Naturally, node reference (such as its
              disk address etc) in the index node entry will be loaded and when I
              need to reference them I will create DbAutoPtr object and use it in
              normal way to traverse down the tree.
              [color=blue][color=green]
              > > I have another question. In my DbAutoPtr<T> class below, which
              > > operator gets called for the rvalue operand, a->? Is there an implicit
              > > or elegant (without creating or typecasting to a const object) way of
              > > "const T& operator->( ) const" version to be called as the rvalue is
              > > not being modified?[/color]
              >
              > 1. What's wrong with const T*?[/color]

              There is nothing wrong except that I have to create one by copying it
              which means that data base implementor has to be aware of the cost of
              dirtying an object as he is doing it unintentionally in my example. I
              was trying to avoid using fault on reference mechanism supported by
              the operating system. It seems that I can't avoid it.
              [color=blue][color=green]
              > > bool isNull( ) const;[/color]
              >
              > unnecessary and unnatural - it should work like an ordinary ptr[/color]

              True, but I needed to know if it is NULL pointer. I could have created
              a NULL pointer class and provided a comparison operator.
              Unfortunately, this class is not intended to be a public/library class
              - just create a nicer database code so that it is easier to maintain.
              Sophistication is not the goal.[color=blue]
              >[color=green]
              > >
              > > fsize_type address( );
              > > fsize_type address( ) const;
              > > size_type size( ) const;
              > > void setAddress( fsize_type nAdr );
              > > void setSize( size_type nSize );
              > > bool loadData( );
              > > bool writeData( );[/color]
              >
              > All this stuff above belongs somewhere else.[/color]

              That's why it called DbAutoPtr not a general AutoPtr class. It simply
              manages references to objects on disk.[color=blue]
              >[color=green]
              > >
              > > const DbBasePtr * basePtr( ) const;[/color]
              >
              > ?[/color]
              I needed this for debugging purposes.
              [color=blue]
              >[color=green]
              > >
              > > // protected:
              > > const T * pointer( ) const;
              > > T * pointer( );
              > >
              > > protected:
              > > DbRefCount< DbBasePtr > _oData; // Object data[/color]
              >
              > How does this work then?
              >[/color]

              DbRefCount manages DbBasePtr class which in turn manages object
              reference, load, write operations and ofcourse memory copy of the
              database object such as B++ Index node. The layout of pointer in
              DbRefCount<> class is as follows:

              reference_count + DbBasePtr class

              So when you copy DbAutoPtr<T>, reference_count gets incremented,
              therefore there is only one copy of DbBasePtr class.

              Thanks again.

              Comment

              Working...