PLearn 0.1
|
Extension registrar for new file types. More...
#include <getDataSet.h>
Public Types | |
typedef VMat(* | VMatrixInstantiator )(const PPath &filename) |
typedef map< string, VMatrixExtensionRegistrar > | ExtensionMap |
Public Member Functions | |
VMatrixExtensionRegistrar (const string &file_extension, VMatrixInstantiator instantiation_function, const string &documentation) | |
const string & | documentation () const |
Return the documentation associated with a registrar. | |
Static Public Member Functions | |
static const ExtensionMap & | registeredExtensions () |
Return the list of all registered extensions. | |
static void | registerExtension (const VMatrixExtensionRegistrar &new_extension) |
Register a new extension. | |
static VMatrixInstantiator | getInstantiator (const string &file_extension) |
Return the instantiator given an extension, or NULL if not found. | |
Static Private Member Functions | |
static ExtensionMap & | registeredExtensionsAux () |
Set of registered extensions. | |
Private Attributes | |
string | m_file_extension |
VMatrixInstantiator | m_instantiator |
string | m_documentation |
Extension registrar for new file types.
If you want to extend getDataSet so that it recognizes new extensions (and constructs the correct type of VMatrix automatically), you should instantiate a static VMatrixExtensionRegistrar object within the .cc of your new VMatrix type. This object will automatically register the new extension with getDataSet, so that when you try to open a file of that extension, the instantiation function is called. For example:
VMatrixExtensionRegistrar( "fancyext", // Note: no leading period &FancyVMatrix::instantiateFromPPath, "Fancy new file format, giving a FancyVMatrix")
This associates files with the ".fancyext" extension with a construction function (that must take a single PPath argument and returns a VMat). The last argument is some documentation.
Definition at line 85 of file getDataSet.h.
typedef map<string,VMatrixExtensionRegistrar> PLearn::VMatrixExtensionRegistrar::ExtensionMap |
Definition at line 89 of file getDataSet.h.
typedef VMat(* PLearn::VMatrixExtensionRegistrar::VMatrixInstantiator)(const PPath &filename) |
Definition at line 88 of file getDataSet.h.
PLearn::VMatrixExtensionRegistrar::VMatrixExtensionRegistrar | ( | const string & | file_extension, |
VMatrixInstantiator | instantiation_function, | ||
const string & | documentation | ||
) |
Definition at line 255 of file getDataSet.cc.
References registerExtension().
: m_file_extension(extension), m_instantiator(instantiator), m_documentation(doc) { registerExtension(*this); }
const string& PLearn::VMatrixExtensionRegistrar::documentation | ( | ) | const [inline] |
Return the documentation associated with a registrar.
Definition at line 97 of file getDataSet.h.
{ return m_documentation; }
VMatrixExtensionRegistrar::VMatrixInstantiator PLearn::VMatrixExtensionRegistrar::getInstantiator | ( | const string & | file_extension | ) | [static] |
Return the instantiator given an extension, or NULL if not found.
Definition at line 280 of file getDataSet.cc.
References registeredExtensions().
Referenced by PLearn::getDataSet().
{ map<string,VMatrixExtensionRegistrar>::const_iterator found = registeredExtensions().find(ext); if (found == registeredExtensions().end()) return 0; else return found->second.m_instantiator; }
static const ExtensionMap& PLearn::VMatrixExtensionRegistrar::registeredExtensions | ( | ) | [inline, static] |
Return the list of all registered extensions.
Definition at line 106 of file getDataSet.h.
Referenced by PLearn::getDataSetHelp(), and getInstantiator().
{ return registeredExtensionsAux(); }
VMatrixExtensionRegistrar::ExtensionMap & PLearn::VMatrixExtensionRegistrar::registeredExtensionsAux | ( | ) | [static, private] |
Set of registered extensions.
Definition at line 292 of file getDataSet.cc.
Referenced by registerExtension().
{ // Sidestep order-or-static-initialization issue across translation units static ExtensionMap extensions; return extensions; }
void PLearn::VMatrixExtensionRegistrar::registerExtension | ( | const VMatrixExtensionRegistrar & | new_extension | ) | [static] |
Register a new extension.
Definition at line 265 of file getDataSet.cc.
References PLearn::endl(), m_documentation, m_file_extension, NAMED_LOG, and registeredExtensionsAux().
Referenced by VMatrixExtensionRegistrar().
{ // If inserting same extension a second time, does NOT override first call if (registeredExtensionsAux().insert( make_pair(ext.m_file_extension,ext)).second) { NAMED_LOG("VMatrixExtensionRegistrar") << "Registered new VMatrix extension: " << ext.m_file_extension << " (" << ext.m_documentation << ")" << endl; } }
string PLearn::VMatrixExtensionRegistrar::m_documentation [private] |
Definition at line 121 of file getDataSet.h.
Referenced by registerExtension().
string PLearn::VMatrixExtensionRegistrar::m_file_extension [private] |
Definition at line 119 of file getDataSet.h.
Referenced by registerExtension().
Definition at line 120 of file getDataSet.h.