Qpid Proton C++ API  0.37.0
byte_array.hpp
Go to the documentation of this file.
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 <proton/type_compat.h>
28 
29 #include <algorithm>
30 #include <iterator>
31 
34 
35 namespace proton {
36 
37 namespace internal {
38 PN_CPP_EXTERN void print_hex(std::ostream& o, const uint8_t* p, size_t n);
39 }
40 
45 template <size_t N> class byte_array : private internal::comparable<byte_array<N> > {
46  public:
49  typedef uint8_t value_type;
50  typedef value_type* pointer;
51  typedef const value_type* const_pointer;
52  typedef value_type& reference;
53  typedef const value_type& const_reference;
54  typedef value_type* iterator;
55  typedef const value_type* const_iterator;
56  typedef std::size_t size_type;
57  typedef std::ptrdiff_t difference_type;
58  typedef std::reverse_iterator<iterator> reverse_iterator;
59  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
61 
63  byte_array() { std::fill(bytes_, bytes_+N, '\0'); }
64 
66  static size_t size() { return N; }
67 
70  value_type* begin() { return bytes_; }
71  value_type* end() { return bytes_+N; }
72  value_type& operator[](size_t i) { return bytes_[i]; }
73 
74  const value_type* begin() const { return bytes_; }
75  const value_type* end() const { return bytes_+N; }
76  const value_type& operator[](size_t i) const { return bytes_[i]; }
78 
81  friend bool operator==(const byte_array& x, const byte_array& y) {
82  return std::equal(x.begin(), x.end(), y.begin());
83  }
84 
85  friend bool operator<(const byte_array& x, const byte_array& y) {
86  return std::lexicographical_compare(x.begin(), x.end(), y.begin(), y.end());
87  }
89 
91  friend std::ostream& operator<<(std::ostream& o, const byte_array& b) {
92  internal::print_hex(o, b.begin(), b.size());
93  return o;
94  }
95 
96  private:
97  value_type bytes_[N];
98 };
99 
100 } // proton
101 
102 #endif // PROTON_BYTE_ARRAY_HPP
Arbitrary fixed-size data.
Definition: byte_array.hpp:45
byte_array()
Zero-initialized byte array.
Definition: byte_array.hpp:63
static size_t size()
Size of the array.
Definition: byte_array.hpp:66
friend std::ostream & operator<<(std::ostream &o, const byte_array &b)
Print byte array in hex.
Definition: byte_array.hpp:91
The main Proton namespace.
Definition: annotation_key.hpp:33
Forward declarations for Proton types used to represent AMQP types.