as_stl_allocator "template alias"
authorMarcelo Juchem <marcelo@fb.com>
Mon, 1 Apr 2013 23:00:01 +0000 (16:00 -0700)
committerJordan DeLong <jdelong@fb.com>
Sun, 21 Apr 2013 20:20:31 +0000 (13:20 -0700)
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

folly/Memory.h
folly/test/MemoryTest.cpp [new file with mode: 0644]

index 09f10f36b351ae79c2aee496b20628c3c8a30304..005341774c67880e4bf70fac887774793668d20f 100644 (file)
@@ -210,6 +210,18 @@ public:
     && !has_destroy<allocator, void(void*)>::value;
 };
 
+template <typename T, typename Allocator>
+struct as_stl_allocator {
+  typedef typename std::conditional<
+    is_simple_allocator<T, Allocator>::value,
+    folly::StlAllocator<
+      typename std::remove_reference<Allocator>::type,
+      typename std::remove_reference<T>::type
+    >,
+    typename std::remove_reference<Allocator>::type
+  >::type type;
+};
+
 template <typename T, typename Allocator>
 typename std::enable_if<
   is_simple_allocator<T, 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 <marcelo@fb.com>
+ */
+
 template <typename T, typename Allocator>
 struct AllocatorUniquePtr {
   typedef std::unique_ptr<T,
@@ -245,6 +264,13 @@ struct AllocatorUniquePtr {
   > type;
 };
 
+/**
+ * Functions to allocate a unique_ptr / shared_ptr, supporting both
+ * STL-style allocators and SimpleAllocator, analog to std::allocate_shared
+ *
+ * @author: Marcelo Juchem <marcelo@fb.com>
+ */
+
 template <typename T, typename Allocator, typename ...Args>
 typename AllocatorUniquePtr<T, Allocator>::type allocate_unique(
   Allocator&& allocator, Args&&... args
diff --git a/folly/test/MemoryTest.cpp b/folly/test/MemoryTest.cpp
new file mode 100644 (file)
index 0000000..82f1a50
--- /dev/null
@@ -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 <glog/logging.h>
+#include <gtest/gtest.h>
+
+#include <type_traits>
+
+using namespace folly;
+
+template <std::size_t> struct T {};
+template <std::size_t> struct S {};
+template <std::size_t> struct P {};
+
+TEST(as_stl_allocator, sanity_check) {
+  typedef StlAllocator<SysArena, int> stl_arena_alloc;
+
+  EXPECT_TRUE((std::is_same<
+    as_stl_allocator<int, SysArena>::type,
+    stl_arena_alloc
+  >::value));
+
+  EXPECT_TRUE((std::is_same<
+    as_stl_allocator<int, stl_arena_alloc>::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();
+}