Next: Alternative creation mechanisms, Previous: Implementing new library classes, Up: Implementing new library classes
The implementation of a new type of library that can be auto-detected needs to
register a function with the signature library::init_function
with
library::addtype
. This gets used by the library class when it attempts
to open a file. It is often convenient to provide
the library user with a registerlib
function that handles this
registration. For example, for a type of library file that always starts
with the line “My Library File”, the following might be used.
class mylibrary : public library_base { public: // Allow the user to initialise the library static void registerlib(void) { library::addtype(&canread); } private: static library_base *canread(const string& name) { ifstream ifs(name); string first_line; getline(ifs, first_line); if (first_line == "My Library File") return new mylibrary(name); return NULL; } mylibrary(const string &name); };
In addition, the implementation must override the library_base::good
and library_base::begin
virtual functions. This is discussed elsewhere.