Skip to content

header support implementation #115

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

Merged
merged 7 commits into from
Oct 16, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ compiler:
- clang

env:
- RDKAFKA_VERSION=v0.11.0
- RDKAFKA_VERSION=v0.11.5

os:
- linux
Expand Down
19 changes: 11 additions & 8 deletions include/cppkafka/clonable_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class ClonablePtr {
*/
ClonablePtr(const ClonablePtr& rhs)
: handle_(rhs.cloner_ ? std::unique_ptr<T, Deleter>(rhs.cloner_(rhs.handle_.get()), rhs.handle_.get_deleter()) :
std::unique_ptr<T, Deleter>(nullptr, nullptr)),
std::unique_ptr<T, Deleter>(rhs.handle_.get(), rhs.handle_.get_deleter())),
cloner_(rhs.cloner_) {

}
Expand All @@ -75,12 +75,8 @@ class ClonablePtr {
if (this == &rhs) {
return *this;
}
if (rhs.cloner_) {
handle_.reset(rhs.cloner_(rhs.handle_.get()));
}
else {
handle_.reset();
}
handle_ = rhs.cloner_ ? std::unique_ptr<T, Deleter>(rhs.cloner_(rhs.handle_.get()), rhs.handle_.get_deleter()) :
std::unique_ptr<T, Deleter>(rhs.handle_.get(), rhs.handle_.get_deleter());
return *this;
}

Expand All @@ -96,7 +92,14 @@ class ClonablePtr {
}

/**
* \brief Indicates whether this clonable pointer is valid (not null)
* \brief Releases ownership of the internal pointer
*/
T* release() {
return handle_.release();
}

/**
* \brief Indicates whether this ClonablePtr instance is valid (not null)
*/
explicit operator bool() const {
return static_cast<bool>(handle_);
Expand Down
3 changes: 3 additions & 0 deletions include/cppkafka/cppkafka.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
#include <cppkafka/error.h>
#include <cppkafka/exceptions.h>
#include <cppkafka/group_information.h>
#include <cppkafka/header.h>
#include <cppkafka/header_list.h>
#include <cppkafka/header_list_iterator.h>
#include <cppkafka/kafka_handle_base.h>
#include <cppkafka/logging.h>
#include <cppkafka/macros.h>
Expand Down
54 changes: 45 additions & 9 deletions include/cppkafka/header.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,60 @@

namespace cppkafka {

/**
* \brief Class representing a rdkafka header.
*
* The template parameter 'BufferType' can represent a cppkafka::Buffer, std::string, std::vector, etc.
* A valid header may contain an empty name as well as null data.
*/
template <typename BufferType>
class Header {
public:
using ValueType = BufferType;

/**
* \brief Build an empty header with no data
*/
Header() = default;

Header(const std::string& name,
/**
* \brief Build a header instance
* \param name The header name
* \param value The non-modifiable header data
*/
Header(const std::string name,
const BufferType& value);

Header(const std::string& name,
/**
* \brief Build a header instance
* \param name The header name
* \param value The header data to be moved
*/
Header(const std::string name,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be const, otherwise it won't be moved. Same with the overload above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Silly me. Of course...good catch!

BufferType&& value);

/**
* \brief Get the header name
* \return A reference to the name
*/
const std::string& get_name() const;

/**
* \brief Get the header value
* \return A const reference to the underlying buffer
*/
const BufferType& get_value() const;

/**
* \brief Get the header value
* \return A non-const reference to the underlying buffer
*/
BufferType& get_value();

/**
* \brief Check if this header is empty
* \return True if the header contains valid data, false otherwise.
*/
operator bool() const;

private:
Expand All @@ -66,6 +102,7 @@ class Header {
BufferType value_;
};

// Comparison operators for Header type
template <typename BufferType>
bool operator==(const Header<BufferType>& lhs, const Header<BufferType>& rhs) {
return std::tie(lhs.get_name(), lhs.get_value()) == std::tie(rhs.get_name(), rhs.get_value());
Expand Down Expand Up @@ -96,20 +133,19 @@ bool operator>=(const Header<BufferType>& lhs, const Header<BufferType>& rhs) {
return !(lhs < rhs);
}

// Implementation
template <typename BufferType>
Header<BufferType>::Header(const std::string& name,
Header<BufferType>::Header(const std::string name,
const BufferType& value)
: name_(name),
: name_(std::move(name)),
value_(make_value(value)) {
assert(!name.empty());
}

template <typename BufferType>
Header<BufferType>::Header(const std::string& name,
BufferType&& value)
: name_(name),
Header<BufferType>::Header(const std::string name,
BufferType&& value)
: name_(std::move(name)),
value_(std::move(value)) {
assert(!name.empty());
}

template <typename BufferType>
Expand Down
Loading