chore: specialize Atomic<std::shared_ptr<T>> (#5133)

This commit is contained in:
nerix 2024-01-26 21:55:30 +01:00 committed by GitHub
parent 5f2e01b611
commit 5cd4c51f07
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 68 additions and 1 deletions

View file

@ -133,6 +133,7 @@
- Dev: Added benchmark for parsing and building recent messages. (#5071) - Dev: Added benchmark for parsing and building recent messages. (#5071)
- Dev: Boost is depended on as a header-only library when using conan. (#5107) - Dev: Boost is depended on as a header-only library when using conan. (#5107)
- Dev: Added signal to invalidate paint buffers of channel views without forcing a relayout. (#5123) - Dev: Added signal to invalidate paint buffers of channel views without forcing a relayout. (#5123)
- Dev: Specialize `Atomic<std::shared_ptr<T>>` if underlying standard library supports it. (#5133)
## 2.4.6 ## 2.4.6

View file

@ -1,5 +1,7 @@
#pragma once #pragma once
#include <atomic>
#include <memory>
#include <mutex> #include <mutex>
namespace chatterino { namespace chatterino {
@ -9,9 +11,10 @@ class Atomic
{ {
public: public:
Atomic() = default; Atomic() = default;
~Atomic() = default;
Atomic(T &&val) Atomic(T &&val)
: value_(val) : value_(std::move(val))
{ {
} }
@ -47,4 +50,67 @@ private:
T value_; T value_;
}; };
#if defined(__cpp_lib_atomic_shared_ptr) && defined(__cpp_concepts)
template <typename T>
class Atomic<std::shared_ptr<T>>
{
// Atomic<std::shared_ptr<T>> must be instantated with a const T
};
template <typename T>
requires std::is_const_v<T>
class Atomic<std::shared_ptr<T>>
{
public:
Atomic() = default;
~Atomic() = default;
Atomic(T &&val)
: value_(std::make_shared<T>(std::move(val)))
{
}
Atomic(std::shared_ptr<T> &&val)
: value_(std::move(val))
{
}
Atomic(const Atomic &) = delete;
Atomic &operator=(const Atomic &) = delete;
Atomic(Atomic &&) = delete;
Atomic &operator=(Atomic &&) = delete;
std::shared_ptr<T> get() const
{
return this->value_.load();
}
void set(const T &val)
{
this->value_.store(std::make_shared<T>(val));
}
void set(T &&val)
{
this->value_.store(std::make_shared<T>(std::move(val)));
}
void set(const std::shared_ptr<T> &val)
{
this->value_.store(val);
}
void set(std::shared_ptr<T> &&val)
{
this->value_.store(std::move(val));
}
private:
std::atomic<std::shared_ptr<T>> value_;
};
#endif
} // namespace chatterino } // namespace chatterino