observable container for C++
Is there an implementation of container classes for C++ which support notificatio开发者_如何学编程n in a similar way as ObservableCollection for C#?
There is no standard class like you describe, but Boost.Signals is quite a powerful notification library. I would create a wrapper for objects that raises a signal when it is changed, along the lines of this:
#include <boost/signals.hpp>
#include <vector>
#include <iostream>
// Wrapper to allow notification when an object is modified.
template <typename Type>
class Observable
{
public:
// Instantiate one of these to allow modification.
// The observers will be notified when this is destroyed after the modification.
class Transaction
{
public:
explicit Transaction(Observable& parent) :
object(parent.object), parent(parent) {}
~Transaction() {parent.changed();}
Type& object;
private:
Transaction(const Transaction&); // prevent copying
void operator=(const Transaction&); // prevent assignment
Observable& parent;
};
// Connect an observer to this object.
template <typename Slot>
void Connect(const Slot& slot) {changed.connect(slot);}
// Read-only access to the object.
const Type& Get() const {return object;}
private:
boost::signal<void()> changed;
Type object;
};
// Usage example
void callback() {std::cout << "Changed\n";}
int main()
{
typedef std::vector<int> Vector;
Observable<Vector> o;
o.Connect(callback);
{
Observable<Vector>::Transaction t(o);
t.object.push_back(1);
t.object.push_back(2);
} // callback called here
}
This is an open-source hpp library only that provides an implementation for observable containers in c++ using signals/slots(https://github.com/ricardocosme/coruja)
Briefing:
“Coruja” means “Owl” in Portuguese. It’s an alternative solution to the Observer pattern using signals&slots or a more classic approach using polymorphic types likes Subject and Observer. It’s a C++11 library with a more high level abstraction over the Observer pattern, avoiding bolierplate code and inversion of control IoC. STL containers like std::vector are adapted to become observables, i.e., observers may be notified when elements are inserted or erased. Actually a Range can be observable and observers may observe a transformation of a container, for example. Observable containers
coruja::vector<string> v{"John Jones", "Robert Plant"};
v.for_each([](auto& member){ cout << member << endl; });
v.emplace_back("Jimmy Page");
//outputs:
//John Jones
//Robert Plant
//Jimmy Page
Observable ranges
struct person_t { std::string first_name, surname; };
vector<person_t> persons;
auto fullnames = transform
(persons, [](auto& person){ return person.first_name + person.surname; });
fullnames.for_each([](auto&& fullname){ cout << fullname << endl; });
//outputs:
//JohnBonham
//JimmyPage
Observable objects
object<string> first_name, surname;
auto fullname = first_name + surname;
fullname.after_change([](auto&& s){ cout << s << endl; });
first_name = "Jimmy";
//outputs:
//Jimmy
surname = "Page";
//outputs:
//JimmyPage
This library is distributed under the Boost Software License, Version 1.0.
There's no such thing in STL. That doesn't mean that someone hasn't created such a thing in an open source library, but I don't believe it's part of the language.
The way I'm doing it I have something like notify_updated
and wait_event
in my collection, and I call notify_updated
after changes are made, and then in other parts I'm waiting for events. My solution is very specific to the problem I'm solving, so it's more C-ish. Thought it's similar conceptually to Mike's sample.
You will need to write your own, and back it on to your favourite container.
精彩评论