|
|
|
@ -1,85 +1,84 @@
|
|
|
|
|
#ifndef MYTINYSTL_CONSTRUCT_H_
|
|
|
|
|
#define MYTINYSTL_CONSTRUCT_H_
|
|
|
|
|
|
|
|
|
|
// 这个头文件包含两个函数 construct,destroy
|
|
|
|
|
// construct : 负责对象的构造
|
|
|
|
|
// destroy : 负责对象的析构
|
|
|
|
|
```cpp
|
|
|
|
|
#ifndef MYTINYSTL_CONSTRUCT_H_ // 预处理器指令,防止头文件被重复包含
|
|
|
|
|
#define MYTINYSTL_CONSTRUCT_H_ // 定义MYTINYSTL_CONSTRUCT_H_宏
|
|
|
|
|
|
|
|
|
|
// 包含标准库中的new头文件
|
|
|
|
|
#include <new>
|
|
|
|
|
|
|
|
|
|
// 包含自定义的类型特征头文件
|
|
|
|
|
#include "type_traits.h"
|
|
|
|
|
// 包含自定义的迭代器头文件
|
|
|
|
|
#include "iterator.h"
|
|
|
|
|
|
|
|
|
|
// 针对MSVC编译器,关闭未使用参数的警告
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
|
#pragma warning(push)
|
|
|
|
|
#pragma warning(disable : 4100) // unused parameter
|
|
|
|
|
#endif // _MSC_VER
|
|
|
|
|
|
|
|
|
|
namespace mystl
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
// construct 构造对象
|
|
|
|
|
// 定义命名空间mystl
|
|
|
|
|
namespace mystl {
|
|
|
|
|
|
|
|
|
|
// construct函数,负责对象的构造
|
|
|
|
|
template <class Ty>
|
|
|
|
|
void construct(Ty* ptr)
|
|
|
|
|
{
|
|
|
|
|
::new ((void*)ptr) Ty();
|
|
|
|
|
void construct(Ty* ptr) {
|
|
|
|
|
::new ((void*)ptr) Ty(); // 使用placement new构造对象
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// construct函数,接受两个参数,用于构造对象并初始化
|
|
|
|
|
template <class Ty1, class Ty2>
|
|
|
|
|
void construct(Ty1* ptr, const Ty2& value)
|
|
|
|
|
{
|
|
|
|
|
::new ((void*)ptr) Ty1(value);
|
|
|
|
|
void construct(Ty1* ptr, const Ty2& value) {
|
|
|
|
|
::new ((void*)ptr) Ty1(value); // 使用placement new构造对象,并使用value初始化
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// construct函数,接受可变参数,用于构造对象
|
|
|
|
|
template <class Ty, class... Args>
|
|
|
|
|
void construct(Ty* ptr, Args&&... args)
|
|
|
|
|
{
|
|
|
|
|
::new ((void*)ptr) Ty(mystl::forward<Args>(args)...);
|
|
|
|
|
void construct(Ty* ptr, Args&&... args) {
|
|
|
|
|
::new ((void*)ptr) Ty(mystl::forward<Args>(args)...); // 使用placement new构造对象,并使用可变参数初始化
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// destroy 将对象析构
|
|
|
|
|
|
|
|
|
|
// destroy_one函数,负责单个对象的析构
|
|
|
|
|
template <class Ty>
|
|
|
|
|
void destroy_one(Ty*, std::true_type) {}
|
|
|
|
|
|
|
|
|
|
template <class Ty>
|
|
|
|
|
void destroy_one(Ty* pointer, std::false_type)
|
|
|
|
|
{
|
|
|
|
|
if (pointer != nullptr)
|
|
|
|
|
{
|
|
|
|
|
pointer->~Ty();
|
|
|
|
|
void destroy_one(Ty* pointer, std::false_type) {
|
|
|
|
|
if (pointer != nullptr) {
|
|
|
|
|
pointer->~Ty(); // 调用对象的析构函数
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// destroy_cat函数,用于迭代器范围内对象的析构
|
|
|
|
|
template <class ForwardIter>
|
|
|
|
|
void destroy_cat(ForwardIter , ForwardIter , std::true_type) {}
|
|
|
|
|
|
|
|
|
|
template <class ForwardIter>
|
|
|
|
|
void destroy_cat(ForwardIter first, ForwardIter last, std::false_type)
|
|
|
|
|
{
|
|
|
|
|
for (; first != last; ++first)
|
|
|
|
|
destroy(&*first);
|
|
|
|
|
void destroy_cat(ForwardIter first, ForwardIter last, std::false_type) {
|
|
|
|
|
for (; first != last; ++first) {
|
|
|
|
|
destroy(&*first); // 析构迭代器指向的对象
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// destroy函数,负责单个对象的析构
|
|
|
|
|
template <class Ty>
|
|
|
|
|
void destroy(Ty* pointer)
|
|
|
|
|
{
|
|
|
|
|
destroy_one(pointer, std::is_trivially_destructible<Ty>{});
|
|
|
|
|
void destroy(Ty* pointer) {
|
|
|
|
|
destroy_one(pointer, std::is_trivially_destructible<Ty>{}); // 根据对象是否具有平凡析构函数来决定是否调用析构函数
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// destroy函数,用于迭代器范围内对象的析构
|
|
|
|
|
template <class ForwardIter>
|
|
|
|
|
void destroy(ForwardIter first, ForwardIter last)
|
|
|
|
|
{
|
|
|
|
|
void destroy(ForwardIter first, ForwardIter last) {
|
|
|
|
|
destroy_cat(first, last, std::is_trivially_destructible<
|
|
|
|
|
typename iterator_traits<ForwardIter>::value_type>{});
|
|
|
|
|
typename iterator_traits<ForwardIter>::value_type>{}); // 根据迭代器指向的对象类型是否具有平凡析构函数来决定是否调用析构函数
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace mystl
|
|
|
|
|
|
|
|
|
|
// 针对MSVC编译器,恢复警告设置
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
|
#pragma warning(pop)
|
|
|
|
|
#endif // _MSC_VER
|
|
|
|
|
|
|
|
|
|
#endif // !MYTINYSTL_CONSTRUCT_H_
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|