26 #ifndef _AMPS_COMPOSITEMESSAGEBUILDER_HPP_ 27 #define _AMPS_COMPOSITEMESSAGEBUILDER_HPP_ 43 static const size_t DEFAULT_INITIAL_CAPACITY = 16 * 1024;
44 static const size_t PART_HEADER_LENGTH = 4;
50 DEFAULT_INITIAL_CAPACITY);
77 const char*
data(
void)
const;
89 void _resize(
size_t required_);
97 : _data(new char[initialCapacity_]),
99 _capacity(initialCapacity_)
102 inline CompositeMessageBuilder::~CompositeMessageBuilder(
void)
111 size_t length = data_.length();
112 size_t required = _position + length + PART_HEADER_LENGTH;
113 if(_capacity < required) _resize(required);
114 char* p = _data + _position;
115 *p++ = (char)((length & 0xFF000000) >> 24);
116 *p++ = (char)((length & 0x00FF0000) >> 16);
117 *p++ = (char)((length & 0x0000FF00) >> 8);
118 *p++ = (char)((length & 0x000000FF) );
119 memcpy(p,data_.c_str(),
length);
120 _position += length + PART_HEADER_LENGTH;
126 size_t required = _position + length_ + PART_HEADER_LENGTH;
127 if(_capacity < required) _resize(required);
128 char* p = _data + _position;
129 *p++ = (char)((length_ & 0xFF000000) >> 24);
130 *p++ = (char)((length_ & 0x00FF0000) >> 16);
131 *p++ = (char)((length_ & 0x0000FF00) >> 8);
132 *p++ = (char)((length_ & 0x000000FF) );
133 memcpy(p,data_,length_);
134 _position += length_ + PART_HEADER_LENGTH;
154 CompositeMessageBuilder::_resize(
size_t required_)
156 if(required_ <= _capacity)
return;
157 char* newData =
new char[required_];
158 memcpy(newData,_data,_position);
161 _capacity = required_;
const char * data(void) const
Returns the composite message's data.
Definition: CompositeMessageBuilder.hpp:138
CompositeMessageBuilder & append(const std::string &data_)
Appends a message part to this object.
Definition: CompositeMessageBuilder.hpp:109
size_t length(void) const
Returns the length of the composite message's data.
Definition: CompositeMessageBuilder.hpp:143
Used to create payloads for AMPS composite messages, which are messages with a number of parts where ...
Definition: CompositeMessageBuilder.hpp:40
CompositeMessageBuilder & clear(void)
Clears this object.
Definition: CompositeMessageBuilder.hpp:148
CompositeMessageBuilder(size_t initialCapacity_=DEFAULT_INITIAL_CAPACITY)
Create a new, empty CompositeMessageBuilder.
Definition: CompositeMessageBuilder.hpp:96
Definition: ampsplusplus.hpp:103