2022-04-10 00:17:06 +00:00
|
|
|
#ifndef _RSTL_OPTIONAL_OBJECT_HPP
|
|
|
|
#define _RSTL_OPTIONAL_OBJECT_HPP
|
|
|
|
|
|
|
|
#include "types.h"
|
|
|
|
|
2022-08-09 23:03:51 +00:00
|
|
|
#include "rstl/construct.hpp"
|
2022-04-10 00:17:06 +00:00
|
|
|
|
|
|
|
namespace rstl {
|
|
|
|
template < typename T >
|
|
|
|
class optional_object {
|
|
|
|
public:
|
|
|
|
optional_object() : x4_valid(false) {}
|
|
|
|
optional_object(const T& item) : x0_item(item), x4_valid(true) {}
|
|
|
|
~optional_object() { clear(); }
|
|
|
|
|
2022-09-13 04:26:54 +00:00
|
|
|
optional_object& operator=(const T& item) {
|
|
|
|
assign(item);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-04-10 00:17:06 +00:00
|
|
|
T& data() { return x0_item; }
|
2022-09-13 04:26:54 +00:00
|
|
|
T* get_ptr() { return &x0_item; }
|
2022-04-10 00:17:06 +00:00
|
|
|
operator bool() const { return x4_valid; }
|
|
|
|
void clear() {
|
|
|
|
rstl::destroy(&x0_item);
|
|
|
|
x4_valid = false;
|
|
|
|
}
|
|
|
|
|
2022-09-13 04:26:54 +00:00
|
|
|
T& operator*() { return data(); }
|
|
|
|
|
2022-04-10 00:17:06 +00:00
|
|
|
private:
|
|
|
|
T x0_item;
|
|
|
|
bool x4_valid;
|
2022-09-13 04:26:54 +00:00
|
|
|
|
|
|
|
void assign(const T& item) {
|
|
|
|
if (!x4_valid) {
|
|
|
|
rstl::construct(get_ptr(), item);
|
|
|
|
x4_valid = true;
|
|
|
|
} else {
|
|
|
|
data() = item;
|
|
|
|
}
|
|
|
|
}
|
2022-04-10 00:17:06 +00:00
|
|
|
};
|
|
|
|
} // namespace rstl
|
|
|
|
|
|
|
|
#endif
|