1use openbim_ifc_binding_core::{BindingError, IfcModel};
8
9use crate::buffer::{bytes, fill, fill_c_string, put, slice, text};
10use crate::registry::{self, lock, Entry};
11use crate::status::{boundary, OpenbimIfcStatus, OpenbimIfcVersion};
12use crate::tape::{OpenbimIfcValueNode, Reader, Tape};
13
14const ABI: (u16, u16, u16) = (0, 1, 24);
16
17pub type OpenbimIfcModel = u64;
19
20pub(crate) fn with_model(
26 handle: OpenbimIfcModel,
27 operation: impl FnOnce(&mut IfcModel) -> Result<OpenbimIfcStatus, BindingError>,
28) -> OpenbimIfcStatus {
29 let Some(shared) = registry::get(handle) else {
30 return OpenbimIfcStatus::InvalidHandle;
31 };
32 let mut entry = lock(&shared);
33 let Entry { model, last_error } = &mut *entry;
34 match operation(model) {
35 Ok(status) => {
36 *last_error = None;
37 status
38 }
39 Err(error) => {
40 let status = OpenbimIfcStatus::from(&error);
41 *last_error = Some(error);
42 status
43 }
44 }
45}
46
47pub(crate) fn done(status: OpenbimIfcStatus) -> Result<OpenbimIfcStatus, BindingError> {
49 Ok(status)
50}
51
52#[no_mangle]
57pub unsafe extern "C" fn openbim_ifc_v0_1_version(
58 out_version: *mut OpenbimIfcVersion,
59) -> OpenbimIfcStatus {
60 boundary(|| {
61 let version = OpenbimIfcVersion {
62 abi_major: ABI.0,
63 abi_minor: ABI.1,
64 abi_patch: ABI.2,
65 crate_major: env!("CARGO_PKG_VERSION_MAJOR").parse().unwrap_or(0),
66 crate_minor: env!("CARGO_PKG_VERSION_MINOR").parse().unwrap_or(0),
67 crate_patch: env!("CARGO_PKG_VERSION_PATCH").parse().unwrap_or(0),
68 };
69 unsafe { put(out_version, version) }
71 .err()
72 .unwrap_or(OpenbimIfcStatus::Ok)
73 })
74}
75
76#[no_mangle]
81pub unsafe extern "C" fn openbim_ifc_v0_1_model_create(
82 out_model: *mut OpenbimIfcModel,
83) -> OpenbimIfcStatus {
84 boundary(|| {
85 if out_model.is_null() {
86 return OpenbimIfcStatus::NullPointer;
87 }
88 let handle = registry::insert(IfcModel::empty());
89 unsafe { put(out_model, handle) }
91 .err()
92 .unwrap_or(OpenbimIfcStatus::Ok)
93 })
94}
95
96#[no_mangle]
111pub unsafe extern "C" fn openbim_ifc_v0_1_model_parse(
112 data: *const u8,
113 len: usize,
114 out_model: *mut OpenbimIfcModel,
115 error_buffer: *mut u8,
116 capacity: usize,
117) -> OpenbimIfcStatus {
118 boundary(|| {
119 if out_model.is_null() || (capacity != 0 && error_buffer.is_null()) {
120 return OpenbimIfcStatus::NullPointer;
121 }
122 let input = match unsafe { bytes(data, len) } {
124 Ok(input) => input,
125 Err(status) => return status,
126 };
127 unsafe { finish_load(IfcModel::parse(input), out_model, error_buffer, capacity) }
129 })
130}
131
132pub(crate) unsafe fn finish_load(
140 loaded: Result<IfcModel, BindingError>,
141 out_model: *mut OpenbimIfcModel,
142 error_buffer: *mut u8,
143 capacity: usize,
144) -> OpenbimIfcStatus {
145 match loaded {
146 Ok(model) => {
147 let handle = registry::insert(model);
148 unsafe { put(out_model, handle) }
150 .err()
151 .unwrap_or(OpenbimIfcStatus::Ok)
152 }
153 Err(error) => {
154 if capacity != 0 {
155 let message = error.to_string();
156 let count = message.len().min(capacity - 1);
157 let count = (0..=count)
159 .rev()
160 .find(|i| message.is_char_boundary(*i))
161 .unwrap_or(0);
162 unsafe {
164 std::ptr::copy_nonoverlapping(message.as_ptr(), error_buffer, count);
165 error_buffer.add(count).write(0);
166 }
167 }
168 OpenbimIfcStatus::from(&error)
169 }
170 }
171}
172
173#[no_mangle]
175pub extern "C" fn openbim_ifc_v0_1_model_destroy(model: OpenbimIfcModel) -> OpenbimIfcStatus {
176 boundary(|| {
177 if registry::remove(model) {
178 OpenbimIfcStatus::Ok
179 } else {
180 OpenbimIfcStatus::InvalidHandle
181 }
182 })
183}
184
185#[no_mangle]
190pub unsafe extern "C" fn openbim_ifc_v0_1_live_models(out_count: *mut usize) -> OpenbimIfcStatus {
191 boundary(|| {
192 unsafe { put(out_count, registry::live()) }
194 .err()
195 .unwrap_or(OpenbimIfcStatus::Ok)
196 })
197}
198
199#[no_mangle]
205pub unsafe extern "C" fn openbim_ifc_v0_1_model_write(
206 model: OpenbimIfcModel,
207 buffer: *mut u8,
208 capacity: usize,
209 out_required: *mut usize,
210) -> OpenbimIfcStatus {
211 boundary(|| {
212 with_model(model, |m| {
213 let bytes = m.write()?;
214 done(unsafe { fill(&bytes, buffer, capacity, out_required) })
216 })
217 })
218}
219
220#[no_mangle]
225pub unsafe extern "C" fn openbim_ifc_v0_1_model_len(
226 model: OpenbimIfcModel,
227 out_count: *mut usize,
228) -> OpenbimIfcStatus {
229 boundary(|| {
230 with_model(model, |m| {
231 done(
233 unsafe { put(out_count, m.len()) }
234 .err()
235 .unwrap_or(OpenbimIfcStatus::Ok),
236 )
237 })
238 })
239}
240
241#[no_mangle]
246pub unsafe extern "C" fn openbim_ifc_v0_1_model_schema(
247 model: OpenbimIfcModel,
248 buffer: *mut u8,
249 capacity: usize,
250 out_required: *mut usize,
251) -> OpenbimIfcStatus {
252 boundary(|| {
253 with_model(model, |m| {
254 Ok(match m.schema() {
255 Some(token) => unsafe { fill_c_string(token, buffer, capacity, out_required) },
257 None => OpenbimIfcStatus::NoValue,
258 })
259 })
260 })
261}
262
263#[no_mangle]
269pub unsafe extern "C" fn openbim_ifc_v0_1_model_ids(
270 model: OpenbimIfcModel,
271 buffer: *mut u64,
272 capacity: usize,
273 out_required: *mut usize,
274) -> OpenbimIfcStatus {
275 boundary(|| {
276 with_model(model, |m| {
277 done(unsafe { fill(&m.ids(), buffer, capacity, out_required) })
279 })
280 })
281}
282
283#[no_mangle]
290pub unsafe extern "C" fn openbim_ifc_v0_1_model_ids_of_type(
291 model: OpenbimIfcModel,
292 type_name: *const u8,
293 type_len: usize,
294 buffer: *mut u64,
295 capacity: usize,
296 out_required: *mut usize,
297) -> OpenbimIfcStatus {
298 boundary(|| {
299 let name = match unsafe { text(type_name, type_len) } {
301 Ok(name) => name,
302 Err(status) => return status,
303 };
304 with_model(model, |m| {
305 done(unsafe { fill(&m.ids_of_type(name), buffer, capacity, out_required) })
307 })
308 })
309}
310
311#[no_mangle]
317pub unsafe extern "C" fn openbim_ifc_v0_1_model_ids_of_type_including_subtypes(
318 model: OpenbimIfcModel,
319 type_name: *const u8,
320 type_len: usize,
321 buffer: *mut u64,
322 capacity: usize,
323 out_required: *mut usize,
324) -> OpenbimIfcStatus {
325 boundary(|| {
326 let name = match unsafe { text(type_name, type_len) } {
328 Ok(name) => name,
329 Err(status) => return status,
330 };
331 with_model(model, |m| {
332 let ids = m.ids_of_type_including_subtypes(name)?;
333 done(unsafe { fill(&ids, buffer, capacity, out_required) })
335 })
336 })
337}
338
339#[no_mangle]
344pub unsafe extern "C" fn openbim_ifc_v0_1_entity_type(
345 model: OpenbimIfcModel,
346 id: u64,
347 buffer: *mut u8,
348 capacity: usize,
349 out_required: *mut usize,
350) -> OpenbimIfcStatus {
351 boundary(|| {
352 with_model(model, |m| {
353 let name = m.type_of(id)?;
354 done(unsafe { fill_c_string(name, buffer, capacity, out_required) })
356 })
357 })
358}
359
360pub(crate) unsafe fn fill_tape(
369 tape: &Tape,
370 nodes: *mut OpenbimIfcValueNode,
371 node_capacity: usize,
372 out_nodes_required: *mut usize,
373 strings: *mut u8,
374 string_capacity: usize,
375 out_strings_required: *mut usize,
376) -> OpenbimIfcStatus {
377 let node_status = unsafe { fill(&tape.nodes, nodes, node_capacity, out_nodes_required) };
379 let string_status = unsafe {
381 fill(
382 &tape.strings,
383 strings,
384 string_capacity,
385 out_strings_required,
386 )
387 };
388 match (node_status, string_status) {
390 (OpenbimIfcStatus::Ok, other) | (other, OpenbimIfcStatus::Ok) => other,
391 (OpenbimIfcStatus::BufferTooSmall, other) => other,
392 (first, _) => first,
393 }
394}
395
396#[no_mangle]
402pub unsafe extern "C" fn openbim_ifc_v0_1_entity_attribute(
403 model: OpenbimIfcModel,
404 id: u64,
405 index: usize,
406 nodes: *mut OpenbimIfcValueNode,
407 node_capacity: usize,
408 out_nodes_required: *mut usize,
409 strings: *mut u8,
410 string_capacity: usize,
411 out_strings_required: *mut usize,
412) -> OpenbimIfcStatus {
413 boundary(|| {
414 with_model(model, |m| {
415 let tape = Tape::encode(&m.attribute(id, index)?);
416 done(unsafe {
418 fill_tape(
419 &tape,
420 nodes,
421 node_capacity,
422 out_nodes_required,
423 strings,
424 string_capacity,
425 out_strings_required,
426 )
427 })
428 })
429 })
430}
431
432#[no_mangle]
439pub unsafe extern "C" fn openbim_ifc_v0_1_entity_attributes(
440 model: OpenbimIfcModel,
441 id: u64,
442 out_count: *mut usize,
443 nodes: *mut OpenbimIfcValueNode,
444 node_capacity: usize,
445 out_nodes_required: *mut usize,
446 strings: *mut u8,
447 string_capacity: usize,
448 out_strings_required: *mut usize,
449) -> OpenbimIfcStatus {
450 boundary(|| {
451 if out_count.is_null() {
452 return OpenbimIfcStatus::NullPointer;
453 }
454 with_model(model, |m| {
455 let values = m.attributes(id)?;
456 let tape = Tape::encode_all(&values);
457 unsafe { out_count.write(values.len()) };
459 done(unsafe {
461 fill_tape(
462 &tape,
463 nodes,
464 node_capacity,
465 out_nodes_required,
466 strings,
467 string_capacity,
468 out_strings_required,
469 )
470 })
471 })
472 })
473}
474
475#[no_mangle]
482pub unsafe extern "C" fn openbim_ifc_v0_1_entity_set_attribute(
483 model: OpenbimIfcModel,
484 id: u64,
485 index: usize,
486 nodes: *const OpenbimIfcValueNode,
487 node_count: usize,
488 strings: *const u8,
489 string_len: usize,
490) -> OpenbimIfcStatus {
491 boundary(|| {
492 let (nodes, strings) =
494 match unsafe { (slice(nodes, node_count), bytes(strings, string_len)) } {
495 (Ok(n), Ok(s)) => (n, s),
496 (Err(status), _) | (_, Err(status)) => return status,
497 };
498 with_model(model, |m| {
499 let value = Reader::new(nodes, strings).single()?;
500 m.set_attribute(id, index, value)?;
501 Ok(OpenbimIfcStatus::Ok)
502 })
503 })
504}
505
506#[no_mangle]
513pub unsafe extern "C" fn openbim_ifc_v0_1_entity_add(
514 model: OpenbimIfcModel,
515 type_name: *const u8,
516 type_len: usize,
517 attribute_count: usize,
518 nodes: *const OpenbimIfcValueNode,
519 node_count: usize,
520 strings: *const u8,
521 string_len: usize,
522 out_id: *mut u64,
523) -> OpenbimIfcStatus {
524 boundary(|| {
525 if out_id.is_null() {
526 return OpenbimIfcStatus::NullPointer;
527 }
528 let inputs = unsafe {
530 (
531 text(type_name, type_len),
532 slice(nodes, node_count),
533 bytes(strings, string_len),
534 )
535 };
536 let (name, nodes, strings) = match inputs {
537 (Ok(name), Ok(n), Ok(s)) => (name, n, s),
538 (Err(status), _, _) | (_, Err(status), _) | (_, _, Err(status)) => return status,
539 };
540 with_model(model, |m| {
541 let values = Reader::new(nodes, strings).many(attribute_count)?;
542 let id = m.add(name, values)?;
543 unsafe { out_id.write(id) };
545 Ok(OpenbimIfcStatus::Ok)
546 })
547 })
548}
549
550#[no_mangle]
557pub extern "C" fn openbim_ifc_v0_1_entity_remove(
558 model: OpenbimIfcModel,
559 id: u64,
560) -> OpenbimIfcStatus {
561 boundary(|| {
562 with_model(model, |m| {
563 m.remove(id)?;
564 Ok(OpenbimIfcStatus::Ok)
565 })
566 })
567}
568
569#[no_mangle]
575pub unsafe extern "C" fn openbim_ifc_v0_1_model_dangling_references(
576 model: OpenbimIfcModel,
577 buffer: *mut u64,
578 capacity: usize,
579 out_required: *mut usize,
580) -> OpenbimIfcStatus {
581 boundary(|| {
582 with_model(model, |m| {
583 let flat: Vec<u64> = m
584 .dangling_references()
585 .into_iter()
586 .flat_map(|(from, to)| [from, to])
587 .collect();
588 done(unsafe { fill(&flat, buffer, capacity, out_required) })
590 })
591 })
592}
593
594#[no_mangle]
599pub unsafe extern "C" fn openbim_ifc_v0_1_model_diagnostic_count(
600 model: OpenbimIfcModel,
601 out_count: *mut usize,
602) -> OpenbimIfcStatus {
603 boundary(|| {
604 with_model(model, |m| {
605 let count = m.diagnostics().len();
606 done(
608 unsafe { put(out_count, count) }
609 .err()
610 .unwrap_or(OpenbimIfcStatus::Ok),
611 )
612 })
613 })
614}
615
616#[no_mangle]
621pub unsafe extern "C" fn openbim_ifc_v0_1_model_diagnostic(
622 model: OpenbimIfcModel,
623 index: usize,
624 buffer: *mut u8,
625 capacity: usize,
626 out_required: *mut usize,
627) -> OpenbimIfcStatus {
628 boundary(|| {
629 with_model(model, |m| {
630 let all = m.diagnostics();
631 let message = all.get(index).ok_or_else(|| {
632 BindingError::OutOfRange(format!("diagnostic {index} of {}", all.len()))
633 })?;
634 done(unsafe { fill_c_string(message, buffer, capacity, out_required) })
636 })
637 })
638}
639
640#[cfg(test)]
641mod tests;