1/*
2 * Distributed under the Boost Software License, Version 1.0.
3 * (See accompanying file LICENSE_1_0.txt or copy at
4 * https://www.boost.org/LICENSE_1_0.txt)
5 *
6 * Copyright (c) 2022 Andrey Semashev
7 */
8/*!
9 * \file scope/detail/compact_storage.hpp
10 *
11 * This header contains utility helpers for implementing compact storage
12 * for class members. In particular, it allows to leverage empty base optimization (EBO).
13 */
14
15#ifndef BOOST_SCOPE_DETAIL_COMPACT_STORAGE_HPP_INCLUDED_
16#define BOOST_SCOPE_DETAIL_COMPACT_STORAGE_HPP_INCLUDED_
17
18#include <type_traits>
19#include <boost/scope/detail/config.hpp>
20#include <boost/scope/detail/type_traits/is_final.hpp>
21#include <boost/scope/detail/type_traits/negation.hpp>
22#include <boost/scope/detail/type_traits/conjunction.hpp>
23#include <boost/scope/detail/header.hpp>
24
25#ifdef BOOST_HAS_PRAGMA_ONCE
26#pragma once
27#endif
28
29namespace boost {
30namespace scope {
31namespace detail {
32
33//! The class allows to place data members in the tail padding of type \a T if the user's class derives from it
34template<
35 typename T,
36 typename Tag = void,
37 bool = detail::conjunction< std::is_class< T >, detail::negation< detail::is_final< T > > >::value
38>
39class compact_storage :
40 private T
41{
42public:
43 template< typename... Args >
44 constexpr compact_storage(Args&&... args) noexcept(std::is_nothrow_constructible< T, Args... >::value) :
45 T(static_cast< Args&& >(args)...)
46 {
47 }
48
49 compact_storage(compact_storage&&) = default;
50 compact_storage& operator= (compact_storage&&) = default;
51
52 compact_storage(compact_storage const&) = default;
53 compact_storage& operator= (compact_storage const&) = default;
54
55 T& get() noexcept
56 {
57 return *static_cast< T* >(this);
58 }
59
60 T const& get() const noexcept
61 {
62 return *static_cast< const T* >(this);
63 }
64};
65
66template< typename T, typename Tag >
67class compact_storage< T, Tag, false >
68{
69private:
70 T m_data;
71
72public:
73 template< typename... Args >
74 constexpr compact_storage(Args&&... args) noexcept(std::is_nothrow_constructible< T, Args... >::value) :
75 m_data(static_cast< Args&& >(args)...)
76 {
77 }
78
79 compact_storage(compact_storage&&) = default;
80 compact_storage& operator= (compact_storage&&) = default;
81
82 compact_storage(compact_storage const&) = default;
83 compact_storage& operator= (compact_storage const&) = default;
84
85 T& get() noexcept
86 {
87 return m_data;
88 }
89
90 T const& get() const noexcept
91 {
92 return m_data;
93 }
94};
95
96} // namespace detail
97} // namespace scope
98} // namespace boost
99
100#include <boost/scope/detail/footer.hpp>
101
102#endif // BOOST_SCOPE_DETAIL_COMPACT_STORAGE_HPP_INCLUDED_
103

source code of boost/libs/scope/include/boost/scope/detail/compact_storage.hpp