项目作者: akitsu-sanae

项目描述 :
header-only C++ error handling library
高级语言: C++
项目地址: git://github.com/akitsu-sanae/keika.git
创建时间: 2017-03-24T12:19:49Z
项目社区:https://github.com/akitsu-sanae/keika

开源协议:Boost Software License 1.0

下载


Keika

Keika is the error-handling library for C++, inspired by Rust’s Result.

How to use

Keika is a header only library.
All you must do to install is #include "keika.hpp"

Examples

The simple example is here:

  1. using namespace keika;
  2. Result<double> divide(double a, double b) {
  3. if (b == 0.0)
  4. return Result<double>::error("division by zero!!");
  5. else
  6. return a / b;
  7. }
  8. int main() {
  9. std::cout << divide(1.0, 1.0) << std::endl; // "Result::ok(1)"
  10. std::cout << divide(1.0, 0.0) << std::endl; // "Result::error(division by zero!!)"
  11. }

You do not want to use std::string as error type? Ok

  1. using namespace keika;
  2. // your custom error type
  3. struct error_info {
  4. ...
  5. };
  6. Result<double, error_info> divide(double a, double b) {
  7. if (b == 0.0)
  8. return Result<double>::error(error_info{...});
  9. else
  10. return a / b;
  11. }

Keika provides some operation for Result:

  1. int main() {
  2. divide(1.0, 1.0) // Result::ok(1)
  3. | keika::map >> [](double d) { return std::to_string(d) + " is result!"; } // Result::ok(2 is result!)
  4. | keika::and_then >> [](std::string const& msg) {
  5. if (msg.size() > 10)
  6. return Result<int>::error("msg is too long!");
  7. else
  8. return Result<int>::ok(msg.size());
  9. } // Result::error(msg is too long)
  10. | keika::map_error >> [](std::string const& msg) {
  11. return msg + "nyan!!";
  12. } // Result::error(msg is too long!nyan!!)
  13. | keika::or_else >> [](std::string const& msg) {
  14. if (msg.size() > 10)
  15. return Result<int>::ok(msg.size());
  16. else
  17. return Result<int>::error("msg is too short!!");
  18. } // Result::ok(22)
  19. ;
  20. // and
  21. divide(1.0, 1.0) && divide(1.0, 0.0); // Result::error(division by zero)
  22. divide(1.0, 0.0) && divide(1.0, 1.0); // Result::error(division by zero)
  23. divide(1.0, 1.0) && divide(1.0, 2.0); // Result::ok(0.5)
  24. // or
  25. divide(1.0, 1.0) || divide(1.0, 0.0); // Result::ok(1)
  26. divide(1.0, 0.0) || divide(1.0, 2.0); // Result::ok(0.5)
  27. divide(1.0, 2.0) || divide(1.0, 1.0); // Result::ok(0.5)
  28. divide(1.0, 0.0) || divide(2.0, 0.0); // Result::error(division by zero)
  29. }

You can also use this pattern-match like operation:

  1. int main() {
  2. divide(1.0, 1.0).case_of(
  3. keika::ok >> [](double d) { return (int)d * 2; },
  4. keika::error >> [](std::string const& str) { return (int)str.size(); }
  5. ); // 2
  6. divide(1.0, 0.0).case_of(
  7. keika::ok >> [](double d) { return (int)d * 2; },
  8. keika::error >> [](std::string const& str) { return (int)str.size(); }
  9. ); // 16
  10. divide(1.0, 0.0).case_of(
  11. keika::ok >> [](double d) { return d * 2.0; },
  12. keika::error >> [](std::string const& str) { return (int)str.size(); }
  13. ); // assertion!! "keika::Result::case_of: return types are different!" (double and int)
  14. }

Copyright

Copyright (C) 2017 akitsu sanae.
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE or copy at http://www.boost/org/LICENSE_1_0.txt)