VLink  2.0.0
A high-performance communication middleware
macros.h File Reference

Cross-platform macros for visibility, branch hints, copy prevention, singletons and string helpers. 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)   VLINK_EXPORT __attribute__((aligned(align_num)))
 Marks a class or variable as both exported and aligned to align_num bytes. More...
 
#define __has_builtin(x)   0
 
#define VLINK_LIKELY(x)   (x)
 
#define VLINK_UNLIKELY(x)   (x)
 
#define VLINK_DISALLOW_COPY_AND_ASSIGN(classname)
 Deletes the copy constructor and copy-assignment operator of classname. More...
 
#define VLINK_SINGLETON_CHECK(classname)
 Embeds a static check that enforces one-instance-per-process for classname. More...
 
#define VLINK_SINGLETON_DECLARE(classname)
 Declares a Meyers singleton get and the matching uniqueness guards on classname. More...
 
#define VLINK_MACRO_STRING_IFY(name)   #name
 Stringifies a token without performing macro expansion. More...
 
#define VLINK_MACRO_STRING_GET(name)   VLINK_MACRO_STRING_IFY(name)
 Stringifies the expanded value of a macro (two-step expansion). More...
 
#define VLINK_ASSERT_CONSTANT(msg)
 
#define VLIKELY(...)   VLINK_LIKELY(__VA_ARGS__)
 Short alias for VLINK_LIKELY. More...
 
#define VUNLIKELY(...)   VLINK_UNLIKELY(__VA_ARGS__)
 Short alias for VLINK_UNLIKELY. More...
 

Detailed Description

Cross-platform macros for visibility, branch hints, copy prevention, singletons and string helpers.

Every header in VLink relies on this file for ABI export decoration, alignment, branch prediction hints, and class-level boilerplate. Macros that depend on compiler features gracefully degrade to no-ops on platforms that do not expose those features.

Macro reference
Category Macro Purpose
Visibility VLINK_EXPORT DLL / shared-object visibility
Visibility + align VLINK_EXPORT_AND_ALIGNED(n) Combine export with byte alignment
Branch hints (long) VLINK_LIKELY(x), VLINK_UNLIKELY(x) C++20 [[likely]] / __builtin_expect
Branch hints (short) VLIKELY(...), VUNLIKELY(...) Aliases of VLINK_LIKELY / VLINK_UNLIKELY
Class hygiene VLINK_DISALLOW_COPY_AND_ASSIGN(class) Delete copy ctor and assignment
Singletons VLINK_SINGLETON_CHECK(class) Enforce one instance per process
Singletons VLINK_SINGLETON_DECLARE(class) Add get accessor + check + no-copy
Stringify VLINK_MACRO_STRING_IFY(name) Stringify a token directly
Stringify (eval) VLINK_MACRO_STRING_GET(name) Stringify after macro expansion
Constant check VLINK_ASSERT_CONSTANT(msg) static_assert message is a literal
Example
class VLINK_EXPORT MyService final {
public:
MyService();
};
if VLIKELY (ptr != nullptr) {
ptr->do_something();
}
#define VLINK_EXPORT
Definition: macros.h:81
#define VLIKELY(...)
Short alias for VLINK_LIKELY.
Definition: macros.h:284
#define VLINK_DISALLOW_COPY_AND_ASSIGN(classname)
Deletes the copy constructor and copy-assignment operator of classname.
Definition: macros.h:174
Note
VLINK_SINGLETON_CHECK enforces uniqueness at run time by tripping a std::atomic_flag the second time the constructor runs; the second instantiation throws std::runtime_error.

Macro Definition Documentation

◆ __has_builtin

#define __has_builtin (   x)    0

◆ VLIKELY

#define VLIKELY (   ...)    VLINK_LIKELY(__VA_ARGS__)

Short alias for VLINK_LIKELY.

◆ 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.

Drop the macro inside the private section of a class to prevent accidental copying.

Parameters
classnameUnqualified name of the enclosing class.

◆ VLINK_EXPORT

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

◆ VLINK_EXPORT_AND_ALIGNED

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

Marks a class or variable as both exported and aligned to align_num bytes.

Expands to __declspec(align(align_num)) on MSVC and to attribute((aligned(align_num))) on GCC / Clang.

Parameters
align_numRequired alignment in bytes; must be a power of two.

◆ VLINK_LIKELY

#define VLINK_LIKELY (   x)    (x)

◆ VLINK_MACRO_STRING_GET

#define VLINK_MACRO_STRING_GET (   name)    VLINK_MACRO_STRING_IFY(name)

Stringifies the expanded value of a macro (two-step expansion).

Use this form instead of VLINK_MACRO_STRING_IFY when name is itself a macro that should be expanded before stringification.

Parameters
nameMacro whose expansion is stringified.

◆ VLINK_MACRO_STRING_IFY

#define VLINK_MACRO_STRING_IFY (   name)    #name

Stringifies a token without performing macro expansion.

Parameters
nameToken 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(std::memory_order_seq_cst)) { \
throw std::runtime_error("Constructor has been instantiated."); \
} \
} \
}; \
\
ConstructorChecker constructor_checker_;
#define VLINK_UNLIKELY(x)
Definition: macros.h:162

Embeds a static check that enforces one-instance-per-process for classname.

Combines two safeguards:

  • Compile-time static_assert that classname is final and not externally default-constructible.
  • Run-time std::atomic_flag trip that throws std::runtime_error on the second instantiation.
Parameters
classnameUnqualified name of the enclosing class.
Note
Place inside the private section of the class body.

◆ 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 Meyers singleton get and the matching uniqueness guards on classname.

Expands to a public static get returning a reference to the single instance plus the VLINK_SINGLETON_CHECK and VLINK_DISALLOW_COPY_AND_ASSIGN macros.

Parameters
classnameUnqualified 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 Meyers singleton get and the matching uniqueness guards on classname.
Definition: macros.h:232

◆ VLINK_UNLIKELY

#define VLINK_UNLIKELY (   x)    (x)

◆ VUNLIKELY

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

Short alias for VLINK_UNLIKELY.