[Glacier] Add a binary tree implementation.
Additionally add an optional class to return found values in the tree. And a reference container (Ref) similar to std::reference_wrapper to allow storing references in containers.
This commit is contained in:
parent
26b61db021
commit
98f029ae23
4 changed files with 234 additions and 0 deletions
41
lib/glacier/container/optional.h
Normal file
41
lib/glacier/container/optional.h
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
#pragma once
|
||||
|
||||
#include "glacier/memory/move.h"
|
||||
|
||||
namespace glcr {
|
||||
|
||||
template <typename T>
|
||||
class Optional {
|
||||
public:
|
||||
Optional() : empty_(nullptr), has_value_(false) {}
|
||||
Optional(const T& value) : value_(value), has_value_(true) {}
|
||||
Optional(T&& value) : value_(Move(value)), has_value_(true) {}
|
||||
Optional(const Optional&) = default;
|
||||
Optional(Optional&&) = default;
|
||||
~Optional() {
|
||||
if (has_value_) {
|
||||
value_.~T();
|
||||
}
|
||||
}
|
||||
|
||||
bool empty() const { return !has_value_; }
|
||||
explicit operator bool() { return has_value_; }
|
||||
|
||||
const T& value() const { return value_; }
|
||||
T&& release_value() {
|
||||
has_value_ = false;
|
||||
return Move(value_);
|
||||
}
|
||||
|
||||
T* operator->() { return &value_; }
|
||||
T& operator*() { return value_; }
|
||||
|
||||
private:
|
||||
union {
|
||||
T value_;
|
||||
void* empty_;
|
||||
};
|
||||
bool has_value_;
|
||||
};
|
||||
|
||||
} // namespace glcr
|
||||
Loading…
Add table
Add a link
Reference in a new issue