计算属性自动检查

This commit is contained in:
2026-08-11 11:54:09 +08:00
parent 11407faf86
commit e91f134f16
14 changed files with 305 additions and 51 deletions
+8 -4
View File
@@ -508,21 +508,25 @@ This acquires all writable managed lock domains; it does not perform validation
A synchronized computed property receives a read view:
```cpp
computed_property<Device, int>([](const auto& view) {
computed_property<Device, int>(depends_on<&Device::min_speed, &Device::max_speed>, [](const auto& view) {
return view.template get<&Device::max_speed>() - view.template get<&Device::min_speed>();
}, key<"speed_span">)
```
Writable dependencies that must form one snapshot should share the computed synchronization domain:
`depends_on<...>` makes dependencies explicit Schema facts. `depends_on_keys<"a", "b">` is available when key-based declaration is more appropriate. A computed view can read only its declared direct dependencies.
Writable dependencies that must form one snapshot should share a synchronization domain with each other:
```cpp
synchronization(
sync_all_independent,
sync_group("speed", "min_speed", "max_speed", "speed_span")
sync_group("speed", "min_speed", "max_speed")
)
```
A read-only stored dependency may be read through the computed view without joining a lock slot, because it has no managed writer.
Do not put the computed property itself in a synchronization rule. Its read slot is derived from the dependency graph; a schema or per-instance topology is rejected when synchronized dependencies do not resolve to one domain. Read-only stored dependencies need no lock slot.
`schema_property_dependency_indices<Schema, Index>()` exposes the direct dependency indices for schema consumers.
## 21. Trusted accessor properties
+8 -4
View File
@@ -500,21 +500,25 @@ device.with_all_writable_locked([&](auto& guard) {
Synchronized computed property 接收 read view
```cpp
computed_property<Device, int>([](const auto& view) {
computed_property<Device, int>(depends_on<&Device::min_speed, &Device::max_speed>, [](const auto& view) {
return view.template get<&Device::max_speed>() - view.template get<&Device::min_speed>();
}, key<"speed_span">)
```
如果 writable dependency 需要同一快照,应共享 computed synchronization domain
`depends_on<...>` 把 dependency 变成 Schema 的显式结构事实。也可以通过 `depends_on_keys<"a", "b">` 按 key 声明。Computed view 只能读取已声明的直接 dependency。
如果 writable dependency 需要同一快照,只需要让这些 writable dependency 共享 synchronization domain
```cpp
synchronization(
sync_all_independent,
sync_group("speed", "min_speed", "max_speed", "speed_span")
sync_group("speed", "min_speed", "max_speed")
)
```
Read-only stored dependency 可以直接通过 computed view 读取,不需要加入 lock slot,因为它没有 managed writer
Computed Property 自身不要加入 synchronization rule。它的读取 slot 根据 dependency graph 自动推导;如果需要同步的 dependency 不在同一个 domainSchema/实例 synchronization topology 会被拒绝。Read-only stored dependency 不需要 lock slot。
Schema 可通过 `schema_property_dependency_indices<Schema, Index>()` 读取某个 Property 的直接 dependency index。
## 21. Trusted Accessor Property
+2 -2
View File
@@ -213,9 +213,9 @@ A computed property may itself be read-only while depending on writable fields.
The computed property does not contain writable storage. However, its read may require a stable snapshot of mutable dependencies.
`Synchronized_Computed_Accessor` therefore uses a synchronized read view. Writable dependencies must share the computed consistency domain when the computed expression needs an atomic snapshot.
Dependencies must be explicit Schema facts. The `Synchronized_Computed_Accessor` read view can access only declared direct dependencies, and the computed property's read slot is derived from the dependency graph instead of requiring its key to be repeated in a synchronization group.
Read-only stored dependencies are safe to read directly through the synchronized view because they have no managed writer.
Writable dependencies that require one atomic snapshot must share one synchronization domain with each other. Read-only stored dependencies are safe to read directly through the synchronized view because they have no managed writer.
### Rule
+2 -2
View File
@@ -211,9 +211,9 @@ Computed Property 自身可以只读,但它可能依赖可写字段。
Computed value 本身没有可写存储,但读取它时可能需要 mutable dependency 的一致快照。
`Synchronized_Computed_Accessor` 因此使用 synchronized read view。需要原子一致性的 writable dependency 应和 computed property 处于同一个同步域
Dependency 必须显式进入 Schema。`Synchronized_Computed_Accessor` 的 read view 只能读取声明过的直接 dependencycomputed property 的读取 slot 从 dependency graph 推导,而不是由调用方再次把 computed key 写进 synchronization group
Read-only stored dependency 没有 managed writer,所以可以直接读取而无需锁。
需要原子一致性的 writable dependency 应彼此处于同一个同步域。Read-only stored dependency 没有 managed writer,所以可以直接读取而无需锁。
### 原则