-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmacros.rs
129 lines (102 loc) · 2.97 KB
/
macros.rs
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
use std::ops::*;
/// macro to roll list of 'classes' with various extra stuff..
///
/// - constructor functions allowing calculation of fields from args
/// - a single 'world' object holding a vec of each class type,
/// - an enum for refering to them
/// - gathers 'documentation strings'
/// (TODO - intent is to assist rolling UI .. hence storing of UI strings
macro_rules! def_classes{
// todo -update/render args
{
$struct_holding_all:ident
{$($all_method_names:ident),*} // list of all method names
$(
$class_name:ident
( $($ctr_arg:ident :$ctr_ty:ty = $ctr_arg_default:expr => $ctr_arg_doc:expr),* ) // constructor args
=>[$class_doc:expr] //UI info for the class itself
{
// struct fields
$( $field:ident : $f_type:ty=$f_init:expr =>$field_doc:expr,)* // struct fields and initializer expressions (using ctr args)
}
{
$(
$method_name:ident $method_body:block
)*
}),*
}
=>
{
//[1] roll structs
#[derive(Debug,Clone)]
$(struct $class_name {
$($field : $f_type),*
})*
//[2] roll constructor functions
$(
fn $class_name( $($ctr_arg:$ctr_ty),* )->$class_name{
$class_name{
$($field: $f_init),*
}
}
)*
//[3] roll 'method' functions
$(
impl $class_name {
$(fn $method_name(&self) $method_body )*
fn get_enum(&self)->EnumOfClasses{ EnumOfClasses::$class_name }
}
)*
//$(def_fn!{$name $($e);*})*
// roll a world structure holding a vec per class
struct $struct_holding_all{
$( $class_name : Vec<$class_name>),*
}
// roll calls to all ..
impl $struct_holding_all{
fn update(&self) {
$(for x in self.$class_name.iter(){
x.update();
})*
}
}
//[4] Roll documentation string of each class
fn get_all_docs()->Vec<&'static str> { vec![ $($class_doc),+ ]}
//[5] roll an enum holding an item per class,
enum EnumOfClasses{
$($class_name),*
}
}
}
def_classes!{
World{update,render}
Foo( a:i32=0 =>"",b:i32=0 =>"") =>["info for foo"]{
x:i32=a+b =>"x position",
y:i32=a-b =>"y position",
}{
update{ println!("foo.update()") }
render{}
},
Bar()=>["bar"]{
}{
update{}
},
Baz()=>["baz"]{
}{
update{}
}
}
/*
TODO: macro for rolling shader node functions
- UI items, and code to generate the shader snippet
*/
/*
fn main() {
let w:World = World{Foo: vec![], Bar: vec![], Baz: vec![],};
for b in w.Bar { };
println!("class docs:{:?}", get_all_docs());
let x=Foo(10,20);
x.update();
println!("created by ctr {:?}", x);
}
*/