module RefTest.main;
import std.stdio;
import RefTest.Ref;
class Foo {
private:
string _val;
public:
this() { }
static Foo opCall(string val) {
Foo f = new Foo();
f.Set(val);
return f;
}
void Set(string val) {
this._val = val;
}
void speak() const {
writeln(this._val);
}
mixin TRef!(typeof(this));
}
class Bar { }
void another_with_ref(Ref!(Foo) rf) {
write("Another: ");
rf.access.speak();
rf.access.Set("Bar");
}
void with_ref(Ref!(Foo) rf) {
//rf = null;
Foo tf = rf.access;
tf.speak();
tf = null;
another_with_ref(rf);
}
void without_ref(Foo f) {
f.speak();
}
void with_const_ref(const Ref!(Foo) rf) {
//rf.access.Set("Quatz");
rf.access.speak();
const Foo tf = rf.access;
//tf.Set("Quatz");
rf.access.speak();
}
void with_const2_ref(const Ref!(const Foo) rf) {
rf.access.speak();
}
void with_ptr_ref(const Ref!Foo rf) {
}
void ref_bar(Ref!Bar rb) {
}
void bar(Bar b) {
}
void main() {
Foo f0;
//with_ref(f0);
//with_ref(null);
//without_ref(null);
with_ref(Foo("One"));
without_ref(Foo("Two"));
with_const_ref(Foo("Three"));
//with_const_ref(null);
Foo f1 = new Foo();
f1.Set("Foo!");
with_ref(f1);
without_ref(f1);
with_const_ref(f1);
//with_const2_ref(f1);
const Foo fc1 = new Foo();
//with_const_ref(fc1);
with_const2_ref(fc1);
writeln("----");
Foo* fptr = &f1;
with_ptr_ref(*fptr);
Ref!Bar rb = new Bar();
//with_ref(rb);
ref_bar(rb);
//bar(rb);
writeln("----");
class MyClass {
int data;
}
MyClass mc = new MyClass();
Ref!MyClass rmc = mc;
mc.data = 42;
writeln(rmc.access.data);
writeln(mc.data);
rmc.access.data = 23;
writeln(rmc.access.data);
writeln(mc.data);
MyClass mc2 = new MyClass();
mc2.data = 72;
rmc = mc2;
writeln(rmc.access.data);
writeln(mc.data);
rmc.access.data++;
writeln(rmc.access.data);
writeln(mc.data);
writeln(mc2.data);
readln();
}