Skip to content

Commit

Permalink
馃摑 Copy from readme to docs
Browse files Browse the repository at this point in the history
  • Loading branch information
OlivierLDff committed Apr 12, 2021
1 parent 5566653 commit c7e38e6
Showing 1 changed file with 22 additions and 34 deletions.
56 changes: 22 additions & 34 deletions docs/quickstartcpp.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,52 +225,40 @@ protected:

### Observe with callback

Regular callback not dependent on qt can be used to handle insert/remove/move operation. We can't use signal with correct pointer type because qt doesn't support template moc.
QOlm provide lambda connection with already `qobject_cast` objects. This is the preferred and easier way to observe the list

```cpp
FooList list;
list.onInserted([](const InsertedCallbackArgs& foo)
{
// foo->foo can be directly accessed
// foo.object gives a _Object*
// foo.index gives inserted object index
});
list.onRemoved([](const RemovedCallbackArgs& foo)
{
// foo->foo can be directly accessed
// foo.object gives a _Object*
// foo.index gives removed object index
});
list.onMoved([](const MovedCallbackArgs& foo)
{
// foo->foo can be directly accessed
// foo.object gives a _Object*
// foo.from gives previous object index
// foo.to gives new object index
});
```

Benefit of this method, is that the list can be observed by non qt object.

Each function return a callback handle that can be used to remove the callback.

```cpp
auto handleInsert = list.onInserted([](const InsertedCallbackArgs& foo) {});
list.stopListenInsert(handleInsert);
auto handleRemove = list.onInserted([](const InsertedCallbackArgs& foo) {});
list.stopListenRemove(handleRemove);
auto handleMove = list.onInserted([](const InsertedCallbackArgs& foo) {});
list.stopListenMove(handleMove);
// Preferred API, safer to use when giving a context
list.onInserted(&list, [](Foo* foo, int index){});
list.onInserted(&list, [](Foo* foo){});
list.onRemoved(&list, [](Foo* foo, int index){});
list.onRemoved(&list, [](Foo* foo){});
list.onMoved(&list, [](Foo* foo, int from, int to){});

// Should only be used when your callback doesn't require any context
list.onInserted([](Foo* foo, int index){});
list.onInserted([](Foo* foo){});
list.onRemoved([](Foo* foo, int index){});
list.onRemoved([](Foo* foo){});
list.onMoved([](Foo* foo, int from, int to){});
```

> When connecting without any `receiver`, this list is used as the context.
## Iterator

`QOlm` is compatible with modern iterator, you can simply do:

```cpp
FooList list;
for(const auto foo : list)
for(const auto* foo : list)
{
//foo->getFoo()
}
```
for(auto* foo : list)
{
//foo->setFoo(12)
}
```

0 comments on commit c7e38e6

Please sign in to comment.