AMPS C/C++ Client Class Reference AMPS C/C++ Client Version 5.3.5.6

CompositeMessageBuilder.hpp
1 //
3 // Copyright (c) 2010-2026 60East Technologies Inc., All Rights Reserved.
4 //
5 // This computer software is owned by 60East Technologies Inc. and is
6 // protected by U.S. copyright laws and other laws and by international
7 // treaties. This computer software is furnished by 60East Technologies
8 // Inc. pursuant to a written license agreement and may be used, copied,
9 // transmitted, and stored only in accordance with the terms of such
10 // license agreement and with the inclusion of the above copyright notice.
11 // This computer software or any other copies thereof may not be provided
12 // or otherwise made available to any other person.
13 //
14 // U.S. Government Restricted Rights. This computer software: (a) was
15 // developed at private expense and is in all respects the proprietary
16 // information of 60East Technologies Inc.; (b) was not developed with
17 // government funds; (c) is a trade secret of 60East Technologies Inc.
18 // for all purposes of the Freedom of Information Act; and (d) is a
19 // commercial item and thus, pursuant to Section 12.212 of the Federal
20 // Acquisition Regulations (FAR) and DFAR Supplement Section 227.7202,
21 // Government's use, duplication or disclosure of the computer software
22 // is subject to the restrictions set forth by 60East Technologies Inc..
23 //
25
26 #ifndef _AMPS_COMPOSITEMESSAGEBUILDER_HPP_
27 #define _AMPS_COMPOSITEMESSAGEBUILDER_HPP_
28
29 #include <string>
30
31 namespace AMPS
32 {
40 class CompositeMessageBuilder
41 {
42 public:
43 static const size_t DEFAULT_INITIAL_CAPACITY = 16 * 1024;
44 static const size_t PART_HEADER_LENGTH = 4;
49 CompositeMessageBuilder(size_t initialCapacity_ =
50 DEFAULT_INITIAL_CAPACITY);
51
52 ~CompositeMessageBuilder(void);
58 CompositeMessageBuilder& append(const std::string& data_);
65 CompositeMessageBuilder& append(const char* data_, size_t length_);
66
71 CompositeMessageBuilder& clear(void);
72
77 const char* data(void) const;
82 size_t length(void) const;
83
84 protected:
85 // Not implemented.
86 CompositeMessageBuilder(const CompositeMessageBuilder&);
87 CompositeMessageBuilder& operator=(const CompositeMessageBuilder&);
88 private:
89 void _resize(size_t required_);
90
91 char* _data;
92 size_t _position;
93 size_t _capacity;
94 };
95
96 inline CompositeMessageBuilder::CompositeMessageBuilder(size_t initialCapacity_)
97 : _data(new char[initialCapacity_]),
98 _position(0),
99 _capacity(initialCapacity_)
100 {;}
101
102 inline CompositeMessageBuilder::~CompositeMessageBuilder(void)
103 {
104 delete [] _data;
105 _data = NULL;
106 }
107
108 inline CompositeMessageBuilder&
109 CompositeMessageBuilder::append(const std::string& data_)
110 {
111 size_t length = data_.length();
112 size_t required = _position + length + PART_HEADER_LENGTH;
113 if (_capacity < required)
114 {
115 _resize(required);
116 }
117 char* p = _data + _position;
118 *p++ = (char)((length & 0xFF000000) >> 24);
119 *p++ = (char)((length & 0x00FF0000) >> 16);
120 *p++ = (char)((length & 0x0000FF00) >> 8);
121 *p++ = (char)((length & 0x000000FF) );
122 memcpy(p, data_.c_str(), length);
123 _position += length + PART_HEADER_LENGTH;
124 return *this;
125 }
126 inline CompositeMessageBuilder&
127 CompositeMessageBuilder::append(const char* data_, size_t length_)
128 {
129 size_t required = _position + length_ + PART_HEADER_LENGTH;
130 if (_capacity < required)
131 {
132 _resize(required);
133 }
134 char* p = _data + _position;
135 *p++ = (char)((length_ & 0xFF000000) >> 24);
136 *p++ = (char)((length_ & 0x00FF0000) >> 16);
137 *p++ = (char)((length_ & 0x0000FF00) >> 8);
138 *p++ = (char)((length_ & 0x000000FF) );
139 memcpy(p, data_, length_);
140 _position += length_ + PART_HEADER_LENGTH;
141 return *this;
142 }
143 inline const char*
144 CompositeMessageBuilder::data(void) const
145 {
146 return _data;
147 }
148 inline size_t
149 CompositeMessageBuilder::length(void) const
150 {
151 return _position;
152 }
153 inline CompositeMessageBuilder&
154 CompositeMessageBuilder::clear(void)
155 {
156 _position = 0;
157 return *this;
158 }
159 inline void
160 CompositeMessageBuilder::_resize(size_t required_)
161 {
162 if (required_ <= _capacity)
163 {
164 return;
165 }
166 char* newData = new char[required_];
167 memcpy(newData, _data, _position);
168 delete[] _data;
169 _data = newData;
170 _capacity = required_;
171 }
172 }
173
174 #endif
AMPS::CompositeMessageBuilder
Used to create payloads for AMPS composite messages, which are messages with a number of parts where ...
Definition: CompositeMessageBuilder.hpp:41
AMPS::CompositeMessageBuilder::length
size_t length(void) const
Returns the length of the composite message's data.
Definition: CompositeMessageBuilder.hpp:149
AMPS::CompositeMessageBuilder::clear
CompositeMessageBuilder & clear(void)
Clears this object.
Definition: CompositeMessageBuilder.hpp:154
AMPS::CompositeMessageBuilder::data
const char * data(void) const
Returns the composite message's data.
Definition: CompositeMessageBuilder.hpp:144
AMPS::CompositeMessageBuilder::append
CompositeMessageBuilder & append(const std::string &data_)
Appends a message part to this object.
Definition: CompositeMessageBuilder.hpp:109
AMPS::CompositeMessageBuilder::CompositeMessageBuilder
CompositeMessageBuilder(size_t initialCapacity_=DEFAULT_INITIAL_CAPACITY)
Create a new, empty CompositeMessageBuilder.
Definition: CompositeMessageBuilder.hpp:96