xref: /haiku/src/kits/shared/MessageBuilder.cpp (revision 1e60bdeab63fa7a57bc9a55b032052e95a18bd2c)
1 /*
2  * Copyright 2014, Augustin Cavalier (waddlesplash)
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include <MessageBuilder.h>
8 
9 #include <AutoDeleter.h>
10 #include <String.h>
11 
12 
13 namespace BPrivate {
14 
15 // #pragma mark - BMessageBuilder
16 
17 
18 BMessageBuilder::BMessageBuilder(BMessage& message)
19 	:
20 	fNameStack(20, true),
21 	fCurrentMessage(&message)
22 {
23 }
24 
25 
26 /*! Creates a new BMessage, makes it a child of the
27     current one with "name", and then pushes the current
28     Message onto the stack and makes the new Message the
29     current one.
30 */
31 status_t
32 BMessageBuilder::PushObject(const char* name)
33 {
34 	BMessage* newMessage = new(std::nothrow) BMessage;
35 	if (newMessage == NULL)
36 		return B_NO_MEMORY;
37 	ObjectDeleter<BMessage> messageDeleter(newMessage);
38 
39 	BString* nameString = new(std::nothrow) BString(name);
40 	if (nameString == NULL)
41 		return B_NO_MEMORY;
42 	ObjectDeleter<BString> stringDeleter(nameString);
43 
44 	if (!fNameStack.AddItem(nameString))
45 		return B_NO_MEMORY;
46 	stringDeleter.Detach();
47 
48 	if (!fStack.AddItem(fCurrentMessage))
49 		return B_NO_MEMORY;
50 	messageDeleter.Detach();
51 
52 	fCurrentMessage = newMessage;
53 	return B_OK;
54 }
55 
56 
57 /*! Convenience function that converts "name"
58 	to a string and calls PushObject(const char*)
59 	with it.
60 */
61 status_t
62 BMessageBuilder::PushObject(uint32 name)
63 {
64 	BString nameString;
65 	nameString.SetToFormat("%" B_PRIu32, name);
66 	return PushObject(nameString.String());
67 }
68 
69 
70 /*! Pops the last BMessage off the stack and makes it
71     the current one.
72 */
73 status_t
74 BMessageBuilder::PopObject()
75 {
76 	if (fStack.CountItems() < 1)
77 		return B_ERROR;
78 
79 	BMessage* previousMessage = fStack.LastItem();
80 	previousMessage->AddMessage(fNameStack.LastItem()->String(),
81 		fCurrentMessage);
82 
83 	delete fCurrentMessage;
84 	fCurrentMessage = previousMessage;
85 
86 	fStack.RemoveItemAt(fStack.CountItems() - 1);
87 	fNameStack.RemoveItemAt(fNameStack.CountItems() - 1);
88 	return B_OK;
89 }
90 
91 
92 /*! Gets the "what" of the current message.
93 */
94 uint32
95 BMessageBuilder::What()
96 {
97 	return fCurrentMessage->what;
98 }
99 
100 
101 /*! Sets the "what" of the current message.
102 */
103 void
104 BMessageBuilder::SetWhat(uint32 what)
105 {
106 	fCurrentMessage->what = what;
107 }
108 
109 
110 /*! Gets the value of CountNames() from the current message.
111 */
112 uint32
113 BMessageBuilder::CountNames(type_code type)
114 {
115 	return fCurrentMessage->CountNames(type);
116 }
117 
118 
119 // #pragma mark - BMessageBuilder::Add (to fCurrentMessage)
120 
121 
122 status_t
123 BMessageBuilder::AddString(const char* name, const char* string)
124 {
125 	return fCurrentMessage->AddString(name, string);
126 }
127 
128 
129 status_t
130 BMessageBuilder::AddString(const char* name, const BString& string)
131 {
132 	return fCurrentMessage->AddString(name, string);
133 }
134 
135 
136 status_t
137 BMessageBuilder::AddInt8(const char* name, int8 value)
138 {
139 	return fCurrentMessage->AddInt8(name, value);
140 }
141 
142 
143 status_t
144 BMessageBuilder::AddUInt8(const char* name, uint8 value)
145 {
146 	return fCurrentMessage->AddUInt8(name, value);
147 }
148 
149 
150 status_t
151 BMessageBuilder::AddInt16(const char* name, int16 value)
152 {
153 	return fCurrentMessage->AddInt16(name, value);
154 }
155 
156 
157 status_t
158 BMessageBuilder::AddUInt16(const char* name, uint16 value)
159 {
160 	return fCurrentMessage->AddUInt16(name, value);
161 }
162 
163 
164 status_t
165 BMessageBuilder::AddInt32(const char* name, int32 value)
166 {
167 	return fCurrentMessage->AddInt32(name, value);
168 }
169 
170 
171 status_t
172 BMessageBuilder::AddUInt32(const char* name, uint32 value)
173 {
174 	return fCurrentMessage->AddUInt32(name, value);
175 }
176 
177 
178 status_t
179 BMessageBuilder::AddInt64(const char* name, int64 value)
180 {
181 	return fCurrentMessage->AddInt64(name, value);
182 }
183 
184 
185 status_t
186 BMessageBuilder::AddUInt64(const char* name, uint64 value)
187 {
188 	return fCurrentMessage->AddUInt64(name, value);
189 }
190 
191 
192 status_t
193 BMessageBuilder::AddBool(const char* name, bool value)
194 {
195 	return fCurrentMessage->AddBool(name, value);
196 }
197 
198 
199 status_t
200 BMessageBuilder::AddFloat(const char* name, float value)
201 {
202 	return fCurrentMessage->AddFloat(name, value);
203 }
204 
205 
206 status_t
207 BMessageBuilder::AddDouble(const char* name, double value)
208 {
209 	return fCurrentMessage->AddDouble(name, value);
210 }
211 
212 
213 status_t
214 BMessageBuilder::AddPointer(const char* name, const void* pointer)
215 {
216 	return fCurrentMessage->AddPointer(name, pointer);
217 }
218 
219 
220 } // namespace BPrivate
221