1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/// The basic macro for making a union of entities.
///
/// This macro is the most generic version.
/// You can find more specific versions in `shape`, `material` and `object` modules.
///
/// You can read more about the technique [here](https://clay-rs.github.io/knowledge/#objects).
#[macro_export]
macro_rules! instance_select {
($Select:ident: $Base:path: $Class:ty { $( $Enum:ident($Param:ident = $Instance:ty) ),+ $(,)? }) => {
pub enum $Select<
$( $Param:
$crate::Pack +
$crate::Instance<$Class> +
$Base
= $Instance
),+
> {
$( $Enum($Param), )+
}
impl $Select {
#[allow(unused_assignments)]
fn index(&self) -> u32 {
let mut i = 0;
$(
if let $Select::$Enum(_) = self {
return i;
}
i += 1;
)+
unreachable!()
}
#[allow(unused_assignments)]
fn method_source(method: &str) -> String {
use $crate::class::*;
let cpref = format!(
"{}_{}",
<$Class>::name(),
method,
).to_uppercase();
let mut cases = Vec::new();
let mut i = 0;
$(
let inst_name = <$Instance as $crate::Instance<$Class>>::inst_name();
cases.push([
format!("\tif (sel_idx == {}) {{", i),
format!(
"\t\treturn {}_{}({}_ARGS_B(1, 0));",
inst_name, method, cpref,
),
"\t}".to_string(),
].join("\n"));
i += 1;
)+
let cases_text = cases.join(" else\n");
[
&format!(
"{}_RET {}_{}({}_ARGS_DEF) {{",
cpref, Self::inst_name(), method, cpref,
),
"\tint sel_idx = ibuf[0];",
&cases_text,
&format!("\treturn {}_RET_BAD;", cpref),
"}",
].join("\n")
}
}
impl $crate::Instance<$Class> for $Select {
fn source(cache: &mut std::collections::HashSet<u64>) -> String {
use $crate::{TypeHash, class::*};
if !cache.insert(Self::type_hash()) {
return String::new()
}
let mut ms = Vec::new();
for method in <$Class>::methods().into_iter() {
ms.push(Self::method_source(&method));
}
[
$( <$Instance as $crate::Instance<$Class>>::source(cache), )+
ms.join("\n"),
].join("\n")
}
fn inst_name() -> String {
use $crate::TypeHash;
format!("__select_{:x}", Self::type_hash())
}
}
impl $crate::Pack for $Select {
fn size_int() -> usize {
let sizes = [
$( <$Instance>::size_int(), )+
];
1 + *sizes.iter().max().unwrap()
}
fn size_float() -> usize {
let sizes = [
$( <$Instance>::size_float(), )+
];
*sizes.iter().max().unwrap()
}
fn pack_to(&self, mut buffer_int: &mut [i32], buffer_float: &mut [f32]) {
use $crate::pack::*;
self.index().pack_int_to(buffer_int);
buffer_int = &mut buffer_int[1..];
match self {
$( $Select::$Enum(x) => x.pack_to(buffer_int, buffer_float), )+
}
}
}
$(
impl From<$Instance> for $Select {
fn from(origin: $Instance) -> Self {
$Select::$Enum(origin)
}
}
)+
};
}
#[cfg(test)]
mod check {
use crate::{
shape::{
Shape, ShapeClass,
test::TestShape,
},
instance_select,
};
instance_select!(
TestSelect: Shape: ShapeClass {
Shape1(T1 = TestShape<i32>),
Shape2(T2 = TestShape<f32>),
}
);
}