RTI Routing Service  Version 6.0.0
 All Data Structures Files Functions Typedefs Enumerations Enumerator Groups Pages
DynamicBuffer.hpp
1 /*
2  * $Id$
3  *
4  * (c) Copyright, Real-Time Innovations, 2016-.
5  * All rights reserved.
6  * No duplications, whole or partial, manual or electronic, may be made
7  * without express written permission. Any such copies, or
8  * revisions thereof, must display this notice unaltered.
9  * This code contains trade secrets of Real-Time Innovations, Inc.
10  */
11 
12 #ifndef HPP_SERVICE_DYNAMIC_BUFFER_HPP_
13 #define HPP_SERVICE_DYNAMIC_BUFFER_HPP_
14 
15 
16 // IMPORTANT: macros.hpp must be the first RTI header included in every header
17 // file so that symbols are exported correctly on Windows
18 #include <dds/core/macros.hpp>
19 
20 #include "osapi/osapi_heap.h"
21 #include "routingservice/routingservice_log.h"
22 #include "routingservice/routingservice_remote_config.h"
23 
24 #include <dds/core/Value.hpp>
25 #include <dds/core/types.hpp>
26 #include <rtiboost/assert.hpp>
27 #include <rti/service/ServiceException.hpp>
28 
29 namespace rti { namespace service {
30 
31 class DynamicBufferProperty {
32 public:
33 
34  DynamicBufferProperty(int32_t min_size, bool trim_to_size)
35  :min_size_(min_size), trim_to_size_(trim_to_size)
36  {
37  }
38 
39  DynamicBufferProperty(const DynamicBufferProperty &property)
40  :min_size_(property.min_size_), trim_to_size_(property.trim_to_size_)
41  {
42  }
43 
44  DynamicBufferProperty& min_size(int32_t min_size)
45  {
46  min_size_ = min_size;
47  return *this;
48  }
49  int32_t min_size() const
50  {
51  return min_size_;
52  }
53 
54  DynamicBufferProperty& trim_to_size(bool trim_to_size)
55  {
56  trim_to_size_ = trim_to_size;
57  return *this;
58  }
59 
60  bool trim_to_size() const
61  {
62  return trim_to_size_;
63  }
64 
65 private:
66  int32_t min_size_;
67  bool trim_to_size_;
68 };
69 
70 
71 class DynamicBuffer {
72 public:
73 
74  DynamicBuffer(const DynamicBufferProperty &property)
75  :property_(property), buffer_(NULL), buffer_length_(0)
76  {
77  ensure_length(property.min_size());
78  }
79 
80  ~DynamicBuffer()
81  {
82  if (buffer_ != NULL) {
83  RTIOsapiHeap_freeArray(buffer_);
84  buffer_ = NULL;
85  }
86  buffer_length_ = 0;
87  }
88 
89  void ensure_length(int32_t new_length)
90  {
91  if (new_length > buffer_length_) {
92  if (!RTIOsapiHeap_reallocateArray(&buffer_, new_length, char)) {
93  RTI_THROW_SERVICE_EXCEPTION(
94  &RTI_OSAPI_MEMORY_LOG_OUT_OF_HEAP_STRING_d,
95  new_length);
96  }
97  buffer_length_ = new_length;
98  }
99  }
100 
101  void trim_to_size()
102  {
103  if (property_.trim_to_size()) {
104  if (!RTIOsapiHeap_reallocateArray(
105  &buffer_,
106  property_.min_size(),
107  char)) {
108  RTI_THROW_SERVICE_EXCEPTION(
109  &RTI_OSAPI_MEMORY_LOG_OUT_OF_HEAP_STRING_d,
110  property_.min_size());
111  }
112  buffer_length_ = property_.min_size();
113  }
114  }
115 
116  void release(char *&buffer, int32_t& length)
117  {
118  buffer = buffer_;
119  length = buffer_length_;
120  buffer_ = NULL;
121  buffer_length_ = 0;
122  }
123 
124  void own(char *buffer, int32_t length)
125  {
126  buffer_ = buffer;
127  buffer_length_ = length;
128  }
129 
130  char *buffer()
131  {
132  return buffer_;
133  }
134 
135  int32_t length()
136  {
137  return buffer_length_;
138  }
139 
140 private:
141  const DynamicBufferProperty property_;
142  char* buffer_;
143  int32_t buffer_length_;
144 };
145 
146 
147 } } // rti::service
148 #endif /* HPP_SERVICE_DYNAMIC_BUFFER_HPP_ */

RTI Routing Service Version 6.0.0 Copyright © Sun Mar 3 2019 Real-Time Innovations, Inc