AMPS C/C++ Client Class Reference
AMPS C/C++ Client Version 5.3.3.0
CompositeMessageBuilder.hpp
1 //
3 // Copyright (c) 2010-2021 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 {
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 
58  CompositeMessageBuilder& append(const std::string& data_);
65  CompositeMessageBuilder& append(const char* data_, size_t length_);
66 
72 
77  const char* data(void) const;
82  size_t length(void) const;
83 
84  protected:
85  // Not implemented.
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 
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) _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;
121  return *this;
122  }
124  CompositeMessageBuilder::append(const char* data_, size_t length_)
125  {
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;
135  return *this;
136  }
137  inline const char*
139  {
140  return _data;
141  }
142  inline size_t
144  {
145  return _position;
146  }
149  {
150  _position = 0;
151  return *this;
152  }
153  inline void
154  CompositeMessageBuilder::_resize(size_t required_)
155  {
156  if(required_ <= _capacity) return;
157  char* newData = new char[required_];
158  memcpy(newData,_data,_position);
159  delete[] _data;
160  _data = newData;
161  _capacity = required_;
162  }
163 }
164 
165 #endif
const char * data(void) const
Returns the composite message&#39;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&#39;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