Allow getting config options and add multiple overloads for set

This commit is contained in:
Matias Fontanini
2016-06-05 15:08:40 -07:00
parent efd2321828
commit 65a60f1690
11 changed files with 120 additions and 5 deletions

View File

@@ -10,6 +10,7 @@
#include "topic_partition_list.h"
#include "topic_configuration.h"
#include "clonable_ptr.h"
#include "configuration_base.h"
namespace cppkafka {
@@ -18,7 +19,7 @@ class Producer;
class Consumer;
class KafkaHandleBase;
class Configuration {
class Configuration : public ConfigurationBase<Configuration> {
public:
using DeliveryReportCallback = std::function<void(Producer& producer, const Message&)>;
using OffsetCommitCallback = std::function<void(Consumer& consumer, rd_kafka_resp_err_t,
@@ -35,6 +36,8 @@ public:
using StatsCallback = std::function<void(KafkaHandleBase& handle, const std::string& json)>;
using SocketCallback = std::function<int(int domain, int type, int protoco)>;
using ConfigurationBase<Configuration>::set;
Configuration();
void set(const std::string& name, const std::string& value);
@@ -48,6 +51,7 @@ public:
void set_default_topic_configuration(boost::optional<TopicConfiguration> config);
rd_kafka_conf_t* get_handle() const;
std::string get(const std::string& name) const;
const DeliveryReportCallback& get_delivery_report_callback() const;
const OffsetCommitCallback& get_offset_commit_callback() const;
const ErrorCallback& get_error_callback() const;

View File

@@ -0,0 +1,33 @@
#ifndef CPPKAFKA_CONFIGURATION_BASE_H
#define CPPKAFKA_CONFIGURATION_BASE_H
#include <string>
namespace cppkafka {
template <typename Concrete>
class ConfigurationBase {
public:
void set(const std::string& name, bool value) {
proxy_set(name, value ? "true" : "false");
}
// Overload for any integral value
template <typename T,
typename = typename std::enable_if<std::is_integral<T>::value>::type>
void set(const std::string& name, T value) {
proxy_set(name, std::to_string(value));
}
void set(const std::string& name, const char* value) {
proxy_set(name, value);
}
private:
void proxy_set(const std::string& name, const std::string& value) {
static_cast<Concrete&>(*this).set(name, value);
}
};
} // cppkafka
#endif // CPPKAFKA_CONFIGURATION_BASE_H

View File

@@ -21,6 +21,11 @@ public:
ConfigException(const std::string& config_name, const std::string& error);
};
class ConfigOptionNotFound : public Exception {
public:
ConfigOptionNotFound(const std::string& config_name);
};
class HandleException : public Exception {
public:
HandleException(rd_kafka_resp_err_t error_code);

View File

@@ -5,17 +5,20 @@
#include <functional>
#include <librdkafka/rdkafka.h>
#include "clonable_ptr.h"
#include "configuration_base.h"
namespace cppkafka {
class Topic;
class Buffer;
class TopicConfiguration {
class TopicConfiguration : public ConfigurationBase<TopicConfiguration> {
public:
using PartitionerCallback = std::function<int32_t(const Topic&, const Buffer& key,
int32_t partition_count)>;
using ConfigurationBase<TopicConfiguration>::set;
TopicConfiguration();
void set(const std::string& name, const std::string& value);
@@ -26,6 +29,7 @@ public:
const PartitionerCallback& get_partitioner_callback() const;
rd_kafka_topic_conf_t* get_handle() const;
std::string get(const std::string& name) const;
private:
using HandlePtr = ClonablePtr<rd_kafka_topic_conf_t,
decltype(&rd_kafka_topic_conf_destroy),