memgraph/include/utils/option.hpp
Kruno Tomola Fabro df0bf6fa5f Database interface refactor.
DbAccessor:
 -Guarantees that access to Vertex and Edge is possible only through
 Vertex::Accessor and Edge::Accessor.
 -Guarantees that changing Vertex and Edge is possible only using
 Vertex::Accessor returned by vertex_insert() method and
 Edge::Accessor returned by edge_insert() method.
 -Offers CRUD for Vertex and Edge except iterating over all edges.

Squashed commit messages:

First step in database accessor refactoring done.
It's compiling.
All tests with exception of integration_querys pass

Tests now initialize logging facilities.

Refactored accessors.
RecordAccessor now has 3 states.
From,To,Out,In in there respecive Accessors return unfilled RecordAccessor.
Added iterator classes into utils/itearator/.
2016-08-15 00:09:58 +01:00

96 lines
1.7 KiB
C++

#pragma once
#include <ext/aligned_buffer.h>
#include <utility>
template <class T>
class Option
{
public:
Option() {}
Option(T const &item)
{
new (data._M_addr()) T(item);
initialized = true;
}
Option(T &&item)
{
new (data._M_addr()) T(std::move(item));
initialized = true;
}
Option(Option &other) = default;
Option(Option &&other) noexcept
{
if (other.initialized) {
data = std::move(other.data);
other.initialized = false;
initialized = true;
}
}
~Option()
{
if (initialized) get().~T();
}
Option<T> &operator=(Option<T> &&other)
{
if (initialized) {
get().~T();
initialized = false;
}
if (other.initialized) {
data = std::move(other.data);
other.initialized = false;
initialized = true;
}
return *this;
}
bool is_present() const { return initialized; }
T &get() noexcept
{
assert(initialized);
return *data._M_ptr();
}
const T &get() const noexcept { assert(initialized); }
T take()
{
assert(initialized);
initialized = false;
return std::move(*data._M_ptr());
}
explicit operator bool() const { return initialized; }
private:
__gnu_cxx::__aligned_buffer<T> data;
bool initialized = false;
};
template <class T>
auto make_option()
{
return Option<T>();
}
template <class T>
auto make_option(T &&data)
{
return Option<T>(std::move(data));
}
template <class T>
auto make_option_const(const T &&data)
{
return Option<const T>(std::move(data));
}