bkloppenborg / simtoi

The SImulation and Modeling Tool for Optical Interferometry
GNU General Public License v3.0
7 stars 6 forks source link

Move models and shaders into a factory class #59

Closed bkloppenborg closed 11 years ago

bkloppenborg commented 11 years ago

In issue #18, we want to convert the model/shader names back to strings. This is, perhaps, not the best idea. Instead, we could start implementing the basic requirements for a plugin architecture for SIMTOI's models/shaders by moving them into factory classes. The idea is described here. Basically, we create an abstract base class for models which requires the following functions be defined:

shared_ptr<Base> Create()
string GetName()

The Create() function then does something like this:

shared_ptr<Base> Create
    return shared_ptr<Base> tmp(new Child())

Then to make everything work we define a singleton Factory class which has a a map and register function:

class Factory
    std::map FactoryMap

    ...
    /// registering is easy:
    Register(string Name, shared_ptr<Base> CreateFunction)
        FactoryMap[Name] = CreateFunction

    /// Creation isn't too hard
    shared_ptr<Base> CreateModel(const string &modelName)
        map::iterator it = FactoryMap.find(modelName)
        if(it != FactoryMap.end())
            return it->second();

        // possibly throw an exception / return null model?

Then, later on, the registering of models could be done via. a plugin architecture. For now we can just hard-code functions like:

Factory.Register("Sphere", CSphere::Create())
Factory.Register("Cylinder", CCylinder::Create())
...