diff options
| author | Maxime Coste <frrrwww@gmail.com> | 2014-06-27 19:34:26 +0100 |
|---|---|---|
| committer | Maxime Coste <frrrwww@gmail.com> | 2014-06-27 21:10:09 +0100 |
| commit | df3bf7445dfd27389551e138333c1b42e92b30ab (patch) | |
| tree | a45f531659dc443e151df53c54df6ec610e98652 /src | |
| parent | 7aa78d726a54fa57a8dc5ed973ab0b30eeba6bf3 (diff) | |
Replace boost::optional with our own implementation
Diffstat (limited to 'src')
| -rw-r--r-- | src/context.hh | 7 | ||||
| -rw-r--r-- | src/optional.hh | 77 | ||||
| -rw-r--r-- | src/selectors.cc | 11 |
3 files changed, 84 insertions, 11 deletions
diff --git a/src/context.hh b/src/context.hh index 57780070..a02f11fd 100644 --- a/src/context.hh +++ b/src/context.hh @@ -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; diff --git a/src/optional.hh b/src/optional.hh new file mode 100644 index 00000000..61dc9618 --- /dev/null +++ b/src/optional.hh @@ -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 + diff --git a/src/selectors.cc b/src/selectors.cc index 838f1de1..b2b08073 100644 --- a/src/selectors.cc +++ b/src/selectors.cc @@ -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) |
