NeL provides a robust class factory system that it uses in a various places. The most notable place that it uses it is in the stream system (described in another section.) If you want classes that can be created by the stream system via poly pointers or if you want to add new shape/model types to the 3D system you will want to know how to use the class management facilities of NeL. This class factory system is actually very simple and involves two macros, an interface and a simple class factory: NLMISC_REGISTER_CLASS, NLMISC_DECLARE_CLASS and IClassable. Here's a simple sample to demonstrate:
// myclass.h
class CMyClass : public IClassable
{
public:
CMyClass();
std::string getFunnyQuote();
NLMISC_DECLARE_CLASS(CMyClass);
};
// main.cpp
int main()
{
try
{
NLMISC_REGISTER_CLASS(CMyClass);
}
catch(ERegisteredClass &e)
{
nlinfo("CMyClass is already registered.");
}
try
{
CMyClass *foo = dynamic_cast<CMyClass *>(CClassRegistry::createClass("CMyClass"));
}
catch(EUnregisteredClass &e)
{
nlinfo("CMyClass wasn't registered.");
}
}
In the above example we inherited IClassable on the class that we want the class registry to create and used the NLMISC_DECLARE_CLASS macro to automatically create required methods for the class registry system. Then in main we used the NLMISC_REGISTER_CLASS macro which actually registers this class with the registry for future creation requests. If you fail to use this macro and attempt to call createClass method the class registry will throw the EUnregisteredClass exception. If you accidentally call this macro twice in a runtime the class registry will throw the ERegisteredClass exception - which simply means this class is already registered. It is important to catch these exceptions so that you can be proactive in finding errors in your code. This is the essence of the class registry system. Do not be confused by the CClassId class - this class will be explained in later sections.
Many plugin developers will put the NLMISC_REGISTER_CLASS macro calls in their implementation of INelLibrary's onLibraryLoad where firstTime is true - this means that as soon as the plugin is loaded by the client developer all of the classes within that plugin are registered with the class registry.