diff --git a/include/mbgl/text/types.hpp b/include/mbgl/text/types.hpp index dbb483ea8..1d21ded2a 100644 --- a/include/mbgl/text/types.hpp +++ b/include/mbgl/text/types.hpp @@ -3,11 +3,10 @@ #include #include +#include #include #include -#include - namespace mbgl { typedef vec2 CollisionPoint; @@ -53,7 +52,7 @@ struct GlyphBox { float minScale = 0.0f; float maxScale = std::numeric_limits::infinity(); float padding = 0.0f; - boost::optional hBox; + mapbox::util::optional hBox; }; typedef std::vector GlyphBoxes; @@ -88,7 +87,7 @@ typedef std::vector PlacedGlyphs; struct PlacementBox { CollisionAnchor anchor; CollisionRect box; - boost::optional hBox; + mapbox::util::optional hBox; PlacementRange placementRange = {{0.0f, 0.0f}}; float placementScale = 0.0f; float maxScale = std::numeric_limits::infinity(); diff --git a/include/mbgl/util/optional.hpp b/include/mbgl/util/optional.hpp new file mode 100644 index 000000000..133e2c8f9 --- /dev/null +++ b/include/mbgl/util/optional.hpp @@ -0,0 +1,69 @@ +#ifndef MAPBOX_UTIL_OPTIONAL_HPP +#define MAPBOX_UTIL_OPTIONAL_HPP + +#include + +#include "variant.hpp" + +namespace mapbox +{ +namespace util +{ + +template class optional +{ + static_assert(!std::is_reference::value, "optional doesn't support references"); + + struct none_type + { + }; + + variant variant_; + + public: + optional() = default; + + optional(optional const &rhs) + { + if (this != &rhs) + { // protect against invalid self-assignment + variant_ = rhs.variant_; + } + } + + optional(T const &v) { variant_ = v; } + + explicit operator bool() const noexcept { return variant_.template is(); } + + T const &get() const { return variant_.template get(); } + T &get() { return variant_.template get(); } + + T const &operator*() const { return this->get(); } + T operator*() { return this->get(); } + + optional &operator=(T const &v) + { + variant_ = v; + return *this; + } + + optional &operator=(optional const &rhs) + { + if (this != &rhs) + { + variant_ = rhs.variant_; + } + return *this; + } + + template void emplace(Args &&... args) + { + variant_ = T{std::forward(args)...}; + } + + void reset() { variant_ = none_type{}; } +}; +} +} + +#endif diff --git a/include/mbgl/util/variant.hpp b/include/mbgl/util/variant.hpp index 1eca5160c..3b5659425 100644 --- a/include/mbgl/util/variant.hpp +++ b/include/mbgl/util/variant.hpp @@ -4,7 +4,6 @@ #include #include #include -#include // std::move/swap #include // runtime_error #include // operator new #include // size_t @@ -515,22 +514,13 @@ public: VARIANT_INLINE variant(no_init) : type_index(detail::invalid_value) {} + // http://isocpp.org/blog/2012/11/universal-references-in-c11-scott-meyers template ::value>::type> - VARIANT_INLINE explicit variant(T const& val) noexcept - : type_index(detail::value_traits::index) - { - constexpr std::size_t index = sizeof...(Types) - detail::value_traits::index - 1; - using target_type = typename detail::select_type::type; - new (&data) target_type(val); - } - - template ::value>::type> + detail::is_valid_type::type, Types...>::value>::type> VARIANT_INLINE variant(T && val) noexcept - : type_index(detail::value_traits::index) + : type_index(detail::value_traits::type, Types...>::index) { - constexpr std::size_t index = sizeof...(Types) - detail::value_traits::index - 1; + constexpr std::size_t index = sizeof...(Types) - detail::value_traits::type, Types...>::index - 1; using target_type = typename detail::select_type::type; new (&data) target_type(std::forward(val)); // nothrow } @@ -565,7 +555,7 @@ public: template VARIANT_INLINE variant& operator=(T && rhs) noexcept { - variant temp(std::move(rhs)); + variant temp(std::forward(rhs)); swap(*this, temp); return *this; } diff --git a/test/fixtures/fixture_log.hpp b/test/fixtures/fixture_log.hpp index 263871575..dc92f9319 100644 --- a/test/fixtures/fixture_log.hpp +++ b/test/fixtures/fixture_log.hpp @@ -3,8 +3,7 @@ #include #include - -#include +#include #include #include @@ -17,11 +16,11 @@ public: inline LogMessage(EventSeverity severity, Event event, int64_t code, const std::string &msg) : severity(severity), event(event), code(code), msg(msg) {} inline LogMessage(EventSeverity severity, Event event, int64_t code) - : severity(severity), event(event), code(code) {} + : severity(severity), event(event), code(code), msg() {} inline LogMessage(EventSeverity severity, Event event, const std::string &msg) - : severity(severity), event(event), msg(msg) {} + : severity(severity), event(event), code(), msg(msg) {} inline LogMessage(EventSeverity severity, Event event) - : severity(severity), event(event) {} + : severity(severity), event(event), code(), msg() {} inline bool operator==(const LogMessage &rhs) const { return (!severity || !rhs.severity || severity.get() == rhs.severity.get()) && @@ -30,10 +29,10 @@ public: (!msg || !rhs.msg || msg.get() == rhs.msg.get()); } - const boost::optional severity; - const boost::optional event; - const boost::optional code; - const boost::optional msg; + const mapbox::util::optional severity; + const mapbox::util::optional event; + const mapbox::util::optional code; + const mapbox::util::optional msg; mutable bool checked = false; }; diff --git a/test/variant.cpp b/test/variant.cpp index dd4578b49..4557465cf 100644 --- a/test/variant.cpp +++ b/test/variant.cpp @@ -4,13 +4,6 @@ #include #include -#pragma GCC diagnostic push -#ifndef __clang__ -#pragma GCC diagnostic ignored "-Wunused-local-typedefs" -#endif -#include -#pragma GCC diagnostic pop - using namespace mbgl; TEST(Variant, toString) {