/* 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 class thread_safe_queue { public: template void emplace(Args &&...args) { _wrap.with([&](std::vector &value) { value.emplace_back(std::forward(args)...); }); } std::vector take() { return _wrap.with([&](std::vector &value) { return std::exchange(value, std::vector()); }); } private: thread_safe_wrap> _wrap; }; } // namespace base