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
13namespace BPrivate {
14
15// #pragma mark - BMessageBuilder
16
17
18BMessageBuilder::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*/
31status_t
32BMessageBuilder::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*/
61status_t
62BMessageBuilder::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*/
73status_t
74BMessageBuilder::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*/
94uint32
95BMessageBuilder::What()
96{
97	return fCurrentMessage->what;
98}
99
100
101/*! Sets the "what" of the current message.
102*/
103void
104BMessageBuilder::SetWhat(uint32 what)
105{
106	fCurrentMessage->what = what;
107}
108
109
110/*! Gets the value of CountNames() from the current message.
111*/
112uint32
113BMessageBuilder::CountNames(type_code type)
114{
115	return fCurrentMessage->CountNames(type);
116}
117
118
119// #pragma mark - BMessageBuilder::Add (to fCurrentMessage)
120
121
122status_t
123BMessageBuilder::AddString(const char* name, const char* string)
124{
125	return fCurrentMessage->AddString(name, string);
126}
127
128
129status_t
130BMessageBuilder::AddString(const char* name, const BString& string)
131{
132	return fCurrentMessage->AddString(name, string);
133}
134
135
136status_t
137BMessageBuilder::AddInt8(const char* name, int8 value)
138{
139	return fCurrentMessage->AddInt8(name, value);
140}
141
142
143status_t
144BMessageBuilder::AddUInt8(const char* name, uint8 value)
145{
146	return fCurrentMessage->AddUInt8(name, value);
147}
148
149
150status_t
151BMessageBuilder::AddInt16(const char* name, int16 value)
152{
153	return fCurrentMessage->AddInt16(name, value);
154}
155
156
157status_t
158BMessageBuilder::AddUInt16(const char* name, uint16 value)
159{
160	return fCurrentMessage->AddUInt16(name, value);
161}
162
163
164status_t
165BMessageBuilder::AddInt32(const char* name, int32 value)
166{
167	return fCurrentMessage->AddInt32(name, value);
168}
169
170
171status_t
172BMessageBuilder::AddUInt32(const char* name, uint32 value)
173{
174	return fCurrentMessage->AddUInt32(name, value);
175}
176
177
178status_t
179BMessageBuilder::AddInt64(const char* name, int64 value)
180{
181	return fCurrentMessage->AddInt64(name, value);
182}
183
184
185status_t
186BMessageBuilder::AddUInt64(const char* name, uint64 value)
187{
188	return fCurrentMessage->AddUInt64(name, value);
189}
190
191
192status_t
193BMessageBuilder::AddBool(const char* name, bool value)
194{
195	return fCurrentMessage->AddBool(name, value);
196}
197
198
199status_t
200BMessageBuilder::AddFloat(const char* name, float value)
201{
202	return fCurrentMessage->AddFloat(name, value);
203}
204
205
206status_t
207BMessageBuilder::AddDouble(const char* name, double value)
208{
209	return fCurrentMessage->AddDouble(name, value);
210}
211
212
213status_t
214BMessageBuilder::AddPointer(const char* name, const void* pointer)
215{
216	return fCurrentMessage->AddPointer(name, pointer);
217}
218
219
220} // namespace BPrivate
221