Skip to content

Commit

Permalink
Syntactic sugar
Browse files Browse the repository at this point in the history
Summary: This makes code easy to read for eyes used to unique/shared ptrs.

Test Plan: unit-tests

Reviewed By: delong.j@fb.com

FB internal diff: D575997
  • Loading branch information
Rajat Goel authored and jdelong committed Oct 12, 2012
1 parent b9836f4 commit 4e74526
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
18 changes: 11 additions & 7 deletions folly/ThreadLocal.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class ThreadLocal {


T* get() const { T* get() const {
T* ptr = tlp_.get(); T* ptr = tlp_.get();
if (UNLIKELY(ptr == NULL)) { if (UNLIKELY(ptr == nullptr)) {
ptr = new T(); ptr = new T();
tlp_.reset(ptr); tlp_.reset(ptr);
} }
Expand All @@ -94,7 +94,7 @@ class ThreadLocal {
return *get(); return *get();
} }


void reset(T* newPtr = NULL) { void reset(T* newPtr = nullptr) {
tlp_.reset(newPtr); tlp_.reset(newPtr);
} }


Expand Down Expand Up @@ -168,7 +168,7 @@ class ThreadLocalPtr {
return *get(); return *get();
} }


void reset(T* newPtr) { void reset(T* newPtr = nullptr) {
threadlocal_detail::ElementWrapper& w = threadlocal_detail::ElementWrapper& w =
threadlocal_detail::StaticMeta<Tag>::get(id_); threadlocal_detail::StaticMeta<Tag>::get(id_);
if (w.ptr != newPtr) { if (w.ptr != newPtr) {
Expand All @@ -177,6 +177,10 @@ class ThreadLocalPtr {
} }
} }


explicit operator bool() const {
return get() != nullptr;
}

/** /**
* reset() with a custom deleter: * reset() with a custom deleter:
* deleter(T* ptr, TLPDestructionMode mode) * deleter(T* ptr, TLPDestructionMode mode)
Expand Down Expand Up @@ -277,7 +281,7 @@ class ThreadLocalPtr {
lock_(other.lock_), lock_(other.lock_),
id_(other.id_) { id_(other.id_) {
other.id_ = 0; other.id_ = 0;
other.lock_ = NULL; other.lock_ = nullptr;
} }


Accessor& operator=(Accessor&& other) FOLLY_NOEXCEPT { Accessor& operator=(Accessor&& other) FOLLY_NOEXCEPT {
Expand All @@ -288,15 +292,15 @@ class ThreadLocalPtr {
// which is impossible, which leaves only one possible scenario -- // which is impossible, which leaves only one possible scenario --
// *this is empty. Assert it. // *this is empty. Assert it.
assert(&meta_ == &other.meta_); assert(&meta_ == &other.meta_);
assert(lock_ == NULL); assert(lock_ == nullptr);
using std::swap; using std::swap;
swap(lock_, other.lock_); swap(lock_, other.lock_);
swap(id_, other.id_); swap(id_, other.id_);
} }


Accessor() Accessor()
: meta_(threadlocal_detail::StaticMeta<Tag>::instance()), : meta_(threadlocal_detail::StaticMeta<Tag>::instance()),
lock_(NULL), lock_(nullptr),
id_(0) { id_(0) {
} }


Expand All @@ -312,7 +316,7 @@ class ThreadLocalPtr {
if (lock_) { if (lock_) {
lock_->unlock(); lock_->unlock();
id_ = 0; id_ = 0;
lock_ = NULL; lock_ = nullptr;
} }
} }
}; };
Expand Down
10 changes: 10 additions & 0 deletions folly/test/ThreadLocalTest.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ TEST(ThreadLocalPtr, CustomDeleter1) {
EXPECT_EQ(10, Widget::totalVal_); 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 deleting the ThreadLocalPtr object
TEST(ThreadLocalPtr, CustomDeleter2) { TEST(ThreadLocalPtr, CustomDeleter2) {
Widget::totalVal_ = 0; Widget::totalVal_ = 0;
Expand Down

0 comments on commit 4e74526

Please sign in to comment.