Utility.Either+Error.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #pragma once
  2. #include <system_error>
  3. #include <memory>
  4. #include "Utility.Expected.tlh"
  5. #include "String.WString.hpp"
  6. #include "String.DString.hpp"
  7. #include "TmplBlockBuffer.tlh"
  8. #include "TmplBlockBuffer.tli"
  9. //-----------------------------------------------------------------------------
  10. //-----------------------------------------------------------------------------
  11. namespace ECOM::Utility
  12. {
  13. //-----------------------------------------------------------------------------
  14. // eiDWString
  15. //-----------------------------------------------------------------------------
  16. class eiDWString : public ECOM::Utility::Either <eSTR::DString, eSTR::WString> // 要么是 DString, 要么是 WString
  17. {
  18. using base = ECOM::Utility::Either <eSTR::DString, eSTR::WString>;
  19. public:
  20. eiDWString () : base { eSTR::DString () } { } // 默认是 DString
  21. eiDWString (eiDWString const & from) : base { from } {}
  22. eiDWString (eiDWString && from) : base { std::move (from) } {}
  23. eiDWString (const eSTR::DString & from) : base { from } { }
  24. eiDWString (const eSTR::WString & from) : base { from } { }
  25. eiDWString (eSTR::DString && from) : base { from } { }
  26. eiDWString (eSTR::WString && from) : base { from } { }
  27. ~eiDWString () = default;
  28. public:
  29. template <typename T> T As () const { static_assert (dependent_false <T>); }
  30. template <> eSTR::DString As () const // 必须返回 DString
  31. {
  32. if (this->is <0> ()) return this->get <0> ();
  33. if (this->is <1> ()) return this->get <1> ().ToDString ();
  34. return eSTR::DString ();
  35. }
  36. template <> eSTR::WString As () const // 必须返回 WString
  37. {
  38. if (this->is <0> ()) return eSTR::WString (this->get <0> ());
  39. if (this->is <1> ()) return this->get <1> ();
  40. return eSTR::WString ();
  41. }
  42. void To (eSTR::DString & _val)
  43. {
  44. _val = As <eSTR::DString> ();
  45. }
  46. void To (eSTR::WString & _val)
  47. {
  48. _val = As <eSTR::WString> ();
  49. }
  50. operator eSTR::DString ()
  51. {
  52. return As <eSTR::DString> ();
  53. }
  54. operator eSTR::WString ()
  55. {
  56. return As <eSTR::WString> ();
  57. }
  58. constexpr eiDWString & operator = (const eiDWString & from)
  59. {
  60. base::operator = (from);
  61. return (*this);
  62. }
  63. constexpr eiDWString & operator = (eiDWString && from)
  64. {
  65. base::operator = (std::move (from));
  66. return (*this);
  67. }
  68. constexpr eiDWString & operator = (base && from)
  69. {
  70. base::operator = (std::move (from));
  71. return (*this);
  72. }
  73. constexpr eiDWString & operator = (const eSTR::DString & _left)
  74. {
  75. base::operator = (_left);
  76. return (*this);
  77. }
  78. constexpr eiDWString & operator = (eSTR::DString && _left)
  79. {
  80. base::operator = (std::move (_left));
  81. return (*this);
  82. }
  83. constexpr eiDWString & operator = (const eSTR::WString & _right)
  84. {
  85. base::operator = (_right);
  86. return (*this);
  87. }
  88. constexpr eiDWString & operator = (eSTR::WString && _right)
  89. {
  90. base::operator = (std::move (_right));
  91. return (*this);
  92. }
  93. };
  94. //-----------------------------------------------------------------------------
  95. // iAck
  96. //-----------------------------------------------------------------------------
  97. class iAck
  98. {
  99. public:
  100. iAck () = default;
  101. iAck (iAck const & from) : IDofCodec {from.IDofCodec }, AckMessage { from.AckMessage } {}
  102. iAck (iAck && from) : IDofCodec{ from.IDofCodec }, AckMessage { std::move (from.AckMessage) } {}
  103. virtual ~iAck () = default;
  104. public:
  105. DWORD IDofCodec = 0;
  106. eiDWString AckMessage;
  107. public:
  108. template <typename T> T AckAs () const { return AckMessage.As <T> (); }
  109. void To (eSTR::DString & _val)
  110. {
  111. _val = AckAs <eSTR::DString> ();
  112. }
  113. void To (eSTR::WString & _val)
  114. {
  115. _val = AckAs <eSTR::WString> ();
  116. }
  117. operator eSTR::DString ()
  118. {
  119. return AckAs <eSTR::DString> ();
  120. }
  121. operator eSTR::WString ()
  122. {
  123. return AckAs <eSTR::WString> ();
  124. }
  125. constexpr iAck & operator = (const iAck & from)
  126. {
  127. IDofCodec = from.IDofCodec;
  128. AckMessage = from.AckMessage;
  129. return (*this);
  130. }
  131. constexpr iAck & operator = (iAck && from)
  132. {
  133. IDofCodec = from.IDofCodec;
  134. AckMessage = std::move (from.AckMessage);
  135. return (*this);
  136. }
  137. };
  138. class iBLOBAck : public iAck
  139. {
  140. using base = iAck;
  141. public:
  142. using base::base;
  143. OBlockBuffer AckBLOB;
  144. };
  145. }
  146. //-----------------------------------------------------------------------------
  147. //-----------------------------------------------------------------------------
  148. //using eiResult = ECOM::Utility::Either <int, ECOM::Utility::Error::ErrorCode>;
  149. //using eiAck = ECOM::Utility::Either <ECOM::Utility::iAck , ECOM::Utility::Error::ErrorCode>;
  150. //using eiBLOBAck = ECOM::Utility::Either <ECOM::Utility::iBLOBAck, ECOM::Utility::Error::ErrorCode>;
  151. using eiAck = ECOM::Utility::Expected <ECOM::Utility::iAck>;
  152. using eiBLOBAck = ECOM::Utility::Expected <ECOM::Utility::iBLOBAck>;
  153. #if 0
  154. //< 不能放在这里, 由各个 Domain 自行定义, 因为每个 Domain 都需要添加自己的 Error::Domain
  155. inline auto eiUnknownError () { return ECOM::Utility::Error::Generic::Make (ECOM::Utility::Error::Generic::enError::enUnknown); }
  156. inline auto eiBadArgument () { return ECOM::Utility::Error::Generic::Make (ECOM::Utility::Error::Generic::enError::invalid_argument); }
  157. inline auto eiTimeOut () { return ECOM::Utility::Error::Generic::Make (ECOM::Utility::Error::Generic::enError::timed_out); }
  158. inline auto eiNoFile () { return ECOM::Utility::Error::Generic::Make (ECOM::Utility::Error::Generic::enError::no_such_file_or_directory); }
  159. inline auto eiNoDevice () { return ECOM::Utility::Error::Generic::Make (ECOM::Utility::Error::Generic::enError::no_such_device); }
  160. inline auto eiBusy () { return ECOM::Utility::Error::Generic::Make (ECOM::Utility::Error::Generic::enError::device_or_resource_busy); }
  161. inline auto eiException () { return ECOM::Utility::Error::Generic::Make (ECOM::Utility::Error::Generic::enError::exception); }
  162. //>
  163. #endif
  164. //>