Skip to content

Commit 1633f5e

Browse files
committed
util, refactor: Add UNIQUE_NAME helper macro
This change replaces repetitive code with a helper macro.
1 parent 1e8aa02 commit 1633f5e

File tree

5 files changed

+11
-8
lines changed

5 files changed

+11
-8
lines changed

src/logging/timer.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,13 @@ class Timer
9797

9898

9999
#define LOG_TIME_MICROS_WITH_CATEGORY(end_msg, log_category) \
100-
BCLog::Timer<std::chrono::microseconds> PASTE2(logging_timer, __COUNTER__)(__func__, end_msg, log_category)
100+
BCLog::Timer<std::chrono::microseconds> UNIQUE_NAME(logging_timer)(__func__, end_msg, log_category)
101101
#define LOG_TIME_MILLIS_WITH_CATEGORY(end_msg, log_category) \
102-
BCLog::Timer<std::chrono::milliseconds> PASTE2(logging_timer, __COUNTER__)(__func__, end_msg, log_category)
102+
BCLog::Timer<std::chrono::milliseconds> UNIQUE_NAME(logging_timer)(__func__, end_msg, log_category)
103103
#define LOG_TIME_MILLIS_WITH_CATEGORY_MSG_ONCE(end_msg, log_category) \
104-
BCLog::Timer<std::chrono::milliseconds> PASTE2(logging_timer, __COUNTER__)(__func__, end_msg, log_category, /* msg_on_completion=*/false)
104+
BCLog::Timer<std::chrono::milliseconds> UNIQUE_NAME(logging_timer)(__func__, end_msg, log_category, /* msg_on_completion=*/false)
105105
#define LOG_TIME_SECONDS(end_msg) \
106-
BCLog::Timer<std::chrono::seconds> PASTE2(logging_timer, __COUNTER__)(__func__, end_msg)
106+
BCLog::Timer<std::chrono::seconds> UNIQUE_NAME(logging_timer)(__func__, end_msg)
107107

108108

109109
#endif // BITCOIN_LOGGING_TIMER_H

src/sync.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,12 @@ class SCOPED_LOCKABLE UniqueLock : public Base
218218
friend class reverse_lock;
219219
};
220220

221-
#define REVERSE_LOCK(g) typename std::decay<decltype(g)>::type::reverse_lock PASTE2(revlock, __COUNTER__)(g, #g, __FILE__, __LINE__)
221+
#define REVERSE_LOCK(g) typename std::decay<decltype(g)>::type::reverse_lock UNIQUE_NAME(revlock)(g, #g, __FILE__, __LINE__)
222222

223223
template<typename MutexArg>
224224
using DebugLock = UniqueLock<typename std::remove_reference<typename std::remove_pointer<MutexArg>::type>::type>;
225225

226-
#define LOCK(cs) DebugLock<decltype(cs)> PASTE2(criticalblock, __COUNTER__)(cs, #cs, __FILE__, __LINE__)
226+
#define LOCK(cs) DebugLock<decltype(cs)> UNIQUE_NAME(criticalblock)(cs, #cs, __FILE__, __LINE__)
227227
#define LOCK2(cs1, cs2) \
228228
DebugLock<decltype(cs1)> criticalblock1(cs1, #cs1, __FILE__, __LINE__); \
229229
DebugLock<decltype(cs2)> criticalblock2(cs2, #cs2, __FILE__, __LINE__);

src/test/util/logging.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,6 @@ class DebugLogHelper
3636
~DebugLogHelper() { check_found(); }
3737
};
3838

39-
#define ASSERT_DEBUG_LOG(message) DebugLogHelper PASTE2(debugloghelper, __COUNTER__)(message)
39+
#define ASSERT_DEBUG_LOG(message) DebugLogHelper UNIQUE_NAME(debugloghelper)(message)
4040

4141
#endif // BITCOIN_TEST_UTIL_LOGGING_H

src/util/epochguard.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#define BITCOIN_UTIL_EPOCHGUARD_H
88

99
#include <threadsafety.h>
10+
#include <util/macros.h>
1011

1112
#include <cassert>
1213

@@ -96,6 +97,6 @@ class LOCKABLE Epoch
9697
}
9798
};
9899

99-
#define WITH_FRESH_EPOCH(epoch) const Epoch::Guard PASTE2(epoch_guard_, __COUNTER__)(epoch)
100+
#define WITH_FRESH_EPOCH(epoch) const Epoch::Guard UNIQUE_NAME(epoch_guard_)(epoch)
100101

101102
#endif // BITCOIN_UTIL_EPOCHGUARD_H

src/util/macros.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#define PASTE(x, y) x ## y
99
#define PASTE2(x, y) PASTE(x, y)
1010

11+
#define UNIQUE_NAME(name) PASTE2(name, __COUNTER__)
12+
1113
/**
1214
* Converts the parameter X to a string after macro replacement on X has been performed.
1315
* Don't merge these into one macro!

0 commit comments

Comments
 (0)