Destructor for const object

V

Virendra Verma

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
 
K

Kevin Goodsell

Virendra said:
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_destroy' 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
 
T

tom_usenet

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

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
 
N

Nick Hounsome

Virendra Verma said:
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 .
 
V

Virendra Verma

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>::DbAutoPtr( )
//////////////////////////////////////////////////////////////////////////
{
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

//////////////////////////////////////////////////////////////////////////
template<class T>
inline bool DbAutoPtr<T>::writeData( )
//////////////////////////////////////////////////////////////////////////
{
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 >::~DbRefCount( )
///////////////////////////////////////////////////////////////////////////////
{
release( );
}

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

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

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

//////////////////////////////////////////////////////////////////////////
template<class T>
inline T& DbRefCount<T>::eek:perator*()
//////////////////////////////////////////////////////////////////////////
{
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 >::eek:perator =( const T& t )
///////////////////////////////////////////////////////////////////////////////
{
if( NULLPTR == _pPtr )
new( alloc( sizeof(T) ) ) T( t );
return *this;
}

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

///////////////////////////////////////////////////////////////////////////////
template<class T>
inline bool DbRefCount< T >::eek:perator==( 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::DbBasePtr( DbStore& rStore, fsize_type nAdr,
size_type nSize )
: DbObjRef( nAdr, nSize ), _pStore( &rStore ),
_pData( NULLPTR )
///////////////////////////////////////////////////////////////////////////////
{
}

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

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

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

//////////////////////////////////////////////////////////////////////////
inline fsize_type DbBasePtr::address( ) const
//////////////////////////////////////////////////////////////////////////
{
return DbObjRef::address( );
}

//////////////////////////////////////////////////////////////////////////
inline fsize_type DbBasePtr::address( clsid_type nClsid )
//////////////////////////////////////////////////////////////////////////
{
writeData( nClsid );
return DbObjRef::address( );
}

#endif
 
N

Nick Hounsome

Virendra Verma said:
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?
 
V

Virendra Verma

Nick Hounsome said:
I believe I have to use a reference counted class. The problem with

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.
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.
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.
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.
I needed this for debugging purposes.
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,440
Latest member
YoungBorel

Latest Threads

Top