From 855cbe5aff93e588df57707d010a1524eaceaf3d Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Thu, 11 May 2017 15:08:38 +0900 Subject: [PATCH] add operator= and ctor for result --- toml/parser.hpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/toml/parser.hpp b/toml/parser.hpp index c2a2893..c521479 100644 --- a/toml/parser.hpp +++ b/toml/parser.hpp @@ -27,13 +27,22 @@ struct result result& operator=(const T& rhs){ok_ = true; value_ = rhs; return *this;} result& operator=(T&& rhs) {ok_ = true; value_ = rhs; return *this;} + template + result& operator=(const result& u) {ok_ = u.ok(); if(ok_)value_ = u.move(); return *this;} + template + result& operator=(result&& u) {ok_ = u.ok(); if(ok_)value_ = u.move(); return *this;} + template + result(const result& u): ok_(u.ok()){if(ok_)value_ = u.get();} + template + result(result&& u): ok_(u.ok()){if(ok_)value_ = u.move();} + bool ok() const {return ok_;} operator bool() const {return ok_;} - T& get() {if(!ok_) throw std::logic_error("result"); return value_;} - T const& get() const {if(!ok_) throw std::logic_error("result"); return value_;} + T& get() {if(!ok_) throw std::logic_error("result::get"); return value_;} + T const& get() const {if(!ok_) throw std::logic_error("result::get"); return value_;} T&& move() - {if(!ok_) throw std::logic_error("result"); ok_ = false; return std::move(value_);} + {if(!ok_) throw std::logic_error("result::move"); ok_ = false; return std::move(value_);} private: bool ok_;