Singleton Listener

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

Global listeners, that is objects that watch for global events rather than events on a specific object, are generally created once and exist as long as they are needed. The CLxSingletonListener class makes it easy to manage a global listener from C++.

You start by declaring a listener class as a singleton. Once declared, the CLxSingletonListener template wraps your listener class as a static object.

class CListener :
		public CLxImpl_SceneItemListener,
		public CLxSingletonPolymorph
{
    public:
	... (see Singleton Polymorph for complete description)
};

static CLxSingletonListener<CListener>	 listener;

This static object is only a container that is empty to start with. The listener will be created as soon as it's needed, for example during the constructor for a package instance. This is called when a plug-in item is created, which can be when the listener is first required, and the acquire() method will allocate and register the listener the first time it's called. Likewise calling release() in the destructor will cause the listener to be removed when the last instance is destroyed.

class CInstance :
		public CLxImpl_PackageInstance
{
    public:
	CInstance ()
	{
		listener.acquire ();
	}

	~CInstance ()
	{
		listener.release ();
	}
};