Skip to content

Commit 8b01d1a

Browse files
committed
Updates most of the files for optional use of the new cpp features.
1 parent 77f0ea1 commit 8b01d1a

8 files changed

+57
-44
lines changed

CMakeLists.txt

+38-27
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ set(CPPKAFKA_VERSION "${CPPKAFKA_VERSION_MAJOR}.${CPPKAFKA_VERSION_MINOR}.${CPPK
1616
set(RDKAFKA_MIN_VERSION "0.9.4")
1717
set(RDKAFKA_MIN_VERSION_HEX 0x00090400)
1818

19+
option(USE_CPP17 "Use C++17 features instead of Boost (still, if you don't have boost you can't build examples" OFF)
20+
1921
if (NOT CMAKE_CXX_FLAGS)
2022
# Set default compile flags for the project
2123
if(MSVC)
@@ -31,8 +33,13 @@ if (NOT CMAKE_CXX_FLAGS)
3133
set(CMAKE_CXX_FLAGS "-Wall")
3234
endif()
3335
endif()
34-
# Use C++17
35-
set(CMAKE_CXX_STANDARD 17)
36+
37+
if(USE_CPP17)
38+
set(CMAKE_CXX_STANDARD 17)
39+
add_definitions("-D_USE_CPP17")
40+
else()
41+
set(CMAKE_CXX_STANDARD 11)
42+
endif()
3643

3744
# Set output directories
3845
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib)
@@ -104,32 +111,11 @@ if (NOT CPPKAFKA_PKGCONFIG_DIR)
104111
set(CPPKAFKA_PKGCONFIG_DIR share/pkgconfig)
105112
endif()
106113

107-
# Try to find the RdKafka configuration file if present.
108-
# This will search default system locations as well as RdKafka_ROOT and RdKafka_Dir paths if specified.
109-
find_package(RdKafka ${FIND_PACKAGE_QUIET} CONFIG)
110-
set(RDKAFKA_TARGET_IMPORTS ${RdKafka_FOUND})
111-
if (NOT RdKafka_FOUND)
112-
message(STATUS "RdKafkaConfig.cmake not found. Attempting to find module instead...")
113-
find_package(RdKafka REQUIRED ${FIND_PACKAGE_QUIET} MODULE)
114-
if (NOT RdKafka_FOUND)
115-
message(FATAL_ERROR "RdKafka module not found. Please set RDKAFKA_ROOT to the install path or RDKAFKA_DIR pointing to the RdKafka configuration file location.")
116-
else()
117-
message(STATUS "RdKafka module found.")
118-
endif()
119-
else()
120-
message(STATUS "RdKafka configuration file found: ${RdKafka_CONFIG}")
121-
endif()
122-
123-
add_subdirectory(src)
124-
add_subdirectory(include/cppkafka)
125-
126-
# Examples target
127-
if (NOT CPPKAFKA_DISABLE_EXAMPLES)
114+
if(NOT USE_CPP17 OR NOT CPPKAFKA_DISABLE_EXAMPLES OR NOT CPPKAFKA_DISABLE_TESTS)
128115
# Look for Boost (just need boost.optional headers here)
129-
find_package(Boost ${FIND_PACKAGE_QUIET} CONFIG)
116+
find_package(Boost REQUIRED ${FIND_PACKAGE_QUIET})
117+
130118
if (Boost_FOUND)
131-
option(CPPKAFKA_BOOST_STATIC_LIBS "Link with Boost static libraries." ON)
132-
option(CPPKAFKA_BOOST_USE_MULTITHREADED "Use Boost multithreaded libraries." ON)
133119
find_package(Boost COMPONENTS program_options ${FIND_PACKAGE_QUIET})
134120
set(Boost_USE_STATIC_LIBS ${CPPKAFKA_BOOST_STATIC_LIBS})
135121
set(Boost_USE_MULTITHREADED ${CPPKAFKA_BOOST_USE_MULTITHREADED})
@@ -142,12 +128,37 @@ if (NOT CPPKAFKA_DISABLE_EXAMPLES)
142128
message(STATUS "Boost is multi-threaded: ${CPPKAFKA_BOOST_USE_MULTITHREADED}")
143129
message(STATUS "Boost libraries: ${Boost_LIBRARIES}")
144130
endif()
131+
endif()
132+
133+
# Examples target
134+
if (Boost_PROGRAM_OPTIONS_FOUND)
145135
add_subdirectory(examples)
146136
else()
147137
message(STATUS "Disabling examples")
148138
endif()
149139
endif()
150140

141+
# Try to find the RdKafka configuration file if present.
142+
# This will search default system locations as well as RdKafka_ROOT and RdKafka_Dir paths if specified.
143+
find_package(RdKafka ${FIND_PACKAGE_QUIET} CONFIG)
144+
set(RDKAFKA_TARGET_IMPORTS ${RdKafka_FOUND})
145+
if (NOT RdKafka_FOUND)
146+
message(STATUS "RdKafkaConfig.cmake not found. Attempting to find module instead...")
147+
find_package(RdKafka REQUIRED ${FIND_PACKAGE_QUIET} MODULE)
148+
if (NOT RdKafka_FOUND)
149+
message(FATAL_ERROR "RdKafka module not found. Please set RDKAFKA_ROOT to the install path or RDKAFKA_DIR pointing to the RdKafka configuration file location.")
150+
else()
151+
message(STATUS "RdKafka module found.")
152+
endif()
153+
else()
154+
message(STATUS "RdKafka configuration file found: ${RdKafka_CONFIG}")
155+
endif()
156+
157+
add_subdirectory(src)
158+
add_subdirectory(include/cppkafka)
159+
160+
161+
151162
# Add a target to generate API documentation using Doxygen
152163
find_package(Doxygen ${FIND_PACKAGE_QUIET})
153164
if(DOXYGEN_FOUND)
@@ -188,4 +199,4 @@ if(NOT TARGET uninstall)
188199
# Add uninstall target
189200
add_custom_target(uninstall
190201
COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)
191-
endif()
202+
endif()

include/cppkafka/configuration.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
#include <functional>
3636
#include <initializer_list>
3737
#include <chrono>
38-
#include <optional>
38+
#include "utils/optional.h"
3939
#include <librdkafka/rdkafka.h>
4040
#include "topic_partition_list.h"
4141
#include "topic_configuration.h"
@@ -226,12 +226,12 @@ class CPPKAFKA_API Configuration : public ConfigurationBase<Configuration> {
226226
/**
227227
* Gets the default topic configuration
228228
*/
229-
const std::optional<TopicConfiguration>& get_default_topic_configuration() const;
229+
const cppkafka::optional<TopicConfiguration>& get_default_topic_configuration() const;
230230

231231
/**
232232
* Gets the default topic configuration
233233
*/
234-
std::optional<TopicConfiguration>& get_default_topic_configuration();
234+
cppkafka::optional<TopicConfiguration>& get_default_topic_configuration();
235235
private:
236236
using HandlePtr = ClonablePtr<rd_kafka_conf_t, decltype(&rd_kafka_conf_destroy),
237237
decltype(&rd_kafka_conf_dup)>;
@@ -240,7 +240,7 @@ class CPPKAFKA_API Configuration : public ConfigurationBase<Configuration> {
240240
static HandlePtr make_handle(rd_kafka_conf_t* ptr);
241241

242242
HandlePtr handle_;
243-
std::optional<TopicConfiguration> default_topic_config_;
243+
cppkafka::optional<TopicConfiguration> default_topic_config_;
244244
DeliveryReportCallback delivery_report_callback_;
245245
OffsetCommitCallback offset_commit_callback_;
246246
ErrorCallback error_callback_;

include/cppkafka/cppkafka.h

+2
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,13 @@
5757
#include <cppkafka/topic_configuration.h>
5858
#include <cppkafka/topic_partition.h>
5959
#include <cppkafka/topic_partition_list.h>
60+
#include <cppkafka/utils/any.h>
6061
#include <cppkafka/utils/backoff_committer.h>
6162
#include <cppkafka/utils/backoff_performer.h>
6263
#include <cppkafka/utils/buffered_producer.h>
6364
#include <cppkafka/utils/compacted_topic_processor.h>
6465
#include <cppkafka/utils/consumer_dispatcher.h>
66+
#include <cppkafka/utils/optional.h>
6567
#include <cppkafka/utils/poll_interface.h>
6668
#include <cppkafka/utils/poll_strategy_base.h>
6769
#include <cppkafka/utils/roundrobin_poll_strategy.h>

include/cppkafka/message.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
#include <cstdint>
3535
#include <chrono>
3636
#include <cassert>
37-
#include <optional>
3837
#include <librdkafka/rdkafka.h>
38+
#include "utils/optional.h"
3939
#include "buffer.h"
4040
#include "macros.h"
4141
#include "error.h"
@@ -189,7 +189,7 @@ class CPPKAFKA_API Message {
189189
*
190190
* If calling rd_kafka_message_timestamp returns -1, then boost::none_t will be returned.
191191
*/
192-
std::optional<MessageTimestamp> get_timestamp() const;
192+
cppkafka::optional<MessageTimestamp> get_timestamp() const;
193193

194194
#if RD_KAFKA_VERSION >= RD_KAFKA_MESSAGE_LATENCY_SUPPORT_VERSION
195195
/**

include/cppkafka/utils/compacted_topic_processor.h

+7-7
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
#include <string>
3535
#include <map>
3636
#include <set>
37-
#include<optional>
37+
#include"optional.h"
3838
#include "../buffer.h"
3939
#include "../consumer.h"
4040
#include "../macros.h"
@@ -106,8 +106,8 @@ class CPPKAFKA_API CompactedTopicEvent {
106106
EventType type_;
107107
std::string topic_;
108108
int partition_;
109-
std::optional<Key> key_;
110-
std::optional<Value> value_;
109+
cppkafka::optional<Key> key_;
110+
cppkafka::optional<Value> value_;
111111
};
112112

113113
template <typename Key, typename Value>
@@ -121,12 +121,12 @@ class CPPKAFKA_API CompactedTopicProcessor {
121121
/**
122122
* Callback used for decoding key objects
123123
*/
124-
using KeyDecoder = std::function<std::optional<Key>(const Buffer&)>;
124+
using KeyDecoder = std::function<cppkakfa::optional<Key>(const Buffer&)>;
125125

126126
/**
127127
* Callback used for decoding value objects
128128
*/
129-
using ValueDecoder = std::function<std::optional<Value>(const Key& key, const Buffer&)>;
129+
using ValueDecoder = std::function<cppkafka::optional<Value>(const Key& key, const Buffer&)>;
130130

131131
/**
132132
* Callback used for event handling
@@ -276,10 +276,10 @@ void CompactedTopicProcessor<Key, Value>::process_event() {
276276
Message message = consumer_.poll();
277277
if (message) {
278278
if (!message.get_error()) {
279-
std::optional<Key> key = key_decoder_(message.get_key());
279+
cppkafka::optional<Key> key = key_decoder_(message.get_key());
280280
if (key) {
281281
if (message.get_payload()) {
282-
std::optional<Value> value = value_decoder_(*key, message.get_payload());
282+
cppkafka::optional<Value> value = value_decoder_(*key, message.get_payload());
283283
if (value) {
284284
// If there's a payload and we managed to parse the value, generate a
285285
// SET_ELEMENT event

include/cppkafka/utils/poll_strategy_base.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#define CPPKAFKA_POLL_STRATEGY_BASE_H
3232

3333
#include <map>
34-
#include <any>
34+
#include "any.h"
3535
#include "../queue.h"
3636
#include "../topic_partition_list.h"
3737
#include "poll_interface.h"
@@ -45,7 +45,7 @@ namespace cppkafka {
4545
*/
4646
struct QueueData {
4747
Queue queue;
48-
std::any metadata;
48+
cppkafka::any metadata;
4949
};
5050

5151
/**

src/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ set_target_properties(${TARGET_NAME} PROPERTIES
4949
# In CMake >= 3.15 Boost::boost == Boost::headers
5050
target_link_libraries(${TARGET_NAME} PUBLIC RdKafka::rdkafka)
5151

52-
if(NOT CPPKAFKA_DISABLE_EXAMPLES)
52+
if(NOT USE_CPP17 OR NOT CPPKAFKA_DISABLE_EXAMPLES OR NOT CPPKAFKA_DISABLE_TESTS)
5353
target_link_libraries(${TARGET_NAME} PUBLIC Boost::boost)
5454
endif()
5555

src/configuration.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ using std::move;
4141
using std::vector;
4242
using std::initializer_list;
4343
using std::chrono::milliseconds;
44-
using std::optional;
44+
using cppkafka::optional;
4545

4646
namespace cppkafka {
4747

0 commit comments

Comments
 (0)