1
1
mirror of https://github.com/mawww/kakoune.git synced 2024-12-24 03:56:32 +03:00

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 #define context_hh_INCLUDED
#include "selection.hh" #include "selection.hh"
#include "optional.hh"
#include <boost/optional.hpp>
namespace Kakoune namespace Kakoune
{ {
@ -34,7 +33,7 @@ public:
Context& operator=(const Context&) = delete; Context& operator=(const Context&) = delete;
Buffer& buffer() const; Buffer& buffer() const;
bool has_buffer() const { return m_selections; } bool has_buffer() const { return (bool)m_selections; }
Window& window() const; Window& window() const;
bool has_window() const { return (bool)m_window; } bool has_window() const { return (bool)m_window; }
@ -88,7 +87,7 @@ private:
safe_ptr<Client> m_client; safe_ptr<Client> m_client;
friend class Client; friend class Client;
boost::optional<SelectionList> m_selections; Optional<SelectionList> m_selections;
String m_name; 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 "selectors.hh"
#include "optional.hh"
#include "string.hh" #include "string.hh"
#include <algorithm> #include <algorithm>
#include <boost/optional.hpp>
namespace Kakoune namespace Kakoune
{ {
@ -74,9 +73,7 @@ Selection select_matching(const Buffer& buffer, const Selection& selection)
return selection; return selection;
} }
// c++14 will add std::optional, so we use boost::optional until then static Optional<Selection> find_surrounding(const Buffer& buffer,
using boost::optional;
static optional<Selection> find_surrounding(const Buffer& buffer,
ByteCoord coord, ByteCoord coord,
CodepointPair matching, CodepointPair matching,
ObjectFlags flags, int init_level) ObjectFlags flags, int init_level)
@ -103,7 +100,7 @@ static optional<Selection> find_surrounding(const Buffer& buffer,
--first; --first;
} }
if (level != 0 or *first != matching.first) if (level != 0 or *first != matching.first)
return optional<Selection>{}; return Optional<Selection>{};
} }
Utf8Iterator last = pos; Utf8Iterator last = pos;
@ -124,7 +121,7 @@ static optional<Selection> find_surrounding(const Buffer& buffer,
++last; ++last;
} }
if (level != 0 or last == buffer.end()) if (level != 0 or last == buffer.end())
return optional<Selection>{}; return Optional<Selection>{};
} }
if (flags & ObjectFlags::Inner) if (flags & ObjectFlags::Inner)