MSVCErrorWorkarounds.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===--- MSVCErrorWorkarounds.h - Enable future<Error> in MSVC --*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // MSVC's promise/future implementation requires types to be default
  15. // constructible, so this header provides analogues of Error an Expected
  16. // that are default constructed in a safely destructible state.
  17. //
  18. // FIXME: Kill off this header and migrate all users to Error/Expected once we
  19. // move to MSVC versions that support non-default-constructible types.
  20. //
  21. //===----------------------------------------------------------------------===//
  22. #ifndef LLVM_SUPPORT_MSVCERRORWORKAROUNDS_H
  23. #define LLVM_SUPPORT_MSVCERRORWORKAROUNDS_H
  24. #include "llvm/Support/Error.h"
  25. namespace llvm {
  26. // A default-constructible llvm::Error that is suitable for use with MSVC's
  27. // std::future implementation which requires default constructible types.
  28. class MSVCPError : public Error {
  29. public:
  30. MSVCPError() { (void)!!*this; }
  31. MSVCPError(MSVCPError &&Other) : Error(std::move(Other)) {}
  32. MSVCPError &operator=(MSVCPError Other) {
  33. Error::operator=(std::move(Other));
  34. return *this;
  35. }
  36. MSVCPError(Error Err) : Error(std::move(Err)) {}
  37. };
  38. // A default-constructible llvm::Expected that is suitable for use with MSVC's
  39. // std::future implementation, which requires default constructible types.
  40. template <typename T> class MSVCPExpected : public Expected<T> {
  41. public:
  42. MSVCPExpected()
  43. : Expected<T>(make_error<StringError>("", inconvertibleErrorCode())) {
  44. consumeError(this->takeError());
  45. }
  46. MSVCPExpected(MSVCPExpected &&Other) : Expected<T>(std::move(Other)) {}
  47. MSVCPExpected &operator=(MSVCPExpected &&Other) {
  48. Expected<T>::operator=(std::move(Other));
  49. return *this;
  50. }
  51. MSVCPExpected(Error Err) : Expected<T>(std::move(Err)) {}
  52. template <typename OtherT>
  53. MSVCPExpected(
  54. OtherT &&Val,
  55. std::enable_if_t<std::is_convertible<OtherT, T>::value> * = nullptr)
  56. : Expected<T>(std::move(Val)) {}
  57. template <class OtherT>
  58. MSVCPExpected(
  59. Expected<OtherT> &&Other,
  60. std::enable_if_t<std::is_convertible<OtherT, T>::value> * = nullptr)
  61. : Expected<T>(std::move(Other)) {}
  62. template <class OtherT>
  63. explicit MSVCPExpected(
  64. Expected<OtherT> &&Other,
  65. std::enable_if_t<!std::is_convertible<OtherT, T>::value> * = nullptr)
  66. : Expected<T>(std::move(Other)) {}
  67. };
  68. } // end namespace llvm
  69. #endif // LLVM_SUPPORT_MSVCERRORWORKAROUNDS_H
  70. #ifdef __GNUC__
  71. #pragma GCC diagnostic pop
  72. #endif