btrfs: Attempt to fix GCC2 build.
[haiku.git] / src / kits / shared / MessageBuilder.cpp
blobe81bd3429cd4d3ddd8f114a645c08c962734312b
1 /*
2 * Copyright 2014, Augustin Cavalier (waddlesplash)
3 * Distributed under the terms of the MIT License.
4 */
7 #include <MessageBuilder.h>
9 #include <AutoDeleter.h>
10 #include <String.h>
13 namespace BPrivate {
15 // #pragma mark - BMessageBuilder
18 BMessageBuilder::BMessageBuilder(BMessage& message)
20 fNameStack(20, true),
21 fCurrentMessage(&message)
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.
31 status_t
32 BMessageBuilder::PushObject(const char* name)
34 BMessage* newMessage = new(std::nothrow) BMessage;
35 if (newMessage == NULL)
36 return B_NO_MEMORY;
37 ObjectDeleter<BMessage> messageDeleter(newMessage);
39 BString* nameString = new(std::nothrow) BString(name);
40 if (nameString == NULL)
41 return B_NO_MEMORY;
42 ObjectDeleter<BString> stringDeleter(nameString);
44 if (!fNameStack.AddItem(nameString))
45 return B_NO_MEMORY;
46 stringDeleter.Detach();
48 if (!fStack.AddItem(fCurrentMessage))
49 return B_NO_MEMORY;
50 messageDeleter.Detach();
52 fCurrentMessage = newMessage;
53 return B_OK;
57 /*! Convenience function that converts "name"
58 to a string and calls PushObject(const char*)
59 with it.
61 status_t
62 BMessageBuilder::PushObject(uint32 name)
64 BString nameString;
65 nameString.SetToFormat("%" B_PRIu32, name);
66 return PushObject(nameString.String());
70 /*! Pops the last BMessage off the stack and makes it
71 the current one.
73 status_t
74 BMessageBuilder::PopObject()
76 if (fStack.CountItems() < 1)
77 return B_ERROR;
79 BMessage* previousMessage = fStack.LastItem();
80 previousMessage->AddMessage(fNameStack.LastItem()->String(),
81 fCurrentMessage);
83 delete fCurrentMessage;
84 fCurrentMessage = previousMessage;
86 fStack.RemoveItemAt(fStack.CountItems() - 1);
87 fNameStack.RemoveItemAt(fNameStack.CountItems() - 1);
88 return B_OK;
92 /*! Gets the "what" of the current message.
94 uint32
95 BMessageBuilder::What()
97 return fCurrentMessage->what;
101 /*! Sets the "what" of the current message.
103 void
104 BMessageBuilder::SetWhat(uint32 what)
106 fCurrentMessage->what = what;
110 /*! Gets the value of CountNames() from the current message.
112 uint32
113 BMessageBuilder::CountNames(type_code type)
115 return fCurrentMessage->CountNames(type);
119 // #pragma mark - BMessageBuilder::Add (to fCurrentMessage)
122 status_t
123 BMessageBuilder::AddString(const char* name, const char* string)
125 return fCurrentMessage->AddString(name, string);
129 status_t
130 BMessageBuilder::AddString(const char* name, const BString& string)
132 return fCurrentMessage->AddString(name, string);
136 status_t
137 BMessageBuilder::AddInt8(const char* name, int8 value)
139 return fCurrentMessage->AddInt8(name, value);
143 status_t
144 BMessageBuilder::AddUInt8(const char* name, uint8 value)
146 return fCurrentMessage->AddUInt8(name, value);
150 status_t
151 BMessageBuilder::AddInt16(const char* name, int16 value)
153 return fCurrentMessage->AddInt16(name, value);
157 status_t
158 BMessageBuilder::AddUInt16(const char* name, uint16 value)
160 return fCurrentMessage->AddUInt16(name, value);
164 status_t
165 BMessageBuilder::AddInt32(const char* name, int32 value)
167 return fCurrentMessage->AddInt32(name, value);
171 status_t
172 BMessageBuilder::AddUInt32(const char* name, uint32 value)
174 return fCurrentMessage->AddUInt32(name, value);
178 status_t
179 BMessageBuilder::AddInt64(const char* name, int64 value)
181 return fCurrentMessage->AddInt64(name, value);
185 status_t
186 BMessageBuilder::AddUInt64(const char* name, uint64 value)
188 return fCurrentMessage->AddUInt64(name, value);
192 status_t
193 BMessageBuilder::AddBool(const char* name, bool value)
195 return fCurrentMessage->AddBool(name, value);
199 status_t
200 BMessageBuilder::AddFloat(const char* name, float value)
202 return fCurrentMessage->AddFloat(name, value);
206 status_t
207 BMessageBuilder::AddDouble(const char* name, double value)
209 return fCurrentMessage->AddDouble(name, value);
213 status_t
214 BMessageBuilder::AddPointer(const char* name, const void* pointer)
216 return fCurrentMessage->AddPointer(name, pointer);
220 } // namespace BPrivate