| 1 |
#ifndef PROTON_MAP_HPP |
| 2 |
#define PROTON_MAP_HPP |
| 3 |
|
| 4 |
/* |
| 5 |
* |
| 6 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 7 |
* or more contributor license agreements. See the NOTICE file |
| 8 |
* distributed with this work for additional information |
| 9 |
* regarding copyright ownership. The ASF licenses this file |
| 10 |
* to you under the Apache License, Version 2.0 (the |
| 11 |
* "License"); you may not use this file except in compliance |
| 12 |
* with the License. You may obtain a copy of the License at |
| 13 |
* |
| 14 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 15 |
* |
| 16 |
* Unless required by applicable law or agreed to in writing, |
| 17 |
* software distributed under the License is distributed on an |
| 18 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 19 |
* KIND, either express or implied. See the License for the |
| 20 |
* specific language governing permissions and limitations |
| 21 |
* under the License. |
| 22 |
* |
| 23 |
*/ |
| 24 |
|
| 25 |
#include "./value.hpp" |
| 26 |
|
| 27 |
#include <cstddef> |
| 28 |
#include <initializer_list> |
| 29 |
#include <map> |
| 30 |
#include <memory> |
| 31 |
|
| 32 |
/// @file |
| 33 |
/// @copybrief proton::map |
| 34 |
|
| 35 |
namespace proton { |
| 36 |
|
| 37 |
namespace codec { |
| 38 |
class decoder; |
| 39 |
class encoder; |
| 40 |
} |
| 41 |
|
| 42 |
template <class K, class T> |
| 43 |
class map_type_impl; |
| 44 |
|
| 45 |
template <class K, class T> |
| 46 |
class map; |
| 47 |
|
| 48 |
/// Decode from a proton::map |
| 49 |
template <class K, class T> |
| 50 |
PN_CPP_EXTERN proton::codec::decoder& operator>>(proton::codec::decoder& d, map<K,T>& m); |
| 51 |
/// Encode to a proton::map |
| 52 |
template <class K, class T> |
| 53 |
PN_CPP_EXTERN proton::codec::encoder& operator<<(proton::codec::encoder& e, const map<K,T>& m); |
| 54 |
/// Swap proton::map instances |
| 55 |
template <class K, class T> |
| 56 |
PN_CPP_EXTERN void swap(map<K,T>&, map<K,T>&); |
| 57 |
|
| 58 |
/// A collection of key-value pairs. |
| 59 |
/// |
| 60 |
/// Used to access standard AMQP property, annotation, and filter maps |
| 61 |
/// attached to proton::message and proton::source. |
| 62 |
/// |
| 63 |
/// This class provides only basic get() and put() operations for |
| 64 |
/// convenience. For more complicated uses (iteration, preserving |
| 65 |
/// order, and so on), convert the value to a standard C++ map type |
| 66 |
/// such as `std::map`. See @ref message_properties.cpp and @ref |
| 67 |
/// types_page. |
| 68 |
template <class K, class T> |
| 69 |
class PN_CPP_CLASS_EXTERN map { |
| 70 |
template <class M, class U=void> |
| 71 |
struct assignable_map : |
| 72 |
public std::enable_if<codec::is_encodable_map<M,K,T>::value, U> {}; |
| 73 |
|
| 74 |
public: |
| 75 |
/// Construct an empty map. |
| 76 |
PN_CPP_EXTERN map(); |
| 77 |
|
| 78 |
/// Copy a map. |
| 79 |
PN_CPP_EXTERN map(const map&); |
| 80 |
|
| 81 |
/// Copy a map. |
| 82 |
PN_CPP_EXTERN map& operator=(const map&); |
| 83 |
|
| 84 |
/// Copy a std::map. |
| 85 |
PN_CPP_EXTERN map(const std::map<K, T>&); |
| 86 |
|
| 87 |
/// Copy a std::map. |
| 88 |
PN_CPP_EXTERN map& operator=(const std::map<K, T>&); |
| 89 |
|
| 90 |
/// Initializer_list constructor. |
| 91 |
PN_CPP_EXTERN map(const std::initializer_list<std::pair<const K, T>>&); |
| 92 |
|
| 93 |
/// Move a map. |
| 94 |
PN_CPP_EXTERN map(map&&); |
| 95 |
|
| 96 |
/// Move a map. |
| 97 |
PN_CPP_EXTERN map& operator=(map&&); |
| 98 |
PN_CPP_EXTERN ~map(); |
| 99 |
|
| 100 |
/// Type-safe assign from a compatible map, for instance |
| 101 |
/// `std::map<K,T>`. See @ref types_page. |
| 102 |
template <class M> |
| 103 |
typename assignable_map<M, map&>::type operator=(const M& x) { value(x); return *this; } |
| 104 |
|
| 105 |
/// Copy from a proton::value. |
| 106 |
/// |
| 107 |
/// @throw proton::conversion_error if `x` does not contain a |
| 108 |
/// compatible map. |
| 109 |
PN_CPP_EXTERN void value(const value& x); |
| 110 |
|
| 111 |
/// Access as a proton::value containing an AMQP map. |
| 112 |
PN_CPP_EXTERN proton::value& value(); |
| 113 |
|
| 114 |
/// Access as a proton::value containing an AMQP map. |
| 115 |
PN_CPP_EXTERN const proton::value& value() const; |
| 116 |
|
| 117 |
/// Get the map entry for key `k`. Return `T()` if there is no |
| 118 |
/// such entry. |
| 119 |
PN_CPP_EXTERN T get(const K& k) const; |
| 120 |
|
| 121 |
/// Put a map entry for key `k`. |
| 122 |
PN_CPP_EXTERN void put(const K& k, const T& v); |
| 123 |
|
| 124 |
/// Erase the map entry at `k`. |
| 125 |
PN_CPP_EXTERN size_t erase(const K& k); |
| 126 |
|
| 127 |
/// True if the map has an entry for `k`. |
| 128 |
PN_CPP_EXTERN bool exists(const K& k) const; |
| 129 |
|
| 130 |
/// Get the number of map entries. |
| 131 |
PN_CPP_EXTERN size_t size() const; |
| 132 |
|
| 133 |
/// Remove all map entries. |
| 134 |
PN_CPP_EXTERN void clear(); |
| 135 |
|
| 136 |
/// True if the map has no entries. |
| 137 |
PN_CPP_EXTERN bool empty() const; |
| 138 |
|
| 139 |
/// @cond INTERNAL |
| 140 |
explicit map(pn_data_t*); |
| 141 |
void reset(pn_data_t*); |
| 142 |
/// @endcond |
| 143 |
|
| 144 |
private: |
| 145 |
typedef map_type_impl<K,T> map_type; |
| 146 |
mutable std::unique_ptr<map_type> map_; |
| 147 |
mutable proton::value value_; |
| 148 |
|
| 149 |
map_type& cache() const; |
| 150 |
proton::value& flush() const; |
| 151 |
|
| 152 |
/// @cond INTERNAL |
| 153 |
friend PN_CPP_EXTERN proton::codec::decoder& operator>> <>(proton::codec::decoder&, map&); |
| 154 |
friend PN_CPP_EXTERN proton::codec::encoder& operator<< <>(proton::codec::encoder&, const map&); |
| 155 |
friend PN_CPP_EXTERN void swap<>(map&, map&); |
| 156 |
/// @endcond |
| 157 |
}; |
| 158 |
|
| 159 |
} // proton |
| 160 |
|
| 161 |
#endif // PROTON_MAP_HPP |