Singleton Polymorph

From The Foundry MODO SDK wiki
Jump to: navigation, search

Most exported objects -- objects created by the plug-in and passed into modo -- are created using servers or spawners. 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.

	CLxGenericPolymorph	*srv;

	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
class CListener :
		public CLxImpl_SceneItemListener,
		public CLxSingletonPolymorph
{
    public:
	LXxSINGLETON_METHOD

	CListener ()
	{
		AddInterface (new CLxIfc_SceneItemListener<CListener>);
	}

		void
	sil_ItemPackage (
		ILxUnknownID		 itemObj)
	{
		CLxUser_Item		 item (itemObj);
		CLxUser_Scene		 scene;

		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.