2022-10-09 05:13:17 +00:00
|
|
|
#ifndef _RSTL_AUTO_PTR
|
|
|
|
#define _RSTL_AUTO_PTR
|
2022-04-11 22:42:08 +00:00
|
|
|
|
|
|
|
#include "types.h"
|
|
|
|
|
|
|
|
namespace rstl {
|
|
|
|
template < typename T >
|
|
|
|
class auto_ptr {
|
2022-07-14 16:24:26 +00:00
|
|
|
mutable bool x0_has;
|
|
|
|
mutable T* x4_item;
|
2022-04-11 22:42:08 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
auto_ptr() : x0_has(false), x4_item(nullptr) {}
|
2022-07-14 16:24:26 +00:00
|
|
|
auto_ptr(T* ptr) : x0_has(ptr != nullptr), x4_item(ptr) {}
|
|
|
|
~auto_ptr() {
|
|
|
|
if (x0_has) {
|
|
|
|
delete x4_item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO check
|
2022-10-10 21:46:32 +00:00
|
|
|
auto_ptr(const auto_ptr& other) : x0_has(other.x0_has), x4_item(other.x4_item) {
|
|
|
|
other.x0_has = false;
|
|
|
|
}
|
2022-10-06 16:39:33 +00:00
|
|
|
auto_ptr& operator=(const auto_ptr& other) {
|
2022-10-11 15:28:32 +00:00
|
|
|
if (&other != this) {
|
|
|
|
if (x0_has) {
|
2022-10-06 16:39:33 +00:00
|
|
|
delete x4_item;
|
|
|
|
}
|
|
|
|
x0_has = other.x0_has;
|
|
|
|
x4_item = other.x4_item;
|
2022-10-10 21:46:32 +00:00
|
|
|
other.x0_has = false;
|
2022-10-06 16:39:33 +00:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
2022-08-16 02:14:28 +00:00
|
|
|
T* get() const { return x4_item; }
|
|
|
|
T* operator->() const { return x4_item; }
|
|
|
|
T& operator*() const { return *x4_item; }
|
2022-07-14 16:24:26 +00:00
|
|
|
T* release() const {
|
|
|
|
x0_has = false;
|
|
|
|
return x4_item;
|
|
|
|
}
|
2022-08-16 02:14:28 +00:00
|
|
|
bool null() const { return x4_item == nullptr; }
|
2022-04-11 22:42:08 +00:00
|
|
|
};
|
|
|
|
} // namespace rstl
|
|
|
|
|
2022-10-09 05:13:17 +00:00
|
|
|
#endif // _RSTL_AUTO_PTR
|