Skip to content
This repository has been archived by the owner on Sep 4, 2021. It is now read-only.

Commit

Permalink
replace signal/slot with event holding destination reference
Browse files Browse the repository at this point in the history
Signed-off-by: Denis Zalevskiy <denis.zalevskiy@jolla.com>
  • Loading branch information
Denis Zalevskiy committed Apr 9, 2015
1 parent 3b57708 commit e100c74
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 28 deletions.
85 changes: 66 additions & 19 deletions src/contextkit-subscriber/property.cpp
Expand Up @@ -104,7 +104,8 @@ class Event : public QEvent
Unsubscribe,
Subscribed,
Write,
Refresh
Refresh,
Data
};

virtual ~Event();
Expand Down Expand Up @@ -262,6 +263,28 @@ Event::Event(Event::Type t)

Event::~Event() {}

class ReplyEvent : public Event
{
public:
ReplyEvent(Event::Type t, QSharedPointer<Adapter> const &tgt)
: Event(t)
, tgt_(tgt)
{}

QSharedPointer<Adapter> tgt_;
};

class DataReplyEvent : public ReplyEvent
{
public:
DataReplyEvent(QVariant v, QSharedPointer<Adapter> const &tgt)
: ReplyEvent(Event::Data, tgt)
, data_(std::move(v))
{}

QVariant data_;
};

class SubscribeRequest : public Event
{
public:
Expand All @@ -288,11 +311,7 @@ SubscribeRequest::~SubscribeRequest()
{
auto notify_fn = [this]() {
value_.set_value(result);
QMetaObject::invokeMethod
(tgt_.data(), "onChanged"
, Qt::QueuedConnection
, Q_ARG(QVariant, result)
, Q_ARG(QSharedPointer<Adapter>, tgt_));
tgt_->postEvent(new DataReplyEvent(result, tgt_));
};
execute_nothrow(notify_fn, __PRETTY_FUNCTION__);
}
Expand Down Expand Up @@ -426,22 +445,19 @@ void PropertyMonitor::write(WriteRequest *req)
}
}

void Property::changed(QVariant const &v) const
{
for (auto tgt : targets_)
tgt->postEvent(new DataReplyEvent(v, tgt));
}

bool Property::add(QSharedPointer<Adapter> const &target)
{
auto res = false;
auto it = targets_.find(target);
if (it == targets_.end()) {
targets_.insert(target);
res = true;
auto hold_invoke = [target](QVariant v) {
QMetaObject::invokeMethod
(target.data(), "onChanged"
, Qt::QueuedConnection
, Q_ARG(QVariant, v)
, Q_ARG(QSharedPointer<Adapter>, target));
};
connect(this, &Property::changed, hold_invoke);

}
return res;
}
Expand Down Expand Up @@ -638,7 +654,7 @@ bool Property::update()
void Property::handleActivated(int)
{
if (update())
emit changed(cache_);
changed(cache_);
}

QVariant Property::subscribe()
Expand All @@ -657,7 +673,7 @@ QVariant Property::subscribe_()
file_.connect(this, &Property::handleActivated);

if (update())
emit changed(cache_);
changed(cache_);

return cache_;
}
Expand All @@ -672,12 +688,43 @@ void Property::unsubscribe()
file_.close();
}

void Adapter::postEvent(ReplyEvent *e)
{
QCoreApplication::postEvent(this, static_cast<QEvent*>(e));
}


bool Adapter::event(QEvent *e)
{
if (e->type() < QEvent::User)
return QObject::event(e);
auto res = true;
auto fn = [this, e, &res]() {
auto t = static_cast<Event::Type>(e->type());
switch (t) {
case Event::Data: {
auto p = dynamic_cast<DataReplyEvent*>(e);
if (p)
onChanged(std::move(p->data_));
else
debug::warning("Property: !DataReplyEvent");
break;
}
default:
debug::warning("Unknown user event");
res = QObject::event(e);
}
};
execute_nothrow(fn, __PRETTY_FUNCTION__);
return res;
}

void Adapter::detach()
{
target_ = nullptr;
}

void Adapter::onChanged(QVariant v, QSharedPointer<Adapter>)
void Adapter::onChanged(QVariant v)
{
if (target_)
target_->onChanged(std::move(v));
Expand All @@ -701,7 +748,6 @@ ContextPropertyPrivate::~ContextPropertyPrivate()
{
unsubscribe();
adapter_->detach();
//waitForUnsubscription();
}

bool ContextPropertyPrivate::waitForUnsubscription() const
Expand All @@ -720,6 +766,7 @@ bool ContextPropertyPrivate::waitForUnsubscription() const
status = cor::wait_for(on_unsubscribed_, min_timeout);
if (status != std::future_status::timeout) {
res = true;
return;
} else if (!count) {
debug::warning("Waiting for ages unsubscribing:", key_);
}
Expand Down
17 changes: 8 additions & 9 deletions src/contextkit-subscriber/property.hpp
Expand Up @@ -134,6 +134,7 @@ class Property : public QObject
Q_OBJECT;
public:
enum class Removed { No, Yes, Last };

Property(QString const &key, QObject *parent);
virtual ~Property();

Expand All @@ -144,8 +145,6 @@ class Property : public QObject

bool add(QSharedPointer<Adapter> const&);
Removed remove(QSharedPointer<Adapter> const &);
signals:
void changed(QVariant) const;

private slots:
void handleActivated(int);
Expand All @@ -155,6 +154,7 @@ private slots:
bool tryOpen();
void resubscribe();
QVariant subscribe_();
void changed(QVariant const&) const;

FileReader file_;
QByteArray buffer_;
Expand Down Expand Up @@ -191,19 +191,18 @@ class PropertyMonitor : public QObject
static monitor_ptr instance_;
};

class ReplyEvent;
class Adapter : public QObject
{
Q_OBJECT;
Q_OBJECT
public:
Adapter(ContextPropertyPrivate *target)
: target_(target)
{}

void postEvent(ReplyEvent *);
virtual bool event(QEvent *);

public slots:
void onChanged(QVariant, QSharedPointer<Adapter>);
private:
friend class ::ContextPropertyPrivate;
Adapter(ContextPropertyPrivate *target) : target_(target) {}
void onChanged(QVariant);
void detach();
ContextPropertyPrivate *target_;
};
Expand Down

0 comments on commit e100c74

Please sign in to comment.