vfs: check userland buffers before reading them.
[haiku.git] / src / build / libbe / app / Messenger.cpp
blob97aae4e54cdd64bfdb1511c06ca09627e4abcb74
1 /*
2 * Copyright 2001-2007, Haiku.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Ingo Weinhold (bonefish@users.sf.net)
7 */
10 #include <AppMisc.h>
11 #include <MessageUtils.h>
12 #include "TokenSpace.h"
14 #include <Application.h>
15 #include <Handler.h>
16 #include <Looper.h>
17 #include <LooperList.h>
18 #include <Message.h>
19 #include <MessagePrivate.h>
20 #include <Messenger.h>
21 #include <OS.h>
22 #include <Roster.h>
23 #include <TokenSpace.h>
25 #include <new>
26 #include <stdio.h>
27 #include <string.h>
30 // debugging
31 //#define DBG(x) x
32 #define DBG(x)
33 #define OUT printf
36 enum {
37 NOT_IMPLEMENTED = B_ERROR,
41 /*! \brief Creates an unitialized BMessenger.
43 BMessenger::BMessenger()
45 fPort(-1),
46 fHandlerToken(B_NULL_TOKEN),
47 fTeam(-1)
52 /*! \brief Creates a BMessenger and initializes it to have the same target
53 as the supplied messemger.
55 \param from The messenger to be copied.
57 BMessenger::BMessenger(const BMessenger& from)
59 fPort(from.fPort),
60 fHandlerToken(from.fHandlerToken),
61 fTeam(from.fTeam)
66 /*! \brief Frees all resources associated with this object.
68 BMessenger::~BMessenger()
73 // #pragma mark - Operators and misc
76 /*! \brief Makes this BMessenger a copy of the supplied one.
78 \param from the messenger to be copied.
79 \return A reference to this object.
81 BMessenger &
82 BMessenger::operator=(const BMessenger &from)
84 if (this != &from) {
85 fPort = from.fPort;
86 fHandlerToken = from.fHandlerToken;
87 fTeam = from.fTeam;
89 return *this;
93 /*! \brief Returns whether this and the supplied messenger have the same
94 target.
96 \param other The other messenger.
97 \return \c true, if the messengers have the same target or if both aren't
98 properly initialzed, \c false otherwise.
100 bool
101 BMessenger::operator==(const BMessenger &other) const
103 // Note: The fTeam fields are not compared.
104 return fPort == other.fPort
105 && fHandlerToken == other.fHandlerToken;
109 /*! \brief Returns whether the messenger's target looper does still exist.
111 It is not checked whether the target handler is also still existing.
113 \return \c true, if the messenger's target looper does still exist,
114 \c false otherwise.
116 bool
117 BMessenger::IsValid() const
119 return fPort >= 0;
123 /*! \brief Returns the ID of the team the messenger's target lives in.
125 \return The team of the messenger's target.
127 team_id
128 BMessenger::Team() const
130 return fTeam;
134 // #pragma mark - Private or reserved
137 /*! \brief Sets the messenger's team, target looper port and handler token.
139 To target the preferred handler, use B_PREFERRED_TOKEN as token.
141 \param team The target's team.
142 \param port The target looper port.
143 \param token The target handler token.
145 void
146 BMessenger::_SetTo(team_id team, port_id port, int32 token)
148 fTeam = team;
149 fPort = port;
150 fHandlerToken = token;
154 /*! \brief Returns whether the first one of two BMessengers is less than the
155 second one.
157 This method defines an order on BMessengers based on their member
158 variables \c fPort, \c fHandlerToken and \c fPreferredTarget.
160 \param a The first messenger.
161 \param b The second messenger.
162 \return \c true, if \a a is less than \a b, \c false otherwise.
164 bool
165 operator<(const BMessenger &_a, const BMessenger &_b)
167 BMessenger::Private a(const_cast<BMessenger&>(_a));
168 BMessenger::Private b(const_cast<BMessenger&>(_b));
170 // significance:
171 // 1. fPort
172 // 2. fHandlerToken
173 // 3. fPreferredTarget
174 // fTeam is insignificant
175 return (a.Port() < b.Port()
176 || (a.Port() == b.Port()
177 && (a.Token() < b.Token()
178 || (a.Token() == b.Token()
179 && !a.IsPreferredTarget()
180 && b.IsPreferredTarget()))));
184 /*! \brief Returns whether two BMessengers have not the same target.
186 \param a The first messenger.
187 \param b The second messenger.
188 \return \c false, if \a a and \a b have the same targets or are both not
189 properly initialized, \c true otherwise.
191 bool
192 operator!=(const BMessenger &a, const BMessenger &b)
194 return !(a == b);