Add BMessenger::SetTo() to reinitialize a BMessenger.

This allows to reuse BMessenger objects for different targets, or to
recheck validity after initial creation. With that one can use the same
BMessenger after launching an application that was previously not found
valid for example.
This commit is contained in:
Michael Lotz
2013-03-31 20:16:04 +02:00
parent 32057ce922
commit 6de478363e
2 changed files with 98 additions and 27 deletions
+5
View File
@@ -48,6 +48,9 @@ public:
// Operators and misc
status_t SetTo(const char *signature, team_id team = -1);
status_t SetTo(const BHandler *handler, const BLooper *looper = NULL);
BMessenger &operator=(const BMessenger &from);
bool operator==(const BMessenger &other) const;
@@ -63,6 +66,8 @@ private:
void _SetTo(team_id team, port_id port, int32 token);
void _InitData(const char *signature, team_id team, status_t *result);
void _InitData(const BHandler *handler, const BLooper *looper,
status_t *result);
private:
port_id fPort;
+93 -27
View File
@@ -98,33 +98,7 @@ BMessenger::BMessenger(const BHandler* handler, const BLooper* looper,
fHandlerToken(B_NULL_TOKEN),
fTeam(-1)
{
status_t error = (handler || looper ? B_OK : B_BAD_VALUE);
if (error == B_OK) {
if (handler) {
// BHandler is given, check/retrieve the looper.
if (looper) {
if (handler->Looper() != looper)
error = B_MISMATCHED_VALUES;
} else {
looper = handler->Looper();
if (looper == NULL)
error = B_MISMATCHED_VALUES;
}
}
// set port, token,...
if (error == B_OK) {
AutoLocker<BLooperList> locker(gLooperList);
if (locker.IsLocked() && gLooperList.IsLooperValid(looper)) {
fPort = looper->fMsgPort;
fHandlerToken = (handler
? _get_object_token_(handler) : B_PREFERRED_TOKEN);
fTeam = looper->Team();
} else
error = B_BAD_VALUE;
}
}
if (_result)
*_result = error;
_InitData(handler, looper, _result);
}
@@ -427,6 +401,50 @@ BMessenger::SendMessage(BMessage *message, BMessage *reply,
// #pragma mark - Operators and misc
/*! \brief Reinitializes a BMessenger to target the already running application
identified by the supplied signature and/or team ID.
When only a signature is given, and multiple instances of the application
are running it is undeterminate which one is chosen as the target. In case
only a team ID is passed, the target application is identified uniquely.
If both are supplied, the application identified by the team ID must have
a matching signature, otherwise the initilization fails.
\param signature The target application's signature. May be \c NULL.
\param team The target application's team ID. May be < 0.
\return The result of the reinitialization.
*/
status_t
BMessenger::SetTo(const char *signature, team_id team)
{
status_t result = B_OK;
_InitData(signature, team, &result);
return result;
}
/*! \brief Reinitializes a BMessenger to target the local BHandler and/or
BLooper.
When a \c NULL handler is supplied, the preferred handler in the given
looper is targeted. If no looper is supplied the looper the given handler
belongs to is used -- that means in particular, that the handler must
already belong to a looper. If both are supplied the handler must actually
belong to looper.
\param handler The target handler. May be \c NULL.
\param looper The target looper. May be \c NULL.
\return The result of the reinitialization.
*/
status_t
BMessenger::SetTo(const BHandler* handler, const BLooper* looper)
{
status_t result = B_OK;
_InitData(handler, looper, &result);
return result;
}
/*! \brief Makes this BMessenger a copy of the supplied one.
\param from the messenger to be copied.
@@ -564,6 +582,54 @@ BMessenger::_InitData(const char* signature, team_id team, status_t* _result)
}
/*! \brief Initializes the BMessenger to target the local BHandler and/or
BLooper.
When a \c NULL handler is supplied, the preferred handler in the given
looper is targeted. If no looper is supplied the looper the given handler
belongs to is used -- that means in particular, that the handler must
already belong to a looper. If both are supplied the handler must actually
belong to looper.
\param handler The target handler. May be \c NULL.
\param looper The target looper. May be \c NULL.
\param result An optional pointer to a pre-allocated status_t into which
the result of the initialization is written.
*/
void
BMessenger::_InitData(const BHandler* handler, const BLooper* looper,
status_t* _result)
{
status_t error = (handler || looper ? B_OK : B_BAD_VALUE);
if (error == B_OK) {
if (handler) {
// BHandler is given, check/retrieve the looper.
if (looper) {
if (handler->Looper() != looper)
error = B_MISMATCHED_VALUES;
} else {
looper = handler->Looper();
if (looper == NULL)
error = B_MISMATCHED_VALUES;
}
}
// set port, token,...
if (error == B_OK) {
AutoLocker<BLooperList> locker(gLooperList);
if (locker.IsLocked() && gLooperList.IsLooperValid(looper)) {
fPort = looper->fMsgPort;
fHandlerToken = (handler
? _get_object_token_(handler) : B_PREFERRED_TOKEN);
fTeam = looper->Team();
} else
error = B_BAD_VALUE;
}
}
if (_result)
*_result = error;
}
/*! \brief Returns whether the first one of two BMessengers is less than the
second one.