Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get reference to underlying object/function of an observer? #37

Open
Robert-M-Muench opened this issue Jun 24, 2019 · 1 comment

Comments

@Robert-M-Muench
Copy link

Robert-M-Muench commented Jun 24, 2019

When using observers, is there a way to access the underlying object implementing the observer?

class myClass {...}
myClass mc = new myClass;
auto observer1 = makeObserver(&mc.myPut);

How can I access/get back mc via observer1?

Since there are several different ways to get an observer (makeObserver which returns an AnonymousObserver struct, inheritance, observerObject) how can I get the pointer/reference to the underlying object/function implementing the functions?

struct TestObserver  {
  void put(int n) { }
  void put(Object obj) { }
}
Observer!int observer = observerObject!int(TestObserver());

How to access the pointer created by TestObserver() call?

The use case is: An instance of myClass can have many observers. These observers are handled around and subscribed to streams etc. Sometimes I need to get back the object an observer belongs to call some functions etc.

@Robert-M-Muench
Copy link
Author

This works (when _range can be accessed), but the necessary cast doesn't look that nice:

  auto test = new TestObserver;
  auto observer = observerObject!int(test);
  assert(test == cast(ObserverObject!(TestObserver,int))(observer._range));
}

adding

    R object() @property {return _range;}

makes things much nicer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant