Qpid Proton C++  0.14.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
byte_array.hpp
1 #ifndef PROTON_BYTE_ARRAY_HPP
2 #define PROTON_BYTE_ARRAY_HPP
3 
4 /*
5  * Licensed to the Apache Software Foundation (ASF) under one
6  * or more contributor license agreements. See the NOTICE file
7  * distributed with this work for additional information
8  * regarding copyright ownership. The ASF licenses this file
9  * to you under the Apache License, Version 2.0 (the
10  * "License"); you may not use this file except in compliance
11  * with the License. You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing,
16  * software distributed under the License is distributed on an
17  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18  * KIND, either express or implied. See the License for the
19  * specific language governing permissions and limitations
20  * under the License.
21  */
22 
23 #include "./internal/export.hpp"
24 #include "./internal/comparable.hpp"
25 #include "./types_fwd.hpp"
26 
27 #include <algorithm>
28 #include <iterator>
29 
30 namespace proton {
31 
32 namespace internal {
33 PN_CPP_EXTERN void print_hex(std::ostream& o, const uint8_t* p, size_t n);
34 }
35 
40 template <size_t N> class byte_array : private internal::comparable<byte_array<N> > {
41  public:
44  typedef uint8_t value_type;
45  typedef value_type* pointer;
46  typedef const value_type* const_pointer;
47  typedef value_type& reference;
48  typedef const value_type& const_reference;
49  typedef value_type* iterator;
50  typedef const value_type* const_iterator;
51  typedef std::size_t size_type;
52  typedef std::ptrdiff_t difference_type;
53  typedef std::reverse_iterator<iterator> reverse_iterator;
54  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
56 
58  byte_array() { std::fill(bytes_, bytes_+N, '\0'); }
59 
61  static size_t size() { return N; }
62 
65  value_type* begin() { return bytes_; }
66  value_type* end() { return bytes_+N; }
67  value_type& operator[](size_t i) { return bytes_[i]; }
68 
69  const value_type* begin() const { return bytes_; }
70  const value_type* end() const { return bytes_+N; }
71  const value_type& operator[](size_t i) const { return bytes_[i]; }
73 
76  friend bool operator==(const byte_array& x, const byte_array& y) {
77  return std::equal(x.begin(), x.end(), y.begin());
78  }
79 
80  friend bool operator<(const byte_array& x, const byte_array& y) {
81  return std::lexicographical_compare(x.begin(), x.end(), y.begin(), y.end());
82  }
84 
86  friend std::ostream& operator<<(std::ostream& o, const byte_array& b) {
87  internal::print_hex(o, b.begin(), b.size());
88  return o;
89  }
90 
91  private:
92  value_type bytes_[N];
93 };
94 
95 } // proton
96 
97 #endif // PROTON_BYTE_ARRAY_HPP
static size_t size()
Size of the array.
Definition: byte_array.hpp:61
byte_array()
Zero-initialized byte array.
Definition: byte_array.hpp:58
Forward declarations for all the C++ types used by Proton to represent AMQP types.
friend std::ostream & operator<<(std::ostream &o, const byte_array &b)
Print byte array in hex.
Definition: byte_array.hpp:86
Arbitrary fixed-size data.
Definition: byte_array.hpp:40