Skip to content

Commit

Permalink
Document weak_ptr references more.
Browse files Browse the repository at this point in the history
  • Loading branch information
5cript committed Nov 5, 2023
1 parent 325c508 commit 4b0e92c
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions docs/tutorials/attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ const auto ui = span{
class_ = observe(spanClasses, spanSubclass).generate([&spanClasses, &spanSubclass](){
// use .value to access the underlying wrapped value of a Nui::Observed:
auto classes = std::accumulate(
std::begin(spanClasses.value()),
std::end(spanClasses.value()),
std::string{},
std::begin(spanClasses.value()),
std::end(spanClasses.value()),
std::string{},
[](auto accum, auto const& elem){
accum = std::move(accum) + " " + elem;
}
Expand All @@ -126,7 +126,7 @@ input{
return num.value() % 2;
})

// Or:
// Or:
// "checked"_prop = observe(num).generate(/*...*/)
}()
```
Expand Down Expand Up @@ -205,6 +205,10 @@ using Nui::Attributes::reference;

Nui::val mySpan;

// When you want to store the reference, do so by weak_ptr to avoid dangling pointers.
// The weak_ptr becomes invalid when the element is removed from the DOM.
std::weak_ptr<Nui::Dom::BasicElement> lifetimeSafe;

const auto ui = span{
// highlight-start
reference = mySpan,
Expand All @@ -216,8 +220,14 @@ const auto ui2 = span{
reference = [&mySpan](auto&& weakElement){ mySpan = weakElement.lock()->val(); }
// highlight-end
};
// and finally:
// or
const auto ui3 = span{
// highlight-start
reference = lifetimeSafe
// highlight-end
};
// and finally:
const auto ui4 = span{
// highlight-start
reference.onMaterialize([&mySpan](Nui::val val){mySpan = val})
// highlight-end
Expand Down

0 comments on commit 4b0e92c

Please sign in to comment.