You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by ac...@apache.org on 2016/03/11 23:55:27 UTC

qpid-proton git commit: PROTON-1138: c++: fix broken binary data type.

Repository: qpid-proton
Updated Branches:
  refs/heads/master 4fa5fa41b -> 6260d5065


PROTON-1138: c++: fix broken binary data type.

- based on vector<char>
- scalar holds data for pn_bytes in a binary.
- byte_array also based on char.


Project: http://git-wip-us.apache.org/repos/asf/qpid-proton/repo
Commit: http://git-wip-us.apache.org/repos/asf/qpid-proton/commit/6260d506
Tree: http://git-wip-us.apache.org/repos/asf/qpid-proton/tree/6260d506
Diff: http://git-wip-us.apache.org/repos/asf/qpid-proton/diff/6260d506

Branch: refs/heads/master
Commit: 6260d50654672a168cef2e2daa4e6c8e2eef8463
Parents: 4fa5fa4
Author: Alan Conway <ac...@redhat.com>
Authored: Fri Mar 11 17:54:32 2016 -0500
Committer: Alan Conway <ac...@redhat.com>
Committed: Fri Mar 11 17:54:32 2016 -0500

----------------------------------------------------------------------
 proton-c/bindings/cpp/CMakeLists.txt            |  1 +
 proton-c/bindings/cpp/include/proton/binary.hpp | 28 +++++++---
 .../bindings/cpp/include/proton/byte_array.hpp  |  2 +-
 .../bindings/cpp/include/proton/decoder.hpp     |  4 +-
 proton-c/bindings/cpp/include/proton/scalar.hpp |  5 +-
 proton-c/bindings/cpp/src/binary.cpp            | 45 +++++++++++++++
 proton-c/bindings/cpp/src/decoder.cpp           | 59 ++++++--------------
 proton-c/bindings/cpp/src/scalar.cpp            | 25 +++++----
 proton-c/bindings/cpp/src/types_internal.hpp    | 12 +++-
 proton-c/bindings/cpp/src/value.cpp             |  4 +-
 proton-c/bindings/cpp/src/value_test.cpp        |  2 +-
 11 files changed, 116 insertions(+), 71 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/6260d506/proton-c/bindings/cpp/CMakeLists.txt
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/CMakeLists.txt b/proton-c/bindings/cpp/CMakeLists.txt
index 7c667bf..11b299d 100644
--- a/proton-c/bindings/cpp/CMakeLists.txt
+++ b/proton-c/bindings/cpp/CMakeLists.txt
@@ -26,6 +26,7 @@ include_directories(
 
 set(qpid-proton-cpp-source
   src/acceptor.cpp
+  src/binary.cpp
   src/scalar.cpp
   src/condition.cpp
   src/connection.cpp

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/6260d506/proton-c/bindings/cpp/include/proton/binary.hpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/include/proton/binary.hpp b/proton-c/bindings/cpp/include/proton/binary.hpp
index e673854..e907f47 100644
--- a/proton-c/bindings/cpp/include/proton/binary.hpp
+++ b/proton-c/bindings/cpp/include/proton/binary.hpp
@@ -19,19 +19,33 @@
  * under the License.
  */
 
-#include <string>
+#include <proton/export.hpp>
+#include <proton/types_fwd.hpp>
+
+#include <iosfwd>
+#include <vector>
 
 namespace proton {
 
-/// symbol is a std::string that represents the AMQP symbol type.
-/// A symbol can only contain 7-bit ASCII characters.
-///
-class binary : public std::string {
+/// Arbitrary binary data.
+class binary : public std::vector<char> {
   public:
-    explicit binary(const std::string& s=std::string()) : std::string(s) {}
-    explicit binary(const char* s) : std::string(s) {}
+    typedef std::vector<char> byte_vector;
+
+    explicit binary() : byte_vector() {}
+    explicit binary(size_t n) : byte_vector(n) {}
+    explicit binary(size_t n, char x) : byte_vector(n, x) {}
+    template <class Iter> binary(Iter first, Iter last) : byte_vector(first, last) {}
+    explicit binary(const std::string& s) : byte_vector(s.begin(), s.end()) {}
+
+    std::string str() const { return std::string(begin(), end()); }
+
+    binary& operator=(const binary& x) { byte_vector::operator=(x); return *this; }
+    binary& operator=(const std::string& x) { assign(x.begin(), x.end()); return *this; }
 };
 
+PN_CPP_EXTERN std::ostream& operator<<(std::ostream&, const binary&);
+
 }
 
 #endif // BINARY_HPP

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/6260d506/proton-c/bindings/cpp/include/proton/byte_array.hpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/include/proton/byte_array.hpp b/proton-c/bindings/cpp/include/proton/byte_array.hpp
index 29ad2ee..c2757fa 100644
--- a/proton-c/bindings/cpp/include/proton/byte_array.hpp
+++ b/proton-c/bindings/cpp/include/proton/byte_array.hpp
@@ -30,7 +30,7 @@ namespace proton {
 /// as an array of bytes.
 template <size_t N> class byte_array : private comparable<byte_array<N> > {
   public:
-    typedef uint8_t value_type;
+    typedef char value_type;
 
     /// Initially all 0.
     byte_array() { std::fill(bytes_, bytes_+N, '\0'); }

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/6260d506/proton-c/bindings/cpp/include/proton/decoder.hpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/include/proton/decoder.hpp b/proton-c/bindings/cpp/include/proton/decoder.hpp
index efa87e4..1df0bba 100644
--- a/proton-c/bindings/cpp/include/proton/decoder.hpp
+++ b/proton-c/bindings/cpp/include/proton/decoder.hpp
@@ -81,6 +81,8 @@ class decoder : public data {
     PN_CPP_EXTERN decoder& operator>>(decimal128&);
     PN_CPP_EXTERN decoder& operator>>(uuid&);
     PN_CPP_EXTERN decoder& operator>>(std::string&);
+    PN_CPP_EXTERN decoder& operator>>(symbol&);
+    PN_CPP_EXTERN decoder& operator>>(binary&);
     PN_CPP_EXTERN decoder& operator>>(message_id&);
     PN_CPP_EXTERN decoder& operator>>(annotation_key&);
     PN_CPP_EXTERN decoder& operator>>(scalar&);
@@ -153,7 +155,7 @@ class decoder : public data {
 
   private:
     type_id pre_get();
-    template <class T, class U> void extract(T& x, U (*get)(pn_data_t*));
+    template <class T, class U> decoder& extract(T& x, U (*get)(pn_data_t*));
 
   friend class message;
 };

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/6260d506/proton-c/bindings/cpp/include/proton/scalar.hpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/include/proton/scalar.hpp b/proton-c/bindings/cpp/include/proton/scalar.hpp
index 9b7e7af..dd55e95 100644
--- a/proton-c/bindings/cpp/include/proton/scalar.hpp
+++ b/proton-c/bindings/cpp/include/proton/scalar.hpp
@@ -20,6 +20,7 @@
  * under the License.
  */
 
+#include <proton/binary.hpp>
 #include <proton/export.hpp>
 #include <proton/comparable.hpp>
 #include <proton/types_fwd.hpp>
@@ -173,10 +174,10 @@ class scalar : private comparable<scalar> {
   private:
     scalar(const pn_atom_t& a);
     void ok(pn_type_t) const;
-    void set(const std::string&, pn_type_t);
+    void set(const binary&, pn_type_t);
     void set(const pn_atom_t&);
     pn_atom_t atom_;
-    std::string str_;           // Owner of string-like data.
+    binary bytes_;              // Hold binary data.
 
   friend class message;
   friend class restricted_scalar;

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/6260d506/proton-c/bindings/cpp/src/binary.cpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/src/binary.cpp b/proton-c/bindings/cpp/src/binary.cpp
new file mode 100644
index 0000000..e99d83e
--- /dev/null
+++ b/proton-c/bindings/cpp/src/binary.cpp
@@ -0,0 +1,45 @@
+/*
+ * 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/binary.hpp>
+
+#include <ostream>
+#include <iomanip>
+
+#include <ctype.h>
+
+namespace proton {
+
+std::ostream& operator<<(std::ostream& o, const binary& x) {
+    std::ios_base::fmtflags flags = o.flags();
+    char fill = o.fill();
+    o << std::setfill('0') << "b\"";
+    for (binary::const_iterator i = x.begin(); i != x.end(); ++i) {
+        if (!isprint(*i) && !isspace(*i)) {
+            o << std::hex << "\\x" << std::setw(2) << static_cast<unsigned>(*i);
+            o.flags(flags);
+        } else {
+            o << *i;
+        }
+    }
+    o.fill(fill);
+    return o << '"';
+}
+
+}

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/6260d506/proton-c/bindings/cpp/src/decoder.cpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/src/decoder.cpp b/proton-c/bindings/cpp/src/decoder.cpp
index e724003..cf0c029 100644
--- a/proton-c/bindings/cpp/src/decoder.cpp
+++ b/proton-c/bindings/cpp/src/decoder.cpp
@@ -80,16 +80,21 @@ void assign(uuid& x, const pn_uuid_t y) { byte_copy(x, y); }
 void assign(decimal32& x, const pn_decimal32_t y) { byte_copy(x, y); }
 void assign(decimal64& x, const pn_decimal64_t y)  { byte_copy(x, y); }
 void assign(decimal128& x, const pn_decimal128_t y) { byte_copy(x, y); }
+void
+assign(std::string& x, const pn_bytes_t y) { x = str(y); }
+void assign(symbol& x, const pn_bytes_t y) { x = str(y); }
+void assign(binary& x, const pn_bytes_t y) { x = bin(y); }
 
 } // namespace
 
 
 // Simple extract with no type conversion.
-template <class T, class U> void decoder::extract(T& x, U (*get)(pn_data_t*)) {
+template <class T, class U> decoder& decoder::extract(T& x, U (*get)(pn_data_t*)) {
     state_guard sg(*this);
     assert_type_equal(type_id_of<T>::value, pre_get());
     assign(x, get(pn_object()));
     sg.cancel();                // No error, cancel the reset.
+    return *this;
 }
 
 type_id decoder::next_type() {
@@ -179,10 +184,7 @@ decoder& decoder::operator>>(scalar& x) {
     return *this;
 }
 
-decoder& decoder::operator>>(bool &x) {
-    extract(x, pn_data_get_bool);
-    return *this;
-}
+decoder& decoder::operator>>(bool &x) { return extract(x, pn_data_get_bool); }
 
 decoder& decoder::operator>>(uint8_t &x) {
     state_guard sg(*this);
@@ -276,15 +278,9 @@ decoder& decoder::operator>>(int64_t &x) {
     return *this;
 }
 
-decoder& decoder::operator>>(wchar_t &x) {
-    extract(x, pn_data_get_char);
-    return *this;
-}
+decoder& decoder::operator>>(wchar_t &x) { return extract(x, pn_data_get_char); }
 
-decoder& decoder::operator>>(timestamp &x) {
-    extract(x, pn_data_get_timestamp);
-    return *this;
-}
+decoder& decoder::operator>>(timestamp &x) { return extract(x, pn_data_get_timestamp); }
 
 decoder& decoder::operator>>(float &x) {
     state_guard sg(*this);
@@ -308,37 +304,14 @@ decoder& decoder::operator>>(double &x) {
     return *this;
 }
 
-decoder& decoder::operator>>(decimal32 &x) {
-    extract(x, pn_data_get_decimal32);
-    return *this;
-}
-
-decoder& decoder::operator>>(decimal64 &x) {
-    extract(x, pn_data_get_decimal64);
-    return *this;
-}
-
-decoder& decoder::operator>>(decimal128 &x)  {
-    extract(x, pn_data_get_decimal128);
-    return *this;
-}
-
-decoder& decoder::operator>>(uuid &x)  {
-    extract(x, pn_data_get_uuid);
-    return *this;
-}
+decoder& decoder::operator>>(decimal32 &x) { return extract(x, pn_data_get_decimal32); }
+decoder& decoder::operator>>(decimal64 &x) { return extract(x, pn_data_get_decimal64); }
+decoder& decoder::operator>>(decimal128 &x)  { return extract(x, pn_data_get_decimal128); }
 
-decoder& decoder::operator>>(std::string &x) {
-    state_guard sg(*this);
-    switch (pre_get()) {
-      case STRING: x = str(pn_data_get_string(pn_object())); break;
-      case BINARY: x = str(pn_data_get_binary(pn_object())); break;
-      case SYMBOL: x = str(pn_data_get_symbol(pn_object())); break;
-      default: assert_type_equal(STRING, type_id(pn_data_type(pn_object())));
-    }
-    sg.cancel();
-    return *this;
-}
+decoder& decoder::operator>>(uuid &x)  { return extract(x, pn_data_get_uuid); }
+decoder& decoder::operator>>(binary &x)  { return extract(x, pn_data_get_binary); }
+decoder& decoder::operator>>(symbol &x)  { return extract(x, pn_data_get_symbol); }
+decoder& decoder::operator>>(std::string &x)  { return extract(x, pn_data_get_string); }
 
 } // codec
 } // proton

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/6260d506/proton-c/bindings/cpp/src/scalar.cpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/src/scalar.cpp b/proton-c/bindings/cpp/src/scalar.cpp
index 2cda8af..b862af0 100644
--- a/proton-c/bindings/cpp/src/scalar.cpp
+++ b/proton-c/bindings/cpp/src/scalar.cpp
@@ -46,15 +46,15 @@ type_id scalar::type() const { return type_id(atom_.type); }
 
 bool scalar::empty() const { return type() == NULL_TYPE; }
 
-void scalar::set(const std::string& x, pn_type_t t) {
+void scalar::set(const binary& x, pn_type_t t) {
     atom_.type = t;
-    str_ = x;
-    atom_.u.as_bytes = pn_bytes(str_);
+    bytes_ = x;
+    atom_.u.as_bytes = pn_bytes(bytes_);
 }
 
 void scalar::set(const pn_atom_t& atom) {
     if (type_id_is_string_like(type_id(atom.type)))
-        set(str(atom.u.as_bytes), atom.type);
+        set(bin(atom.u.as_bytes), atom.type);
     else
         atom_ = atom;
 }
@@ -119,10 +119,10 @@ scalar& scalar::operator=(const uuid& x) {
     return *this;
 }
 
-scalar& scalar::operator=(const std::string& x) { set(x, PN_STRING); return *this; }
-scalar& scalar::operator=(const symbol& x) { set(x, PN_SYMBOL); return *this; }
+scalar& scalar::operator=(const std::string& x) { set(binary(x), PN_STRING); return *this; }
+scalar& scalar::operator=(const symbol& x) { set(binary(x), PN_SYMBOL); return *this; }
 scalar& scalar::operator=(const binary& x) { set(x, PN_BINARY); return *this; }
-scalar& scalar::operator=(const char* x) { set(x, PN_STRING); return *this; }
+scalar& scalar::operator=(const char* x) { set(binary(std::string(x)), PN_STRING); return *this; }
 
 void scalar::ok(pn_type_t t) const {
     if (atom_.type != t) throw make_conversion_error(type_id(t), type());
@@ -145,9 +145,12 @@ void scalar::get(decimal32& x) const { ok(PN_DECIMAL32); byte_copy(x, atom_.u.as
 void scalar::get(decimal64& x) const { ok(PN_DECIMAL64); byte_copy(x, atom_.u.as_decimal64); }
 void scalar::get(decimal128& x) const { ok(PN_DECIMAL128); byte_copy(x, atom_.u.as_decimal128); }
 void scalar::get(uuid& x) const { ok(PN_UUID); byte_copy(x, atom_.u.as_uuid); }
-void scalar::get(std::string& x) const { ok(PN_STRING); x = std::string(str_); }
-void scalar::get(symbol& x) const { ok(PN_SYMBOL); x = symbol(str_); }
-void scalar::get(binary& x) const { ok(PN_BINARY); x = binary(str_); }
+void scalar::get(std::string& x) const {
+    ok(PN_STRING);
+    x = bytes_.str();
+}
+void scalar::get(symbol& x) const { ok(PN_SYMBOL); x = symbol(bytes_.str()); }
+void scalar::get(binary& x) const { ok(PN_BINARY); x = bytes_; }
 
 int64_t scalar::as_int() const {
     if (type_id_is_floating_point(type()))
@@ -187,7 +190,7 @@ double scalar::as_double() const {
 
 std::string scalar::as_string() const {
     if (type_id_is_string_like(type()))
-        return str_;
+        return bytes_.str();
     throw make_conversion_error(STRING, type());
 }
 

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/6260d506/proton-c/bindings/cpp/src/types_internal.hpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/src/types_internal.hpp b/proton-c/bindings/cpp/src/types_internal.hpp
index 3cf775c..c3ad1d4 100644
--- a/proton-c/bindings/cpp/src/types_internal.hpp
+++ b/proton-c/bindings/cpp/src/types_internal.hpp
@@ -19,8 +19,9 @@
  * under the License.
  */
 
-#include "proton/type_traits.hpp"
-#include "proton/error.hpp"
+#include <proton/type_traits.hpp>
+#include <proton/error.hpp>
+#include <proton/binary.hpp>
 #include <sstream>
 
 ///@file
@@ -49,8 +50,13 @@ inline pn_bytes_t pn_bytes(const std::string& s) {
     return b;
 }
 
-/// Convert pn_bytes_t to str
+inline pn_bytes_t pn_bytes(const binary& s) {
+    pn_bytes_t b = { s.size(), const_cast<char*>(&s[0]) };
+    return b;
+}
+
 inline std::string str(const pn_bytes_t& b) { return std::string(b.start, b.size); }
+inline binary bin(const pn_bytes_t& b) { return binary(b.start, b.start+b.size); }
 
 inline bool type_id_is_signed_int(type_id t) { return t == BYTE || t == SHORT || t == INT || t == LONG; }
 inline bool type_id_is_unsigned_int(type_id t) { return t == UBYTE || t == USHORT || t == UINT || t == ULONG; }

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/6260d506/proton-c/bindings/cpp/src/value.cpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/src/value.cpp b/proton-c/bindings/cpp/src/value.cpp
index 44d696d..34db621 100644
--- a/proton-c/bindings/cpp/src/value.cpp
+++ b/proton-c/bindings/cpp/src/value.cpp
@@ -176,8 +176,8 @@ std::ostream& operator<<(std::ostream& o, exact_cref<value> v) {
     if (v.ref.empty())
         return o << "<null>";
     switch (v.ref.type()) {
-      case STRING:
-      case SYMBOL: return o << get<std::string>(v.ref);
+      case STRING: return o << get<std::string>(v.ref);
+      case SYMBOL: return o << get<symbol>(v.ref);
       case DECIMAL32: return o << get<decimal32>(v.ref);
       case DECIMAL64: return o << get<decimal64>(v.ref);
       case DECIMAL128: return o << get<decimal128>(v.ref);

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/6260d506/proton-c/bindings/cpp/src/value_test.cpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/src/value_test.cpp b/proton-c/bindings/cpp/src/value_test.cpp
index 60343e5..be24da4 100644
--- a/proton-c/bindings/cpp/src/value_test.cpp
+++ b/proton-c/bindings/cpp/src/value_test.cpp
@@ -139,7 +139,7 @@ int main(int, char**) {
                  uuid::copy("\xff\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff")));
     RUN_TEST(failed, simple_type_test(std::string("xxx"), STRING, "xxx", std::string("yyy")));
     RUN_TEST(failed, simple_type_test(symbol("aaa"), SYMBOL, "aaa", symbol("aaaa")));
-    RUN_TEST(failed, simple_type_test(binary("aaa"), BINARY, "b\"aaa\"", binary("aaaa")));
+    RUN_TEST(failed, simple_type_test(binary("\010aaa"), BINARY, "b\"\\x08aaa\"", binary("aaaa")));
 
     // Test native C++ integral types.
     RUN_TEST(failed, simple_integral_test<char>());


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org