proton/byte_array.hpp

Go to the documentation of this file.
00001 #ifndef PROTON_BYTE_ARRAY_HPP
00002 #define PROTON_BYTE_ARRAY_HPP
00003 
00004 /*
00005  * Licensed to the Apache Software Foundation (ASF) under one
00006  * or more contributor license agreements.  See the NOTICE file
00007  * distributed with this work for additional information
00008  * regarding copyright ownership.  The ASF licenses this file
00009  * to you under the Apache License, Version 2.0 (the
00010  * "License"); you may not use this file except in compliance
00011  * with the License.  You may obtain a copy of the License at
00012  *
00013  *   http://www.apache.org/licenses/LICENSE-2.0
00014  *
00015  * Unless required by applicable law or agreed to in writing,
00016  * software distributed under the License is distributed on an
00017  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
00018  * KIND, either express or implied.  See the License for the
00019  * specific language governing permissions and limitations
00020  * under the License.
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 } // proton
00101 
00102 #endif // PROTON_BYTE_ARRAY_HPP

Generated on 1 Jun 2020 for Qpid Proton C++ by  doxygen 1.6.1