提交 6ae0b91b 编写于 作者: Y Yu Yang

Clean LockGuardPtr

test=develop
上级 1420c3b1
...@@ -29,17 +29,18 @@ namespace platform { ...@@ -29,17 +29,18 @@ namespace platform {
*/ */
template <typename LockType> template <typename LockType>
class LockGuardPtr { class LockGuardPtr {
using LockGuardType = std::lock_guard<LockType>;
public:
class LockGuardDeleter {
public: public:
void operator()(LockGuardType* guard) { guard->~LockGuardType(); }
};
explicit LockGuardPtr(std::unique_ptr<LockType>& lock_ptr) // NOLINT explicit LockGuardPtr(std::unique_ptr<LockType>& lock_ptr) // NOLINT
: guard_ptr_(lock_ptr ? new (guard_buffer_) LockGuardType(*lock_ptr) : lock_(lock_ptr.get()) {
: nullptr) {} if (lock_) {
lock_->lock();
}
}
~LockGuardPtr() {
if (lock_) {
lock_->unlock();
}
}
LockGuardPtr(const LockGuardPtr&) = delete; LockGuardPtr(const LockGuardPtr&) = delete;
LockGuardPtr& operator=(const LockGuardPtr&) = delete; LockGuardPtr& operator=(const LockGuardPtr&) = delete;
...@@ -47,8 +48,7 @@ class LockGuardPtr { ...@@ -47,8 +48,7 @@ class LockGuardPtr {
LockGuardPtr& operator=(LockGuardPtr&&) = delete; LockGuardPtr& operator=(LockGuardPtr&&) = delete;
private: private:
uint8_t guard_buffer_[sizeof(LockGuardType)]; LockType* lock_;
std::unique_ptr<LockGuardType, LockGuardDeleter> guard_ptr_;
}; };
} // namespace platform } // namespace platform
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册