NexusForce 1.0.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
construct.hpp
浏览该文件的文档.
1#ifndef NEFORCE_CORE_MEMORY_CONSTRUCT_HPP__
2#define NEFORCE_CORE_MEMORY_CONSTRUCT_HPP__
3
11
13NEFORCE_BEGIN_NAMESPACE__
14
20
32template <typename T, typename... Args>
33NEFORCE_CONSTEXPR20 T* construct(T* ptr, Args&&... args) noexcept(is_nothrow_constructible_v<T, Args...>) {
34 static_assert(is_constructible_v<T, Args...>, "T must be constructible with arguments");
35 return static_cast<T*>(new (static_cast<void*>(ptr)) T(_NEFORCE forward<Args>(args)...));
36}
37
45template <typename T>
46NEFORCE_CONSTEXPR20 void destroy(T* pointer) noexcept(is_nothrow_destructible_v<T>) {
47 pointer->~T();
48}
49
59template <typename Iterator>
61destroy(Iterator first, Iterator last) noexcept(is_nothrow_destructible_v<iter_value_t<Iterator>>) {
62 for (; first < last; ++first) {
63 _NEFORCE destroy(&*first);
64 }
65 return;
66}
67
77template <typename Iterator>
79destroy(Iterator first, Iterator last) noexcept {
80 return;
81}
82
83
84template <typename T>
85struct temporary_guard {
86 static_assert(is_nothrow_move_constructible_v<T>, "T must be nothrow move constructible");
87
88private:
89 T* guarded_ptr;
90 T temp;
91
92public:
93 constexpr explicit temporary_guard(T& value) :
94 guarded_ptr(_NEFORCE addressof(value)),
95 temp(_NEFORCE move(value)) {
96 _NEFORCE destroy(guarded_ptr);
97 }
98
99 NEFORCE_CONSTEXPR20 ~temporary_guard() {
100 if (guarded_ptr) {
101 _NEFORCE construct(guarded_ptr, _NEFORCE move(temp));
102 }
103 }
104
105 temporary_guard(const temporary_guard&) = delete;
106 temporary_guard& operator=(const temporary_guard&) = delete;
107
108 constexpr T&& release() noexcept {
109 guarded_ptr = nullptr;
110 return _NEFORCE move(temp);
111 }
112};
113
114template <typename NT, typename OT, typename Arg>
116reinitialize(NT* new_val, OT* old_val, Arg&& arg) noexcept(is_nothrow_constructible_v<NT, Arg>) {
117 _NEFORCE destroy(old_val);
118 _NEFORCE construct(new_val, _NEFORCE forward<Arg>(arg));
119 return;
120}
121
122template <typename NT, typename OT, typename Arg>
124reinitialize(NT* new_val, OT* old_val, Arg&& arg) noexcept(is_nothrow_constructible_v<NT, Arg>) {
125 NT temp(_NEFORCE forward<Arg>(arg));
126 _NEFORCE destroy(old_val);
127 _NEFORCE construct(new_val, _NEFORCE move(temp));
128 return;
129}
130
131template <typename NT, typename OT, typename Arg>
133reinitialize(NT* new_val, OT* old_val, Arg&& arg) noexcept(is_nothrow_constructible_v<NT, Arg>) {
134 temporary_guard<OT> guard(*old_val);
135 _NEFORCE construct(new_val, _NEFORCE forward<Arg>(arg));
136 guard.release();
137 return;
138}
139 // InplaceMemoryFunction
141
142NEFORCE_END_NAMESPACE__
143#endif // NEFORCE_CORE_MEMORY_CONSTRUCT_HPP__
概念和类型约束
NEFORCE_NODISCARD constexpr T * addressof(T &x) noexcept
获取对象的地址
NEFORCE_NODISCARD constexpr T && forward(remove_reference_t< T > &x) noexcept
完美转发左值
NEFORCE_CONSTEXPR20 T * construct(T *ptr, Args &&... args) noexcept(is_nothrow_constructible_v< T, Args... >)
在指定内存位置构造对象
NEFORCE_CONSTEXPR20 void destroy(T *pointer) noexcept(is_nothrow_destructible_v< T >)
销毁单个对象
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result) noexcept(noexcept(inner::__move_aux(first, last, result)))
移动范围元素
NEFORCE_INLINE17 constexpr bool is_nothrow_constructible_v
is_nothrow_constructible的便捷变量模板
NEFORCE_INLINE17 constexpr bool is_trivially_destructible_v
is_trivially_destructible的便捷变量模板
NEFORCE_INLINE17 constexpr bool is_nothrow_destructible_v
is_nothrow_destructible的便捷变量模板
NEFORCE_INLINE17 constexpr bool is_constructible_v
is_constructible的便捷变量模板
NEFORCE_INLINE17 constexpr bool is_nothrow_move_constructible_v
is_nothrow_move_constructible的便捷变量模板
typename enable_if< Test, T >::type enable_if_t
enable_if的便捷别名