Skip to content

Commit 0ae4c0e

Browse files
authored
Add convenience macro for defining strong types (correctly) (#451)
1 parent 2101cb3 commit 0ae4c0e

2 files changed

Lines changed: 28 additions & 37 deletions

File tree

include/cuco/detail/utility/strong_type.cuh

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ namespace cuco::detail {
2121
* @brief A strong type wrapper
2222
*
2323
* @tparam T Type of the value
24+
*
2425
*/
25-
template <typename T>
26+
template <class T>
2627
struct strong_type {
2728
/**
2829
* @brief Constructs a strong type
@@ -42,3 +43,23 @@ struct strong_type {
4243
};
4344

4445
} // namespace cuco::detail
46+
47+
/**
48+
* @brief Convenience wrapper for defining a strong type
49+
*/
50+
#define CUCO_DEFINE_STRONG_TYPE(Name, Type) \
51+
struct Name : public cuco::detail::strong_type<Type> { \
52+
__host__ __device__ explicit constexpr Name(Type value) \
53+
: cuco::detail::strong_type<Type>(value) \
54+
{ \
55+
} \
56+
};
57+
58+
/**
59+
* @brief Convenience wrapper for defining a templated strong type
60+
*/
61+
#define CUCO_DEFINE_TEMPLATE_STRONG_TYPE(Name) \
62+
template <typename T> \
63+
struct Name : public cuco::detail::strong_type<T> { \
64+
__host__ __device__ explicit constexpr Name(T value) : cuco::detail::strong_type<T>(value) {} \
65+
};

include/cuco/sentinel.cuh

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,47 +20,17 @@
2020

2121
namespace cuco {
2222
/**
23-
* @brief A strong type wrapper used to denote the empty key sentinel.
24-
*
25-
* @tparam T Type of the key values
23+
* @brief A strong type wrapper `cuco::empty_key<Key>` used to denote the empty key sentinel.
2624
*/
27-
template <typename T>
28-
struct empty_key : public cuco::detail::strong_type<T> {
29-
/**
30-
* @brief Constructs an empty key sentinel with the given `v`.
31-
*
32-
* @param v The empty key sentinel value
33-
*/
34-
__host__ __device__ explicit constexpr empty_key(T v) : cuco::detail::strong_type<T>(v) {}
35-
};
25+
CUCO_DEFINE_TEMPLATE_STRONG_TYPE(empty_key);
3626

3727
/**
38-
* @brief A strong type wrapper used to denote the empty value sentinel.
39-
*
40-
* @tparam T Type of the mapped values
28+
* @brief A strong type wrapper `cuco::empty_value<T>` used to denote the empty value sentinel.
4129
*/
42-
template <typename T>
43-
struct empty_value : public cuco::detail::strong_type<T> {
44-
/**
45-
* @brief Constructs an empty value sentinel with the given `v`.
46-
*
47-
* @param v The empty value sentinel value
48-
*/
49-
__host__ __device__ explicit constexpr empty_value(T v) : cuco::detail::strong_type<T>(v) {}
50-
};
30+
CUCO_DEFINE_TEMPLATE_STRONG_TYPE(empty_value);
5131

5232
/**
53-
* @brief A strong type wrapper used to denote the erased key sentinel.
54-
*
55-
* @tparam T Type of the key values
33+
* @brief A strong type wrapper `cuco::erased_key<Key>` used to denote the erased key sentinel.
5634
*/
57-
template <typename T>
58-
struct erased_key : public cuco::detail::strong_type<T> {
59-
/**
60-
* @brief Constructs an erased key sentinel with the given `v`.
61-
*
62-
* @param v The erased key sentinel value
63-
*/
64-
__host__ __device__ explicit constexpr erased_key(T v) : cuco::detail::strong_type<T>(v) {}
65-
};
35+
CUCO_DEFINE_TEMPLATE_STRONG_TYPE(erased_key);
6636
} // namespace cuco

0 commit comments

Comments
 (0)