Commit 2624a682 authored by Marcelo Juchem's avatar Marcelo Juchem Committed by Jordan DeLong

easier rebinding of allocators

Summary: rebinding allocators is too cumbersome, this diff implements a helper to make this job easier.

Test Plan: unit test added

Reviewed By: tudorb@fb.com

FB internal diff: D766451
parent 36b36626
...@@ -169,6 +169,18 @@ class StlAllocator { ...@@ -169,6 +169,18 @@ class StlAllocator {
Alloc* alloc_; Alloc* alloc_;
}; };
/**
* Helper function to obtain rebound allocators
*
* @author: Marcelo Juchem <marcelo@fb.com>
*/
template <typename T, typename Allocator>
typename Allocator::template rebind<T>::other rebind_allocator(
Allocator const &allocator
) {
return typename Allocator::template rebind<T>::other(allocator);
}
/* /*
* Helper classes/functions for creating a unique_ptr using a custom allocator * Helper classes/functions for creating a unique_ptr using a custom allocator
* *
......
...@@ -58,6 +58,34 @@ TEST(StlAllocator, void_allocator) { ...@@ -58,6 +58,34 @@ TEST(StlAllocator, void_allocator) {
ASSERT_EQ(nullptr, i.get()); ASSERT_EQ(nullptr, i.get());
} }
TEST(rebind_allocator, sanity_check) {
std::allocator<long> alloc;
auto i = std::allocate_shared<int>(
rebind_allocator<int, decltype(alloc)>(alloc), 10
);
ASSERT_NE(nullptr, i.get());
EXPECT_EQ(10, *i);
i.reset();
ASSERT_EQ(nullptr, i.get());
auto d = std::allocate_shared<double>(
rebind_allocator<double>(alloc), 5.6
);
ASSERT_NE(nullptr, d.get());
EXPECT_EQ(5.6, *d);
d.reset();
ASSERT_EQ(nullptr, d.get());
auto s = std::allocate_shared<std::string>(
rebind_allocator<std::string>(alloc), "HELLO, WORLD"
);
ASSERT_NE(nullptr, s.get());
EXPECT_EQ("HELLO, WORLD", *s);
s.reset();
ASSERT_EQ(nullptr, s.get());
}
int main(int argc, char **argv) { int main(int argc, char **argv) {
FLAGS_logtostderr = true; FLAGS_logtostderr = true;
google::InitGoogleLogging(argv[0]); 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