Commit 36b36626 authored by Marcelo Juchem's avatar Marcelo Juchem Committed by Jordan DeLong

Making StlAllocator<Alloc, void> usable when rebinding.

Summary:
currently, StlAllocator<Alloc, void> can't be used when you
want an untyped allocator that can be rebound later since it doesn't
carry the SimpleAllocator pointer with it. This diff fixes that.

Test Plan: unit test added

Reviewed By: jon.coens@fb.com

FB internal diff: D766559
parent 8a4545ed
......@@ -78,9 +78,28 @@ template <class Alloc> class StlAllocator<Alloc, void> {
typedef void value_type;
typedef void* pointer;
typedef const void* const_pointer;
StlAllocator() : alloc_(nullptr) { }
explicit StlAllocator(Alloc* alloc) : alloc_(alloc) { }
Alloc* alloc() const {
return alloc_;
}
template <class U> struct rebind {
typedef StlAllocator<Alloc, U> other;
};
bool operator!=(const StlAllocator<Alloc, void>& other) const {
return alloc_ != other.alloc_;
}
bool operator==(const StlAllocator<Alloc, void>& other) const {
return alloc_ == other.alloc_;
}
private:
Alloc* alloc_;
};
template <class Alloc, class T>
......
......@@ -43,6 +43,21 @@ TEST(as_stl_allocator, sanity_check) {
>::value));
}
TEST(StlAllocator, void_allocator) {
typedef StlAllocator<SysArena, void> void_allocator;
SysArena arena;
void_allocator valloc(&arena);
typedef void_allocator::rebind<int>::other int_allocator;
int_allocator ialloc(valloc);
auto i = std::allocate_shared<int>(ialloc, 10);
ASSERT_NE(nullptr, i.get());
EXPECT_EQ(10, *i);
i.reset();
ASSERT_EQ(nullptr, i.get());
}
int main(int argc, char **argv) {
FLAGS_logtostderr = true;
google::InitGoogleLogging(argv[0]);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment