result.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* C interface for result
  2. (C) 2017-2024 Niall Douglas <http://www.nedproductions.biz/> (6 commits)
  3. File Created: Aug 2017
  4. Boost Software License - Version 1.0 - August 17th, 2003
  5. Permission is hereby granted, free of charge, to any person or organization
  6. obtaining a copy of the software and accompanying documentation covered by
  7. this license (the "Software") to use, reproduce, display, distribute,
  8. execute, and transmit the Software, and to prepare derivative works of the
  9. Software, and to permit third-parties to whom the Software is furnished to
  10. do so, all subject to the following:
  11. The copyright notices in the Software and this entire statement, including
  12. the above license grant, this restriction and the following disclaimer,
  13. must be included in all copies of the Software, in whole or in part, and
  14. all derivative works of the Software, unless such copies or derivative
  15. works are solely in the form of machine-executable object code generated by
  16. a source language processor.
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  20. SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  21. FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  22. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. DEALINGS IN THE SOFTWARE.
  24. */
  25. #ifndef BOOST_OUTCOME_EXPERIMENTAL_RESULT_H
  26. #define BOOST_OUTCOME_EXPERIMENTAL_RESULT_H
  27. #include <stdint.h> // for intptr_t
  28. #ifdef __cplusplus
  29. extern "C"
  30. {
  31. #endif
  32. #define BOOST_OUTCOME_C_DECLARE_RESULT(ident, R, S) \
  33. struct cxx_result_##ident \
  34. { \
  35. union \
  36. { \
  37. R value; \
  38. S error; \
  39. }; \
  40. unsigned flags; \
  41. }
  42. #define BOOST_OUTCOME_C_RESULT(ident) struct cxx_result_##ident
  43. #define BOOST_OUTCOME_C_RESULT_HAS_VALUE(r) (((r).flags & 1U) == 1U)
  44. #define BOOST_OUTCOME_C_RESULT_HAS_ERROR(r) (((r).flags & 2U) == 2U)
  45. #define BOOST_OUTCOME_C_RESULT_ERROR_IS_ERRNO(r) (((r).flags & (1U << 4U)) == (1U << 4U))
  46. /***************************** <system_error2> support ******************************/
  47. #define BOOST_OUTCOME_C_DECLARE_STATUS_CODE(ident, value_type) \
  48. struct cxx_status_code_##ident \
  49. { \
  50. void *domain; \
  51. value_type value; \
  52. };
  53. #define BOOST_OUTCOME_C_STATUS_CODE(ident) struct cxx_status_code_##ident
  54. #define BOOST_OUTCOME_C_DECLARE_RESULT_STATUS_CODE(ident, R, S) \
  55. struct cxx_result_status_code_##ident \
  56. { \
  57. R value; \
  58. unsigned flags; \
  59. S error; \
  60. }
  61. #define BOOST_OUTCOME_C_RESULT_STATUS_CODE(ident) struct cxx_result_status_code_##ident
  62. struct cxx_status_code_posix
  63. {
  64. void *domain;
  65. int value;
  66. };
  67. #define BOOST_OUTCOME_C_DECLARE_RESULT_ERRNO(ident, R) BOOST_OUTCOME_C_DECLARE_RESULT_STATUS_CODE(posix_##ident, R, struct cxx_status_code_posix)
  68. #define BOOST_OUTCOME_C_RESULT_ERRNO(ident) BOOST_OUTCOME_C_RESULT_STATUS_CODE(posix_##ident)
  69. struct cxx_status_code_system
  70. {
  71. void *domain;
  72. intptr_t value;
  73. };
  74. #define BOOST_OUTCOME_C_DECLARE_RESULT_SYSTEM(ident, R) BOOST_OUTCOME_C_DECLARE_RESULT_STATUS_CODE(system_##ident, R, struct cxx_status_code_system)
  75. #define BOOST_OUTCOME_C_RESULT_SYSTEM(ident) BOOST_OUTCOME_C_RESULT_STATUS_CODE(system_##ident)
  76. #ifdef __cplusplus
  77. }
  78. #endif
  79. #endif