-
Notifications
You must be signed in to change notification settings - Fork 792
Add duration template for all function using std::chrono::milliseconds #666
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cacharle
wants to merge
3
commits into
zeromq:master
Choose a base branch
from
cacharle:std-chrono-any-duration
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -351,16 +351,19 @@ inline int poll(std::vector<zmq_pollitem_t> const &items, long timeout_ = -1) | |
| return detail::poll(const_cast<zmq_pollitem_t *>(items.data()), items.size(), timeout_); | ||
| } | ||
|
|
||
| inline int | ||
| poll(zmq_pollitem_t *items, size_t nitems, std::chrono::milliseconds timeout = std::chrono::milliseconds{-1}) | ||
| template<typename Duration = std::chrono::milliseconds> | ||
| int | ||
| poll(zmq_pollitem_t *items, size_t nitems, Duration timeout = std::chrono::milliseconds{-1}) | ||
| { | ||
| return detail::poll(items, nitems, static_cast<long>(timeout.count())); | ||
| auto timeout_ms = std::chrono::duration_cast<std::chrono::milliseconds>(timeout); | ||
| return detail::poll(items, nitems, static_cast<long>(timeout_ms.count())); | ||
| } | ||
|
|
||
| inline int poll(std::vector<zmq_pollitem_t> &items, | ||
| std::chrono::milliseconds timeout = std::chrono::milliseconds{-1}) | ||
| template<typename Duration = std::chrono::milliseconds> | ||
| int poll(std::vector<zmq_pollitem_t> &items, Duration timeout = std::chrono::milliseconds{-1}) | ||
| { | ||
| return detail::poll(items.data(), items.size(), static_cast<long>(timeout.count())); | ||
| auto timeout_ms = std::chrono::duration_cast<std::chrono::milliseconds>(timeout); | ||
| return detail::poll(items.data(), items.size(), static_cast<long>(timeout_ms.count())); | ||
| } | ||
|
|
||
| ZMQ_DEPRECATED("from 4.3.1, use poll taking std::chrono::duration instead of long") | ||
|
|
@@ -369,11 +372,11 @@ inline int poll(std::vector<zmq_pollitem_t> &items, long timeout_) | |
| return detail::poll(items.data(), items.size(), timeout_); | ||
| } | ||
|
|
||
| template<std::size_t SIZE> | ||
| inline int poll(std::array<zmq_pollitem_t, SIZE> &items, | ||
| std::chrono::milliseconds timeout = std::chrono::milliseconds{-1}) | ||
| template<std::size_t SIZE, typename Duration> | ||
| int poll(std::array<zmq_pollitem_t, SIZE> &items, Duration timeout = std::chrono::milliseconds{-1}) | ||
| { | ||
| return detail::poll(items.data(), items.size(), static_cast<long>(timeout.count())); | ||
| auto timeout_ms = std::chrono::duration_cast<std::chrono::milliseconds>(timeout); | ||
| return detail::poll(items.data(), items.size(), static_cast<long>(timeout_ms.count())); | ||
| } | ||
| #endif | ||
|
|
||
|
|
@@ -2366,6 +2369,14 @@ class monitor_t | |
| on_monitor_started(); | ||
| } | ||
|
|
||
| #ifdef ZMQ_CPP11 | ||
| template<typename Duration = std::chrono::milliseconds> | ||
| bool check_event(Duration timeout = std::chrono::milliseconds{0}) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. check_event() is ambiguous |
||
| { | ||
| return check_event(static_cast<int>(std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count())); | ||
| } | ||
| #endif | ||
|
|
||
| bool check_event(int timeout = 0) | ||
| { | ||
| assert(_monitor_socket); | ||
|
|
@@ -2725,17 +2736,17 @@ template<typename T = no_user_data> class poller_t | |
| } | ||
| } | ||
|
|
||
| template <typename Sequence> | ||
| template <typename Sequence, typename Duration = std::chrono::milliseconds> | ||
| size_t wait_all(Sequence &poller_events, | ||
| const std::chrono::milliseconds timeout) | ||
| const Duration timeout) | ||
| { | ||
| static_assert(std::is_same<typename Sequence::value_type, event_type>::value, | ||
| "Sequence::value_type must be of poller_t::event_type"); | ||
| int rc = zmq_poller_wait_all( | ||
| poller_ptr.get(), | ||
| reinterpret_cast<zmq_poller_event_t *>(poller_events.data()), | ||
| static_cast<int>(poller_events.size()), | ||
| static_cast<long>(timeout.count())); | ||
| static_cast<long>(std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count())); | ||
| if (rc > 0) | ||
| return static_cast<size_t>(rc); | ||
|
|
||
|
|
@@ -2824,9 +2835,11 @@ class timers | |
| ZMQ_ASSERT(rc == 0); | ||
| } | ||
|
|
||
| id_t add(std::chrono::milliseconds interval, zmq_timer_fn handler, void *arg) | ||
| template<typename Duration = std::chrono::milliseconds> | ||
| id_t add(Duration interval, zmq_timer_fn handler, void *arg) | ||
| { | ||
| id_t timer_id = zmq_timers_add(_timers, interval.count(), handler, arg); | ||
| auto interval_ms = std::chrono::duration_cast<std::chrono::milliseconds>(interval); | ||
| id_t timer_id = zmq_timers_add(_timers, interval_ms.count(), handler, arg); | ||
| if (timer_id == -1) | ||
| throw zmq::error_t(); | ||
| return timer_id; | ||
|
|
@@ -2839,9 +2852,11 @@ class timers | |
| throw zmq::error_t(); | ||
| } | ||
|
|
||
| void set_interval(id_t timer_id, std::chrono::milliseconds interval) | ||
| template<typename Duration = std::chrono::milliseconds> | ||
| void set_interval(id_t timer_id, Duration interval) | ||
| { | ||
| int rc = zmq_timers_set_interval(_timers, timer_id, interval.count()); | ||
| auto interval_ms = std::chrono::duration_cast<std::chrono::milliseconds>(interval); | ||
| int rc = zmq_timers_set_interval(_timers, timer_id, interval_ms.count()); | ||
| if (rc == -1) | ||
| throw zmq::error_t(); | ||
| } | ||
|
|
@@ -2853,12 +2868,13 @@ class timers | |
| throw zmq::error_t(); | ||
| } | ||
|
|
||
| template<typename Duration = std::chrono::milliseconds> | ||
| timeout_result_t timeout() const | ||
| { | ||
| int timeout = zmq_timers_timeout(_timers); | ||
| if (timeout == -1) | ||
| return timeout_result_t{}; | ||
| return std::chrono::milliseconds{timeout}; | ||
| return std::chrono::duration_cast<Duration>(std::chrono::milliseconds{timeout}); | ||
| } | ||
|
|
||
| void execute() | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One concern is that for poll and poller, -1 millisecond is a special value that may need special handling. E.g. does -1s or -1ns do the right thing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Negative duration work:
Output:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With c++17:
Output:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but you cast the input Duration to milliseconds then pass that to libzmq.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, casting from -1ns to ms give 0ms, I'll change that