Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

observable container for C++

Tags:

c++

Is there an implementation of container classes for C++ which support notification in a similar way as ObservableCollection for C#?

like image 915
AlexEzh Avatar asked Jul 06 '10 10:07

AlexEzh


2 Answers

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
}
like image 62
Mike Seymour Avatar answered Nov 16 '22 03:11

Mike Seymour


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.

like image 45
Hugo Costa Machado Avatar answered Nov 16 '22 01:11

Hugo Costa Machado