From f73aa9ca161285d860b164a8923729388ffd940f Mon Sep 17 00:00:00 2001 From: Evgeniy Alekseev Date: Thu, 26 Oct 2017 03:49:36 +0300 Subject: [PATCH] some improvements * add const modifiers to constructors * replace class to typename in templates * add Result::match function --- .README.md.kate-swp | Bin 0 -> 2488 bytes result.hpp | 50 +++++++++++++++++++++++++++++++++++++------- 2 files changed, 43 insertions(+), 7 deletions(-) create mode 100644 .README.md.kate-swp diff --git a/.README.md.kate-swp b/.README.md.kate-swp new file mode 100644 index 0000000000000000000000000000000000000000..f976aaad7de3b63d2abec696ffddb6116d7fa03d GIT binary patch literal 2488 zcmaKt=}r?-6o98L;EEfH;C4mDRsU|KIca2WU2*lDhGgFip9?19<{_r9E z;ZNghc)!y-xyMF5$xY{*v)^;)HVA^<_rf$XwGUz2+*xWwW}EL!aOO8u+?$ z?%SgIvcK>9&$~bF*PdFtW;VH^9rDH^QOkJ~;K< z57#|!f)_n+hE@D6@H?;H3U@qjgPWeW!z%s&tn%-Gm%RN>xaD~ltl~+3vtBR#MV_U< z#Iy9L>XZH&UN8NrdZfRS*GqqjrJlE5FZEbvn9T1W!vr4M6=air1X#&UcRgX5tg8f; z$+}8lnXKy!mQ1ntm(JbJpqI|wq+#h?#goq6PNSF3SA0C_T;-F_RedtqRc|kobti*( zGFhdU$+}6Rm&v+Gz`N|qP8Pfyb|(ei1FL=pVb$+mSmoabtN25(I|;rDtY?<}n)V;w~aU(WbcI-)Z09N(~VfWdhKLodZ`opl=j3clbj)B#% zkHRYcD6Hx^2D{Ige8*w6c_(1C*<-L8&PiD1%fsq$6<{^|Q?QD68dmj;!z$hxSos%w z?9amUet)=Swf*@X`w3WmJQrZKKNn$jST4b8KQ6;+zpucm-m9?c=Nhc$S%TI4uETae zv*QGpeLJF7;mgjvYJVBm4cOh%@FZ++sVp2k<->`3-18LXs@j{d+Kp+r=zagpgVWi%O&W(3s^2e%VdW8vSf1> zkSrOjlfhZCe>zX5(Q>;H+JE$Ny%8pfnVU4TQPOForP55RnMTdjwBw~_`X>4qTFWS3 z?W)VxFfI_6Jd=#=v0X^+6R)8hubtVVjMN+8`p`$nj+{kyATPu zrmCn`;@>pDsk|bP+@&&5(0)){}; + Error(const std::string message) + : Error(message, ErrorEnum()){}; /** * @brief Error class destructor */ @@ -110,7 +110,7 @@ private: * @tparam ErrorEnum * error code enumeration */ -template +template class Result : public std::variant> { public: @@ -123,14 +123,14 @@ public: * @param value * result value */ - Result(T value) + Result(const T value) : std::variant>(value){}; /** * @brief Result constructor with error * @param error * result error */ - Result(Error error) + Result(const Error error) : std::variant>(error){}; /** @@ -150,6 +150,7 @@ public: }; /** * @brief get result content + * @return result object content type */ Content type() const { @@ -164,6 +165,41 @@ public: }; }; }; + +// additional functions +/** + * @brief match result function + * @tparam T + * value class name + * @tparam ErrorEnum + * error code enumeration + * @tparam UnaryFunctionValue + * function type which will be called if result contains value + * @tparam UnaryFunctionError + * function type which will be called if result contains error + * @param result + * result object + * @param apply_value + * function which will be called if result contains value + * @param apply_error + * function which will be called if result contains error + */ +template +void match(const Result &result, UnaryFunctionValue apply_value, + UnaryFunctionError apply_error) +{ + switch (result.type()) { + case Content::Value: + apply_value(result.get()); + break; + case Content::Error: + apply_error(result.error()); + break; + case Content::Empty: + break; + } +}; }; #endif /* _RESULT_HPP_ */