JX Application Framework
Loading...
Searching...
No Matches
Classes | Public Member Functions | Protected Member Functions | List of all members
JBroadcaster Class Reference

#include <JBroadcaster.h>

Inheritance diagram for JBroadcaster:
[legend]

Classes

struct  ClearPointer
 
class  Message
 

Public Member Functions

 JBroadcaster ()
 
virtual ~JBroadcaster ()
 
JBroadcasteroperator= (const JBroadcaster &source)
 
bool HasSenders () const
 
JSize GetSenderCount () const
 
bool HasRecipients () const
 
JSize GetRecipientCount () const
 
virtual JString ToString () const
 
template<class T >
void ListenTo (const JBroadcaster *sender, const std::function< void(const T &)> &f)
 

Protected Member Functions

 JBroadcaster (const JBroadcaster &source)
 
void ListenTo (const JBroadcaster *sender)
 
void StopListening (const JBroadcaster *sender)
 
void ClearWhenGoingAway (const JBroadcaster *sender, void *pointerToMember)
 
void StopListening (const JBroadcaster *sender, const std::type_info &messageType)
 
template<class T >
void Send (JBroadcaster *recipient, const T &message)
 
template<class T >
void Broadcast (const T &message)
 
virtual void Receive (JBroadcaster *sender, const Message &message)
 
void SendWithFeedback (JBroadcaster *recipient, Message *message)
 
void BroadcastWithFeedback (Message *message)
 
virtual void ReceiveWithFeedback (JBroadcaster *sender, Message *message)
 
virtual void ReceiveGoingAway (JBroadcaster *sender)
 

Detailed Description

Mixin class to allow objects to send messages to each other.  A JBroadcaster
maintains a list of objects that send it messages (senders) and a list
of objects to which it sends messages (recipients).

To send a message, call Broadcast().  The Receive() method is then called
for each recipient that called ListenTo(obj).  For every recipient that
called ListenTo(obj,fn), the given function will be invoked.

    ListenTo(obj,fn) should only be used in leaf classes.  Base classes
    should use ListenTo(obj) in case derived classes need to intercept
    messages in Receive().

To send a message that requires feedback, call BroadcastWithFeedback().
The ReceiveWithFeedback() method is then called for each recipient.

To listen for the death of a sender, override ReceiveGoingAway().
    Be sure to read the warnings associated with using this function!

Because the overhead is so low if there is nobody to broadcast to or
nobody to listen to, there is no point in making a separate JListener class.

    Since this is a mixin class, all classes that wish to inherit from it
    must use "virtual public" inheritance.

Constructor & Destructor Documentation

◆ JBroadcaster() [1/2]

JBroadcaster::JBroadcaster ( )

◆ ~JBroadcaster()

JBroadcaster::~JBroadcaster ( )
virtual

Close all connections.

◆ JBroadcaster() [2/2]

JBroadcaster::JBroadcaster ( const JBroadcaster source)
protected

Don't copy the connections. We want this to be identical to the default constructor so nobody has to worry about calling us. (Otherwise, they would because we are linked in via virtual inheritance.)

Member Function Documentation

◆ Broadcast()

template<class T >
void JBroadcaster::Broadcast ( const T &  message)
inlineprotected

Send the given message to all recipients. It is the responsibility of derived classes to implement a set of useful messages. Every message must be derived from JBroadcaster::Message and should contain all the information necessary to process the message.

By inlining this part of the function, we avoid the overhead of a function call unless somebody is actually listening.

◆ BroadcastWithFeedback()

void JBroadcaster::BroadcastWithFeedback ( Message message)
inlineprotected

Send the given message to all recipients and expect a reply. The reply is message dependent and therefore stored in the message, so we simply send the message as a non-const object and let the receiver who understands the message deal with it.

By inlining this part of the function, we avoid the overhead of a function call unless somebody is actually listening.

◆ ClearWhenGoingAway()

void JBroadcaster::ClearWhenGoingAway ( const JBroadcaster csender,
void *  pointerToMember 
)
protected

Register an object that needs to be forgotten when it is deleted.

◆ GetRecipientCount()

JSize JBroadcaster::GetRecipientCount ( ) const

◆ GetSenderCount()

JSize JBroadcaster::GetSenderCount ( ) const

◆ HasRecipients()

bool JBroadcaster::HasRecipients ( ) const

◆ HasSenders()

bool JBroadcaster::HasSenders ( ) const

◆ ListenTo() [1/2]

void JBroadcaster::ListenTo ( const JBroadcaster csender)
protected

Open a connection by adding the given object to this object's sender list, and adding this object to the given object's recipient list.

If the connection already exists, then we do nothing.

◆ ListenTo() [2/2]

template<class T >
void JBroadcaster::ListenTo ( const JBroadcaster csender,
const std::function< void(const T &)> &  f 
)

Open a connection by adding the given object to this object's sender list, and adding this object & function to the given object's recipient list.

If the connection already exists, then we replace the previous function.

NOTE: This cannot be used if you intend to use ReceiveWithFeedback().

◆ operator=()

JBroadcaster & JBroadcaster::operator= ( const JBroadcaster source)

Don't copy the connections.

◆ Receive()

void JBroadcaster::Receive ( JBroadcaster sender,
const Message message 
)
protectedvirtual

◆ ReceiveGoingAway()

void JBroadcaster::ReceiveGoingAway ( JBroadcaster sender)
protectedvirtual

The given sender has been deleted.

Warning: Since this function may be called from within a -chain- of destructors, it is not usually safe to do anything inside this function other than directly changing instance variables (e.g. setting pointers to nullptr).

This function is not pure virtual because not all classes will want to implement it.

Reimplemented in JAliasArray< T >, JAliasArray< JString * >, JBroadcastSnooper, JBroadcastTester, JTableSelectionIterator, JTextEditor, JXCheckboxGroup, JXDNDManager, JXDockWidget, JXSelectionData, and JXStringList.

◆ ReceiveWithFeedback()

void JBroadcaster::ReceiveWithFeedback ( JBroadcaster sender,
Message message 
)
protectedvirtual

Process the given message from the given sender and provide feedback via the message as appropriate. This function is not pure virtual because not all classes will want to implement it.

Reimplemented in JXApplication, JXDNDManager, JXSearchTextDialog, and JXSelectionManager.

◆ Send()

template<class T >
void JBroadcaster::Send ( JBroadcaster recipient,
const T &  message 
)
protected

Send the given message to the specified recipient. It is the responsibility of derived classes to implement a set of useful messages. Every message must be derived from JBroadcaster::Message and should contain all the information necessary to process the message.

This function allows messages to be sent to an object in a way that is safe but that does not require the C++ type safety mechanism of dynamic_cast. Essentially, this feature is equivalent to the object methods provided by Objective C.

◆ SendWithFeedback()

void JBroadcaster::SendWithFeedback ( JBroadcaster recipient,
Message message 
)
protected

Send the given message to the specified recipients. It is the responsibility of derived classes to implement a set of useful messages. Every message must be derived from JBroadcaster::Message and should contain all the information necessary to process the message.

This function allows messages to be sent to an object in a way that is safe but that does not require the C++ type safety mechanism of dynamic_cast. Essentially, this feature is equivalent to the object methods provided by Objective C.

◆ StopListening() [1/2]

void JBroadcaster::StopListening ( const JBroadcaster csender)
protected

Close a connection by removing the given object from this object's sender list, and removing this object from the given objects's recipient list.

◆ StopListening() [2/2]

void JBroadcaster::StopListening ( const JBroadcaster csender,
const std::type_info &  messageType 
)
protected

Close a connection by removing the given object from this object's sender list, and removing this object from the given objects's recipient list.

◆ ToString()

JString JBroadcaster::ToString ( ) const
virtual

The documentation for this class was generated from the following files: