VLink 2.0.0
A high-performance communication middleware
Loading...
Searching...
No Matches
macros.h File Reference

Platform-independent macro definitions for the VLink library. More...

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define VLINK_EXPORT   __attribute__((visibility("default")))
#define VLINK_EXPORT_AND_ALIGNED(align_num)
 Marks a class or variable as both exported and aligned to the given byte boundary.
#define __has_builtin(x)
#define VLINK_LIKELY(x)
#define VLINK_UNLIKELY(x)
#define VLINK_DISALLOW_COPY_AND_ASSIGN(classname)
 Deletes the copy constructor and copy-assignment operator of classname.
#define VLINK_SINGLETON_CHECK(classname)
 Injects a compile-time and run-time singleton enforcement guard.
#define VLINK_SINGLETON_DECLARE(classname)
 Declares a complete Meyer's-singleton get() method with safety guards.
#define VLINK_MACRO_STRING_IFY(name)
 Converts name to a C string literal using the preprocessor stringification operator.
#define VLINK_MACRO_STRING_GET(name)
 Stringifies the expanded value of macro name (two-level expansion).
#define VLINK_ASSERT_CONSTANT(msg)
#define VLIKELY(...)
 Shorthand alias for VLINK_LIKELY. Hints that the expression is likely true.
#define VUNLIKELY(...)
 Shorthand alias for VLINK_UNLIKELY. Hints that the expression is unlikely true.

Detailed Description

Platform-independent macro definitions for the VLink library.

This header provides a unified set of macros used throughout VLink to handle platform differences, visibility control, branch prediction hints, and common class-level utilities such as singleton enforcement and copy-prevention.

The macros are grouped as follows:

Group Macros Purpose
Symbol visibility VLINK_EXPORT, VLINK_EXPORT_AND_ALIGNED DLL export / visibility control
Branch hints VLINK_LIKELY, VLINK_UNLIKELY, VLIKELY, ... Branch prediction optimization
Copy prevention VLINK_DISALLOW_COPY_AND_ASSIGN Disable copy constructor/operator
Singleton guard VLINK_SINGLETON_CHECK, VLINK_SINGLETON_DECLARE Enforce single-instance classes
String utilities VLINK_MACRO_STRING_IFY, VLINK_MACRO_STRING_GET Stringify macro tokens
Constant check VLINK_ASSERT_CONSTANT Static-assert constant string args
Note
  • All macros are designed to be no-ops when the relevant feature is unavailable (e.g., VLINK_LIKELY falls back to a plain cast on platforms without __builtin_expect).
  • The VLINK_EXPORT macro must be included before any class or function declaration that needs to be part of the public ABI.
  • VLINK_SINGLETON_CHECK enforces that a class can only ever be instantiated once per process via a static atomic flag; any second instantiation will throw std::runtime_error.
Example
// Marking a class as exported:
class VLINK_EXPORT MyClass final { ... };
// Preventing copy and assignment:
class MyClass {
};
// Branch prediction hint:
if VLIKELY(ptr != nullptr) {
ptr->do_something();
}
#define VLINK_EXPORT
Definition macros.h:85
#define VLIKELY(...)
Shorthand alias for VLINK_LIKELY. Hints that the expression is likely true.
Definition macros.h:297
#define VLINK_DISALLOW_COPY_AND_ASSIGN(classname)
Deletes the copy constructor and copy-assignment operator of classname.
Definition macros.h:184

Macro Definition Documentation

◆ __has_builtin

#define __has_builtin ( x)
Value:
0

◆ VLIKELY

#define VLIKELY ( ...)
Value:
VLINK_LIKELY(__VA_ARGS__)
#define VLINK_LIKELY(x)
Definition macros.h:166

Shorthand alias for VLINK_LIKELY. Hints that the expression is likely true.

◆ VLINK_ASSERT_CONSTANT

#define VLINK_ASSERT_CONSTANT ( msg)

◆ VLINK_DISALLOW_COPY_AND_ASSIGN

#define VLINK_DISALLOW_COPY_AND_ASSIGN ( classname)
Value:
classname(const classname&) = delete; \
classname& operator=(const classname&) = delete;

Deletes the copy constructor and copy-assignment operator of classname.

Place this macro in the private section of a class to prevent accidental copying. It expands to:

classname(const classname&) = delete;
classname& operator=(const classname&) = delete;
Parameters
classnameThe unqualified name of the enclosing class.

◆ VLINK_EXPORT

#define VLINK_EXPORT   __attribute__((visibility("default")))

◆ VLINK_EXPORT_AND_ALIGNED

#define VLINK_EXPORT_AND_ALIGNED ( align_num)
Value:
VLINK_EXPORT __attribute__((aligned(align_num)))

Marks a class or variable as both exported and aligned to the given byte boundary.

On MSVC this expands to __declspec(align(align_num)); on GCC/Clang it expands to __attribute__((aligned(align_num))).

Parameters
align_numThe required alignment in bytes (must be a power of two).

◆ VLINK_LIKELY

#define VLINK_LIKELY ( x)
Value:
(x)

◆ VLINK_MACRO_STRING_GET

#define VLINK_MACRO_STRING_GET ( name)
Value:
#define VLINK_MACRO_STRING_IFY(name)
Converts name to a C string literal using the preprocessor stringification operator.
Definition macros.h:261

Stringifies the expanded value of macro name (two-level expansion).

This two-step macro ensures that name is fully expanded before stringification. Use this instead of VLINK_MACRO_STRING_IFY when name is itself a macro that should be expanded first.

Parameters
nameA macro whose expansion will be converted to a string literal.

◆ VLINK_MACRO_STRING_IFY

#define VLINK_MACRO_STRING_IFY ( name)
Value:
#name

Converts name to a C string literal using the preprocessor stringification operator.

Parameters
nameA macro token (not a string literal) to stringify.

◆ VLINK_SINGLETON_CHECK

#define VLINK_SINGLETON_CHECK ( classname)
Value:
private: \
struct ConstructorChecker { \
inline ConstructorChecker() { \
static_assert(std::is_final_v<classname>, "Constructor must be final."); \
static_assert(!std::is_default_constructible_v<classname>, "Constructor must be private."); \
\
static std::atomic_flag flag; \
\
if VLINK_UNLIKELY (flag.test_and_set()) { \
throw std::runtime_error("Constructor has been instantiated."); \
} \
} \
}; \
\
ConstructorChecker constructor_checker_;
#define VLINK_UNLIKELY(x)
Definition macros.h:167

Injects a compile-time and run-time singleton enforcement guard.

This macro:

  • Uses static_assert to verify that classname is final and has a private (non-default-constructible-from-outside) constructor.
  • Uses a std::atomic_flag to guarantee that the constructor is only invoked once per process. A second instantiation throws std::runtime_error.
Note
Must be placed in the private section of the class body.
Parameters
classnameThe unqualified name of the class being protected.

◆ VLINK_SINGLETON_DECLARE

#define VLINK_SINGLETON_DECLARE ( classname)
Value:
public: \
inline static classname& get() { \
static classname singleton = classname(); \
return singleton; \
} \
\
private: \
VLINK_SINGLETON_CHECK(classname) \
VLINK_DISALLOW_COPY_AND_ASSIGN(classname)

Declares a complete Meyer's-singleton get() method with safety guards.

Combines VLINK_SINGLETON_CHECK with a public static get() accessor that returns a reference to the single static instance. The constructor must be private and final, otherwise the embedded static_assert will fail.

Parameters
classnameThe unqualified name of the singleton class.
Example
class MyService final {
public:
void do_work();
private:
MyService() = default;
};
MyService::get().do_work();
#define VLINK_SINGLETON_DECLARE(classname)
Declares a complete Meyer's-singleton get() method with safety guards.
Definition macros.h:244

◆ VLINK_UNLIKELY

#define VLINK_UNLIKELY ( x)
Value:
(x)

◆ VUNLIKELY

#define VUNLIKELY ( ...)
Value:
VLINK_UNLIKELY(__VA_ARGS__)

Shorthand alias for VLINK_UNLIKELY. Hints that the expression is unlikely true.