Skip to content

Commit a823dd9

Browse files
committed
refactor: add GUARD macro for RAII with C pointers and deleters
1 parent 358326b commit a823dd9

File tree

5 files changed

+33
-15
lines changed

5 files changed

+33
-15
lines changed

include/snctl-cpp/raii_helper.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Copyright 2025 Yunze Xu
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#pragma once
17+
18+
// Call the deleter on ptr when it goes out of scope
19+
// ptr must be a pointer type T*
20+
// deleter must be a function pointer type void(*)(T*)
21+
#define GUARD(ptr, deleter) \
22+
std::unique_ptr<std::remove_reference_t<decltype(*(ptr))>, \
23+
decltype((deleter))> \
24+
ptr##_guard{ptr, deleter};

include/snctl-cpp/topics/create_topic.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
#pragma once
1717

18+
#include "snctl-cpp/raii_helper.h"
1819
#include "snctl-cpp/rk_event_wrapper.h"
1920
#include <array>
2021
#include <iostream>
@@ -33,9 +34,7 @@ inline void create_topic(rd_kafka_t *rk, rd_kafka_queue_t *rkqu,
3334
throw std::runtime_error("Failed to create topic: " +
3435
std::string(errstr.data()));
3536
}
36-
std::unique_ptr<std::remove_reference_t<decltype(*rk_topic)>,
37-
decltype(&rd_kafka_NewTopic_destroy)>
38-
rk_topic_guard{rk_topic, &rd_kafka_NewTopic_destroy};
37+
GUARD(rk_topic, rd_kafka_NewTopic_destroy);
3938

4039
rd_kafka_CreateTopics(rk, &rk_topic, 1, nullptr, rkqu);
4140

include/snctl-cpp/topics/delete_topic.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
#pragma once
1717

18+
#include "snctl-cpp/raii_helper.h"
1819
#include "snctl-cpp/rk_event_wrapper.h"
1920
#include <iostream>
2021
#include <librdkafka/rdkafka.h>
@@ -26,9 +27,7 @@
2627
inline void delete_topic(rd_kafka_t *rk, rd_kafka_queue_t *rkqu,
2728
const std::string &topic) {
2829
auto rk_topic = rd_kafka_DeleteTopic_new(topic.c_str());
29-
std::unique_ptr<std::remove_reference_t<decltype(*rk_topic)>,
30-
decltype(&rd_kafka_DeleteTopic_destroy)>
31-
rk_topic_guard{rk_topic, &rd_kafka_DeleteTopic_destroy};
30+
GUARD(rk_topic, rd_kafka_DeleteTopic_destroy);
3231

3332
rd_kafka_DeleteTopics(rk, &rk_topic, 1, nullptr, rkqu);
3433

include/snctl-cpp/topics/describe_topic.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
#pragma once
1717

18+
#include "snctl-cpp/raii_helper.h"
1819
#include "snctl-cpp/rk_event_wrapper.h"
1920
#include <iostream>
2021
#include <librdkafka/rdkafka.h>
@@ -25,9 +26,7 @@ inline void describe_topic(rd_kafka_t *rk, rd_kafka_queue_t *rkqu,
2526
const std::string &topic) {
2627
const char *topics[] = {topic.c_str()};
2728
auto topic_names = rd_kafka_TopicCollection_of_topic_names(topics, 1);
28-
std::unique_ptr<std::remove_reference_t<decltype(*topic_names)>,
29-
decltype(&rd_kafka_TopicCollection_destroy)>
30-
topic_names_guard{topic_names, &rd_kafka_TopicCollection_destroy};
29+
GUARD(topic_names, rd_kafka_TopicCollection_destroy);
3130

3231
rd_kafka_DescribeTopics(rk, topic_names, nullptr, rkqu);
3332

src/main.cc

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <type_traits>
3030
#include <vector>
3131

32+
#include "snctl-cpp/raii_helper.h"
3233
#include "snctl-cpp/topics.h"
3334

3435
int main(int argc, char *argv[]) {
@@ -137,14 +138,10 @@ int main(int argc, char *argv[]) {
137138
if (!rk) {
138139
fail("create producer");
139140
}
140-
std::unique_ptr<std::remove_reference_t<decltype(*rk)>,
141-
decltype(&rd_kafka_destroy)>
142-
rk_guard{rk, &rd_kafka_destroy};
141+
GUARD(rk, rd_kafka_destroy);
143142

144143
auto rkqu = rd_kafka_queue_new(rk);
145-
std::unique_ptr<std::remove_reference_t<decltype(*rkqu)>,
146-
decltype(&rd_kafka_queue_destroy)>
147-
rkque_guard{rkqu, &rd_kafka_queue_destroy};
144+
GUARD(rkqu, rd_kafka_queue_destroy);
148145

149146
if (program.is_subcommand_used(topics.handle())) {
150147
return topics.run(rk, rkqu) ? 0 : 1;

0 commit comments

Comments
 (0)