1/* 2 * Copyright 2007, Haiku, Inc. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Niels Sascha Reedijk, niels.reedijk@gmail.com 7 * 8 * Corresponds to: 9 * /trunk/headers/os/app/MessageQueue.h rev 19956 10 * /trunk/src/kits/app/MessageQueue.cpp rev 19956 11 */ 12 13/*! 14 \file MessageQueue.h 15 \ingroup app 16 \ingroup libroot 17 \brief Provides the BMessageQueue class. 18*/ 19 20 21/*! 22 \class BMessageQueue 23 \ingroup app 24 \brief A container that maintains a queue of messages. 25 26 This class is used by BLooper to maintain a queue of messages that need to 27 be processed. This class has been designed as a first in, first out 28 container. 29 30 The default message handling of a BLooper probably suffices for most uses, 31 but if you want more control, you can perform operations using the methods 32 of this class. Use BLooper::MessageQueue() to retrieve the specific 33 BMessageQueue instance. 34 35 Note that you are encouraged to make sure that whichever operation you 36 perform, that you only do this after the object has been locked (see 37 Lock()). The most important method, NextMessage() will fail if you have not 38 complied with this requirement. 39*/ 40 41 42/*! 43 \fn BMessageQueue::BMessageQueue() 44 \brief Constructs an empty message queue. 45*/ 46 47 48/*! 49 \fn BMessageQueue::~BMessageQueue() 50 \brief Destruct the BMessageQueue. It iterates over any messages left on 51 the queue and deletes them. 52 53 The implementation is careful not to release the lock when the 54 BMessageQueue is deconstructed. If the lock is released, it is 55 possible another thread will start an AddMessage() operation before 56 the BLocker is deleted. The safe thing to do is not to unlock the 57 BLocker from the destructor once it is acquired. That way, any thread 58 waiting to do a AddMessage() will fail to acquire the lock since the 59 BLocker will be deleted before they can acquire it. 60*/ 61 62 63/*! 64 \fn void BMessageQueue::AddMessage(BMessage* message) 65 \brief Add a \a message to the end of the queue. 66 67 The message has to be allocated on the heap with \c new, because the queue 68 claims ownership of the message. Messages that were constructed on the 69 stack will corrupt the queue. 70 71 Because a BMessageQueue claims ownership of the \a message, it is important 72 that the message does not belong to another BMessageQueue. 73*/ 74 75 76/*! 77 \fn void BMessageQueue::RemoveMessage(BMessage* message) 78 \brief Remove a \a message from the queue. 79 80 If the \a message is indeed associated with this queue, it is removed from 81 it. This effectively means that you regain ownership of the message. 82*/ 83 84 85/*! 86 \fn int32 BMessageQueue::CountMessages() const 87 \brief Return the number of messages waiting in the queue. 88*/ 89 90 91/*! 92 \fn bool BMessageQueue::IsEmpty() const 93 \brief Check if there are messages waiting in the queue. 94*/ 95 96 97/*! 98 \fn BMessage *BMessageQueue::FindMessage(int32 index) const 99 \brief Retrieve the message at the \a index of this queue. 100 101 \param index A zero-based index of the message you want to retrieve. 102 103 \return A pointer to a message, or \c NULL if the \a index is out of 104 bounds. 105 \see FindMessage(uint32, int32) for a variant that takes a specific \c what 106 identifier. 107*/ 108 109 110/*! 111 \fn BMessage *BMessageQueue::FindMessage(uint32 what, int32 index) const 112 \brief Retrieve the message at the \a index of this queue, but only if it 113 has a specific \a what constant. 114 115 \param index A zero-based index of the message you want to retrieve. 116 \param what The \a what code of the message. 117 118 \return A pointer to a message, or \c NULL if there is no message at the 119 \a index with that \a what constant, or if the \a index is out of 120 bounds. 121*/ 122 123 124/*! 125 \fn bool BMessageQueue::Lock() 126 \brief Lock the queue so no other thread can perform operations on it. 127 128 \see Unlock() 129*/ 130 131 132/*! 133 \fn void BMessageQueue::Unlock() 134 \brief Unlock the queue after a Lock() request. 135 136 \see Lock() 137*/ 138 139 140/*! 141 \fn bool BMessageQueue::IsLocked() const 142 \brief Check if the queue is locked. 143 144 \see Lock() and Unlock() 145*/ 146 147 148/*! 149 \fn BMessage *BMessageQueue::NextMessage() 150 \brief Remove the first BMessage on the queue and return it to the caller. 151 152 After calling this method, you get the ownership of the message, so make 153 sure it is deleted after you are done. 154 155 \return A pointer to a message, or \c NULL if the queue is empty, or the 156 object has not been properly locked. 157 \see Lock() 158 \see IsNextMessage() 159*/ 160 161 162/*! 163 \fn bool BMessageQueue::IsNextMessage(const BMessage* message) const 164 \brief Check if the pointer to a \a message points at the next message on 165 the queue. 166*/ 167