]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // |
2 | // Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com) | |
3 | // | |
4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
6 | // | |
7 | ||
8 | #ifndef BEAST_DETAIL_EMPTY_BASE_OPTIMIZATION_HPP | |
9 | #define BEAST_DETAIL_EMPTY_BASE_OPTIMIZATION_HPP | |
10 | ||
11 | #include <type_traits> | |
12 | #include <utility> | |
13 | ||
14 | namespace beast { | |
15 | namespace detail { | |
16 | ||
17 | template<class T> | |
18 | struct empty_base_optimization_decide | |
19 | : std::integral_constant <bool, | |
20 | std::is_empty <T>::value | |
21 | #ifdef __clang__ | |
22 | && !__is_final(T) | |
23 | #endif | |
24 | > | |
25 | { | |
26 | }; | |
27 | ||
28 | template< | |
29 | class T, | |
30 | int UniqueID = 0, | |
31 | bool ShouldDeriveFrom = | |
32 | empty_base_optimization_decide<T>::value | |
33 | > | |
34 | class empty_base_optimization : private T | |
35 | { | |
36 | public: | |
37 | empty_base_optimization() = default; | |
38 | ||
39 | empty_base_optimization(T const& t) | |
40 | : T (t) | |
41 | {} | |
42 | ||
43 | empty_base_optimization(T&& t) | |
44 | : T (std::move (t)) | |
45 | {} | |
46 | ||
47 | T& member() noexcept | |
48 | { | |
49 | return *this; | |
50 | } | |
51 | ||
52 | T const& member() const noexcept | |
53 | { | |
54 | return *this; | |
55 | } | |
56 | }; | |
57 | ||
58 | //------------------------------------------------------------------------------ | |
59 | ||
60 | template< | |
61 | class T, | |
62 | int UniqueID | |
63 | > | |
64 | class empty_base_optimization <T, UniqueID, false> | |
65 | { | |
66 | public: | |
67 | empty_base_optimization() = default; | |
68 | ||
69 | empty_base_optimization(T const& t) | |
70 | : m_t (t) | |
71 | {} | |
72 | ||
73 | empty_base_optimization(T&& t) | |
74 | : m_t (std::move (t)) | |
75 | {} | |
76 | ||
77 | T& member() noexcept | |
78 | { | |
79 | return m_t; | |
80 | } | |
81 | ||
82 | T const& member() const noexcept | |
83 | { | |
84 | return m_t; | |
85 | } | |
86 | ||
87 | private: | |
88 | T m_t; | |
89 | }; | |
90 | ||
91 | } // detail | |
92 | } // beast | |
93 | ||
94 | #endif |