/* * * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. * */ #include "proton/map.hpp" #include "proton/annotation_key.hpp" #include "proton/scalar.hpp" #include "proton/value.hpp" #include "proton/codec/decoder.hpp" #include "proton/codec/encoder.hpp" #include "proton/codec/map.hpp" #include #include // IMPLEMENTATION NOTES: // - if (map_.get()) then *map_ is the authority and value_ is empty() // - cache() ensures that *map_ is up to date and value_ is cleared. // - flush() ensures value_ is up to date and map_ is cleared. namespace proton { // use std::map as the actual map implementation type template class map_type_impl : public std::map { // Inherit constructors from std::map for convenience using std::map::map; public: map_type_impl() = default; map_type_impl(const std::map& x) : std::map(x) {}; }; template map::map() = default; template map::map(const map& x) { *this = x; } template map::map(const std::initializer_list>& x) : map_(new map_type(x)) { } template map::map(const std::map& x) : map_(new map_type(x)) { } template map& map::operator=(const std::map& x) { map_.reset(new map_type(x)); return *this; } template map::map(pn_data_t *d) : value_(d) { // NOTE: for internal use. Don't verify that the data is valid here as that // would forcibly decode message maps immediately, we want to decode on-demand. } template PN_CPP_EXTERN void swap(map& x, map& y) { using namespace std; swap(x.map_, y.map_); swap(x.value_, y.value_); } template map& map::operator=(const map& x) { if (&x != this) { map_.reset(x.map_.get() ? new map_type(*x.map_) : 0); value_ = x.value_; } return *this; } template map::map(map&& x) : map_(std::move(x.map_)), value_(std::move(x.value_)) {} template map& map::operator=(map&& x) { if (&x != this) { map_.reset(x.map_.release()); value_ = std::move(x.value_); } return *this; } template map::~map() = default; // Make sure map_ is valid template typename map::map_type& map::cache() const { if (!map_) { map_.reset(new map_type); if (!value_.empty()) { proton::get(value_, *map_); value_.clear(); } } return *map_; } // Make sure value_ is valid template value& map::flush() const { if (map_.get()) { value_ = *map_; map_.reset(); } else if (value_.empty()) { // Must contain an empty map, not be an empty value. codec::encoder(value_) << codec::start::map() << codec::finish(); } return value_; } template void map::value(const proton::value& x) { if (x.empty()) { clear(); } else { std::unique_ptr tmp(new map_type); proton::get(x, *tmp); // Validate by decoding, may throw map_.reset(tmp.release()); value_.clear(); } } template proton::value& map::value() { return flush(); } template const proton::value& map::value() const { return flush(); } template T map::get(const K& k) const { if (this->empty()) return T(); typename map_type::const_iterator i = cache().find(k); if (i == map_->end()) return T(); return i->second; } template void map::put(const K& k, const T& v) { cache()[k] = v; } template size_t map::erase(const K& k) { if (this->empty()) { return 0; } else { return cache().erase(k); } } template bool map::exists(const K& k) const { return this->empty() ? 0 : cache().find(k) != cache().end(); } template size_t map::size() const { return this->empty() ? 0 : cache().size(); } template void map::clear() { map_.reset(); // Must invalidate the cache on clear() value_.clear(); } template bool map::empty() const { if (map_.get()) { return map_->empty(); } if (value_.empty()) { return true; } // We must decode the non-empty value to see if it is an empty map. return cache().empty(); } // Point to a different underlying pn_data_t, no copy template void map::reset(pn_data_t *d) { value_.reset(d); // Points to d, not copy of d. map_.reset(); // NOTE: for internal use. Don't verify that the data is valid here as that // would forcibly decode message maps immediately, we want to decode on-demand. } template PN_CPP_EXTERN proton::codec::decoder& operator>>(proton::codec::decoder& d, map& m) { m.map_.reset(); d >> m.value_; m.cache(); // Validate the value return d; } template PN_CPP_EXTERN proton::codec::encoder& operator<<(proton::codec::encoder& e, const map& m) { return e << m.value(); // Copy the value } // Force the necessary template instantiations so that the library exports the correct symbols template class PN_CPP_CLASS_EXTERN map; typedef map cm1; template PN_CPP_EXTERN void swap<>(cm1&, cm1&); template PN_CPP_EXTERN proton::codec::decoder& operator>> <>(proton::codec::decoder& d, cm1& m); template PN_CPP_EXTERN proton::codec::encoder& operator<< <>(proton::codec::encoder& e, const cm1& m); template class PN_CPP_CLASS_EXTERN map; typedef map cm2; template PN_CPP_EXTERN void swap<>(cm2&, cm2&); template PN_CPP_EXTERN proton::codec::decoder& operator>> <>(proton::codec::decoder& d, cm2& m); template PN_CPP_EXTERN proton::codec::encoder& operator<< <>(proton::codec::encoder& e, const cm2& m); template class PN_CPP_CLASS_EXTERN map; typedef map cm3; template PN_CPP_EXTERN void swap<>(cm3&, cm3&); template PN_CPP_EXTERN proton::codec::decoder& operator>> <>(proton::codec::decoder& d, cm3& m); template PN_CPP_EXTERN proton::codec::encoder& operator<< <>(proton::codec::encoder& e, const cm3& m); } // namespace proton