473,748 Members | 2,361 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Jul 22 '05 #1
6 2421
Virendra Verma wrote:
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.
I don't see what 'const' has to do with this.
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.
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.
void commit( );
"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.
void operator=( const DbPtr<T>& rP ) { *this = rP; }
void operator=( DbPtr<T>& rP ) { rP.commit(); *this = rP; }
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.
}


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.
Jul 22 '05 #2
On 12 Apr 2004 08:35:33 -0700, vi********@hotm ail.com (Virendra Verma)
wrote:
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
}
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?

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.
Do you mean whether you are destroying the last reference to an
object? Just use reference counting...
void commit( );
void operator=( const DbPtr<T>& rP ) { *this = rP; }
void operator=( DbPtr<T>& rP ) { rP.commit(); *this = rP; }
}

Thanks in advance.


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
Jul 22 '05 #3

"Virendra Verma" <vi********@hot mail.com> wrote in message
news:30******** *************** ***@posting.goo gle.com...
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


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 .
Jul 22 '05 #4
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
Jul 22 '05 #5

"Virendra Verma" <vi********@hot mail.com> wrote in message
news:30******** *************** ***@posting.goo gle.com...
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.

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.

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?
1. What's wrong with const T*?
2. -> operates on pointers not references.

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;
unnecessary and unnatural - it should work like an ordinary ptr

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( );
All this stuff above belongs somewhere else.

const DbBasePtr * basePtr( ) const;
?

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

protected:
DbRefCount< DbBasePtr > _oData; // Object data
How does this work then?
};

//////////////////////////////////////////////////////////////////////////
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

Jul 22 '05 #6
"Nick Hounsome" <nh***@blueyond er.co.uk> wrote in message news:<FJ******* ********@news-binary.blueyond er.co.uk>...
"Virendra Verma" <vi********@hot mail.com> wrote in message
news:30******** *************** ***@posting.goo gle.com...
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.

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


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.

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.
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.
2. DbAutoPtr ctor should take a T* because it is supposed to point to a T. Nop because these objects need to be on a special heap so that I can
apply recovery mechanism (rollback or commit).
3. The pointer should ONLY do the owning/commit. Use a separate mechanism to
create new instances or find them initially in the db. In my case, the pointer also manages the memory for reasons mentioned
in 2) above.
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; 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.
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?


1. What's wrong with const T*?


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.
bool isNull( ) const;


unnecessary and unnatural - it should work like an ordinary ptr


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.

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( );
All this stuff above belongs somewhere else.


That's why it called DbAutoPtr not a general AutoPtr class. It simply
manages references to objects on disk.

const DbBasePtr * basePtr( ) const;
?

I needed this for debugging purposes.

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

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


How does this work then?


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.
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2842
by: Virendra Verma | last post by:
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....
39
736
by: Ele | last post by:
Is it correct to say that Whenever a class has a virtual member function, define its destructor as "virtual"? Can a destructor as "pure virtual"? When is it needed to do so? For an interface, Interf: class Interf { public:
11
4367
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining it inline. Like this.
11
1991
by: AB | last post by:
Hi All, I've got an array of objects, during the execution of the program I'd like to assign a particular object to a certain element in the object array. The sample code's like this... class ClassA { public: ClassA()
5
3342
by: Frederick Gotham | last post by:
If we have a simple class such as follows: #include <string> struct MyStruct { std::string member; MyStruct(unsigned const i) {
5
11360
by: druberego | last post by:
I read google and tried to find the solution myself. YES I do know that you can get undefined references if you: a) forget to implement the code for a prototype/header file item, or b) you forget to pass all the necessary object files to the linker. Neither of those are my problem. Please bear with me as the question I ask is rather long and I think it's beyond a CS101 level of linker stupidity. If it is a stupid CS101 mistake I'm making...
7
2133
by: michael | last post by:
Hi All, I have written the following to illustrate a problem. I know I have some magic numbers etc please ignore them. What I do not follow is why the line marked results in a call to the destructor for the object. Can someone please explain it for me? #include <iostream>
1
1660
by: tom | last post by:
I have the section of code listed below, but when I return from main an exception is thrown from the library, because while returning, destructor is called on each of the object in the sequence below: step1: destroy m2 step2: destroy f step3: destroy m1 notice, "m1" and "m2" have the class type of Message, and will both use object "f" in the destructor. The problem happens when run to step3, it uses an already destroyed object f....
9
2056
by: itdevries | last post by:
Hi, I've ran into some trouble with an overloaded + operator, maybe someone can give me some hints what to look out for. I've got my own custom vector class, as a part of that I've overloaded the + operator by means of a friend function. Everything worked fine until I decided to use a variable array size (by using new/delete), now I get an error when a temporary object is deleted (e.g. after addition), the error occurs at the delete...
12
2355
by: mc | last post by:
Hi Experts, This may be obvious but I can't find anything one way or another. We have a class which is always allocated using new, e.g. Foo* foo = new Foo() so we came up with the idea of releasing the memory from the destructor as follows: Foo::Foo() { // Initialize stuff
0
8984
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8823
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9530
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9363
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9238
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.