/* This file is part of Telegram Desktop, the official desktop application for the Telegram messaging service. For license and copyright information please follow this link: https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #pragma once #include namespace base { template class thread_safe_wrap { public: template thread_safe_wrap(Args &&...args) : _value(std::forward(args)...) { } template auto with(Callback &&callback) { QMutexLocker lock(&_mutex); return callback(_value); } template auto with(Callback &&callback) const { QMutexLocker lock(&_mutex); return callback(_value); } private: T _value; QMutex _mutex; }; template typename Container = std::deque> class thread_safe_queue { public: template void emplace(Args &&...args) { _wrap.with([&](Container &value) { value.emplace_back(std::forward(args)...); }); } Container take() { return _wrap.with([&](Container &value) { return std::exchange(value, Container()); }); } private: thread_safe_wrap> _wrap; }; } // namespace base