Bond
 
Loading...
Searching...
No Matches
alloc.h
1// Copyright (c) Microsoft. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
4#pragma once
5
6#include <bond/core/config.h>
7
8#include <boost/utility/enable_if.hpp>
9
10#include <type_traits>
11
12
13namespace bond
14{
15namespace detail
16{
17
21template <typename Alloc, typename Enable = void>
23
24template <typename Alloc>
25using empty_base_eligible = std::integral_constant<bool,
26 true
27 #if __cplusplus >= 201402L
28 && !std::is_final<Alloc>::value
29 #endif
30 && std::is_empty<Alloc>::value
31 && std::is_copy_constructible<Alloc>::value>;
32
33template <typename Alloc>
34class allocator_holder<Alloc, typename boost::enable_if<empty_base_eligible<Alloc>>::type>
35 : private Alloc
36{
37public:
38 allocator_holder() = default;
39
40 explicit allocator_holder(const Alloc& alloc) BOND_NOEXCEPT_IF(
41 std::is_nothrow_copy_constructible<Alloc>::value)
42 : Alloc{ alloc }
43 {}
44
45 const Alloc& get() const BOND_NOEXCEPT
46 {
47 return *this;
48 }
49
50 Alloc& get() BOND_NOEXCEPT
51 {
52 return *this;
53 }
54};
55
56template <typename Alloc>
57class allocator_holder<Alloc, typename boost::disable_if<empty_base_eligible<Alloc>>::type>
58{
59public:
60 allocator_holder() = default;
61
62 explicit allocator_holder(const Alloc& alloc) BOND_NOEXCEPT_IF(
63 std::is_nothrow_copy_constructible<Alloc>::value)
64 : _alloc{ alloc }
65 {}
66
67 const Alloc& get() const BOND_NOEXCEPT
68 {
69 return _alloc;
70 }
71
72 Alloc& get() BOND_NOEXCEPT
73 {
74 return _alloc;
75 }
76
77private:
78 Alloc _alloc;
79};
80} // namespace detail
81} // namespace bond
Helper type that holds an allocator.
Definition: alloc.h:22
namespace bond
Definition: apply.h:17