October 11, 2013 Re: My design need friends | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | On Sunday, 6 October 2013 at 13:11:02 UTC, Andrej Mitrovic wrote:
> On 10/6/13, Namespace <rswhite4@googlemail.com> wrote:
>> And I should write a blog post about your and my solution. :)
>
> Let me try to hack on __PRETTY_FUNCTION__ first and I'll post a
> working example here soon.
Is there something new?
|
October 11, 2013 Re: My design need friends | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namespace | On Friday, 11 October 2013 at 22:39:18 UTC, Namespace wrote:
> On Sunday, 6 October 2013 at 13:11:02 UTC, Andrej Mitrovic wrote:
>> On 10/6/13, Namespace <rswhite4@googlemail.com> wrote:
>>> And I should write a blog post about your and my solution. :)
>>
>> Let me try to hack on __PRETTY_FUNCTION__ first and I'll post a
>> working example here soon.
>
> Is there something new?
I have.
Foo_1:
----
import std.stdio;
import Bar_1;
struct friend(T) {
public:
alias Friend = T;
}
@friend!(Bar)
class Foo {
public:
void call(Bar b) {
_call_a_friend(b, this, &b.bar, 42);
//b.bar(42);
}
}
----
Bar_1:
----
import std.stdio;
import std.string : format;
class Bar {
package:
void bar(int id) {
writeln("Bar.bar with ", id);
}
}
void _call_a_friend(F, T, string filename = __FILE__, size_t line = __LINE__, Args...)(const F friend, const T caller, void delegate(Args) dg, Args args) {
bool isFriend = false;
foreach (attr; __traits(getAttributes, T)) {
isFriend = is(attr.Friend) && is(attr.Friend == F);
if (isFriend)
break;
}
//writeln(isFriend);
if (isFriend)
dg(args);
else
throw new Exception(format("%s is not a friend of %s.", __traits(identifier, T), __traits(identifier, F)), filename, line);
}
----
main:
----
import Foo_1;
import Bar_1;
void main() {
Foo f = new Foo();
Bar b = new Bar();
f.call(b);
}
----
I have to admit that the '_call_a_friend' method is a bit parameter heavy. :D
Suggestions?
|
Copyright © 1999-2021 by the D Language Foundation