#ifndef NEER_RESULT_H #define NEER_RESULT_H //#define Ok(v) (nerr::Result(v)) //#define Err(e) (nerr::Result(e)) namespace nerr { //template class Option; template class Result { public: Result(T obj) : m_result(obj) {}; Result(E err) { m_error.insert(err); }; Result(){}; inline Result& operator=(const T& value) {m_result = value; m_error = None(E); return *this;}; inline Result& operator=(const E& err) {m_error.insert(err); return *this;}; inline bool is_ok(){return m_error.is_none();}; inline bool is_err(){return m_error.is_some();}; inline T& unwrap() { if(is_ok()) { return m_result; } throw m_error.unwrap(); } inline T& expect(E e) { if(is_ok()) { return m_result; } throw e + m_error.unwrap(); } template inline Result map(const std::function& fn) { if(is_ok()) { return fn(m_result); } return m_error.unwrap(); } template inline Result map_err(const std::function& fn) { if(!is_ok()) { return fn(m_error.unwrap()); } return m_result; } template inline U map_or(U defv, const std::function& fn) { if(is_ok()) { return fn(m_result); } return defv; } Option err() { return m_error; } Option ok() { return is_ok()?Option(m_result): Option(); } private: T m_result; Option m_error; }; } #endif