| 1 |
/* |
| 2 |
* |
| 3 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 4 |
* or more contributor license agreements. See the NOTICE file |
| 5 |
* distributed with this work for additional information |
| 6 |
* regarding copyright ownership. The ASF licenses this file |
| 7 |
* to you under the Apache License, Version 2.0 (the |
| 8 |
* "License"); you may not use this file except in compliance |
| 9 |
* with the License. You may obtain a copy of the License at |
| 10 |
* |
| 11 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
* |
| 13 |
* Unless required by applicable law or agreed to in writing, |
| 14 |
* software distributed under the License is distributed on an |
| 15 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 |
* KIND, either express or implied. See the License for the |
| 17 |
* specific language governing permissions and limitations |
| 18 |
* under the License. |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "proton/map.hpp" |
| 23 |
|
| 24 |
#include "proton/annotation_key.hpp" |
| 25 |
#include "proton/scalar.hpp" |
| 26 |
#include "proton/value.hpp" |
| 27 |
#include "proton/codec/decoder.hpp" |
| 28 |
#include "proton/codec/encoder.hpp" |
| 29 |
#include "proton/codec/map.hpp" |
| 30 |
|
| 31 |
#include <map> |
| 32 |
#include <string> |
| 33 |
|
| 34 |
// IMPLEMENTATION NOTES: |
| 35 |
// - if (map_.get()) then *map_ is the authority and value_ is empty() |
| 36 |
// - cache() ensures that *map_ is up to date and value_ is cleared. |
| 37 |
// - flush() ensures value_ is up to date and map_ is cleared. |
| 38 |
|
| 39 |
namespace proton { |
| 40 |
|
| 41 |
// use std::map as the actual map implementation type |
| 42 |
template <class K, class T> |
| 43 |
class map_type_impl : public std::map<K, T> { |
| 44 |
// Inherit constructors from std::map for convenience |
| 45 |
using std::map<K,T>::map; |
| 46 |
|
| 47 |
public: |
| 48 |
map_type_impl() = default; |
| 49 |
map_type_impl(const std::map<K, T>& x) : std::map<K, T>(x) {}; |
| 50 |
}; |
| 51 |
|
| 52 |
template <class K, class T> |
| 53 |
map<K,T>::map() = default; |
| 54 |
|
| 55 |
template <class K, class T> |
| 56 |
map<K,T>::map(const map& x) { *this = x; } |
| 57 |
|
| 58 |
template <class K, class T> |
| 59 |
map<K,T>::map(const std::initializer_list<std::pair<const K, T>>& x) : map_(new map_type(x)) { |
| 60 |
} |
| 61 |
|
| 62 |
template <class K, class T> |
| 63 |
map<K,T>::map(const std::map<K, T>& x) : map_(new map_type(x)) { |
| 64 |
} |
| 65 |
|
| 66 |
template <class K, class T> |
| 67 |
map<K,T>& map<K,T>::operator=(const std::map<K, T>& x) { |
| 68 |
map_.reset(new map_type(x)); |
| 69 |
return *this; |
| 70 |
} |
| 71 |
|
| 72 |
template <class K, class T> |
| 73 |
map<K,T>::map(pn_data_t *d) : value_(d) { |
| 74 |
// NOTE: for internal use. Don't verify that the data is valid here as that |
| 75 |
// would forcibly decode message maps immediately, we want to decode on-demand. |
| 76 |
} |
| 77 |
|
| 78 |
template <class K, class T> |
| 79 |
PN_CPP_EXTERN void swap(map<K,T>& x, map<K,T>& y) { |
| 80 |
using namespace std; |
| 81 |
swap(x.map_, y.map_); |
| 82 |
swap(x.value_, y.value_); |
| 83 |
} |
| 84 |
|
| 85 |
template <class K, class T> |
| 86 |
map<K,T>& map<K,T>::operator=(const map& x) { |
| 87 |
if (&x != this) { |
| 88 |
map_.reset(x.map_.get() ? new map_type(*x.map_) : 0); |
| 89 |
value_ = x.value_; |
| 90 |
} |
| 91 |
return *this; |
| 92 |
} |
| 93 |
|
| 94 |
template <class K, class T> |
| 95 |
map<K,T>::map(map&& x) : |
| 96 |
map_(std::move(x.map_)), value_(std::move(x.value_)) {} |
| 97 |
|
| 98 |
template <class K, class T> |
| 99 |
map<K,T>& map<K,T>::operator=(map&& x) { |
| 100 |
if (&x != this) { |
| 101 |
map_.reset(x.map_.release()); |
| 102 |
value_ = std::move(x.value_); |
| 103 |
} |
| 104 |
return *this; |
| 105 |
} |
| 106 |
|
| 107 |
template <class K, class T> |
| 108 |
map<K,T>::~map() = default; |
| 109 |
|
| 110 |
// Make sure map_ is valid |
| 111 |
template <class K, class T> |
| 112 |
typename map<K,T>::map_type& map<K,T>::cache() const { |
| 113 |
if (!map_) { |
| 114 |
map_.reset(new map_type); |
| 115 |
if (!value_.empty()) { |
| 116 |
proton::get(value_, *map_); |
| 117 |
value_.clear(); |
| 118 |
} |
| 119 |
} |
| 120 |
return *map_; |
| 121 |
} |
| 122 |
|
| 123 |
// Make sure value_ is valid |
| 124 |
template <class K, class T> |
| 125 |
value& map<K,T>::flush() const { |
| 126 |
if (map_.get()) { |
| 127 |
value_ = *map_; |
| 128 |
map_.reset(); |
| 129 |
} else if (value_.empty()) { |
| 130 |
// Must contain an empty map, not be an empty value. |
| 131 |
codec::encoder(value_) << codec::start::map() << codec::finish(); |
| 132 |
} |
| 133 |
return value_; |
| 134 |
} |
| 135 |
|
| 136 |
template <class K, class T> |
| 137 |
void map<K,T>::value(const proton::value& x) { |
| 138 |
if (x.empty()) { |
| 139 |
clear(); |
| 140 |
} else { |
| 141 |
std::unique_ptr<map_type> tmp(new map_type); |
| 142 |
proton::get(x, *tmp); // Validate by decoding, may throw |
| 143 |
map_.reset(tmp.release()); |
| 144 |
value_.clear(); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
template <class K, class T> |
| 149 |
proton::value& map<K,T>::value() { return flush(); } |
| 150 |
|
| 151 |
template <class K, class T> |
| 152 |
const proton::value& map<K,T>::value() const { return flush(); } |
| 153 |
|
| 154 |
template <class K, class T> |
| 155 |
T map<K,T>::get(const K& k) const { |
| 156 |
if (this->empty()) return T(); |
| 157 |
typename map_type::const_iterator i = cache().find(k); |
| 158 |
if (i == map_->end()) return T(); |
| 159 |
return i->second; |
| 160 |
} |
| 161 |
|
| 162 |
template <class K, class T> |
| 163 |
void map<K,T>::put(const K& k, const T& v) { |
| 164 |
cache()[k] = v; |
| 165 |
} |
| 166 |
|
| 167 |
template <class K, class T> |
| 168 |
size_t map<K,T>::erase(const K& k) { |
| 169 |
if (this->empty()) { |
| 170 |
return 0; |
| 171 |
} else { |
| 172 |
return cache().erase(k); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
template <class K, class T> |
| 177 |
bool map<K,T>::exists(const K& k) const { |
| 178 |
return this->empty() ? 0 : cache().find(k) != cache().end(); |
| 179 |
} |
| 180 |
|
| 181 |
template <class K, class T> |
| 182 |
size_t map<K,T>::size() const { |
| 183 |
return this->empty() ? 0 : cache().size(); |
| 184 |
} |
| 185 |
|
| 186 |
template <class K, class T> |
| 187 |
void map<K,T>::clear() { |
| 188 |
map_.reset(); // Must invalidate the cache on clear() |
| 189 |
value_.clear(); |
| 190 |
} |
| 191 |
|
| 192 |
template <class K, class T> |
| 193 |
bool map<K,T>::empty() const { |
| 194 |
if (map_.get()) { |
| 195 |
return map_->empty(); |
| 196 |
} |
| 197 |
if (value_.empty()) { |
| 198 |
return true; |
| 199 |
} |
| 200 |
// We must decode the non-empty value to see if it is an empty map. |
| 201 |
return cache().empty(); |
| 202 |
} |
| 203 |
|
| 204 |
// Point to a different underlying pn_data_t, no copy |
| 205 |
template <class K, class T> |
| 206 |
void map<K,T>::reset(pn_data_t *d) { |
| 207 |
value_.reset(d); // Points to d, not copy of d. |
| 208 |
map_.reset(); |
| 209 |
// NOTE: for internal use. Don't verify that the data is valid here as that |
| 210 |
// would forcibly decode message maps immediately, we want to decode on-demand. |
| 211 |
} |
| 212 |
|
| 213 |
template <class K, class T> |
| 214 |
PN_CPP_EXTERN proton::codec::decoder& operator>>(proton::codec::decoder& d, map<K,T>& m) |
| 215 |
{ |
| 216 |
m.map_.reset(); |
| 217 |
d >> m.value_; |
| 218 |
m.cache(); // Validate the value |
| 219 |
return d; |
| 220 |
} |
| 221 |
|
| 222 |
template <class K, class T> |
| 223 |
PN_CPP_EXTERN proton::codec::encoder& operator<<(proton::codec::encoder& e, const map<K,T>& m) |
| 224 |
{ |
| 225 |
return e << m.value(); // Copy the value |
| 226 |
} |
| 227 |
|
| 228 |
// Force the necessary template instantiations so that the library exports the correct symbols |
| 229 |
template class PN_CPP_CLASS_EXTERN map<std::string, scalar>; |
| 230 |
typedef map<std::string, scalar> cm1; |
| 231 |
template PN_CPP_EXTERN void swap<>(cm1&, cm1&); |
| 232 |
template PN_CPP_EXTERN proton::codec::decoder& operator>> <>(proton::codec::decoder& d, cm1& m); |
| 233 |
template PN_CPP_EXTERN proton::codec::encoder& operator<< <>(proton::codec::encoder& e, const cm1& m); |
| 234 |
|
| 235 |
template class PN_CPP_CLASS_EXTERN map<annotation_key, value>; |
| 236 |
typedef map<annotation_key, value> cm2; |
| 237 |
template PN_CPP_EXTERN void swap<>(cm2&, cm2&); |
| 238 |
template PN_CPP_EXTERN proton::codec::decoder& operator>> <>(proton::codec::decoder& d, cm2& m); |
| 239 |
template PN_CPP_EXTERN proton::codec::encoder& operator<< <>(proton::codec::encoder& e, const cm2& m); |
| 240 |
|
| 241 |
template class PN_CPP_CLASS_EXTERN map<symbol, value>; |
| 242 |
typedef map<symbol, value> cm3; |
| 243 |
template PN_CPP_EXTERN void swap<>(cm3&, cm3&); |
| 244 |
template PN_CPP_EXTERN proton::codec::decoder& operator>> <>(proton::codec::decoder& d, cm3& m); |
| 245 |
template PN_CPP_EXTERN proton::codec::encoder& operator<< <>(proton::codec::encoder& e, const cm3& m); |
| 246 |
|
| 247 |
} // namespace proton |