Add initial config classes

This commit is contained in:
Matias Fontanini
2016-05-12 20:37:58 -07:00
commit f86c9c1f57
8 changed files with 200 additions and 0 deletions

7
src/CMakeLists.txt Normal file
View File

@@ -0,0 +1,7 @@
set(SOURCES
configuration.cpp
topic_configuration.cpp
exceptions.cpp
)
add_library(cppkafka ${SOURCES})

42
src/configuration.cpp Normal file
View File

@@ -0,0 +1,42 @@
#include "configuration.h"
#include <librdkafka/rdkafka.h>
#include "exceptions.h"
using std::string;
namespace cppkafka {
Configuration::Configuration()
: handle_(make_handle(rd_kafka_conf_new())) {
}
Configuration::Configuration(rd_kafka_conf_t* ptr)
: handle_(make_handle(ptr)) {
}
Configuration::Configuration(const Configuration& rhs)
: handle_(make_handle(rd_kafka_conf_dup(rhs.handle_.get()))) {
}
Configuration& Configuration::operator=(const Configuration& rhs) {
handle_.reset(rd_kafka_conf_dup(rhs.handle_.get()));
}
void Configuration::set(const string& name, const string& value) {
char error_buffer[512];
rd_kafka_conf_res_t result;
result = rd_kafka_conf_set(handle_.get(), name.data(), value.data(), error_buffer,
sizeof(error_buffer));
if (result != RD_KAFKA_CONF_OK) {
throw KafkaConfigException(name, error_buffer);
}
}
Configuration::HandlePtr Configuration::make_handle(rd_kafka_conf_t* ptr) {
return HandlePtr(ptr, &rd_kafka_conf_destroy);
}
} // cppkafka

16
src/exceptions.cpp Normal file
View File

@@ -0,0 +1,16 @@
#include "exceptions.h"
using std::string;
namespace cppkafka {
KafkaConfigException::KafkaConfigException(const string& config_name,
const string& error)
: message_("Failed to set " + config_name + ": " + error) {
}
const char* KafkaConfigException::what() const noexcept {
return message_.data();
}
} // cppkafka

View File

@@ -0,0 +1,42 @@
#include "topic_configuration.h"
#include <librdkafka/rdkafka.h>
#include "exceptions.h"
using std::string;
namespace cppkafka {
TopicConfiguration::TopicConfiguration()
: handle_(make_handle(rd_kafka_topic_conf_new())) {
}
TopicConfiguration::TopicConfiguration(rd_kafka_topic_conf_t* ptr)
: handle_(make_handle(ptr)) {
}
TopicConfiguration::TopicConfiguration(const TopicConfiguration& rhs)
: handle_(make_handle(rd_kafka_topic_conf_dup(rhs.handle_.get()))) {
}
TopicConfiguration& TopicConfiguration::operator=(const TopicConfiguration& rhs) {
handle_.reset(rd_kafka_topic_conf_dup(rhs.handle_.get()));
}
void TopicConfiguration::set(const string& name, const string& value) {
char error_buffer[512];
rd_kafka_conf_res_t result;
result = rd_kafka_topic_conf_set(handle_.get(), name.data(), value.data(), error_buffer,
sizeof(error_buffer));
if (result != RD_KAFKA_CONF_OK) {
throw KafkaConfigException(name, error_buffer);
}
}
TopicConfiguration::HandlePtr TopicConfiguration::make_handle(rd_kafka_topic_conf_t* ptr) {
return HandlePtr(ptr, &rd_kafka_topic_conf_destroy);
}
} // cppkafka