quick_status_code_from_enum.hpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /* Proposed SG14 status_code
  2. (C) 2018 - 2020 Niall Douglas <http://www.nedproductions.biz/> (5 commits)
  3. File Created: May 2020
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License in the accompanying file
  7. Licence.txt or at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. Distributed under the Boost Software License, Version 1.0.
  15. (See accompanying file Licence.txt or copy at
  16. http://www.boost.org/LICENSE_1_0.txt)
  17. */
  18. #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_QUICK_STATUS_CODE_FROM_ENUM_HPP
  19. #define BOOST_OUTCOME_SYSTEM_ERROR2_QUICK_STATUS_CODE_FROM_ENUM_HPP
  20. #ifndef BOOST_OUTCOME_SYSTEM_ERROR2_QUICK_STATUS_CODE_FROM_ENUM_ASSERT_ON_MISSING_MAPPING_TABLE_ENTRIES
  21. #define BOOST_OUTCOME_SYSTEM_ERROR2_QUICK_STATUS_CODE_FROM_ENUM_ASSERT_ON_MISSING_MAPPING_TABLE_ENTRIES 1
  22. #endif
  23. #include "generic_code.hpp"
  24. BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_BEGIN
  25. template <class Enum> class _quick_status_code_from_enum_domain;
  26. //! A status code wrapping `Enum` generated from `quick_status_code_from_enum`.
  27. template <class Enum> using quick_status_code_from_enum_code = status_code<_quick_status_code_from_enum_domain<Enum>>;
  28. //! Defaults for an implementation of `quick_status_code_from_enum<Enum>`
  29. template <class Enum> struct quick_status_code_from_enum_defaults
  30. {
  31. //! The type of the resulting code
  32. using code_type = quick_status_code_from_enum_code<Enum>;
  33. //! Used within `quick_status_code_from_enum` to define a mapping of enumeration value with its status code
  34. struct mapping
  35. {
  36. //! The enumeration type
  37. using enumeration_type = Enum;
  38. //! The value being mapped
  39. const Enum value;
  40. //! A string representation for this enumeration value
  41. const char *message;
  42. //! A list of `errc` equivalents for this enumeration value
  43. const std::initializer_list<errc> code_mappings;
  44. };
  45. //! Used within `quick_status_code_from_enum` to define mixins for the status code wrapping `Enum`
  46. template <class Base> struct mixin : Base
  47. {
  48. using Base::Base;
  49. };
  50. };
  51. /*! The implementation of the domain for status codes wrapping `Enum` generated from `quick_status_code_from_enum`.
  52. */
  53. template <class Enum> class _quick_status_code_from_enum_domain : public status_code_domain
  54. {
  55. template <class DomainType> friend class status_code;
  56. using _base = status_code_domain;
  57. using _src = quick_status_code_from_enum<Enum>;
  58. public:
  59. //! The value type of the quick status code from enum
  60. using value_type = Enum;
  61. using _base::string_ref;
  62. constexpr _quick_status_code_from_enum_domain()
  63. : status_code_domain(_src::domain_uuid, _uuid_size<detail::cstrlen(_src::domain_uuid)>())
  64. {
  65. }
  66. _quick_status_code_from_enum_domain(const _quick_status_code_from_enum_domain &) = default;
  67. _quick_status_code_from_enum_domain(_quick_status_code_from_enum_domain &&) = default;
  68. _quick_status_code_from_enum_domain &operator=(const _quick_status_code_from_enum_domain &) = default;
  69. _quick_status_code_from_enum_domain &operator=(_quick_status_code_from_enum_domain &&) = default;
  70. ~_quick_status_code_from_enum_domain() = default;
  71. #if __cplusplus < 201402L && !defined(_MSC_VER)
  72. static inline const _quick_status_code_from_enum_domain &get()
  73. {
  74. static _quick_status_code_from_enum_domain v;
  75. return v;
  76. }
  77. #else
  78. static inline constexpr const _quick_status_code_from_enum_domain &get();
  79. #endif
  80. virtual string_ref name() const noexcept override { return string_ref(_src::domain_name); }
  81. virtual payload_info_t payload_info() const noexcept override
  82. {
  83. return {sizeof(value_type), sizeof(status_code_domain *) + sizeof(value_type),
  84. (alignof(value_type) > alignof(status_code_domain *)) ? alignof(value_type) : alignof(status_code_domain *)};
  85. }
  86. protected:
  87. // Not sure if a hash table is worth it here, most enumerations won't be long enough to be worth it
  88. // Also, until C++ 20's consteval, the hash table would get emitted into the binary, bloating it
  89. static BOOST_OUTCOME_SYSTEM_ERROR2_CONSTEXPR14 const typename _src::mapping *_find_mapping(value_type v) noexcept
  90. {
  91. for(const auto &i : _src::value_mappings())
  92. {
  93. if(i.value == v)
  94. {
  95. return &i;
  96. }
  97. }
  98. return nullptr;
  99. }
  100. virtual bool _do_failure(const status_code<void> &code) const noexcept override
  101. {
  102. assert(code.domain() == *this); // NOLINT
  103. // If `errc::success` is in the generic code mapping, it is not a failure
  104. const auto *mapping = _find_mapping(static_cast<const quick_status_code_from_enum_code<value_type> &>(code).value());
  105. #if BOOST_OUTCOME_SYSTEM_ERROR2_QUICK_STATUS_CODE_FROM_ENUM_ASSERT_ON_MISSING_MAPPING_TABLE_ENTRIES
  106. assert(mapping != nullptr); // if this fires, you forgot to add the enum to the mapping table
  107. #endif
  108. if(mapping != nullptr)
  109. {
  110. for(errc ec : mapping->code_mappings)
  111. {
  112. if(ec == errc::success)
  113. {
  114. return false;
  115. }
  116. }
  117. }
  118. return true;
  119. }
  120. virtual bool _do_equivalent(const status_code<void> &code1, const status_code<void> &code2) const noexcept override
  121. {
  122. assert(code1.domain() == *this); // NOLINT
  123. const auto &c1 = static_cast<const quick_status_code_from_enum_code<value_type> &>(code1); // NOLINT
  124. if(code2.domain() == *this)
  125. {
  126. const auto &c2 = static_cast<const quick_status_code_from_enum_code<value_type> &>(code2); // NOLINT
  127. return c1.value() == c2.value();
  128. }
  129. if(code2.domain() == generic_code_domain)
  130. {
  131. const auto &c2 = static_cast<const generic_code &>(code2); // NOLINT
  132. const auto *mapping = _find_mapping(c1.value());
  133. #if BOOST_OUTCOME_SYSTEM_ERROR2_QUICK_STATUS_CODE_FROM_ENUM_ASSERT_ON_MISSING_MAPPING_TABLE_ENTRIES
  134. assert(mapping != nullptr); // if this fires, you forgot to add the enum to the mapping table
  135. #endif
  136. if(mapping != nullptr)
  137. {
  138. for(errc ec : mapping->code_mappings)
  139. {
  140. if(ec == c2.value())
  141. {
  142. return true;
  143. }
  144. }
  145. }
  146. }
  147. return false;
  148. }
  149. virtual generic_code _generic_code(const status_code<void> &code) const noexcept override
  150. {
  151. assert(code.domain() == *this); // NOLINT
  152. const auto *mapping = _find_mapping(static_cast<const quick_status_code_from_enum_code<value_type> &>(code).value());
  153. #if BOOST_OUTCOME_SYSTEM_ERROR2_QUICK_STATUS_CODE_FROM_ENUM_ASSERT_ON_MISSING_MAPPING_TABLE_ENTRIES
  154. assert(mapping != nullptr); // if this fires, you forgot to add the enum to the mapping table
  155. #endif
  156. if(mapping != nullptr)
  157. {
  158. if(mapping->code_mappings.size() > 0)
  159. {
  160. return *mapping->code_mappings.begin();
  161. }
  162. }
  163. return errc::unknown;
  164. }
  165. virtual string_ref _do_message(const status_code<void> &code) const noexcept override
  166. {
  167. assert(code.domain() == *this); // NOLINT
  168. const auto *mapping = _find_mapping(static_cast<const quick_status_code_from_enum_code<value_type> &>(code).value());
  169. #if BOOST_OUTCOME_SYSTEM_ERROR2_QUICK_STATUS_CODE_FROM_ENUM_ASSERT_ON_MISSING_MAPPING_TABLE_ENTRIES
  170. assert(mapping != nullptr); // if this fires, you forgot to add the enum to the mapping table
  171. #endif
  172. if(mapping != nullptr)
  173. {
  174. return string_ref(mapping->message);
  175. }
  176. return string_ref("unknown");
  177. }
  178. #if defined(_CPPUNWIND) || defined(__EXCEPTIONS) || defined(BOOST_OUTCOME_STANDARDESE_IS_IN_THE_HOUSE)
  179. BOOST_OUTCOME_SYSTEM_ERROR2_NORETURN virtual void _do_throw_exception(const status_code<void> &code) const override
  180. {
  181. assert(code.domain() == *this); // NOLINT
  182. const auto &c = static_cast<const quick_status_code_from_enum_code<value_type> &>(code); // NOLINT
  183. throw status_error<_quick_status_code_from_enum_domain>(c);
  184. }
  185. #endif
  186. };
  187. #if __cplusplus >= 201402L || defined(_MSC_VER)
  188. template <class Enum> constexpr _quick_status_code_from_enum_domain<Enum> quick_status_code_from_enum_domain = {};
  189. template <class Enum> inline constexpr const _quick_status_code_from_enum_domain<Enum> &_quick_status_code_from_enum_domain<Enum>::get()
  190. {
  191. return quick_status_code_from_enum_domain<Enum>;
  192. }
  193. #endif
  194. namespace mixins
  195. {
  196. template <class Base, class Enum>
  197. struct mixin<Base, _quick_status_code_from_enum_domain<Enum>> : public quick_status_code_from_enum<Enum>::template mixin<Base>
  198. {
  199. using quick_status_code_from_enum<Enum>::template mixin<Base>::mixin;
  200. };
  201. } // namespace mixins
  202. BOOST_OUTCOME_SYSTEM_ERROR2_NAMESPACE_END
  203. #endif