From: Marcelo Juchem Date: Mon, 1 Apr 2013 23:00:01 +0000 (-0700) Subject: as_stl_allocator "template alias" X-Git-Tag: v0.22.0~1017 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=c246dd22a0dc296fc3bab7837e790b0fbf4deed5;p=folly.git as_stl_allocator "template alias" Summary: Implementing as_stl_allocator as a companion to the existing make_stl_allocator Test Plan: unit tests added Reviewed By: jon.coens@fb.com FB internal diff: D755207 --- diff --git a/folly/Memory.h b/folly/Memory.h index 09f10f36..00534177 100644 --- a/folly/Memory.h +++ b/folly/Memory.h @@ -210,6 +210,18 @@ public: && !has_destroy::value; }; +template +struct as_stl_allocator { + typedef typename std::conditional< + is_simple_allocator::value, + folly::StlAllocator< + typename std::remove_reference::type, + typename std::remove_reference::type + >, + typename std::remove_reference::type + >::type type; +}; + template typename std::enable_if< is_simple_allocator::value, @@ -232,6 +244,13 @@ typename std::enable_if< return std::move(allocator); } +/** + * AllocatorUniquePtr: a unique_ptr that supports both STL-style + * allocators and SimpleAllocator + * + * @author: Marcelo Juchem + */ + template struct AllocatorUniquePtr { typedef std::unique_ptr type; }; +/** + * Functions to allocate a unique_ptr / shared_ptr, supporting both + * STL-style allocators and SimpleAllocator, analog to std::allocate_shared + * + * @author: Marcelo Juchem + */ + template typename AllocatorUniquePtr::type allocate_unique( Allocator&& allocator, Args&&... args diff --git a/folly/test/MemoryTest.cpp b/folly/test/MemoryTest.cpp new file mode 100644 index 00000000..82f1a50b --- /dev/null +++ b/folly/test/MemoryTest.cpp @@ -0,0 +1,52 @@ +/* + * Copyright 2013 Facebook, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "folly/Memory.h" +#include "folly/Arena.h" +#include "folly/String.h" + +#include +#include + +#include + +using namespace folly; + +template struct T {}; +template struct S {}; +template struct P {}; + +TEST(as_stl_allocator, sanity_check) { + typedef StlAllocator stl_arena_alloc; + + EXPECT_TRUE((std::is_same< + as_stl_allocator::type, + stl_arena_alloc + >::value)); + + EXPECT_TRUE((std::is_same< + as_stl_allocator::type, + stl_arena_alloc + >::value)); +} + +int main(int argc, char **argv) { + FLAGS_logtostderr = true; + google::InitGoogleLogging(argv[0]); + testing::InitGoogleTest(&argc, argv); + + return RUN_ALL_TESTS(); +}