Skip to content
Snippets Groups Projects
Select Git revision
  • 37b596db3747628cc74066bfccca543fb8c7cfb2
  • master default
  • feature_scripts
  • develop
  • feature_without_logging
  • feature_opc_server
  • feature_seperate_apps
  • fix_raspi_cmake
  • ss19 protected
  • ss20
10 results

ObserverPattern.hpp

Blame
  • Armin Co's avatar
    Armin Co authored
    37b596db
    History
    ObserverPattern.hpp 700 B
    /// Subject of oberver pattern
    
    #ifndef OBSERVER_PATTERN_HPP
    #define OBSERVER_PATTERN_HPP
    
    #include <vector>
    #include <memory>
    
    /// Type sub has to be an observable subject.
    template<typename T>
    class IObserver
    {
    public:
        virtual void update(const T &subject) = 0;
    };
    
    template<typename Observer, typename Value>
    class Subject
    {
    public:
        virtual ~Subject() = default;
        void attach(std::shared_ptr<Observer> observer){
            m_observers.push_back(observer);
        }
    
    protected:
        void notify(const Value &value) const{
            for (auto observer : m_observers){
                observer->update(value);
            }
        }
    private:
        std::vector<std::shared_ptr<Observer>> m_observers;
    };
    
    #endif