You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm working on reactive library µReact as a hobby.
Reactive variables works better when they are public members of classes, because such way they are more expressive and require less (or even no) boilerplate code.
But if they made so, then their assignment operator is exposed too, so such members can be reassigned to new values by mistake.
I've seen attempt to protect from it by declaring public members as const, but const members always have been kind of problem in C++.
So observable property approach from Getting started with observable properties comes to the rescue. To protect from reassigned public members should have class that makes construction and assignment private and allows it only for their parameterized Owner class.
#include<iostream>
#include"https://raw.githubusercontent.com/YarikTH/ureact/dev/single_include/ureact/ureact_amalgamated.hpp"classFoo : ureact::member_signal_user<Foo> {
// the same as inheritance from ureact::member_signal_user<Foo>// UREACT_USE_MEMBER_SIGNALS(Foo);public:Foo(ureact::context& ctx, int aValue, int bValue) {
// Defining reactive variables. We can reassign their values later
a = make_var(ctx, aValue);
b = make_var(ctx, bValue);
// Defining reactive signal using overloaded operator// Its value will be updated each time its dependencies are changed
x = a + b;
}
member_var_signal<int> a;
member_var_signal<int> b;
member_signal<int> x;
};
intmain() {
ureact::context ctx;
Foo foo{ctx, 1, 2};
std::cout << "x (init): " << foo.x() << "\n"; // 3// Assign a new value to 'a'. Value of 'x' is recalculated automatically
foo.a <<= 10;
std::cout << "x (new): " << foo.x() << "\n"; // 12// member can't be reassignment from outside of Foo class// so next line won't compile// foo.a = make_var( ctx, 20 );
}
The text was updated successfully, but these errors were encountered:
I remember it took me a while to come up with this approach, but haven't used it anywhere else since I wrote this code. It's great to hear you found it useful, thanks for posting, it made my day.
I'm working on reactive library µReact as a hobby.
Reactive variables works better when they are public members of classes, because such way they are more expressive and require less (or even no) boilerplate code.
But if they made so, then their assignment operator is exposed too, so such members can be reassigned to new values by mistake.
I've seen attempt to protect from it by declaring public members as const, but const members always have been kind of problem in C++.
So observable property approach from Getting started with observable properties comes to the rescue. To protect from reassigned public members should have class that makes construction and assignment private and allows it only for their parameterized Owner class.
So here is the result: https://godbolt.org/z/8rKPWW8fq
The text was updated successfully, but these errors were encountered: