1 | //===----------------------------------------------------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include <__type_traits/is_trivially_relocatable.h> |
10 | #include <memory> |
11 | #include <string> |
12 | |
13 | #include "constexpr_char_traits.h" |
14 | #include "test_allocator.h" |
15 | |
16 | static_assert(std::__libcpp_is_trivially_relocatable<char>::value, "" ); |
17 | static_assert(std::__libcpp_is_trivially_relocatable<int>::value, "" ); |
18 | static_assert(std::__libcpp_is_trivially_relocatable<double>::value, "" ); |
19 | |
20 | struct Empty {}; |
21 | static_assert(std::__libcpp_is_trivially_relocatable<Empty>::value, "" ); |
22 | |
23 | struct TriviallyCopyable { |
24 | char c; |
25 | int i; |
26 | Empty s; |
27 | }; |
28 | static_assert(std::__libcpp_is_trivially_relocatable<TriviallyCopyable>::value, "" ); |
29 | |
30 | struct NotTriviallyCopyable { |
31 | NotTriviallyCopyable(const NotTriviallyCopyable&); |
32 | ~NotTriviallyCopyable(); |
33 | }; |
34 | static_assert(!std::__libcpp_is_trivially_relocatable<NotTriviallyCopyable>::value, "" ); |
35 | |
36 | struct MoveOnlyTriviallyCopyable { |
37 | MoveOnlyTriviallyCopyable(const MoveOnlyTriviallyCopyable&) = delete; |
38 | MoveOnlyTriviallyCopyable& operator=(const MoveOnlyTriviallyCopyable&) = delete; |
39 | MoveOnlyTriviallyCopyable(MoveOnlyTriviallyCopyable&&) = default; |
40 | MoveOnlyTriviallyCopyable& operator=(MoveOnlyTriviallyCopyable&&) = default; |
41 | }; |
42 | #ifndef _MSC_VER |
43 | static_assert(std::__libcpp_is_trivially_relocatable<MoveOnlyTriviallyCopyable>::value, "" ); |
44 | #else |
45 | static_assert(!std::__libcpp_is_trivially_relocatable<MoveOnlyTriviallyCopyable>::value, "" ); |
46 | #endif |
47 | // standard library types |
48 | // ---------------------- |
49 | |
50 | // basic_string |
51 | struct MyChar { |
52 | char c; |
53 | }; |
54 | template <class T> |
55 | struct NotTriviallyRelocatableCharTraits : constexpr_char_traits<T> { |
56 | NotTriviallyRelocatableCharTraits(const NotTriviallyRelocatableCharTraits&); |
57 | NotTriviallyRelocatableCharTraits& operator=(const NotTriviallyRelocatableCharTraits&); |
58 | ~NotTriviallyRelocatableCharTraits(); |
59 | }; |
60 | |
61 | static_assert(std::__libcpp_is_trivially_relocatable< |
62 | std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::value, |
63 | "" ); |
64 | static_assert(std::__libcpp_is_trivially_relocatable< |
65 | std::basic_string<char, NotTriviallyRelocatableCharTraits<char>, std::allocator<char> > >::value, |
66 | "" ); |
67 | static_assert(std::__libcpp_is_trivially_relocatable< |
68 | std::basic_string<MyChar, constexpr_char_traits<MyChar>, std::allocator<MyChar> > >::value, |
69 | "" ); |
70 | static_assert( |
71 | std::__libcpp_is_trivially_relocatable< |
72 | std::basic_string<MyChar, NotTriviallyRelocatableCharTraits<MyChar>, std::allocator<MyChar> > >::value, |
73 | "" ); |
74 | static_assert(!std::__libcpp_is_trivially_relocatable< |
75 | std::basic_string<char, std::char_traits<char>, test_allocator<char> > >::value, |
76 | "" ); |
77 | static_assert( |
78 | !std::__libcpp_is_trivially_relocatable< |
79 | std::basic_string<MyChar, NotTriviallyRelocatableCharTraits<MyChar>, test_allocator<MyChar> > >::value, |
80 | "" ); |
81 | |
82 | // unique_ptr |
83 | struct NotTriviallyRelocatableDeleter { |
84 | NotTriviallyRelocatableDeleter(const NotTriviallyRelocatableDeleter&); |
85 | NotTriviallyRelocatableDeleter& operator=(const NotTriviallyRelocatableDeleter&); |
86 | ~NotTriviallyRelocatableDeleter(); |
87 | |
88 | template <class T> |
89 | void operator()(T*); |
90 | }; |
91 | |
92 | struct NotTriviallyRelocatablePointer { |
93 | struct pointer { |
94 | pointer(const pointer&); |
95 | pointer& operator=(const pointer&); |
96 | ~pointer(); |
97 | }; |
98 | |
99 | template <class T> |
100 | void operator()(T*); |
101 | }; |
102 | |
103 | static_assert(std::__libcpp_is_trivially_relocatable<std::unique_ptr<int> >::value, "" ); |
104 | static_assert(std::__libcpp_is_trivially_relocatable<std::unique_ptr<NotTriviallyCopyable> >::value, "" ); |
105 | static_assert(std::__libcpp_is_trivially_relocatable<std::unique_ptr<int[]> >::value, "" ); |
106 | static_assert(!std::__libcpp_is_trivially_relocatable<std::unique_ptr<int, NotTriviallyRelocatableDeleter> >::value, |
107 | "" ); |
108 | static_assert(!std::__libcpp_is_trivially_relocatable<std::unique_ptr<int[], NotTriviallyRelocatableDeleter> >::value, |
109 | "" ); |
110 | static_assert(!std::__libcpp_is_trivially_relocatable<std::unique_ptr<int, NotTriviallyRelocatablePointer> >::value, |
111 | "" ); |
112 | static_assert(!std::__libcpp_is_trivially_relocatable<std::unique_ptr<int[], NotTriviallyRelocatablePointer> >::value, |
113 | "" ); |
114 | |
115 | // TODO: Mark all the trivially relocatable STL types as such |
116 | |