Skip to content

Commit

Permalink
reformat optional.hpp to be more legible and less linty
Browse files Browse the repository at this point in the history
  • Loading branch information
DennisOSRM committed Aug 29, 2014
1 parent 4cdd805 commit 54e1dfe
Showing 1 changed file with 50 additions and 46 deletions.
96 changes: 50 additions & 46 deletions optional.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,69 +5,73 @@

#include "variant.hpp"

namespace mapbox { namespace util {
namespace mapbox
{
namespace util
{

template<typename T>
class optional {
static_assert(!std::is_reference<T>::value, "optional doesn't support references");
template <typename T> class optional
{
static_assert(!std::is_reference<T>::value, "optional doesn't support references");

struct none_type { };
struct none_type
{
};

variant<none_type, T> variant_;
variant<none_type, T> variant_;

public:
optional() = default;
public:
optional() = default;

optional(optional const& rhs) {
if (this != &rhs) { // protect against invalid self-assignment
variant_ = rhs.variant_;
}
optional(optional const &rhs)
{
if (this != &rhs)
{ // protect against invalid self-assignment
variant_ = rhs.variant_;
}
}

optional(T const& v) {
variant_ = v;
}
optional(T const &v) { variant_ = v; }

optional& operator=(optional other) { // note: argument passed by value!
if (this != &other)
{
swap(other);
}
return *this;
optional &operator=(optional other)
{ // note: argument passed by value!
if (this != &other)
{
swap(other);
}
return *this;
}

explicit operator bool() const noexcept {
return variant_.template is<T>();
}
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 &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(); }
T const &operator*() const { return this->get(); }
T operator*() { return this->get(); }

optional& operator = ( T const& v ) {
variant_ = v;
return *this;
}
optional &operator=(T const &v)
{
variant_ = v;
return *this;
}

optional& operator = ( optional const& rhs ) {
if (this != &rhs)
{
variant_ = rhs.variant_;
}
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)...};
}
template <typename... Args> void emplace(Args &&... args)
{
variant_ = T{std::forward<Args>(args)...};
}

void reset() {
variant_ = none_type{};
}
};
void reset() { variant_ = none_type{}; }
};
}
}

Expand Down

0 comments on commit 54e1dfe

Please sign in to comment.