replace boost optional with mapbox optional

This commit is contained in:
Konstantin Käfer
2014-10-22 16:29:10 +02:00
parent 2422e938ac
commit 7d6feffd56
5 changed files with 85 additions and 35 deletions

View File

@@ -3,11 +3,10 @@
#include <mbgl/util/vec.hpp>
#include <mbgl/util/rect.hpp>
#include <mbgl/util/optional.hpp>
#include <array>
#include <vector>
#include <boost/optional.hpp>
namespace mbgl {
typedef vec2<float> CollisionPoint;
@@ -53,7 +52,7 @@ struct GlyphBox {
float minScale = 0.0f;
float maxScale = std::numeric_limits<float>::infinity();
float padding = 0.0f;
boost::optional<CollisionRect> hBox;
mapbox::util::optional<CollisionRect> hBox;
};
typedef std::vector<GlyphBox> GlyphBoxes;
@@ -88,7 +87,7 @@ typedef std::vector<PlacedGlyph> PlacedGlyphs;
struct PlacementBox {
CollisionAnchor anchor;
CollisionRect box;
boost::optional<CollisionRect> hBox;
mapbox::util::optional<CollisionRect> hBox;
PlacementRange placementRange = {{0.0f, 0.0f}};
float placementScale = 0.0f;
float maxScale = std::numeric_limits<float>::infinity();

View File

@@ -0,0 +1,69 @@
#ifndef MAPBOX_UTIL_OPTIONAL_HPP
#define MAPBOX_UTIL_OPTIONAL_HPP
#include <type_traits>
#include "variant.hpp"
namespace mapbox
{
namespace util
{
template <typename T> class optional
{
static_assert(!std::is_reference<T>::value, "optional doesn't support references");
struct none_type
{
};
variant<none_type, T> 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>(); }
T const &get() const { return variant_.template get<T>(); }
T &get() { return variant_.template get<T>(); }
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 <typename... Args> void emplace(Args &&... args)
{
variant_ = T{std::forward<Args>(args)...};
}
void reset() { variant_ = none_type{}; }
};
}
}
#endif

View File

@@ -4,7 +4,6 @@
#include <utility>
#include <typeinfo>
#include <type_traits>
#include <algorithm> // std::move/swap
#include <stdexcept> // runtime_error
#include <new> // operator new
#include <cstddef> // 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 <typename T, class = typename std::enable_if<
detail::is_valid_type<T, Types...>::value>::type>
VARIANT_INLINE explicit variant(T const& val) noexcept
: type_index(detail::value_traits<T, Types...>::index)
{
constexpr std::size_t index = sizeof...(Types) - detail::value_traits<T, Types...>::index - 1;
using target_type = typename detail::select_type<index, Types...>::type;
new (&data) target_type(val);
}
template <typename T, class = typename std::enable_if<
detail::is_valid_type<T, Types...>::value>::type>
detail::is_valid_type<typename std::remove_reference<T>::type, Types...>::value>::type>
VARIANT_INLINE variant(T && val) noexcept
: type_index(detail::value_traits<T, Types...>::index)
: type_index(detail::value_traits<typename std::remove_reference<T>::type, Types...>::index)
{
constexpr std::size_t index = sizeof...(Types) - detail::value_traits<T, Types...>::index - 1;
constexpr std::size_t index = sizeof...(Types) - detail::value_traits<typename std::remove_reference<T>::type, Types...>::index - 1;
using target_type = typename detail::select_type<index, Types...>::type;
new (&data) target_type(std::forward<T>(val)); // nothrow
}
@@ -565,7 +555,7 @@ public:
template <typename T>
VARIANT_INLINE variant<Types...>& operator=(T && rhs) noexcept
{
variant<Types...> temp(std::move(rhs));
variant<Types...> temp(std::forward<T>(rhs));
swap(*this, temp);
return *this;
}

View File

@@ -3,8 +3,7 @@
#include <mbgl/platform/log.hpp>
#include <mbgl/util/noncopyable.hpp>
#include <boost/optional.hpp>
#include <mbgl/util/optional.hpp>
#include <vector>
#include <cstdarg>
@@ -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<EventSeverity> severity;
const boost::optional<Event> event;
const boost::optional<int64_t> code;
const boost::optional<std::string> msg;
const mapbox::util::optional<EventSeverity> severity;
const mapbox::util::optional<Event> event;
const mapbox::util::optional<int64_t> code;
const mapbox::util::optional<std::string> msg;
mutable bool checked = false;
};

View File

@@ -4,13 +4,6 @@
#include <mbgl/style/value.hpp>
#include <mbgl/style/value_comparison.hpp>
#pragma GCC diagnostic push
#ifndef __clang__
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#endif
#include <boost/lexical_cast.hpp>
#pragma GCC diagnostic pop
using namespace mbgl;
TEST(Variant, toString) {