Commit c246dd22 authored by Marcelo Juchem's avatar Marcelo Juchem Committed by Jordan DeLong

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
parent a25e59da
......@@ -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
......
/*
* 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();
}
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