#include "optional.h" Optional_Integer::Optional_Integer(int i) : data { new int {i} } {} Optional_Integer::Optional_Integer(Optional_Integer const & other) : Optional_Integer{} { if ( other.has_value() ) { data = new int{*other.data}; } } Optional_Integer::~Optional_Integer() { delete data; } void Optional_Integer::swap(Optional_Integer & other) { std::swap(data, other.data); } int & Optional_Integer::operator*() noexcept { return *data; } int & Optional_Integer::value() { if ( has_value() ) return *data; throw invalid_optional_access{"no value stored"}; } Optional_Integer & Optional_Integer::operator=(int val) { if ( has_value() ) *data = val; else data = new int{val}; return *this; } Optional_Integer & Optional_Integer::operator=(Optional_Integer const & rhs) { Optional_Integer{rhs}.swap(*this); return *this; } void Optional_Integer::reset() { delete data; data = nullptr; } bool Optional_Integer::has_value() const noexcept { return data != nullptr; }