CoreDX DDS C++ Reference Manual
dds_seq.hh
Go to the documentation of this file.
1 /*****************************************************************
2  *
3  * file: dds_seq.hh
4  * desc: CoreDX DDS API for sequences
5  *
6  *****************************************************************
7  *
8  * Copyright(C) 2006-2016 Twin Oaks Computing, Inc
9  * All rights reserved. Castle Rock, CO 80108
10  *
11  *****************************************************************
12  *
13  * This software has been provided pursuant to a License Agreement
14  * containing restrictions on its use. This software contains
15  * valuable trade secrets and proprietary information of
16  * Twin Oaks Computing, Inc and is protected by law. It may not be
17  * copied or distributed in any form or medium, disclosed to third
18  * parties, reverse engineered or used in any manner not provided
19  * for in said License Agreement except with the prior written
20  * authorization from Twin Oaks Computing, Inc.
21  *
22  *****************************************************************/
23 
24 #ifndef _DDS_SEQ_HH
25 #define _DDS_SEQ_HH
26 
27 # include <dds/toc_util.h>
28 
43 namespace DDS
44 {
55  template <class T> class sequence {
56  public:
58  sequence();
62  sequence( const sequence &x);
66  ~sequence();
71  this->resize(x.core_seq._length);
72  for (unsigned int i = 0; i < this->core_seq._length; i++)
73  this->core_seq._buffer[i] = x.core_seq._buffer[i]; // will do deep copy if implemented on T
74  return *this;
75  };
76 
77  // capacity
79  uint32_t size() const;
81  uint32_t length() const;
85  uint32_t capacity() const; // get current capactity
94  bool reserve(uint32_t n); // set capacity
103  bool resize(uint32_t n); // set capacity and size
106  void maximum(uint32_t n); // set maximum
109  uint32_t maximum() const; // get current maximum
110 
112  bool empty() const; // test if empty
114  void shrink_to_fit(); // reduce capacity to fit size
115 
119  void assign(T* buffer, uint32_t n); // provide array of elements of size 'n', takes ownership of the memory
120 
124  T* release(); // release sequence contents to caller (if successful, sequence is empty after this)
125 
126  // element access
128  T& operator[] (uint32_t n);
130  const T& operator[] (uint32_t n) const;
132  T& at (uint32_t n);
134  const T& at (uint32_t n) const;
136  T& front ();
138  const T& front () const;
140  T& back ();
142  const T& back () const;
143 
145  const T* buffer() const;
146 
147  // modifiers
151  void push_back(const T& v);
153  void pop_back();
155  void swap(sequence& x);
157  void clear();
158 
159  private:
160 
161  typedef struct CoreSeqType
162  {
163  T * _buffer;
164  unsigned int _maximum;
165  unsigned int _length;
166  unsigned int _size;
167  unsigned char _own;
168  unsigned int _item_size;
169  } CoreSeqType_t;
170 
171  CoreSeqType_t core_seq;
172  };
173 
174  template<class T>
176  INIT_SEQ(this->core_seq);
177  this->core_seq._item_size = sizeof(T);
178  }
179 
180  // copy constructor
181  template<class T>
183  INIT_SEQ(this->core_seq);
184  this->core_seq._item_size = sizeof(T);
185  (*this) = x;
186  }
187 
188  template<class T>
190  clear();
191  }
192 
193  // capacity
194  template<class T>
195  uint32_t
197  return this->core_seq._size;
198  }
199 
200  template<class T>
201  uint32_t sequence<T>::size() const {
202  return this->core_seq._length;
203  }
204 
205  template<class T>
206  uint32_t sequence<T>::length() const {
207  return this->core_seq._length;
208  }
209 
210  template<class T>
211  void sequence<T>::maximum(uint32_t n)
212  {
213  this->core_seq._maximum = n;
214  }
215 
216  template<class T>
217  uint32_t sequence<T>::maximum() const {
218  return this->core_seq._maximum;
219  }
220 
221  template<class T>
222  bool
224  return (this->core_seq._length == 0)?true:false;
225  }
226 
227  // reserve(n)
228  // allocate memory to hold up to 's' items (capacity)
229  // will truncate if 'n' is smaller than current capacity
230  template<class T>
231  bool
232  sequence<T>::reserve(uint32_t n) {
233  bool retval = true;
234  T * tmpbuf = (T*)0;
235  if (n > 0)
236  {
237  T * newbuf = new T[n];
238  if (newbuf)
239  {
240  this->core_seq._size = n;
241  if (this->core_seq._length > n)
242  this->core_seq._length = n; // truncated
243 
244  if (this->core_seq._buffer)
245  for (uint32_t i = 0;i < this->core_seq._length; i++)
246  newbuf[i] = this->core_seq._buffer[i];
247  tmpbuf = this->core_seq._buffer;
248  this->core_seq._buffer = newbuf;
249  }
250  else
251  retval = false; // failed to alloc
252  }
253  else
254  {
255  this->core_seq._size = 0;
256  this->core_seq._length = 0;
257  tmpbuf = this->core_seq._buffer;
258  this->core_seq._buffer = 0;
259  }
260  if ( tmpbuf )
261  delete[] tmpbuf;
262  return retval;
263  }
264 
265  template<class T>
266  bool sequence<T>::resize(uint32_t n) {
267  bool retval;
268  retval = reserve(n);
269  if (this->core_seq._size == n)
270  this->core_seq._length = n;
271  return retval;
272  }
273 
274  template<class T>
275  void
277  resize(this->core_seq._length);
278  }
279 
280  // element access
281  template<class T>
282  T&
284  return this->core_seq._buffer[n];
285  }
286 
287  template<class T>
288  const T&
289  sequence<T>::operator[] (uint32_t n) const {
290  return this->core_seq._buffer[n];
291  }
292 
293  template<class T> T& sequence<T>::at (uint32_t n) { return (*this)[n]; }
294  template<class T> const T& sequence<T>::at (uint32_t n) const { return (*this)[n]; }
295  template<class T> T& sequence<T>::front () { return (*this)[0]; }
296  template<class T> const T& sequence<T>::front () const { return (*this)[0]; }
297 
298  template<class T> T& sequence<T>::back () { return (*this)[this->core_seq._length-1]; }
299  template<class T> const T& sequence<T>::back () const { return (*this)[this->core_seq._length-1]; }
300  template<class T> const T* sequence<T>::buffer () const { return this->core_seq._buffer; }
301 
302  // modifiers
303  template<class T> void sequence<T>::push_back(const T& v) {
304  if (this->capacity() <= this->size())
305  reserve(this->size() + 2);
306 
307  if (this->size() < this->capacity())
308  {
309  this->core_seq._buffer[this->size()] = v;
310  this->core_seq._length ++;
311  }
312  }
313 
314  template<class T> void sequence<T>::pop_back() {
315  if (this->core_seq._length > 0) this->core_seq._length--;
316  }
317 
318  template<class T> void sequence<T>::swap(sequence& x) {
319  CoreSeqType tmp;
320  tmp = this->core_seq;
321  this->core_seq = x.core_seq;
322  x.core_seq = tmp;
323  }
324 
325  template<class T> void sequence<T>::clear() {
326  delete[] this->core_seq._buffer;
327  this->core_seq._buffer = 0;
328  this->core_seq._length = 0;
329  this->core_seq._size = 0;
330  }
331 
332  template<class T>
333  void sequence<T>::assign(T* buffer, uint32_t n) {
334  delete[] this->core_seq._buffer;
335  this->core_seq._buffer = buffer;
336  this->core_seq._length = n;
337  this->core_seq._size = n;
338  }
339 
340  template<class T>
342  T* retval = this->core_seq._buffer;
343  this->core_seq._buffer = NULL;
344  this->core_seq._length = 0;
345  this->core_seq._size = 0;
346  return retval;
347  }
348 
349 # define CPP_DECLARE_UNBOUNDED_SEQ( type, name ) typedef DDS::sequence<type> name
350 # define DECLARE_CPP_UNBOUNDED_SEQ( type, name ) typedef DDS::sequence<type> name
351 
352 } // namespace DDS
353 #endif
354 
355 
T & front()
Definition: dds_seq.hh:295
T * release()
Release current sequence contents, caller takes ownership of the memory. If successful, the sequence is empty after this operation.
Definition: dds_seq.hh:341
bool empty() const
test if the sequence is empty
Definition: dds_seq.hh:223
uint32_t size() const
Returns the current size (number of elements) of the sequence.
Definition: dds_seq.hh:201
void assign(T *buffer, uint32_t n)
Caller provides an array of elements of size &#39;n&#39;. The Sequence takes ownership of the memory...
Definition: dds_seq.hh:333
T & back()
Definition: dds_seq.hh:298
uint32_t length() const
alias for size()
Definition: dds_seq.hh:206
Sequence data type.
Definition: dds_seq.hh:55
void clear()
Definition: dds_seq.hh:325
T & operator[](uint32_t n)
Definition: dds_seq.hh:283
~sequence()
destroy the sequence and release resources. Destroys sequence contents.
Definition: dds_seq.hh:189
const T * buffer() const
Definition: dds_seq.hh:300
sequence & operator=(const sequence &x)
assignment operator Copies seqeuence contents.
Definition: dds_seq.hh:70
void pop_back()
Definition: dds_seq.hh:314
Provides the DDS infrastructure.
Definition: dds_builtin_basic.hh:28
void shrink_to_fit()
reduce &#39;capacity&#39; to match &#39;size&#39;
Definition: dds_seq.hh:276
bool resize(uint32_t n)
Set the &#39;capacity&#39; and &#39;size&#39; of the sequence.
Definition: dds_seq.hh:266
bool reserve(uint32_t n)
Set the capacity of the sequence.
Definition: dds_seq.hh:232
uint32_t maximum() const
set the &#39;maximum&#39; length of the sequence
Definition: dds_seq.hh:217
uint32_t capacity() const
Returns the current capacity (number of elements) of the sequence. The equation "size <= capacity" is...
Definition: dds_seq.hh:196
sequence()
construct an empty sequence
Definition: dds_seq.hh:175
void push_back(const T &v)
Definition: dds_seq.hh:303
T & at(uint32_t n)
Definition: dds_seq.hh:293
void swap(sequence &x)
Definition: dds_seq.hh:318

© 2009-2017 Twin Oaks Computing, Inc
Castle Rock, CO 80108
All rights reserved.