qpid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From acon...@apache.org
Subject [2/2] qpid-proton git commit: PROTON-865: blocking_connection::wait template to allow arbitrary functor as test.
Date Mon, 10 Aug 2015 21:42:46 GMT
PROTON-865: blocking_connection::wait template to allow arbitrary functor as test.


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

Branch: refs/heads/cjansen-cpp-client
Commit: d5ec4da7d033e6096420109551175278490a976c
Parents: 422c102
Author: Alan Conway <aconway@redhat.com>
Authored: Mon Aug 10 17:14:51 2015 -0400
Committer: Alan Conway <aconway@redhat.com>
Committed: Mon Aug 10 17:42:28 2015 -0400

----------------------------------------------------------------------
 .../cpp/include/proton/blocking_connection.hpp  | 33 +++++++++++---
 .../include/proton/sync_request_response.hpp    |  1 -
 .../cpp/include/proton/wait_condition.hpp       | 47 --------------------
 .../bindings/cpp/src/blocking_connection.cpp    |  6 +--
 .../cpp/src/blocking_connection_impl.cpp        | 28 ++++--------
 .../cpp/src/blocking_connection_impl.hpp        | 12 +++--
 proton-c/bindings/cpp/src/blocking_link.cpp     | 13 +++---
 proton-c/bindings/cpp/src/blocking_receiver.cpp |  5 +--
 proton-c/bindings/cpp/src/blocking_sender.cpp   |  5 +--
 .../bindings/cpp/src/sync_request_response.cpp  |  5 +--
 10 files changed, 58 insertions(+), 97 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/d5ec4da7/proton-c/bindings/cpp/include/proton/blocking_connection.hpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/include/proton/blocking_connection.hpp b/proton-c/bindings/cpp/include/proton/blocking_connection.hpp
index a8eac72..e82a2ee 100644
--- a/proton-c/bindings/cpp/include/proton/blocking_connection.hpp
+++ b/proton-c/bindings/cpp/include/proton/blocking_connection.hpp
@@ -39,7 +39,6 @@ class blocking_connection_impl;
 class ssl_domain;
 class blocking_sender;
 class blocking_receiver;
-class wait_condition;
 
 // TODO documentation
 class blocking_connection : public handle<blocking_connection_impl>
@@ -57,12 +56,36 @@ class blocking_connection : public handle<blocking_connection_impl>
     PN_CPP_EXTERN blocking_sender create_sender(const std::string &address, handler *h=0);
     PN_CPP_EXTERN blocking_receiver create_receiver(const std::string &address, int credit
= 0,
                                                     bool dynamic = false, handler *h=0, std::string
name = std::string());
-    PN_CPP_EXTERN void wait(wait_condition &condition);
-    PN_CPP_EXTERN void wait(wait_condition &condition, const std::string &msg);
-    PN_CPP_EXTERN void wait(wait_condition &condition, const std::string &msg, duration
timeout);
+
+    /// Abstract condition class for wait.
+    struct condition {
+        virtual ~condition() {}
+        virtual bool operator()() = 0;
+    };
+
+    /** Wait till cond returns true. 
+     * C must be copyable and callable with no arguments and bool return value.
+     * Wait up to timeout if specified or blocking_connection::timeout() if not.
+     * @throws timeout_error with message msg if timeout is exceeded.
+     */
+    template <class C> void wait(C cond, const std::string &msg="", duration timeout=duration(-1))
{
+        condition_impl<C> c(cond);
+        wait(dynamic_cast<condition&>(c), msg, timeout);
+    }
+
     PN_CPP_EXTERN duration timeout();
   private:
-    friend class private_impl_ref<blocking_connection>;
+
+    PN_CPP_EXTERN void wait(condition &, const std::string &msg="", duration timeout=duration(-1));
+
+
+    template <class C> struct condition_impl : public condition {
+        C cond_;
+        condition_impl(C c) : cond_(c) {}
+        bool operator()() { return cond_(); }
+    };
+
+  friend class private_impl_ref<blocking_connection>;
 };
 
 }

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/d5ec4da7/proton-c/bindings/cpp/include/proton/sync_request_response.hpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/include/proton/sync_request_response.hpp b/proton-c/bindings/cpp/include/proton/sync_request_response.hpp
index 5557448..397e181 100644
--- a/proton-c/bindings/cpp/include/proton/sync_request_response.hpp
+++ b/proton-c/bindings/cpp/include/proton/sync_request_response.hpp
@@ -25,7 +25,6 @@
 #include "proton/messaging_handler.hpp"
 #include "proton/blocking_receiver.hpp"
 #include "proton/blocking_sender.hpp"
-#include "proton/wait_condition.hpp"
 #include <string>
 #include <memory>
 

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/d5ec4da7/proton-c/bindings/cpp/include/proton/wait_condition.hpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/include/proton/wait_condition.hpp b/proton-c/bindings/cpp/include/proton/wait_condition.hpp
deleted file mode 100644
index 760928e..0000000
--- a/proton-c/bindings/cpp/include/proton/wait_condition.hpp
+++ /dev/null
@@ -1,47 +0,0 @@
-#ifndef PROTON_CPP_WAITCONDITION_H
-#define PROTON_CPP_WAITCONDITION_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 "proton/export.hpp"
-
-namespace proton {
-
-// TODO aconway 2015-07-15: c++11 should use std::function
-// c++03 could use a function template.
-
-// Interface class to indicates that an expected contion has been
-// achieved, i.e. for blocking_connection.wait()
-
-class wait_condition
-{
-  public:
-    PN_CPP_EXTERN virtual ~wait_condition();
-
-    // Overide this member function to indicate whether an expected
-    // condition is achieved and requires no further waiting.
-    virtual bool achieved() = 0;
-};
-
-
-}
-
-#endif  /*!PROTON_CPP_WAITCONDITION_H*/

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/d5ec4da7/proton-c/bindings/cpp/src/blocking_connection.cpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/src/blocking_connection.cpp b/proton-c/bindings/cpp/src/blocking_connection.cpp
index 61475bc..c54005e 100644
--- a/proton-c/bindings/cpp/src/blocking_connection.cpp
+++ b/proton-c/bindings/cpp/src/blocking_connection.cpp
@@ -49,11 +49,7 @@ blocking_connection::blocking_connection(const proton::url &url, duration
d, ssl
 
 void blocking_connection::close() { impl_->close(); }
 
-void blocking_connection::wait(wait_condition &cond) { return impl_->wait(cond); }
-void blocking_connection::wait(wait_condition &cond, const std::string &msg) {
-    return impl_->wait(cond, msg);
-}
-void blocking_connection::wait(wait_condition &cond, const std::string &msg, duration
timeout) {
+void blocking_connection::wait(condition &cond, const std::string &msg, duration
timeout) {
     return impl_->wait(cond, msg, timeout);
 }
 

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/d5ec4da7/proton-c/bindings/cpp/src/blocking_connection_impl.cpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/src/blocking_connection_impl.cpp b/proton-c/bindings/cpp/src/blocking_connection_impl.cpp
index 10b40ee..e552830 100644
--- a/proton-c/bindings/cpp/src/blocking_connection_impl.cpp
+++ b/proton-c/bindings/cpp/src/blocking_connection_impl.cpp
@@ -22,7 +22,6 @@
 #include "proton/messaging_handler.hpp"
 #include "proton/duration.hpp"
 #include "proton/error.hpp"
-#include "proton/wait_condition.hpp"
 #include "blocking_connection_impl.hpp"
 #include "msg.hpp"
 #include "contexts.hpp"
@@ -31,9 +30,6 @@
 
 namespace proton {
 
-wait_condition::~wait_condition() {}
-
-
 void blocking_connection_impl::incref(blocking_connection_impl *impl_) {
     impl_->refcount_++;
 }
@@ -45,15 +41,15 @@ void blocking_connection_impl::decref(blocking_connection_impl *impl_)
{
 }
 
 namespace {
-struct connection_opening : public wait_condition {
+struct connection_opening {
     connection_opening(pn_connection_t *c) : pn_connection(c) {}
-    bool achieved() { return (pn_connection_state(pn_connection) & PN_REMOTE_UNINIT);
}
+    bool operator()() { return (pn_connection_state(pn_connection) & PN_REMOTE_UNINIT);
}
     pn_connection_t *pn_connection;
 };
 
-struct connection_closed : public wait_condition {
+struct connection_closed {
     connection_closed(pn_connection_t *c) : pn_connection(c) {}
-    bool achieved() { return !(pn_connection_state(pn_connection) & PN_REMOTE_ACTIVE);
}
+    bool operator()() { return !(pn_connection_state(pn_connection) & PN_REMOTE_ACTIVE);
}
     pn_connection_t *pn_connection;
 };
 
@@ -83,18 +79,10 @@ void blocking_connection_impl::close() {
     wait(cond);
 }
 
-void blocking_connection_impl::wait(wait_condition &condition) {
-    std::string empty;
-    wait(condition, empty, timeout_);
-}
-
-void blocking_connection_impl::wait(wait_condition &condition, const std::string &msg)
{
-    wait(condition, msg, timeout_);
-}
-
-void blocking_connection_impl::wait(wait_condition &condition, const std::string &msg,
duration wait_timeout) {
+void blocking_connection_impl::wait(blocking_connection::condition &condition, const
std::string &msg, duration wait_timeout) {
+    if (wait_timeout == duration(-1)) wait_timeout = timeout_;
     if (wait_timeout == duration::FOREVER) {
-        while (!condition.achieved()) {
+        while (!condition()) {
             container_.process();
         }
     }
@@ -105,7 +93,7 @@ void blocking_connection_impl::wait(wait_condition &condition, const
std::string
     try {
         pn_timestamp_t now = pn_reactor_mark(reactor);
         pn_timestamp_t deadline = now + wait_timeout.milliseconds;
-        while (!condition.achieved()) {
+        while (!condition()) {
             container_.process();
             if (deadline < pn_reactor_mark(reactor)) {
                 std::string txt = "connection timed out";

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/d5ec4da7/proton-c/bindings/cpp/src/blocking_connection_impl.hpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/src/blocking_connection_impl.hpp b/proton-c/bindings/cpp/src/blocking_connection_impl.hpp
index b70193d..17f8f57 100644
--- a/proton-c/bindings/cpp/src/blocking_connection_impl.hpp
+++ b/proton-c/bindings/cpp/src/blocking_connection_impl.hpp
@@ -24,6 +24,7 @@
 #include "proton/export.hpp"
 #include "proton/endpoint.hpp"
 #include "proton/container.hpp"
+#include "proton/blocking_connection.hpp"
 #include "proton/types.h"
 #include <string>
 
@@ -41,15 +42,20 @@ class ssl_domain;
     PN_CPP_EXTERN blocking_connection_impl(const url &url, duration d, ssl_domain *ssld,
container *c);
     PN_CPP_EXTERN ~blocking_connection_impl();
     PN_CPP_EXTERN void close();
-    PN_CPP_EXTERN void wait(wait_condition &condition);
-    PN_CPP_EXTERN void wait(wait_condition &condition, const std::string &msg);
-    PN_CPP_EXTERN void wait(wait_condition &condition, const std::string &msg, duration
timeout);
+    template <class C> void wait(C c, const std::string &msg="", duration timeout=duration(-1))
{
+        blocking_connection::condition_impl<C> cond(c);
+        wait(dynamic_cast<blocking_connection::condition&>(cond), msg, timeout);
+    }
+
     PN_CPP_EXTERN pn_connection_t *pn_blocking_connection();
     duration timeout() { return timeout_; }
     static void incref(blocking_connection_impl *);
     static void decref(blocking_connection_impl *);
   private:
     friend class blocking_connection;
+    PN_CPP_EXTERN void wait(blocking_connection::condition &, const std::string &
="",
+                            duration=duration(-1));
+
     container container_;
     connection connection_;
     url url_;

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/d5ec4da7/proton-c/bindings/cpp/src/blocking_link.cpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/src/blocking_link.cpp b/proton-c/bindings/cpp/src/blocking_link.cpp
index c7f3551..43d1b83 100644
--- a/proton-c/bindings/cpp/src/blocking_link.cpp
+++ b/proton-c/bindings/cpp/src/blocking_link.cpp
@@ -21,7 +21,6 @@
 #include "proton/blocking_link.hpp"
 #include "proton/blocking_connection.hpp"
 #include "proton/messaging_handler.hpp"
-#include "proton/wait_condition.hpp"
 #include "proton/error.hpp"
 #include "msg.hpp"
 
@@ -29,21 +28,21 @@
 namespace proton {
 
 namespace {
-struct link_opened : public wait_condition {
+struct link_opened {
     link_opened(pn_link_t *l) : pn_link(l) {}
-    bool achieved() { return !(pn_link_state(pn_link) & PN_REMOTE_UNINIT); }
+    bool operator()() { return !(pn_link_state(pn_link) & PN_REMOTE_UNINIT); }
     pn_link_t *pn_link;
 };
 
-struct link_closed : public wait_condition {
+struct link_closed {
     link_closed(pn_link_t *l) : pn_link(l) {}
-    bool achieved() { return (pn_link_state(pn_link) & PN_REMOTE_CLOSED); }
+    bool operator()() { return (pn_link_state(pn_link) & PN_REMOTE_CLOSED); }
     pn_link_t *pn_link;
 };
 
-struct link_not_open : public wait_condition {
+struct link_not_open {
     link_not_open(pn_link_t *l) : pn_link(l) {}
-    bool achieved() { return !(pn_link_state(pn_link) & PN_REMOTE_ACTIVE); }
+    bool operator()() { return !(pn_link_state(pn_link) & PN_REMOTE_ACTIVE); }
     pn_link_t *pn_link;
 };
 

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/d5ec4da7/proton-c/bindings/cpp/src/blocking_receiver.cpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/src/blocking_receiver.cpp b/proton-c/bindings/cpp/src/blocking_receiver.cpp
index 3ca53c0..f4736e5 100644
--- a/proton-c/bindings/cpp/src/blocking_receiver.cpp
+++ b/proton-c/bindings/cpp/src/blocking_receiver.cpp
@@ -20,7 +20,6 @@
  */
 #include "proton/blocking_receiver.hpp"
 #include "proton/blocking_connection.hpp"
-#include "proton/wait_condition.hpp"
 #include "proton/error.hpp"
 #include "fetcher.hpp"
 #include "msg.hpp"
@@ -30,9 +29,9 @@ namespace proton {
 
 namespace {
 
-struct fetcher_has_message : public wait_condition {
+struct fetcher_has_message {
     fetcher_has_message(fetcher &f) : fetcher_(f) {}
-    bool achieved() { return fetcher_.has_message(); }
+    bool operator()() { return fetcher_.has_message(); }
     fetcher &fetcher_;
 };
 

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/d5ec4da7/proton-c/bindings/cpp/src/blocking_sender.cpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/src/blocking_sender.cpp b/proton-c/bindings/cpp/src/blocking_sender.cpp
index 8fea98a..a4757eb 100644
--- a/proton-c/bindings/cpp/src/blocking_sender.cpp
+++ b/proton-c/bindings/cpp/src/blocking_sender.cpp
@@ -20,7 +20,6 @@
  */
 #include "proton/blocking_sender.hpp"
 #include "proton/blocking_connection.hpp"
-#include "proton/wait_condition.hpp"
 #include "proton/error.hpp"
 #include "msg.hpp"
 
@@ -28,9 +27,9 @@
 namespace proton {
 
 namespace {
-struct delivery_settled : public wait_condition {
+struct delivery_settled {
     delivery_settled(pn_delivery_t *d) : pn_delivery(d) {}
-    bool achieved() { return pn_delivery_settled(pn_delivery); }
+    bool operator()() { return pn_delivery_settled(pn_delivery); }
     pn_delivery_t *pn_delivery;
 };
 

http://git-wip-us.apache.org/repos/asf/qpid-proton/blob/d5ec4da7/proton-c/bindings/cpp/src/sync_request_response.cpp
----------------------------------------------------------------------
diff --git a/proton-c/bindings/cpp/src/sync_request_response.cpp b/proton-c/bindings/cpp/src/sync_request_response.cpp
index 3da4ceb..f3c76a2 100644
--- a/proton-c/bindings/cpp/src/sync_request_response.cpp
+++ b/proton-c/bindings/cpp/src/sync_request_response.cpp
@@ -28,10 +28,9 @@ namespace proton {
 namespace {
 amqp_ulong global_correlation_id = 0;
 
-struct response_received : public wait_condition {
+struct response_received {
     response_received(std::auto_ptr<message>& m, amqp_ulong id) : message_(m),
id_(id) {}
-    bool achieved() {
-        return message_.get() && message_->correlation_id() == id_; }
+    bool operator()() { return message_.get() && message_->correlation_id() ==
id_; }
     std::auto_ptr<message>& message_;
     value id_;
 };


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


Mime
View raw message