openbim_ifc_capi/inverse.rs
1//! `INVERSE` attributes and the entities referencing one (#462).
2//!
3//! `openbim_ifc_v0_1_entity_attribute_by_name` reads an `INVERSE` name
4//! (`IsDefinedBy`) as well: a `LIST` of `REF`s for a `SET` or `BAG`
5//! (`SET [0:1]` included: zero or one element), a `REF` or `NULL` for a
6//! single-valued inverse. These list the declarations and the referrers,
7//! through the shared core's reverse index, built on first use and again
8//! only after the model changes.
9
10use openbim_ifc_binding_core::record::to_records;
11
12use crate::buffer::put;
13use crate::domains::{list_export, Out};
14use crate::model::{done, with_model, OpenbimIfcModel};
15use crate::status::{boundary, OpenbimIfcStatus};
16use crate::tape::OpenbimIfcValueNode;
17
18/// Every `INVERSE` attribute of entity `id`, inherited ones first: a
19/// `LIST` of `InverseAttributeInfo` records, their number in `out_count`.
20/// `InverseAttributeInfo`: name (`TEXT`, the schema's spelling), entity
21/// (`TEXT`, the referencing type before `FOR`), for attribute (`TEXT`, its
22/// attribute after `FOR`), aggregate (`BOOL`: a `SET` or `BAG`, read as a
23/// `LIST`), lower (`INTEGER`), upper (`INTEGER`, or `NULL` for `?`),
24/// declared by (`TEXT`, the entity introducing it).
25///
26/// `MissingEntity`; `UnsupportedSchema` as for
27/// `openbim_ifc_v0_1_entity_attribute_names`.
28///
29/// # Safety
30/// As for `openbim_ifc_v0_1_entity_attribute_names`.
31#[no_mangle]
32pub unsafe extern "C" fn openbim_ifc_v0_1_entity_inverse_attribute_names(
33 model: OpenbimIfcModel,
34 id: u64,
35 out_count: *mut usize,
36 nodes: *mut OpenbimIfcValueNode,
37 node_capacity: usize,
38 out_nodes_required: *mut usize,
39 strings: *mut u8,
40 string_capacity: usize,
41 out_strings_required: *mut usize,
42) -> OpenbimIfcStatus {
43 let out = Out {
44 nodes,
45 node_capacity,
46 out_nodes_required,
47 strings,
48 string_capacity,
49 out_strings_required,
50 };
51 // SAFETY: caller contract above.
52 unsafe {
53 list_export(model, out_count, out, |m| {
54 Ok(to_records(&m.inverse_attribute_names(id)?))
55 })
56 }
57}
58
59/// Every reference to entity `id`, in ascending order: a `LIST` of
60/// `InverseReference` records, their number in `out_count`, one per
61/// referencing entity and attribute. `InverseReference`: id (`REF`, the
62/// referencing entity), index (`INTEGER`, its attribute holding the
63/// reference, directly or inside an aggregate). Needs no schema.
64///
65/// `MissingEntity`.
66///
67/// # Safety
68/// As for `openbim_ifc_v0_1_entity_attribute_names`.
69#[no_mangle]
70pub unsafe extern "C" fn openbim_ifc_v0_1_entity_inverse_references(
71 model: OpenbimIfcModel,
72 id: u64,
73 out_count: *mut usize,
74 nodes: *mut OpenbimIfcValueNode,
75 node_capacity: usize,
76 out_nodes_required: *mut usize,
77 strings: *mut u8,
78 string_capacity: usize,
79 out_strings_required: *mut usize,
80) -> OpenbimIfcStatus {
81 let out = Out {
82 nodes,
83 node_capacity,
84 out_nodes_required,
85 strings,
86 string_capacity,
87 out_strings_required,
88 };
89 // SAFETY: caller contract above.
90 unsafe {
91 list_export(model, out_count, out, |m| {
92 Ok(to_records(&m.inverse_references(id)?))
93 })
94 }
95}
96
97/// How many distinct entities reference entity `id`, through any
98/// attribute, written to `out_count`. Needs no schema. `MissingEntity`.
99///
100/// # Safety
101/// `out_count` must be null or valid for one write.
102#[no_mangle]
103pub unsafe extern "C" fn openbim_ifc_v0_1_entity_total_inverses(
104 model: OpenbimIfcModel,
105 id: u64,
106 out_count: *mut usize,
107) -> OpenbimIfcStatus {
108 boundary(|| {
109 with_model(model, |m| {
110 let total = m.total_inverses(id)?;
111 // SAFETY: caller contract above.
112 done(
113 unsafe { put(out_count, total) }
114 .err()
115 .unwrap_or(OpenbimIfcStatus::Ok),
116 )
117 })
118 })
119}