Replace boost::optional with our own implementation

This commit is contained in:
Maxime Coste 2014-06-27 19:34:26 +01:00
parent 7aa78d726a
commit df3bf7445d
3 changed files with 84 additions and 11 deletions

View File

@ -2,8 +2,7 @@
#define context_hh_INCLUDED
#include "selection.hh"
#include <boost/optional.hpp>
#include "optional.hh"
namespace Kakoune
{
@ -34,7 +33,7 @@ public:
Context& operator=(const Context&) = delete;
Buffer& buffer() const;
bool has_buffer() const { return m_selections; }
bool has_buffer() const { return (bool)m_selections; }
Window& window() const;
bool has_window() const { return (bool)m_window; }
@ -88,7 +87,7 @@ private:
safe_ptr<Client> m_client;
friend class Client;
boost::optional<SelectionList> m_selections;
Optional<SelectionList> m_selections;
String m_name;

77
src/optional.hh Normal file
View File

@ -0,0 +1,77 @@
#ifndef optional_hh_INCLUDED
#define optional_hh_INCLUDED
namespace Kakoune
{
template<typename T>
struct Optional
{
public:
constexpr Optional() : m_valid(false) {}
Optional(const T& other) : m_valid(true) { new (&m_value) T(other); }
Optional(T&& other) : m_valid(true) { new (&m_value) T(std::move(other)); }
Optional(const Optional& other)
: m_valid(other.m_valid)
{
if (m_valid)
new (&m_value) T(other.m_value);
}
Optional(Optional&& other)
: m_valid(other.m_valid)
{
if (m_valid)
new (&m_value) T(std::move(other.m_value));
}
Optional& operator=(const Optional& other)
{
if (m_valid)
m_value.~T();
if ((m_valid = other.m_valid))
new (&m_value) T(other.m_value);
return *this;
}
Optional& operator=(Optional&& other)
{
if (m_valid)
m_value.~T();
if ((m_valid = other.m_valid))
new (&m_value) T(std::move(other.m_value));
return *this;
}
~Optional()
{
if (m_valid)
m_value.~T();
}
constexpr explicit operator bool() const noexcept { return m_valid; }
T& operator*()
{
kak_assert(m_valid);
return m_value;
}
const T& operator*() const { return *const_cast<Optional&>(*this); }
T* operator->()
{
kak_assert(m_valid);
return &m_value;
}
const T* operator->() const { return const_cast<Optional&>(*this).operator->(); }
private:
bool m_valid;
union { T m_value; };
};
}
#endif // optional_hh_INCLUDED

View File

@ -1,11 +1,10 @@
#include "selectors.hh"
#include "optional.hh"
#include "string.hh"
#include <algorithm>
#include <boost/optional.hpp>
namespace Kakoune
{
@ -74,9 +73,7 @@ Selection select_matching(const Buffer& buffer, const Selection& selection)
return selection;
}
// c++14 will add std::optional, so we use boost::optional until then
using boost::optional;
static optional<Selection> find_surrounding(const Buffer& buffer,
static Optional<Selection> find_surrounding(const Buffer& buffer,
ByteCoord coord,
CodepointPair matching,
ObjectFlags flags, int init_level)
@ -103,7 +100,7 @@ static optional<Selection> find_surrounding(const Buffer& buffer,
--first;
}
if (level != 0 or *first != matching.first)
return optional<Selection>{};
return Optional<Selection>{};
}
Utf8Iterator last = pos;
@ -124,7 +121,7 @@ static optional<Selection> find_surrounding(const Buffer& buffer,
++last;
}
if (level != 0 or last == buffer.end())
return optional<Selection>{};
return Optional<Selection>{};
}
if (flags & ObjectFlags::Inner)