Line data Source code
1 : // Copyright (c) 2020-2021 The PIVX Core developers 2 : // Distributed under the MIT software license, see the accompanying 3 : // file COPYING or https://www.opensource.org/licenses/mit-license.php. 4 : 5 : #ifndef PIVX_OPERATIONRESULT_H 6 : #define PIVX_OPERATIONRESULT_H 7 : 8 : #include "optional.h" 9 : #include <string> 10 : 11 11899 : class OperationResult 12 : { 13 : private: 14 : bool m_res{false}; 15 : Optional<std::string> m_error{nullopt}; 16 : 17 : public: 18 303 : OperationResult(bool _res, const std::string& _error) : m_res(_res), m_error(_error) { } 19 12932 : OperationResult(bool _res) : m_res(_res) { } 20 : 21 307 : std::string getError() const { return (m_error ? *m_error : ""); } 22 : bool getRes() const { return m_res; } 23 13022 : explicit operator bool() const { return m_res; } 24 : }; 25 : 26 148 : inline OperationResult errorOut(const std::string& errorStr) 27 : { 28 148 : return OperationResult(false, errorStr); 29 : } 30 : 31 : 32 : template <class T> 33 1 : class CallResult : public OperationResult 34 : { 35 : private: 36 : Optional<T> m_obj_res{nullopt}; 37 : public: 38 : CallResult() : OperationResult(false) {} 39 2285 : explicit CallResult(T _obj) : OperationResult(true), m_obj_res(_obj) { } 40 3 : explicit CallResult(const std::string& error) : OperationResult(false, error) { } 41 2285 : const Optional<T>& getObjResult() const { return m_obj_res; } 42 : }; 43 : 44 : 45 : #endif // PIVX_OPERATIONRESULT_H