Skip to content

Commit

Permalink
lib: convert Channel::CheckExchangeExists to use boost::string_ref
Browse files Browse the repository at this point in the history
Prepare for an easier transition to std::string_view.
  • Loading branch information
alanxz committed Nov 13, 2020
1 parent ba5baa8 commit 3274e1b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/Channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -474,12 +474,12 @@ int Channel::GetSocketFD() const {
return amqp_get_sockfd(m_impl->m_connection);
}

bool Channel::CheckExchangeExists(const std::string &exchange_name) {
bool Channel::CheckExchangeExists(boost::string_ref exchange_name) {
const boost::array<boost::uint32_t, 1> DECLARE_OK = {
{AMQP_EXCHANGE_DECLARE_OK_METHOD}};

amqp_exchange_declare_t declare = {};
declare.exchange = StringToBytes(exchange_name);
declare.exchange = StringRefToBytes(exchange_name);
declare.passive = true;
declare.nowait = false;

Expand Down
8 changes: 8 additions & 0 deletions src/SimpleAmqpClient/Bytes.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define SIMPLEAMQPCLIENT_BYTES_H

#include <amqp.h>
#include <boost/utility/string_ref.hpp>

#include <string>

Expand All @@ -14,5 +15,12 @@ amqp_bytes_t StringToBytes(const std::string& str) {
return ret;
}

amqp_bytes_t StringRefToBytes(boost::string_ref str) {
amqp_bytes_t ret;
ret.bytes = reinterpret_cast<void*>(const_cast<char*>(str.data()));
ret.len = str.length();
return ret;
}

} // namespace AmqpClient
#endif // SIMPLEAMQPCLIENT_BYTES_H
3 changes: 2 additions & 1 deletion src/SimpleAmqpClient/Channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <boost/scoped_ptr.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/utility.hpp>
#include <boost/utility/string_ref.hpp>
#include <boost/variant.hpp>
#include <string>
#include <vector>
Expand Down Expand Up @@ -350,7 +351,7 @@ class SIMPLEAMQPCLIENT_EXPORT Channel : boost::noncopyable {
* @param exchange_name the name of the exchange to check for.
* @returns true if the exchange exists on the broker, false otherwise.
*/
bool CheckExchangeExists(const std::string &exchange_name);
bool CheckExchangeExists(boost::string_ref exchange_name);

/**
* Declares an exchange
Expand Down

0 comments on commit 3274e1b

Please sign in to comment.