Cross-platform macros for visibility, branch hints, copy prevention, singletons and string helpers.
More...
Go to the source code of this file.
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
- Example
public:
MyService();
};
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.
◆ __has_builtin
| #define __has_builtin |
( |
|
x | ) |
0 |
◆ VLIKELY
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
-
| classname | 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 | ) |
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_num | Required alignment in bytes; must be a power of two. |
◆ VLINK_LIKELY
| #define VLINK_LIKELY |
( |
|
x | ) |
(x) |
◆ VLINK_MACRO_STRING_GET
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
-
| name | Macro whose expansion is stringified. |
◆ VLINK_MACRO_STRING_IFY
| #define VLINK_MACRO_STRING_IFY |
( |
|
name | ) |
#name |
Stringifies a token without performing macro expansion.
- Parameters
-
◆ 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; \
\
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
-
| classname | Unqualified 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
-
| classname | Unqualified name of the singleton class. |
- Example
class MyService final {
public:
void do_work();
private:
MyService() = default;
};
#define VLINK_SINGLETON_DECLARE(classname)
Declares a Meyers singleton get and the matching uniqueness guards on classname.
Definition: macros.h:232
constexpr std::string_view get() noexcept
Returns the trimmed compile-time identifier for TypeT.
Definition: name_detector.h:603
◆ VLINK_UNLIKELY
| #define VLINK_UNLIKELY |
( |
|
x | ) |
(x) |
◆ VUNLIKELY
Short alias for VLINK_UNLIKELY.