memgraph/include/utils/placeholder.hpp
Kruno Tomola Fabro b2ce3d58a4 Added cleaner.
Added multithreading to dbms.
Skiplist now supports emplace insert.
2016-08-30 00:45:07 +01:00

57 lines
998 B
C++

#pragma once
#include <ext/aligned_buffer.h>
#include <utility>
template <class T>
class Placeholder
{
public:
Placeholder() = default;
Placeholder(Placeholder &) = delete;
Placeholder(Placeholder &&) = delete;
~Placeholder()
{
if (initialized) get().~T();
};
bool is_initialized() { return initialized; }
T &get() noexcept
{
assert(initialized);
return *data._M_ptr();
}
const T &get() const noexcept
{
assert(initialized);
return *data._M_ptr();
}
void set(const T &item)
{
new (data._M_addr()) T(item);
initialized = true;
}
void set(T &&item)
{
new (data._M_addr()) T(std::move(item));
initialized = true;
}
template <class... Args>
void emplace(Args &&... args)
{
new (data._M_addr()) T(args...);
initialized = true;
}
private:
__gnu_cxx::__aligned_buffer<T> data;
bool initialized = false;
};