rustorb_core/idl/abstract_iface.rs
1//! Abstract interfaces and abstract valuetypes of generated code.
2//!
3//! An abstract interface `A` is passed either as an object reference or as
4//! a value of a valuetype that supports it (CORBA 3.4 part 2, 9.3.7). The
5//! generator makes an enum `A` with a variant for each: `Object` holds the
6//! reference, and `Value` an [`AbstractValue`]: the instance and the
7//! operations of `A` on it (the servant trait `AServant`, which the
8//! generator implements for the handle of every valuetype that supports
9//! `A`).
10//!
11//! A value that arrives where an abstract interface is expected is read as
12//! a `ValueBase` ([`ValueHandle::read_base`]), and then turned into the
13//! enum by the function the valuetype's file registers for it with
14//! [`register_supported`] (its `register_value_types` does).
15
16use std::any::Any;
17use std::collections::BTreeMap;
18use std::fmt;
19use std::sync::{Arc, RwLock};
20
21use super::{Sys, ValueHandle};
22use crate::{InStream, ObjectRef, OutStream};
23
24/// A value passed as an abstract interface: the instance, and the
25/// operations of the interface on it.
26pub struct AbstractValue<T: ?Sized> {
27 handle: ValueHandle,
28 ops: Arc<T>,
29}
30
31impl<T: ?Sized> AbstractValue<T> {
32 /// The instance `handle`, whose operations are `ops` (usually the
33 /// typed handle of the same instance).
34 pub fn new(handle: ValueHandle, ops: Arc<T>) -> AbstractValue<T> {
35 AbstractValue { handle, ops }
36 }
37
38 /// The untyped handle of the instance.
39 pub fn handle(&self) -> &ValueHandle {
40 &self.handle
41 }
42
43 /// The operations of the interface on the instance.
44 pub fn ops(&self) -> &Arc<T> {
45 &self.ops
46 }
47
48 /// Whether both are the same instance.
49 pub fn ptr_eq(&self, other: &AbstractValue<T>) -> bool {
50 self.handle.ptr_eq(&other.handle)
51 }
52}
53
54impl<T: ?Sized> Clone for AbstractValue<T> {
55 fn clone(&self) -> Self {
56 AbstractValue {
57 handle: self.handle.clone(),
58 ops: self.ops.clone(),
59 }
60 }
61}
62
63impl<T: ?Sized> fmt::Debug for AbstractValue<T> {
64 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65 self.handle.fmt(f)
66 }
67}
68
69/// The operations of a derived abstract interface used as those of a base
70/// one: the generator implements the servant trait of each abstract
71/// interface for `Upcast<T>` of every `T` that implements it, so that an
72/// `Arc<dyn DerivedServant>` becomes an `Arc<dyn BaseServant>` without
73/// trait upcasting.
74pub struct Upcast<T: ?Sized>(pub Arc<T>);
75
76impl<T: ?Sized> fmt::Debug for Upcast<T> {
77 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
78 f.write_str("Upcast")
79 }
80}
81
82/// Turns an instance into the enum of an abstract interface it supports
83/// (as a `Box<dyn Any>`), or `None` if it is not of the valuetype.
84pub type SupportedFn = fn(&ValueHandle) -> Option<Box<dyn Any + Send>>;
85
86type Supported = BTreeMap<(&'static str, &'static str), SupportedFn>;
87
88static SUPPORTED: RwLock<Supported> = RwLock::new(BTreeMap::new());
89
90/// Records that the valuetype `value_id` supports the abstract interface
91/// `abstract_id`, and how its instances become the generated enum of the
92/// interface. Instances of types derived from `value_id` are turned the
93/// same way.
94pub fn register_supported(abstract_id: &'static str, value_id: &'static str, f: SupportedFn) {
95 SUPPORTED
96 .write()
97 .unwrap_or_else(|e| e.into_inner())
98 .insert((abstract_id, value_id), f);
99}
100
101/// The instance `v` as the abstract interface `abstract_id` (its enum
102/// `A`), if its type or a base type of it was registered as supporting it
103/// with [`register_supported`].
104pub fn supported<A: 'static>(abstract_id: &str, v: &ValueHandle) -> Option<A> {
105 let table = SUPPORTED.read().unwrap_or_else(|e| e.into_inner());
106 v.info().ancestors.iter().find_map(|id| {
107 let f = table.get(&(abstract_id, *id))?;
108 f(v)?.downcast::<A>().ok().map(|a| *a)
109 })
110}
111
112/// Writes an abstract interface as the object reference `obj` (TRUE and
113/// the IOR); `None` is the nil reference.
114pub fn write_abstract_object(obj: Option<&ObjectRef>, out: &mut OutStream<'_>) -> Sys<()> {
115 out.write_bool(true);
116 out.write_object(obj)
117}
118
119/// Writes an abstract interface as the value `v` (FALSE and the value).
120pub fn write_abstract_value(v: &ValueHandle, out: &mut OutStream<'_>) -> Sys<()> {
121 out.write_bool(false);
122 ValueHandle::write(Some(v), out)
123}
124
125/// Reads the abstract interface `abstract_id`: an object reference made
126/// into an `A` by `object`, a value made into one by [`supported`], or
127/// `None` for a nil reference or a null value. A value of a type that is
128/// not known to support the interface is `MARSHAL`.
129pub fn read_abstract<A: 'static>(
130 input: &mut InStream<'_>,
131 abstract_id: &str,
132 object: fn(ObjectRef) -> A,
133) -> Sys<Option<A>> {
134 if input.read_bool().map_err(|_| input.marshal_error())? {
135 return Ok(input.read_object()?.map(object));
136 }
137 read_abstract_value(input, abstract_id)
138}
139
140/// Reads a value of the abstract valuetype or interface `abstract_id`:
141/// as a `ValueBase`, made into an `A` by [`supported`]. A value of a type
142/// that is not known to derive from it (or support it) is `MARSHAL`.
143pub fn read_abstract_value<A: 'static>(
144 input: &mut InStream<'_>,
145 abstract_id: &str,
146) -> Sys<Option<A>> {
147 match ValueHandle::read_base(input)? {
148 None => Ok(None),
149 Some(v) => supported(abstract_id, &v)
150 .map(Some)
151 .ok_or_else(|| input.marshal_error()),
152 }
153}