cpp-common/bt2c/fmt.hpp: use `wise_enum::string_type` in `EnableIfIsWiseEnum` definition
[babeltrace.git] / src / common / macros.h
index 23d4b310d573ab49c0f001d900fef04cf678b307..8120515c63fe5bbcd81672f8c7b4cf1df89d9ce8 100644 (file)
-#ifndef _BABELTRACE_INTERNAL_H
-#define _BABELTRACE_INTERNAL_H
-
 /*
- * Copyright 2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+ * SPDX-License-Identifier: MIT
  *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
+ * Copyright 2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
  */
 
+#ifndef _BABELTRACE_INTERNAL_H
+#define _BABELTRACE_INTERNAL_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef __cplusplus
+#define BT_EXTERN_C extern "C"
+#else
+#define BT_EXTERN_C
+#endif
 
 #define bt_max_t(type, a, b)   \
        ((type) (a) > (type) (b) ? (type) (a) : (type) (b))
 
 /*
- * BT_HIDDEN: set the hidden attribute for internal functions
- * On Windows, symbols are local unless explicitly exported,
- * see https://gcc.gnu.org/wiki/Visibility
+ * BT_EXPORT: set the visibility for exported functions.
  */
 #if defined(_WIN32) || defined(__CYGWIN__)
-#define BT_HIDDEN
+#define BT_EXPORT
 #else
-#define BT_HIDDEN __attribute__((visibility("hidden")))
+#define BT_EXPORT __attribute__((visibility("default")))
+#endif
+
+/*
+ * BT_NOEXCEPT: defined to `noexcept` if compiling as C++, else empty.
+ */
+#if defined(__cplusplus)
+#define BT_NOEXCEPT noexcept
+#else
+#define BT_NOEXCEPT
+#endif
+
+/* Enable `txt` if developer mode is enabled. */
+#ifdef BT_DEV_MODE
+#define BT_IF_DEV_MODE(txt) txt
+#else
+#define BT_IF_DEV_MODE(txt)
+#endif
+
+/*
+ * Yield `ref`'s value while setting `ref` to NULL.
+ *
+ * This can be used to give a strong reference to a callee:
+ *
+ *   add_foo_to_list(list, BT_MOVE_REF(foo));
+ *
+ * or in a simple assignment:
+ *
+ *   my_struct->foo = BT_MOVE_REF(foo);
+ *
+ * When moving a reference in a function call, the reference is given to the
+ * callee even if that function call fails, so make sure the called function
+ * is written accordingly.
+ */
+
+#define BT_MOVE_REF(ref)               \
+       ({                              \
+               __typeof__(ref) _ref = ref;     \
+               ref = NULL;             \
+               _ref;                   \
+       })
+
+/* Wrapper for g_array_index that adds bound checking.  */
+#define bt_g_array_index(a, t, i)              \
+       g_array_index((a), t, ({ BT_ASSERT_DBG((i) < (a)->len); (i); }))
+
+/*
+ * Copied from:
+ * <https://stackoverflow.com/questions/37411809/how-to-elegantly-fix-this-unused-variable-warning/37412551#37412551>:
+ *
+ * * sizeof() ensures that the expression is not evaluated at all, so
+ *   its side-effects don't happen. That is to be consistent with the
+ *   usual behaviour of debug-only constructs, such as assert().
+ *
+ * * `((_expr), 0)` uses the comma operator to swallow the actual type
+ *   of `(_expr)`. This is to prevent VLAs from triggering evaluation.
+ *
+ * * `(void)` explicitly ignores the result of `(_expr)` and sizeof() so
+ *   no "unused value" warning appears.
+ */
+
+#define BT_USE_EXPR(_expr)             ((void) sizeof((void) (_expr), 0))
+#define BT_USE_EXPR2(_expr1, _expr2)                                   \
+       ((void) sizeof((void) (_expr1), (void) (_expr2), 0))
+#define BT_USE_EXPR3(_expr1, _expr2, _expr3)                           \
+       ((void) sizeof((void) (_expr1), (void) (_expr2), (void) (_expr3), 0))
+#define BT_USE_EXPR4(_expr1, _expr2, _expr3, _expr4)                   \
+       ((void) sizeof((void) (_expr1), (void) (_expr2),                \
+               (void) (_expr3), (void) (_expr4), 0))
+#define BT_USE_EXPR5(_expr1, _expr2, _expr3, _expr4, _expr5)           \
+       ((void) sizeof((void) (_expr1), (void) (_expr2),                \
+               (void) (_expr3), (void) (_expr4), (void) (_expr5), 0))
+
+#define BT_DIAG_PUSH _Pragma ("GCC diagnostic push")
+#define BT_DIAG_POP _Pragma ("GCC diagnostic push")
+
+#define BT_DIAG_IGNORE_SHADOW _Pragma("GCC diagnostic ignored \"-Wshadow\"")
+
+#if defined __clang__
+#  if __has_warning("-Wunused-but-set-variable")
+#    define BT_DIAG_IGNORE_UNUSED_BUT_SET_VARIABLE \
+       _Pragma("GCC diagnostic ignored \"-Wunused-but-set-variable\"")
+#  endif
+#endif
+
+#if !defined BT_DIAG_IGNORE_UNUSED_BUT_SET_VARIABLE
+#  define BT_DIAG_IGNORE_UNUSED_BUT_SET_VARIABLE
+#endif
+
+#ifdef __cplusplus
+}
 #endif
 
 #endif
This page took 0.024029 seconds and 4 git commands to generate.