optional: fix optional being not set

fixes #588
parent 4dc9e3ef
...@@ -106,6 +106,7 @@ public: ...@@ -106,6 +106,7 @@ public:
{ {
if (!other.isEmpty()) { if (!other.isEmpty()) {
::new (data()) T(*other.data()); ::new (data()) T(*other.data());
none_flag = ValueMarker;
} }
else { else {
none_flag = NoneMarker; none_flag = NoneMarker;
...@@ -153,6 +154,7 @@ public: ...@@ -153,6 +154,7 @@ public:
data()->~T(); data()->~T();
} }
::new (data()) T(*other.data()); ::new (data()) T(*other.data());
none_flag = ValueMarker;
} }
else { else {
if (none_flag != NoneMarker) { if (none_flag != NoneMarker) {
...@@ -237,33 +239,36 @@ private: ...@@ -237,33 +239,36 @@ private:
void move_helper(Optional<T> &&other, std::true_type) { void move_helper(Optional<T> &&other, std::true_type) {
::new (data()) T(std::move(*other.data())); ::new (data()) T(std::move(*other.data()));
none_flag = ValueMarker;
} }
void move_helper(Optional<T> &&other, std::false_type) { void move_helper(Optional<T> &&other, std::false_type) {
::new (data()) T(*other.data()); ::new (data()) T(*other.data());
none_flag = ValueMarker;
} }
template<typename U> template<typename U>
void from_some_helper(types::Some<U> some, std::true_type) { void from_some_helper(types::Some<U> some, std::true_type) {
::new (data()) T(std::move(some.val_)); ::new (data()) T(std::move(some.val_));
none_flag = ValueMarker;
} }
template<typename U> template<typename U>
void from_some_helper(types::Some<U> some, std::false_type) { void from_some_helper(types::Some<U> some, std::false_type) {
::new (data()) T(some.val_); ::new (data()) T(some.val_);
none_flag = ValueMarker;
} }
typedef uint8_t none_flag_t; typedef uint8_t none_flag_t;
static constexpr none_flag_t NoneMarker = 1; static constexpr none_flag_t NoneMarker = 1;
static constexpr none_flag_t ValueMarker = 0;
union {
uint8_t bytes[sizeof(T)]; uint8_t bytes[sizeof(T)];
none_flag_t none_flag; none_flag_t none_flag;
};
}; };
#define PistacheCheckSize(Type) \ #define PistacheCheckSize(Type) \
static_assert(sizeof(Optional<Type>) == sizeof(Type), "Size differs") static_assert(sizeof(Optional<Type>) == sizeof(Type) + sizeof(uint8_t), "Size differs")
PistacheCheckSize(uint8_t); PistacheCheckSize(uint8_t);
PistacheCheckSize(uint16_t); PistacheCheckSize(uint16_t);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment