Resources | developer.brewmp.com Resources | developer.brewmp.com

Developer

resources

Singleton

The Singleton primitive declares a singleton object. The object to be made into a singleton may be defined in a different module.

Note: The Singleton primitive is available only in Brew MP 1.0 and newer versions.

Singletons that implement ISupportsWeakRef will be destroyed when the last instance of the singleton is no longer used. If the user wants the singleton to persist for the lifetime of the environment, the user can specify a strong reference and clean it up using IEnv_AtExit ; or the user can choose not to implement ISupportsWeakRef.

Prototype

In the example below, the class that is specified by the singled_id field is instantiated. The class must already have been declared using the Class primitive.

Singleton { 
  classid = AEECLSID_CameraSingleton, 
  singled_id = AEECLSID_Camera, 
  singleton_type = SINGLE_TYPE_STRONG, --optional
}

The following declares a class that can be instantiated only as a singleton. The singled_newfunc field specifies the constructor function for this singleton class.

Singleton {
   classid = AEECLSID_CameraSingleton,
   singled_newfunc = Camera_New,
   singleton_type = SINGLETON_TYPE_AUTO, --optional
}
  • classid: A unique 32-bit ID. All CreateInstance calls for this ClassID will return the same instance of the class specified by the singled_id.
  • singled_id: The ClassID of the object to be made into a singleton.
  • singleton_type (Optional): One of the following values:
    • SINGLETON_TYPE_AUTO: the default behavior where the singleton type is determined by testing if the object implements ISupportsWeakRef.
    • SINGLETON_TYPE_STRONG: the object is held as a strong singleton regardless of whether it supports weak references.
    • SINGLETON_TYPE_WEAK: the object is expected to support ISupportsWeakRef and fails if it does not.
  • singled_newfunc: (Optional) This is the constructor function of the object to be made into a singleton. This can be used if the user wants to hide the non-singleton instance of this object.

Example 1

To declare a pure singleton (where there will be no non-singleton version of the class to be created in the system):

Include "foosingleton.bid" -- Need to include to use AEECLSID_FooSingleton,
include "foo.bid" -- Need to include to use AEECLSID_Foo,

Singleton { 
  classid = AEECLSID_FooSingleton, 
  singled_id = AEECLSID_Foo, 
  singled_newfunc = Foo_New, 
}

Note: classid can be the same as the singled_id, and the singleton will be created using classid.

Example 2

To declare a singleton from a regular class (where both singleton and non-singleton version of the class can be created).

Include "foosingleton.bid" -- Need to include to use AEECLSID_FooSingleton,
include "foo.bid" -- Need to include to use AEECLSID_Foo,

Class{ 
  classid = AEECLSID_Foo, 
  newfunc = Foo_New,
}
Singleton{
  classid = AEECLSID_FooSingleton,
  singled_id = AEECLSID_Foo,  
}

Note: A non-singleton version of the class will be created via AEECLSID_Foo. A singleton version of the class will be created via AEECLSID_FooSingleton.