GithubHelp home page GithubHelp logo

Better callback system about rhea HOT 19 CLOSED

nocte- avatar nocte- commented on June 28, 2024
Better callback system

from rhea.

Comments (19)

hfossli avatar hfossli commented on June 28, 2024

I tried to make this change

screen shot 2014-09-23 at 15 43 13
screen shot 2014-09-23 at 15 42 51

And then use it like this

containerHeight.on_change = [self](const rhea::variable &v) {
    std::cout << "containerHeight changed to: " << v.value() << std::endl;
};

But it seems like that this is a different instance.. Look at the pointer address in highlighted in blue (they are pointing at the correct value).

screen shot 2014-09-23 at 15 53 47
screen shot 2014-09-23 at 15 53 39

So I guess it is not straight forward...

from rhea.

Nocte- avatar Nocte- commented on June 28, 2024

You're right, that would get unwieldy after a while. You were on the right track, but the callbacks should be handled in abstract_variable (variable is not much more than a convenience wrapper around a smart pointer).

from rhea.

hfossli avatar hfossli commented on June 28, 2024

Okay. I will have a go at that. 👍

from rhea.

hfossli avatar hfossli commented on June 28, 2024

Aaaaand here's the result.

https://github.com/hfossli/rhea/compare/callbacks

I have no clue how I can make the API better. First time I'm coding c++ ever so this is quite the challenge. Ideally I would like something like

x.on_change([](float old_value, float new_value) {
    // code
});

Instead of

x.on_change([]() {
    // code
});

from rhea.

Nocte- avatar Nocte- commented on June 28, 2024

Looking at the code, I wonder why there are separate calls for 'set' and 'change'. I'd like to try and simplify that before adding the callback.

from rhea.

hfossli avatar hfossli commented on June 28, 2024

I agree. 👍

from rhea.

hfossli avatar hfossli commented on June 28, 2024

I'm not entirely sure, but I think the callback should be called when adding a variable to the solver as well. So that I from a user perspective can link the current value to properties on my views.

Maybe on_change should rather be apply or something like that? What do you think?

from rhea.

Nocte- avatar Nocte- commented on June 28, 2024

I think I have found a good way to extend the API, while you still only pay for what you use. You can either link an existing variable to a rhea::variable:

float kitty = 41.0f;
rhea::variable x (kitty, rhea::linked());

If you link to an integer, the library does the rounding for you.
Or use a callback if you need one:

rhea::variable x ([&](double v){ aargh(v); }, 41.0);

from rhea.

hfossli avatar hfossli commented on June 28, 2024

Hm. Seems interesting, I don't understand the mechanisms though. :-) Can you explain a little bit more? Will the value of x change whenever the float kitty changes?

from rhea.

Nocte- avatar Nocte- commented on June 28, 2024

Yes, but don't do that. ;) It's meant to write the result of the solution back to kitty. It is basically a more efficient shortcut for:

rhea::variable x ([&](double v){ kitty = static_cast<float>(v); }, 41.0);

from rhea.

hfossli avatar hfossli commented on June 28, 2024

Ah, I see :) Looks nice

from rhea.

hfossli avatar hfossli commented on June 28, 2024

It would be awesome to be able to have n subscribers and not just one

from rhea.

Nocte- avatar Nocte- commented on June 28, 2024

If you know your subscribers ahead of time, that wouldn't be too difficult:

variable x ([&](double v){ some_var = other_var = yet_another = v; });
variable y ([&](double v){ call_me(v); call_me_on_the_line(); call_me_anytime(v); });

But if you need a more elaborate subscriber mechanism, I'd suggest picking a library that suits your needs (Boost signal2, Sigslot, FastDelegate, ...) and writing a small helper class to connect the variable to the signal.

I have the feeling there might be an easier way though. :) Could I take a look at your project?

from rhea.

hfossli avatar hfossli commented on June 28, 2024

Well, for me it is sufficient that I initialize the variable with a callback.

Rhea variables are instantiated just by declaring them. How can I declare an instance variable and do the initialization myself?

@implementation BoxModel  {
    rhea::variable left;
}

- (id)init {
    self = [super init];
    if(self) {
        // I could write
        left = rhea::variable([self](double newValue) {
             [self leftChangedTo:newValue];
        });

       // But then a rhea::variable is allocated 1 time excessively, right?
    }
    return self;
}

@end

from rhea.

hfossli avatar hfossli commented on June 28, 2024

I would be happy to let you have a look at some time, but not right now :-) Still a couple of proof of concepts left

from rhea.

Nocte- avatar Nocte- commented on June 28, 2024

Yeah indeed, 'left' starts as a normal variable and gets swapped out for a callback variable later. This is probably the best solution; the overhead is negligible (unless you're instancing BoxModel so often it starts showing up on the profiler) and 'left' is always in a well-defined state.

from rhea.

hfossli avatar hfossli commented on June 28, 2024

I agree!

Will the memory be okay? The old variable will get released automatically? (I'm not very familiar with std and c++)

from rhea.

Nocte- avatar Nocte- commented on June 28, 2024

Yep!

from rhea.

hfossli avatar hfossli commented on June 28, 2024

👍

from rhea.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.