T* get() const {
T* ptr = tlp_.get();
- if (UNLIKELY(ptr == NULL)) {
+ if (UNLIKELY(ptr == nullptr)) {
ptr = new T();
tlp_.reset(ptr);
}
return *get();
}
- void reset(T* newPtr = NULL) {
+ void reset(T* newPtr = nullptr) {
tlp_.reset(newPtr);
}
return *get();
}
- void reset(T* newPtr) {
+ void reset(T* newPtr = nullptr) {
threadlocal_detail::ElementWrapper& w =
threadlocal_detail::StaticMeta<Tag>::get(id_);
if (w.ptr != newPtr) {
}
}
+ explicit operator bool() const {
+ return get() != nullptr;
+ }
+
/**
* reset() with a custom deleter:
* deleter(T* ptr, TLPDestructionMode mode)
lock_(other.lock_),
id_(other.id_) {
other.id_ = 0;
- other.lock_ = NULL;
+ other.lock_ = nullptr;
}
Accessor& operator=(Accessor&& other) FOLLY_NOEXCEPT {
// which is impossible, which leaves only one possible scenario --
// *this is empty. Assert it.
assert(&meta_ == &other.meta_);
- assert(lock_ == NULL);
+ assert(lock_ == nullptr);
using std::swap;
swap(lock_, other.lock_);
swap(id_, other.id_);
Accessor()
: meta_(threadlocal_detail::StaticMeta<Tag>::instance()),
- lock_(NULL),
+ lock_(nullptr),
id_(0) {
}
if (lock_) {
lock_->unlock();
id_ = 0;
- lock_ = NULL;
+ lock_ = nullptr;
}
}
};
EXPECT_EQ(10, Widget::totalVal_);
}
+TEST(ThreadLocalPtr, resetNull) {
+ ThreadLocalPtr<int> tl;
+ EXPECT_FALSE(tl);
+ tl.reset(new int(4));
+ EXPECT_TRUE(static_cast<bool>(tl));
+ EXPECT_EQ(*tl.get(), 4);
+ tl.reset();
+ EXPECT_FALSE(tl);
+}
+
// Test deleting the ThreadLocalPtr object
TEST(ThreadLocalPtr, CustomDeleter2) {
Widget::totalVal_ = 0;