Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Check if container of shared_ptr contains a pointer?
- std::list<SomeSharedPointer> items_;
- ...
- void Monitor::Update(Subject *subject)
- {
- if(subject == something_)
- {
- DoSomething();
- }
- else if
- ??
- // if subject is one of the objects in our collection then do something..
- }
- auto i = std::find_if(items_.begin(), items_.end(),
- [=](const SomeSharedPointer& x) { return x.get() == subject; });
- if (i != c.end())
- {
- // Object found, and i is an iterator pointing to it
- }
- typedef std::list<SomeSharedPtr> ObserverCollection;
- // You can also add a const version if needed
- ObserverCollection::iterator find_observer(Subject* s)
- {
- return std::find_if(items_.begin(), items_.end(),
- [=](const SomeSharedPointer& x) { return x.get() == s; });
- }
- auto i = find_observer(subject);
- if (i != items_.end())
- {
- // Object found
- }
- if (find_observer(subject) != items_.end())
- {
- ...
- }
- for (auto iter = items_.begin(); iter != items_.end(); ++iter)
- {
- if (subject == iter->get())
- {
- .. do stuff ..
- }
- }
- enum State{ STATE_1, STATE_2 };
- void Monitor::Update(Subject *subject)
- {
- switch( subject->getState() )
- {
- case STATE_1:
- // do something 1
- break;
- case STATE_2:
- // do something 2
- break;
- default:
- //error
- }
- }
Add Comment
Please, Sign In to add comment