Skip to content

Class: Broadcaster

Properties | Events | Methods | Index of Classes

Provides grouping and message-relaying services for a set of objects

Hierarchy

cObject > Array > Set > Broadcaster

Show full hierarchy and direct subclasses

Library: Common Class Library

Package: brdcster.pkg

Description

The Broadcaster class is used to provide grouping and message-relaying services for a set of objects. Broadcasters are provided to allow you to create a specialized set of objects that will act in concert with any received messages. The Broadcaster-class object provides an alternative to assigning parentage to a set of objects in order to allow the broadcasting of messages to each child object.

Usage

Use BrdCster.pkg

Object ObjectName is a Broadcaster
    :
End_Object

Of Special Note

Broadcasting

Instances of this class are intended to hold a set of object identifiers for the purpose of broadcasting messages to all listed objects. Object identifiers may be contained in more than one broadcaster at a time.

Common Issues

Problem:

We want to make a broadcaster send the Destroy message to all of its elements (in this case a number of buttons). The code is:

Procedure RemoveButtons
        Set Broadcast_State to True
        Send Destroy of oButton_Broadcaster
    End_Procedure

This does not work. The Destroy message is handled by the Broadcaster object and the Broadcaster destroys itself rather than the element objects.

Explanation:

This is acting exactly as it should. The key point to understand is that the Broadcaster class is just a class, like any other class. Thus, you can create objects and add methods, and they will be executed just like any other method. The Broadcaster class doesn't modify or change the behavior of the OO subsystem in some special way. It merely broadcasts any message that isn't caught by a subclass to its objects.

Any of the "for cObject" methods we create (i.e. using Create and Destroy) cannot be used in a broadcaster. These methods are sent directly to the broadcaster. In addition, adding a method to a broadcaster object has the same effect. For example:

Object oB is a Broadcaster
     Set Broadcast_State to True
     Procedure Refresh
        :
     End_Procedure
End_Object

If we send "Refresh of oB", it will not get broadcasted to the objects: it will get sent to oB instead. However, if we remove the Refresh method and send Refresh, it will then broadcast to the element objects.

To get the same effect, you can create special methods in each button to handle the destroy like this:

Procedure DoBroadcasterDestroy
    Send Destroy
End_Procedure

Then, in the broadcaster, you can "send DoBroadcasterDestroy".

Other suggested options are: (1) to loop through the items within the broadcaster. For example, put the following procedure in the broadcaster:

Procedure RemoveButtons
        Handle ohId
        Integer iMax iPtr

        If (Item_Count(Self)) Begin
            Get Item_Count To iMax
            For iPtr From 1 To iMax
                Get Array_Value Item (iPtr - 1) To ohId
                If ohId ;
                    Send Destroy of ohId
            Loop
        End
    End_Procedure

(2) to augment the Destroy method in a subclass of the Broadcaster. If the Broadcast_State is true, then destroy the registered objects (with the loop method above), else destroy the broadcaster.

Class cMyBroadcaster is a Broadcaster
    :
    Procedure Destroy
        Handle ohId
        Integer iMax iPtr

        If (Broadcast_State(Self)) Begin
            Set Broadcast_State To False
            Get Item_Count To iMax
            If iMax Begin
                For iPtr From 1 To iMax
                    Get Array_Value Item (iPtr - 1) To ohId
                    If ohId ;
                        Send Destroy Of ohId
                Loop
                Set Broadcast_State To True
            End
        End
        Else ;
            Forward Send Destroy
    End_Procedure // Destroy
    :
End_Class // cMyBroadcaster
Col 1 Col 2
Note: Re-sending of messages only occurs if Broadcast_State is True. If Broadcast_State is False, a broadcaster object is equivalent to a set object. Also, when Broadcast_State is True, it is no longer possible to retrieve the Object_Name _Id through the Object_Id function. To reference a broadcaster object by its ID, use the Make_Broadcaster function when creating it.