2 * Copyright 2014, Augustin Cavalier (waddlesplash)
3 * Distributed under the terms of the MIT License.
7 #include <MessageBuilder.h>
9 #include <AutoDeleter.h>
15 // #pragma mark - BMessageBuilder
18 BMessageBuilder::BMessageBuilder(BMessage
& message
)
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
32 BMessageBuilder::PushObject(const char* name
)
34 BMessage
* newMessage
= new(std::nothrow
) BMessage
;
35 if (newMessage
== NULL
)
37 ObjectDeleter
<BMessage
> messageDeleter(newMessage
);
39 BString
* nameString
= new(std::nothrow
) BString(name
);
40 if (nameString
== NULL
)
42 ObjectDeleter
<BString
> stringDeleter(nameString
);
44 if (!fNameStack
.AddItem(nameString
))
46 stringDeleter
.Detach();
48 if (!fStack
.AddItem(fCurrentMessage
))
50 messageDeleter
.Detach();
52 fCurrentMessage
= newMessage
;
57 /*! Convenience function that converts "name"
58 to a string and calls PushObject(const char*)
62 BMessageBuilder::PushObject(uint32 name
)
65 nameString
.SetToFormat("%" B_PRIu32
, name
);
66 return PushObject(nameString
.String());
70 /*! Pops the last BMessage off the stack and makes it
74 BMessageBuilder::PopObject()
76 if (fStack
.CountItems() < 1)
79 BMessage
* previousMessage
= fStack
.LastItem();
80 previousMessage
->AddMessage(fNameStack
.LastItem()->String(),
83 delete fCurrentMessage
;
84 fCurrentMessage
= previousMessage
;
86 fStack
.RemoveItemAt(fStack
.CountItems() - 1);
87 fNameStack
.RemoveItemAt(fNameStack
.CountItems() - 1);
92 /*! Gets the "what" of the current message.
95 BMessageBuilder::What()
97 return fCurrentMessage
->what
;
101 /*! Sets the "what" of the current message.
104 BMessageBuilder::SetWhat(uint32 what
)
106 fCurrentMessage
->what
= what
;
110 /*! Gets the value of CountNames() from the current message.
113 BMessageBuilder::CountNames(type_code type
)
115 return fCurrentMessage
->CountNames(type
);
119 // #pragma mark - BMessageBuilder::Add (to fCurrentMessage)
123 BMessageBuilder::AddString(const char* name
, const char* string
)
125 return fCurrentMessage
->AddString(name
, string
);
130 BMessageBuilder::AddString(const char* name
, const BString
& string
)
132 return fCurrentMessage
->AddString(name
, string
);
137 BMessageBuilder::AddInt8(const char* name
, int8 value
)
139 return fCurrentMessage
->AddInt8(name
, value
);
144 BMessageBuilder::AddUInt8(const char* name
, uint8 value
)
146 return fCurrentMessage
->AddUInt8(name
, value
);
151 BMessageBuilder::AddInt16(const char* name
, int16 value
)
153 return fCurrentMessage
->AddInt16(name
, value
);
158 BMessageBuilder::AddUInt16(const char* name
, uint16 value
)
160 return fCurrentMessage
->AddUInt16(name
, value
);
165 BMessageBuilder::AddInt32(const char* name
, int32 value
)
167 return fCurrentMessage
->AddInt32(name
, value
);
172 BMessageBuilder::AddUInt32(const char* name
, uint32 value
)
174 return fCurrentMessage
->AddUInt32(name
, value
);
179 BMessageBuilder::AddInt64(const char* name
, int64 value
)
181 return fCurrentMessage
->AddInt64(name
, value
);
186 BMessageBuilder::AddUInt64(const char* name
, uint64 value
)
188 return fCurrentMessage
->AddUInt64(name
, value
);
193 BMessageBuilder::AddBool(const char* name
, bool value
)
195 return fCurrentMessage
->AddBool(name
, value
);
200 BMessageBuilder::AddFloat(const char* name
, float value
)
202 return fCurrentMessage
->AddFloat(name
, value
);
207 BMessageBuilder::AddDouble(const char* name
, double value
)
209 return fCurrentMessage
->AddDouble(name
, value
);
214 BMessageBuilder::AddPointer(const char* name
, const void* pointer
)
216 return fCurrentMessage
->AddPointer(name
, pointer
);
220 } // namespace BPrivate