S
Scott Gifford
Hello,
I'm working on an providing an iterator interface to a database. The
basic thing I'm trying to accomplish is to have my iterator read rows
from the database and return constructed objects. My goal here is to
abstract away the database part, so that in the future these objects
could be constructed from a data source on disk, across the network,
etc. The interface I'm after is similar to the standard iterator
interface for an "input iterator":
DataSource source;
for(DataSource::iterator iter = source.findAll(); iter++; iter != DataSource::end()) {
/* ... */
}
The problem I'm running into is that this idiom requires that the
iterator be constructed in findAll(), then copied into a local
variable with the copy constructor. My constructor creates a database
cursor object to read rows, so my copy constructor needs to make a
copy of that cursor. However my database driver won't allow me to
copy the cursor object, or even to create a new cursor object before
the first cursor is destroyed.
The solution I'm currently using is to store the cursor object in a
pointer, then have a copy constructor which takes over ownership of
the cursor, setting it to NULL in the copied object so it won't be
destoyed in the destructor. This basically works, but seems very
kludgey, and has some issues I'll address in another post,
I'm having a similar problem implementing postfix increment
(operator++(int)). I basically need to return a copy of the iterator
at the previous position in the database, but again I can't copy the
database cursor or construct a new cursor because of driver
limitations.
These seem like they would be common problems. Are there common
solutions to them? Can anybody point me to a good reference for
creating iterator interfaces?
Thanks!
----ScottG.
I'm working on an providing an iterator interface to a database. The
basic thing I'm trying to accomplish is to have my iterator read rows
from the database and return constructed objects. My goal here is to
abstract away the database part, so that in the future these objects
could be constructed from a data source on disk, across the network,
etc. The interface I'm after is similar to the standard iterator
interface for an "input iterator":
DataSource source;
for(DataSource::iterator iter = source.findAll(); iter++; iter != DataSource::end()) {
/* ... */
}
The problem I'm running into is that this idiom requires that the
iterator be constructed in findAll(), then copied into a local
variable with the copy constructor. My constructor creates a database
cursor object to read rows, so my copy constructor needs to make a
copy of that cursor. However my database driver won't allow me to
copy the cursor object, or even to create a new cursor object before
the first cursor is destroyed.
The solution I'm currently using is to store the cursor object in a
pointer, then have a copy constructor which takes over ownership of
the cursor, setting it to NULL in the copied object so it won't be
destoyed in the destructor. This basically works, but seems very
kludgey, and has some issues I'll address in another post,
I'm having a similar problem implementing postfix increment
(operator++(int)). I basically need to return a copy of the iterator
at the previous position in the database, but again I can't copy the
database cursor or construct a new cursor because of driver
limitations.
These seem like they would be common problems. Are there common
solutions to them? Can anybody point me to a good reference for
creating iterator interfaces?
Thanks!
----ScottG.