Apama  10.1.0.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
buffer_t.hpp
Go to the documentation of this file.
1 /*
2  * Title: bits/buffer_t.hpp
3  * Description: C++ header-only wrapper for C-ABI data_t type
4  * $Copyright (c) 2015-2017 Software AG, Darmstadt, Germany and/or Software AG USA Inc., Reston, VA, USA, and/or its subsidiaries and/or its affiliates and/or their licensors.$
5  * Use, reproduction, transfer, publication or disclosure is prohibited except as specifically provided for in your License Agreement with Software AG
6  * @Version: $Id: buffer_t.hpp 309474 2017-06-13 10:13:34Z mben $
7  */
8 
15 #ifndef _DATAT_BITS_INCLUDE_TAG
16 #error Must only be included from sag_connectivity_cpp.hpp, use that instead
17 #endif
18 
27 class buffer_t: protected sag_underlying_buffer_t
28 {
29  friend class data_t;
30 public:
33  {
34  table = nullptr;
35  }
37  explicit buffer_t(size_t n);
38 
41  {
42  clear();
43  }
44 
48  {
49  buffer_t tmp;
50  // put us in tmp
51  swap(std::move(tmp));
52  // put other in us (and by extension the null tmp in other)
53  swap(std::move(other));
54  return *this;
55  }
57  buffer_t(buffer_t &&other)
58  {
59  table = nullptr;
60  swap(std::move(other));
61  }
63  void swap(buffer_t &&other)
64  {
65  std::swap(table, other.table);
66  }
67 
69  buffer_t copy() const;
70 
71 private:
73  buffer_t &operator=(const buffer_t &other) { abort(); }
75  buffer_t(const buffer_t &other) { abort(); }
76 public:
77 
78  typedef uint8_t *iterator;
79  typedef const uint8_t *const_iterator;
80 
82  bool operator==(const buffer_t &other) const
83  {
84  return (this->size() == other.size() &&
85  std::equal(this->cbegin(), this->cend(), other.begin())
86  );
87  }
89  bool operator!=(const buffer_t &other) const
90  {
91  return !(this->operator==(other));
92  }
93 
94  /* Iteration:
95  * begin() and end() methods, both const and non-const, forward and backward.
96  */
98  iterator begin() { return table?table->data:0; }
100  iterator end() { return table?table->data+table->length:0; }
102  const_iterator begin() const { return table?table->data:0; }
104  const_iterator end() const { return table?table->data+table->length:0; }
106  const_iterator cbegin() const { return table?table->data:0; }
108  const_iterator cend() const { return table?table->data+table->length:0; }
109 
111  size_t size() const { return table ? table->length : 0; }
113  bool empty() const { return 0 == size(); }
114 
117  uint8_t &operator[](size_t n)
118  {
119  if (n >= size()) throw std::runtime_error("Out of bounds");
120  return table->data[n];
121  }
124  const uint8_t &operator[](size_t n) const
125  {
126  if (n >= size()) throw std::runtime_error("Out of bounds");
127  return table->data[n];
128  }
132  void clear();
133 };
134 
bool empty() const
Returns true if the buffer is unallocated (size() == 0)
Definition: buffer_t.hpp:113
~buffer_t()
Free the underlying buffer contents.
Definition: buffer_t.hpp:40
bool operator==(const buffer_t &other) const
Returns true if both buffers are the same size and have the same contents.
Definition: buffer_t.hpp:82
buffer_t(buffer_t &&other)
Move constructor.
Definition: buffer_t.hpp:57
buffer_t & operator=(buffer_t &&other)
Move assignment.
Definition: buffer_t.hpp:47
size_t size() const
Returns the size of the buffer.
Definition: buffer_t.hpp:111
const_iterator end() const
Forward const_iterator end.
Definition: buffer_t.hpp:104
void swap(buffer_t &&other)
Swap the contents of this buffer and other.
Definition: buffer_t.hpp:63
A class that holds an untyped byte buffer.
Definition: buffer_t.hpp:27
bool operator!=(const buffer_t &other) const
Returns true if both buffers are different sizes or have different contents.
Definition: buffer_t.hpp:89
iterator begin()
Forward iterator begin.
Definition: buffer_t.hpp:98
buffer_t()
Construct an empty buffer.
Definition: buffer_t.hpp:32
uint8_t & operator[](size_t n)
Return a reference to the byte at the given offset.
Definition: buffer_t.hpp:117
const_iterator cend() const
Forward const_iterator end.
Definition: buffer_t.hpp:108
iterator end()
Forward iterator end.
Definition: buffer_t.hpp:100
A variant type which can be one of the following:
Definition: data_t.hpp:42
buffer_t copy() const
Return a deep copy of this buffer.
const_iterator begin() const
Forward const_iterator begin.
Definition: buffer_t.hpp:102
const uint8_t & operator[](size_t n) const
Return a reference the byte at the given offset.
Definition: buffer_t.hpp:124
void clear()
Free the underlying buffer;.
const_iterator cbegin() const
Forward const_iterator begin.
Definition: buffer_t.hpp:106