lfstack C++ API: implement overloaded wrappers with templates
[urcu.git] / include / urcu / lfstack.h
index 5a9bca368775db300cbd1bac44e5fbcd51c6ce47..b994ea6b5385285d7357da5c0a3fb2fc74eea257 100644 (file)
@@ -29,6 +29,7 @@ extern "C" {
 
 #include <stdbool.h>
 #include <pthread.h>
+#include <urcu/compiler.h>
 
 /*
  * Lock-free stack.
@@ -80,14 +81,17 @@ struct cds_lfs_stack {
 };
 
 /*
- * The transparent union allows calling functions that work on both
- * struct cds_lfs_stack and struct __cds_lfs_stack on any of those two
- * types.
+ * In C, the transparent union allows calling functions that work on
+ * both struct cds_lfs_stack and struct __cds_lfs_stack on any of those
+ * two types.
+ *
+ * In C++, implement static inline wrappers using function overloading
+ * to obtain an API similar to C.
  */
 typedef union {
        struct __cds_lfs_stack *_s;
        struct cds_lfs_stack *s;
-} __attribute__((__transparent_union__)) cds_lfs_stack_ptr_t;
+} caa_c_transparent_union cds_lfs_stack_ptr_t;
 
 #ifdef _LGPL_SOURCE
 
@@ -255,6 +259,49 @@ extern struct cds_lfs_head *__cds_lfs_pop_all(cds_lfs_stack_ptr_t s);
 
 #ifdef __cplusplus
 }
-#endif
+
+/*
+ * In C++, implement static inline wrappers using function overloading
+ * to obtain an API similar to C.
+ */
+
+static inline cds_lfs_stack_ptr_t cds_lfs_stack_cast(struct __cds_lfs_stack *s)
+{
+       cds_lfs_stack_ptr_t ret = {
+               ._s = s,
+       };
+       return ret;
+}
+
+static inline cds_lfs_stack_ptr_t cds_lfs_stack_cast(struct cds_lfs_stack *s)
+{
+       cds_lfs_stack_ptr_t ret = {
+               .s = s,
+       };
+       return ret;
+}
+
+template<typename T> static inline bool cds_lfs_empty(T s)
+{
+       return cds_lfs_empty(cds_lfs_stack_cast(s));
+}
+
+template<typename T> static inline bool cds_lfs_push(T s,
+                       struct cds_lfs_node *node)
+{
+       return cds_lfs_push(cds_lfs_stack_cast(s), node);
+}
+
+template<typename T> static inline struct cds_lfs_node *__cds_lfs_pop(T s)
+{
+       return __cds_lfs_pop(cds_lfs_stack_cast(s));
+}
+
+template<typename T> static inline struct cds_lfs_head *__cds_lfs_pop_all(T s)
+{
+       return __cds_lfs_pop_all(cds_lfs_stack_cast(s));
+}
+
+#endif /* __cplusplus */
 
 #endif /* _URCU_LFSTACK_H */
This page took 0.023877 seconds and 4 git commands to generate.