Skip to content
Snippets Groups Projects
Commit 1e0c75f7 authored by Philippe Gorley's avatar Philippe Gorley
Browse files

refactor: abstract out observer from video section

Audio will soon be implemented using Observer/Observable, so move these
classes somewhere more sensible.

Change-Id: Iff0ca61a4c73c1ed7fa82ed91f8ca2d11afd65d9
parent b2654881
No related branches found
No related tags found
No related merge requests found
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
#pragma once #pragma once
#include "noncopyable.h" #include "noncopyable.h"
#include "observer.h"
#pragma GCC diagnostic push #pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations" #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
...@@ -57,70 +58,6 @@ using VideoFrame = DRing::VideoFrame; ...@@ -57,70 +58,6 @@ using VideoFrame = DRing::VideoFrame;
namespace ring { namespace video { namespace ring { namespace video {
template <typename T> class Observer;
template <typename T> class Observable;
/*=== Observable =============================================================*/
template <typename T>
class Observable
{
public:
Observable() : observers_(), mutex_() {}
virtual ~Observable() {
std::lock_guard<std::mutex> lk(mutex_);
for (auto& o : observers_)
o->detached(this);
};
bool attach(Observer<T>* o) {
std::lock_guard<std::mutex> lk(mutex_);
if (o and observers_.insert(o).second) {
o->attached(this);
return true;
}
return false;
}
bool detach(Observer<T>* o) {
std::lock_guard<std::mutex> lk(mutex_);
if (o and observers_.erase(o)) {
o->detached(this);
return true;
}
return false;
}
void notify(T data) {
std::lock_guard<std::mutex> lk(mutex_);
for (auto observer : observers_)
observer->update(this, data);
}
int getObserversCount() {
std::lock_guard<std::mutex> lk(mutex_);
return observers_.size();
}
private:
NON_COPYABLE(Observable<T>);
std::set<Observer<T>*> observers_;
std::mutex mutex_; // lock observers_
};
/*=== Observer =============================================================*/
template <typename T>
class Observer
{
public:
virtual ~Observer() {};
virtual void update(Observable<T>*, const T&) = 0;
virtual void attached(Observable<T>*) {};
virtual void detached(Observable<T>*) {};
};
struct VideoFrameActiveWriter: Observable<std::shared_ptr<VideoFrame>> {}; struct VideoFrameActiveWriter: Observable<std::shared_ptr<VideoFrame>> {};
struct VideoFramePassiveReader: Observer<std::shared_ptr<VideoFrame>> {}; struct VideoFramePassiveReader: Observer<std::shared_ptr<VideoFrame>> {};
......
/*
* Copyright (C) 2013-2018 Savoir-faire Linux Inc.
*
* Author: Guillaume Roguez <Guillaume.Roguez@savoirfairelinux.com>
* Author: Adrien Béraud <adrien.beraud@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#pragma once
#include "noncopyable.h"
#include <cstdlib>
#include <cstdint>
#include <memory>
#include <set>
#include <mutex>
#include <ciso646> // fix windows compiler bug
namespace ring {
template <typename T> class Observer;
template <typename T> class Observable;
/*=== Observable =============================================================*/
template <typename T>
class Observable
{
public:
Observable() : observers_(), mutex_() {}
virtual ~Observable() {
std::lock_guard<std::mutex> lk(mutex_);
for (auto& o : observers_)
o->detached(this);
};
bool attach(Observer<T>* o) {
std::lock_guard<std::mutex> lk(mutex_);
if (o and observers_.insert(o).second) {
o->attached(this);
return true;
}
return false;
}
bool detach(Observer<T>* o) {
std::lock_guard<std::mutex> lk(mutex_);
if (o and observers_.erase(o)) {
o->detached(this);
return true;
}
return false;
}
void notify(T data) {
std::lock_guard<std::mutex> lk(mutex_);
for (auto observer : observers_)
observer->update(this, data);
}
int getObserversCount() {
std::lock_guard<std::mutex> lk(mutex_);
return observers_.size();
}
private:
NON_COPYABLE(Observable<T>);
std::set<Observer<T>*> observers_;
std::mutex mutex_; // lock observers_
};
/*=== Observer =============================================================*/
template <typename T>
class Observer
{
public:
virtual ~Observer() {};
virtual void update(Observable<T>*, const T&) = 0;
virtual void attached(Observable<T>*) {};
virtual void detached(Observable<T>*) {};
};
}; // namespace ring
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment