improving TwitchChannel
This commit is contained in:
@@ -215,4 +215,9 @@ void Channel::onConnected()
|
||||
{
|
||||
}
|
||||
|
||||
std::weak_ptr<Channel> Channel::weak_from_this()
|
||||
{
|
||||
return std::weak_ptr<Channel>(this->shared_from_this());
|
||||
}
|
||||
|
||||
} // namespace chatterino
|
||||
|
||||
@@ -65,6 +65,9 @@ public:
|
||||
|
||||
CompletionModel completionModel;
|
||||
|
||||
// pre c++17 polyfill
|
||||
std::weak_ptr<Channel> weak_from_this();
|
||||
|
||||
protected:
|
||||
virtual void onConnected();
|
||||
|
||||
|
||||
@@ -27,14 +27,14 @@ const Qt::KeyboardModifiers showResizeHandlesModifiers = Qt::ControlModifier;
|
||||
|
||||
static const char *ANONYMOUS_USERNAME_LABEL ATTR_UNUSED = " - anonymous - ";
|
||||
|
||||
#define return_if(x) \
|
||||
if ((x)) { \
|
||||
return; \
|
||||
#define return_if(condition) \
|
||||
if ((condition)) { \
|
||||
return; \
|
||||
}
|
||||
|
||||
#define return_unless(x) \
|
||||
if (!(x)) { \
|
||||
return; \
|
||||
#define return_unless(condition) \
|
||||
if (!(condition)) { \
|
||||
return; \
|
||||
}
|
||||
|
||||
} // namespace chatterino
|
||||
|
||||
@@ -212,9 +212,7 @@ void NetworkRequest::doRequest()
|
||||
return;
|
||||
}
|
||||
|
||||
QByteArray readBytes = reply->readAll();
|
||||
QByteArray bytes;
|
||||
bytes.setRawData(readBytes.data(), readBytes.size());
|
||||
QByteArray bytes = reply->readAll();
|
||||
data->writeToCache(bytes);
|
||||
|
||||
NetworkResult result(bytes);
|
||||
|
||||
@@ -25,7 +25,7 @@ QJsonObject NetworkResult::parseJson() const
|
||||
|
||||
rapidjson::Document NetworkResult::parseRapidJson() const
|
||||
{
|
||||
rapidjson::Document ret(rapidjson::kNullType);
|
||||
rapidjson::Document ret(rapidjson::kObjectType);
|
||||
|
||||
rapidjson::ParseResult result = ret.Parse(this->data_.data(), this->data_.length());
|
||||
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
#include "uniqueaccess.hpp"
|
||||
@@ -0,0 +1,81 @@
|
||||
#pragma once
|
||||
|
||||
#include <mutex>
|
||||
#include <type_traits>
|
||||
|
||||
namespace chatterino {
|
||||
|
||||
template <typename T>
|
||||
class AccessGuard
|
||||
{
|
||||
public:
|
||||
AccessGuard(T &element, std::mutex &mutex)
|
||||
: element_(element)
|
||||
, mutex_(mutex)
|
||||
{
|
||||
this->mutex_.lock();
|
||||
}
|
||||
|
||||
~AccessGuard()
|
||||
{
|
||||
this->mutex_.unlock();
|
||||
}
|
||||
|
||||
T *operator->() const
|
||||
{
|
||||
return &this->element_;
|
||||
}
|
||||
|
||||
T &operator*() const
|
||||
{
|
||||
return this->element_;
|
||||
}
|
||||
|
||||
private:
|
||||
T &element_;
|
||||
std::mutex &mutex_;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
class UniqueAccess
|
||||
{
|
||||
public:
|
||||
template <typename std::enable_if<std::is_default_constructible<T>::value>::type * = 0>
|
||||
UniqueAccess()
|
||||
: element_(T())
|
||||
{
|
||||
}
|
||||
|
||||
UniqueAccess(const T &element)
|
||||
: element_(element)
|
||||
{
|
||||
}
|
||||
|
||||
UniqueAccess(T &&element)
|
||||
: element_(element)
|
||||
{
|
||||
}
|
||||
|
||||
UniqueAccess<T> &operator=(const T &element)
|
||||
{
|
||||
this->element_ = element;
|
||||
return *this;
|
||||
}
|
||||
|
||||
UniqueAccess<T> &operator=(T &&element)
|
||||
{
|
||||
this->element_ = element;
|
||||
return *this;
|
||||
}
|
||||
|
||||
AccessGuard<T> access()
|
||||
{
|
||||
return AccessGuard<T>(this->element_, this->mutex_);
|
||||
}
|
||||
|
||||
private:
|
||||
T element_;
|
||||
std::mutex mutex_;
|
||||
};
|
||||
|
||||
} // namespace chatterino
|
||||
Reference in New Issue
Block a user