Singleton Listener

From The Foundry MODO SDK wiki
Revision as of 02:02, 28 February 2014 by Shf (Talk | contribs) (Created page with "Global [listener]s, 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 ...")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Global [listener]s, 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 | Listener_%28lx-listener.hpp%29] class makes it easy to manage a global listener from C++.

You start by creating a singleton listener class. The full text of the class can be found with the example of how to uses. Once declared, the CLxSingletonListener template can create your listener as a static object.

class CListener :
		public CLxImpl_SceneItemListener,
		public CLxSingletonPolymorph
{
    public:
	... (see [Singleton Polymorph] for the full text)
};

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 called 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 ();
	}
};