Next: Subclassing library_entry impl, Previous: Alternative creation mechanisms, Up: Implementing new library classes
In addition to providing mechanism for opening libraries, implementations
of new library types must also override at least the library_base::good
and library_base::begin
virtual functions. The implementation of the
good
function is usually trivial, and begin
only slightly less
so. Continuing with the example used in Supporting auto-detection, they
might be as follows:
class mylibrary : public library_base { mylibrary(const string &filename) : ifs(filename) {} class entry_type : public library_entry { // ... }; const_iterator make_begin() { ifs.clear(); ifs.seekg(0, ios::beg); // Rewind the stream return const_iterator( this, new entry_type ); } virtual bool good() const { return ifs; } virtual const_iterator begin() const { return const_cast< mylibrary* >( this )->make_begin(); } ifstream ifs; }
Notice the slight awkwardness of the const_cast
in begin
. This
is necessary because the begin
function is sematically constant—it
does not change the visible state of the library—but it needs to rewind
ifs. The stream's position is not part of the visible state
of the library because the library::const_iterator
is only an
Input Iterator and so only one of them can ever be present at any time.