diff --git a/headers/os/app/Messenger.h b/headers/os/app/Messenger.h index 3c68fee9ed..1285014d82 100644 --- a/headers/os/app/Messenger.h +++ b/headers/os/app/Messenger.h @@ -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; diff --git a/src/kits/app/Messenger.cpp b/src/kits/app/Messenger.cpp index 230136fa49..3e284a3743 100644 --- a/src/kits/app/Messenger.cpp +++ b/src/kits/app/Messenger.cpp @@ -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 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 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.