morph 0.1.0
Asynchronous bridge between GUI thread and business models
Loading...
Searching...
No Matches
attributes.hpp
1// SPDX-License-Identifier: Apache-2.0
2
3#pragma once
4
5/// @brief Declares that the annotated parameter is *borrowed*, not owned: what
6/// it refers to must outlive the value the call produces.
7///
8/// Applied to a function parameter, it says the return value may refer to that
9/// parameter's referent. Applied to the implicit object parameter — written
10/// after the parameter list and any `const`/`noexcept` — it says the same of
11/// `*this`, which is the shape of every accessor that hands out a reference
12/// into a member, and of every fluent method that returns `*this`. Applied to a
13/// constructor parameter, it says the constructed object keeps referring to the
14/// argument for as long as it lives, which is the "must outlive" half of the
15/// destruction-ordering rules in `docs/spec/concurrency_and_lifetimes.md`.
16///
17/// The attribute states a contract that already holds; it generates no code and
18/// changes no behaviour. What it buys is that Clang can now diagnose a call site
19/// that breaks the contract — and, under `-Weverything`, that it stops asking
20/// for the annotation on every declaration that visibly needs one.
21///
22/// Clang spells it `[[clang::lifetimebound]]` and MSVC `[[msvc::lifetimebound]]`;
23/// GCC has no equivalent, and a raw `[[clang::…]]` there is a `-Wattributes`
24/// diagnostic — a build break under the project's `-Werror`. So every use in
25/// morph goes through this macro, which expands to nothing on a compiler that
26/// does not know the attribute.
27#ifdef __has_cpp_attribute
28#if __has_cpp_attribute(clang::lifetimebound)
29#define MORPH_LIFETIMEBOUND [[clang::lifetimebound]]
30#elif __has_cpp_attribute(msvc::lifetimebound)
31#define MORPH_LIFETIMEBOUND [[msvc::lifetimebound]]
32#endif
33#endif
34
35#ifndef MORPH_LIFETIMEBOUND
36#define MORPH_LIFETIMEBOUND
37#endif