00001 #ifndef PROTON_BYTE_ARRAY_HPP
00002 #define PROTON_BYTE_ARRAY_HPP
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "./internal/export.hpp"
00024 #include "./internal/comparable.hpp"
00025 #include "./types_fwd.hpp"
00026
00027 #include <proton/type_compat.h>
00028
00029 #include <algorithm>
00030 #include <iterator>
00031
00034
00035 namespace proton {
00036
00037 namespace internal {
00038 PN_CPP_EXTERN void print_hex(std::ostream& o, const uint8_t* p, size_t n);
00039 }
00040
00045 template <size_t N> class byte_array : private internal::comparable<byte_array<N> > {
00046 public:
00049 typedef uint8_t value_type;
00050 typedef value_type* pointer;
00051 typedef const value_type* const_pointer;
00052 typedef value_type& reference;
00053 typedef const value_type& const_reference;
00054 typedef value_type* iterator;
00055 typedef const value_type* const_iterator;
00056 typedef std::size_t size_type;
00057 typedef std::ptrdiff_t difference_type;
00058 typedef std::reverse_iterator<iterator> reverse_iterator;
00059 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
00061
00063 byte_array() { std::fill(bytes_, bytes_+N, '\0'); }
00064
00066 static size_t size() { return N; }
00067
00070 value_type* begin() { return bytes_; }
00071 value_type* end() { return bytes_+N; }
00072 value_type& operator[](size_t i) { return bytes_[i]; }
00073
00074 const value_type* begin() const { return bytes_; }
00075 const value_type* end() const { return bytes_+N; }
00076 const value_type& operator[](size_t i) const { return bytes_[i]; }
00078
00081 friend bool operator==(const byte_array& x, const byte_array& y) {
00082 return std::equal(x.begin(), x.end(), y.begin());
00083 }
00084
00085 friend bool operator<(const byte_array& x, const byte_array& y) {
00086 return std::lexicographical_compare(x.begin(), x.end(), y.begin(), y.end());
00087 }
00089
00091 friend std::ostream& operator<<(std::ostream& o, const byte_array& b) {
00092 internal::print_hex(o, b.begin(), b.size());
00093 return o;
00094 }
00095
00096 private:
00097 value_type bytes_[N];
00098 };
00099
00100 }
00101
00102 #endif // PROTON_BYTE_ARRAY_HPP