prime/include/rstl/auto_ptr.hpp

47 lines
1018 B
C++
Raw Normal View History

#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 {
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) {}
auto_ptr(T* ptr) : x0_has(ptr != nullptr), x4_item(ptr) {}
~auto_ptr() {
if (x0_has) {
delete x4_item;
}
}
// TODO check
auto_ptr(const auto_ptr& other) : x0_has(other.x0_has), x4_item(other.x4_item) {
other.x0_has = false;
}
auto_ptr& operator=(const auto_ptr& other) {
2022-10-11 15:28:32 +00:00
if (&other != this) {
if (x0_has) {
delete x4_item;
}
x0_has = other.x0_has;
x4_item = other.x4_item;
other.x0_has = false;
}
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; }
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
#endif // _RSTL_AUTO_PTR