Ice 3.7 C++11 API Reference
Buffer.h
Go to the documentation of this file.
1 //
2 // Copyright (c) ZeroC, Inc. All rights reserved.
3 //
4 
5 #ifndef ICE_BUFFER_H
6 #define ICE_BUFFER_H
7 
8 #include <Ice/Config.h>
9 
10 namespace IceInternal
11 {
12 
13 class ICE_API Buffer : private IceUtil::noncopyable
14 {
15 public:
16 
17  Buffer() : i(b.begin()) { }
18  Buffer(const Ice::Byte* beg, const Ice::Byte* end) : b(beg, end), i(b.begin()) { }
19  Buffer(const std::vector<Ice::Byte>& v) : b(v), i(b.begin()) { }
20  Buffer(Buffer& o, bool adopt) : b(o.b, adopt), i(b.begin()) { }
21 
22  void swapBuffer(Buffer&);
23 
24  class ICE_API Container : private IceUtil::noncopyable
25  {
26  public:
27 
28  //
29  // Standard vector-like operations.
30  //
31 
32  typedef Ice::Byte value_type;
33  typedef Ice::Byte* iterator;
34  typedef const Ice::Byte* const_iterator;
35  typedef Ice::Byte& reference;
36  typedef const Ice::Byte& const_reference;
37  typedef Ice::Byte* pointer;
38  typedef size_t size_type;
39 
40  Container();
41  Container(const_iterator, const_iterator);
42  Container(const std::vector<value_type>&);
43  Container(Container&, bool);
44 
45  ~Container();
46 
47  iterator begin()
48  {
49  return _buf;
50  }
51 
52  const_iterator begin() const
53  {
54  return _buf;
55  }
56 
57  iterator end()
58  {
59  return _buf + _size;
60  }
61 
62  const_iterator end() const
63  {
64  return _buf + _size;
65  }
66 
67  size_type size() const
68  {
69  return _size;
70  }
71 
72  bool empty() const
73  {
74  return !_size;
75  }
76 
77  void swap(Container&);
78 
79  void clear();
80 
81  void resize(size_type n) // Inlined for performance reasons.
82  {
83  if(n == 0)
84  {
85  clear();
86  }
87  else if(n > _capacity)
88  {
89  reserve(n);
90  }
91  _size = n;
92  }
93 
94  void reset()
95  {
96  if(_size > 0 && _size * 2 < _capacity)
97  {
98  //
99  // If the current buffer size is smaller than the
100  // buffer capacity, we shrink the buffer memory to the
101  // current size. This is to avoid holding onto too much
102  // memory if it's not needed anymore.
103  //
104  if(++_shrinkCounter > 2)
105  {
106  reserve(_size);
107  _shrinkCounter = 0;
108  }
109  }
110  else
111  {
112  _shrinkCounter = 0;
113  }
114  _size = 0;
115  }
116 
117  void push_back(value_type v)
118  {
119  resize(_size + 1);
120  _buf[_size - 1] = v;
121  }
122 
123  reference operator[](size_type n)
124  {
125  assert(n < _size);
126  return _buf[n];
127  }
128 
129  const_reference operator[](size_type n) const
130  {
131  assert(n < _size);
132  return _buf[n];
133  }
134 
135  private:
136 
137  Container(const Container&);
138  void operator=(const Container&);
139  void reserve(size_type);
140 
141  pointer _buf;
142  size_type _size;
143  size_type _capacity;
144  int _shrinkCounter;
145  bool _owned;
146  };
147 
148  Container b;
149  Container::iterator i;
150 };
151 
152 }
153 
154 #endif
ICE_API
#define ICE_API
Definition: Config.h:197
Ice::Byte
unsigned char Byte
The mapping for the Slice byte type.
Definition: Config.h:50
IceUtil::noncopyable
Definition: Config.h:313
Config.h