Singleton Polymorph

Most exported objects – objects created by the plug-in and passed into modo – are created using servers or Using a Spawner . The technique involves creating a polymorph for the C++ class and adding the desired COM interfaces. This polymorph is then cached in a table and used either as an exported server or as a spawner for allocating instances.

1
        CLxGenericPolymorph     *srv;
1
2
3
        srv = new CLxPolymorph<COperator>;
        srv->AddInterface (new CLxIfc_ParticleCoOperator<COperator>);
        lx::AddSpawner (SPNNAME_OPERATOR, srv);

A third approach is to create a singleton. By deriving from the CLxSingletonPolymorph the C++ object itself serves as its own polymorph, and is capable of casting itself to ILxUnknown. This is most useful for objects that need only one instance, like listeners or visitors.

The sublcass needs to do two things.

  • include a method given by the LXxSINGLETON_METHOD define

  • add interfaces to itself in its constructor

1
2
3
4
5
                public CLxImpl_SceneItemListener,
                public CLxSingletonPolymorph
 {
     public:
        LXxSINGLETON_METHOD
1
2
3
4
        CListener ()
        {
                AddInterface (new CLxIfc_SceneItemListener<CListener>);
        }
1
2
3
4
5
6
                void
        sil_ItemPackage (
                ILxUnknownID             itemObj)
        {
                CLxUser_Item             item (itemObj);
                CLxUser_Scene            scene;
1
2
3
4
5
6
7
                if (item.IsA (cit_collector))
                {
                        item.GetContext (scene);
                        scene.EvalModInvalidate (SRVNAME_MODIFIER);
                }
        }
 };

Casting an object of this type to ILxUnknownID will allocate the COM object.