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 <algorithm>
00028 #include <iterator>
00029
00030 namespace proton {
00031
00032 namespace internal {
00033 PN_CPP_EXTERN void print_hex(std::ostream& o, const uint8_t* p, size_t n);
00034 }
00035
00040 template <size_t N> class byte_array : private internal::comparable<byte_array<N> > {
00041 public:
00044 typedef uint8_t value_type;
00045 typedef value_type* pointer;
00046 typedef const value_type* const_pointer;
00047 typedef value_type& reference;
00048 typedef const value_type& const_reference;
00049 typedef value_type* iterator;
00050 typedef const value_type* const_iterator;
00051 typedef std::size_t size_type;
00052 typedef std::ptrdiff_t difference_type;
00053 typedef std::reverse_iterator<iterator> reverse_iterator;
00054 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
00056
00058 byte_array() { std::fill(bytes_, bytes_+N, '\0'); }
00059
00061 static size_t size() { return N; }
00062
00065 value_type* begin() { return bytes_; }
00066 value_type* end() { return bytes_+N; }
00067 value_type& operator[](size_t i) { return bytes_[i]; }
00068
00069 const value_type* begin() const { return bytes_; }
00070 const value_type* end() const { return bytes_+N; }
00071 const value_type& operator[](size_t i) const { return bytes_[i]; }
00073
00076 friend bool operator==(const byte_array& x, const byte_array& y) {
00077 return std::equal(x.begin(), x.end(), y.begin());
00078 }
00079
00080 friend bool operator<(const byte_array& x, const byte_array& y) {
00081 return std::lexicographical_compare(x.begin(), x.end(), y.begin(), y.end());
00082 }
00084
00086 friend std::ostream& operator<<(std::ostream& o, const byte_array& b) {
00087 internal::print_hex(o, b.begin(), b.size());
00088 return o;
00089 }
00090
00091 private:
00092 value_type bytes_[N];
00093 };
00094
00095 }
00096
00097 #endif // PROTON_BYTE_ARRAY_HPP