Repository: qpid-proton Updated Branches: refs/heads/master 5a1a32cbc -> fc82f55b8
PROTON-1214: [C++ binding] Very simple implementation of cached_map to get correct API Project: http://git-wip-us.apache.org/repos/asf/qpid-proton/repo Commit: http://git-wip-us.apache.org/repos/asf/qpid-proton/commit/676a2a88 Tree: http://git-wip-us.apache.org/repos/asf/qpid-proton/tree/676a2a88 Diff: http://git-wip-us.apache.org/repos/asf/qpid-proton/diff/676a2a88 Branch: refs/heads/master Commit: 676a2a88168ef099252fb4bb952ba51de99bdd16 Parents: 5a1a32c Author: Andrew Stitcher <[email protected]> Authored: Fri May 20 18:27:48 2016 -0400 Committer: Andrew Stitcher <[email protected]> Committed: Fri May 27 15:11:00 2016 -0400 ---------------------------------------------------------------------- examples/cpp/selected_recv.cpp | 2 +- .../cpp/include/proton/internal/cached_map.hpp | 97 ++++++++++++++++++++ .../bindings/cpp/include/proton/message.hpp | 8 +- proton-c/bindings/cpp/include/proton/source.hpp | 12 ++- proton-c/bindings/cpp/src/message_test.cpp | 28 +++--- 5 files changed, 124 insertions(+), 23 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/676a2a88/examples/cpp/selected_recv.cpp ---------------------------------------------------------------------- diff --git a/examples/cpp/selected_recv.cpp b/examples/cpp/selected_recv.cpp index c7a63d9..2c1a6b6 100644 --- a/examples/cpp/selected_recv.cpp +++ b/examples/cpp/selected_recv.cpp @@ -47,7 +47,7 @@ namespace { << proton::binary(selector_str) << proton::codec::finish(); // In our case, the map has this one element - map[filter_key] = filter_value; + map.put(filter_key, filter_value); opts.filters(map); } } http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/676a2a88/proton-c/bindings/cpp/include/proton/internal/cached_map.hpp ---------------------------------------------------------------------- diff --git a/proton-c/bindings/cpp/include/proton/internal/cached_map.hpp b/proton-c/bindings/cpp/include/proton/internal/cached_map.hpp new file mode 100644 index 0000000..ced52c6 --- /dev/null +++ b/proton-c/bindings/cpp/include/proton/internal/cached_map.hpp @@ -0,0 +1,97 @@ +#ifndef PROTON_CPP_CACHED_MAP_H +#define PROTON_CPP_CACHED_MAP_H + +/* + * + * 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 "../value.hpp" +#include "../codec/decoder.hpp" +#include "../codec/encoder.hpp" + +#include "./config.hpp" +#include "./pn_unique_ptr.hpp" + +#include <map> + +namespace proton { +namespace internal { + +/// A convenience class to view and manage AMQP map data contained in +/// a proton::value. An internal cache of the map data is created as +/// needed. If desired, a std::map can be extracted from or inserted +/// into the cached_map value directly. +template <class key_type, class value_type> +class cached_map { + typedef std::map<key_type, value_type> map_type; + public: + +#if PN_CPP_HAS_DEFAULTED_FUNCTIONS + cached_map() = default; + cached_map(const cached_map&) = default; + cached_map& operator=(const cached_map&) = default; + cached_map(cached_map&&) = default; + cached_map& operator=(cached_map&&) = default; + ~cached_map() = default; +#endif + + value_type get(const key_type& k) const { + typename map_type::const_iterator i = map_.find(k); + if ( i==map_.end() ) return value_type(); + return i->second; + } + void put(const key_type& k, const value_type& v) { + map_[k] = v; + } + size_t erase(const key_type& k) { + return map_.erase(k); + } + bool exists(const key_type& k) const { + return map_.count(k) > 0; + } + + size_t size() { + return map_.size(); + } + void clear() { + map_.clear(); + } + bool empty() { + return map_.empty(); + } + + /// @cond INTERNAL + private: + map_type map_; + /// @endcond + + friend proton::codec::decoder& operator>>(proton::codec::decoder& d, cached_map& m) { + return d >> m.map_; + } + friend proton::codec::encoder& operator<<(proton::codec::encoder& e, const cached_map& m) { + return e << m.map_; + } +}; + + +} +} + +#endif // PROTON_CPP_CACHED_MAP_H http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/676a2a88/proton-c/bindings/cpp/include/proton/message.hpp ---------------------------------------------------------------------- diff --git a/proton-c/bindings/cpp/include/proton/message.hpp b/proton-c/bindings/cpp/include/proton/message.hpp index 754b39f..4a562f7 100644 --- a/proton-c/bindings/cpp/include/proton/message.hpp +++ b/proton-c/bindings/cpp/include/proton/message.hpp @@ -27,9 +27,11 @@ #include "./duration.hpp" #include "./internal/export.hpp" #include "./message_id.hpp" -#include "./internal/pn_unique_ptr.hpp" #include "./value.hpp" +#include "./internal/cached_map.hpp" +#include "./internal/pn_unique_ptr.hpp" + #include <string> #include <vector> #include <utility> @@ -50,11 +52,11 @@ class message { public: /// **Experimental** - A map of string keys and AMQP scalar /// values. - typedef std::map<std::string, scalar> property_map; + class property_map : public internal::cached_map<std::string, scalar> {}; /// **Experimental** - A map of AMQP annotation keys and AMQP /// values. - typedef std::map<annotation_key, value> annotation_map; + class annotation_map : public internal::cached_map<annotation_key, value> {}; /// Create an empty message. PN_CPP_EXTERN message(); http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/676a2a88/proton-c/bindings/cpp/include/proton/source.hpp ---------------------------------------------------------------------- diff --git a/proton-c/bindings/cpp/include/proton/source.hpp b/proton-c/bindings/cpp/include/proton/source.hpp index 0c0998c..de60079 100644 --- a/proton-c/bindings/cpp/include/proton/source.hpp +++ b/proton-c/bindings/cpp/include/proton/source.hpp @@ -22,12 +22,14 @@ * */ -#include "./internal/export.hpp" -#include "./internal/object.hpp" #include "./value.hpp" #include "./terminus.hpp" #include "./codec/map.hpp" +#include "./internal/cached_map.hpp" +#include "./internal/export.hpp" +#include "./internal/object.hpp" + #include <string> namespace proton { @@ -42,7 +44,7 @@ class source : public terminus { public: /// **Experimental** - A map of AMQP symbol keys and filter /// specifiers. - typedef std::map<symbol, value> filter_map; + class filter_map : public internal::cached_map<symbol, value> {}; /// Create an empty source. source() : terminus() {} @@ -53,10 +55,10 @@ class source : public terminus { // it. /// Unspecified UNSPECIFIED = PN_DIST_MODE_UNSPECIFIED, - /// Once transferred, the message remains available to ther links + /// Once transferred, the message remains available to other links COPY = PN_DIST_MODE_COPY, /// Once transferred, the message is unavailable to other links - MOVE = PN_DIST_MODE_MOVE + MOVE = PN_DIST_MODE_MOVE }; using terminus::durability_mode; http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/676a2a88/proton-c/bindings/cpp/src/message_test.cpp ---------------------------------------------------------------------- diff --git a/proton-c/bindings/cpp/src/message_test.cpp b/proton-c/bindings/cpp/src/message_test.cpp index 2379af7..0d9e7cf 100644 --- a/proton-c/bindings/cpp/src/message_test.cpp +++ b/proton-c/bindings/cpp/src/message_test.cpp @@ -110,30 +110,30 @@ void test_message_maps() { ASSERT(m.message_annotations().empty()); ASSERT(m.delivery_annotations().empty()); - m.properties()["foo"] = 12; - m.delivery_annotations()["bar"] = "xyz"; + m.properties().put("foo", 12); + m.delivery_annotations().put("bar", "xyz"); - m.message_annotations()[23] = "23"; - ASSERT_EQUAL(m.properties()["foo"], scalar(12)); - ASSERT_EQUAL(m.delivery_annotations()["bar"], scalar("xyz")); - ASSERT_EQUAL(m.message_annotations()[23], scalar("23")); + m.message_annotations().put(23, "23"); + ASSERT_EQUAL(m.properties().get("foo"), scalar(12)); + ASSERT_EQUAL(m.delivery_annotations().get("bar"), scalar("xyz")); + ASSERT_EQUAL(m.message_annotations().get(23), scalar("23")); message m2(m); - ASSERT_EQUAL(m2.properties()["foo"], scalar(12)); - ASSERT_EQUAL(m2.delivery_annotations()["bar"], scalar("xyz")); - ASSERT_EQUAL(m2.message_annotations()[23], scalar("23")); + ASSERT_EQUAL(m2.properties().get("foo"), scalar(12)); + ASSERT_EQUAL(m2.delivery_annotations().get("bar"), scalar("xyz")); + ASSERT_EQUAL(m2.message_annotations().get(23), scalar("23")); - m.properties()["foo"] = "newfoo"; - m.delivery_annotations()[24] = 1000; + m.properties().put("foo","newfoo"); + m.delivery_annotations().put(24, 1000); m.message_annotations().erase(23); m2 = m; ASSERT_EQUAL(1u, m2.properties().size()); - ASSERT_EQUAL(m2.properties()["foo"], scalar("newfoo")); + ASSERT_EQUAL(m2.properties().get("foo"), scalar("newfoo")); ASSERT_EQUAL(2u, m2.delivery_annotations().size()); - ASSERT_EQUAL(m2.delivery_annotations()["bar"], scalar("xyz")); - ASSERT_EQUAL(m2.delivery_annotations()[24], scalar(1000)); + ASSERT_EQUAL(m2.delivery_annotations().get("bar"), scalar("xyz")); + ASSERT_EQUAL(m2.delivery_annotations().get(24), scalar(1000)); ASSERT(m2.message_annotations().empty()); } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
