Comments (19)
I tried to make this change
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).
So I guess it is not straight forward...
from rhea.
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.
Okay. I will have a go at that. 👍
from rhea.
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.
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.
I agree. 👍
from rhea.
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.
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.
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.
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.
Ah, I see :) Looks nice
from rhea.
It would be awesome to be able to have n subscribers and not just one
from rhea.
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.
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.
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.
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.
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.
Yep!
from rhea.
👍
from rhea.
Related Issues (20)
- Crash when adding required edit variable HOT 4
- Is there any way to create read-only external variables? HOT 2
- License HOT 2
- Tag HOT 3
- Question: Hashmap string => variable HOT 2
- Adding stay with required strength HOT 6
- Nonlinear expressions HOT 2
- Documentation HOT 4
- Is the warning on the README accurate? HOT 10
- Named variables HOT 2
- `simplex_solver::change_weight()` and `simplex_solver::change_strength()` don't work HOT 16
- Understanding the edit protocol HOT 2
- Subclassing variables HOT 2
- Stays do not always stay HOT 4
- Solving constraints does not work in a dynamic application HOT 9
- Breaking changes between 0.2.x and 0.3 HOT 3
- Building branch 0.3 HOT 2
- Maybe glitch in simplex solver when choosing subject? HOT 9
- Getting rhea::internal_error("objective function is unbounded.") on removing constraint. HOT 7
- Nothing happens on setting constant through set_constant method HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rhea.