abstractcredentials.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2019 Alexey Edelev <semlanik@gmail.com>
  5. *
  6. * This file is part of qtprotobuf project https://git.semlanik.org/semlanik/qtprotobuf
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of this
  9. * software and associated documentation files (the "Software"), to deal in the Software
  10. * without restriction, including without limitation the rights to use, copy, modify,
  11. * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
  12. * to permit persons to whom the Software is furnished to do so, subject to the following
  13. * conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all copies
  16. * or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  19. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  20. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  21. * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. #pragma once
  26. #include <QString>
  27. #include <QHash>
  28. #include <QVariant>
  29. #include <functional>
  30. #include "qtgrpc_global.h"
  31. namespace qtprotobuf {
  32. /**
  33. * \addtogroup QtGrpc
  34. * @{
  35. */
  36. class CallCredentials;
  37. class ChannelCredentials;
  38. /**
  39. * @brief The AbstractCredentials class
  40. */
  41. class QTGRPCSHARED_EXPORT AbstractCredentials
  42. {
  43. public:
  44. template<typename Call, typename Channel,
  45. typename std::enable_if_t<std::is_base_of<qtprotobuf::CallCredentials, Call>::value
  46. && std::is_base_of<qtprotobuf::ChannelCredentials, Channel>::value, int> = 0>
  47. AbstractCredentials(const Call &call, const Channel &channel);
  48. template<typename Call,
  49. typename std::enable_if_t<std::is_base_of<qtprotobuf::CallCredentials, Call>::value, int> = 0>
  50. AbstractCredentials(const Call &call);
  51. template<typename Channel,
  52. typename std::enable_if_t<std::is_base_of<qtprotobuf::ChannelCredentials, Channel>::value, int> = 0>
  53. AbstractCredentials(const Channel &channel);
  54. using CredentialMap = QHash<QLatin1String, QVariant>;
  55. CredentialMap callCredentials() {
  56. return mCallCredentials;
  57. }
  58. CredentialMap channelCredentials() {
  59. return mChannelCredentials;
  60. }
  61. protected:
  62. AbstractCredentials() = default;
  63. void setCallCredentials(const CredentialMap &credentialMap) { mCallCredentials = credentialMap; }
  64. void setChannelCredentials(const CredentialMap &credentialMap) { mChannelCredentials = credentialMap; }
  65. private:
  66. CredentialMap mCallCredentials;
  67. CredentialMap mChannelCredentials;
  68. };
  69. /**
  70. * @brief The CallCredentials class
  71. */
  72. class QTGRPCSHARED_EXPORT CallCredentials : public AbstractCredentials
  73. {
  74. protected:
  75. CallCredentials(const CredentialMap &credentialMap) {
  76. setCallCredentials(credentialMap);
  77. setChannelCredentials(CredentialMap{});
  78. }
  79. private:
  80. CallCredentials() = default;
  81. };
  82. /**
  83. * @brief The ChannelCredentials class
  84. */
  85. class QTGRPCSHARED_EXPORT ChannelCredentials : public AbstractCredentials
  86. {
  87. protected:
  88. ChannelCredentials(const CredentialMap &credentialMap) {
  89. setCallCredentials(CredentialMap{});
  90. setChannelCredentials(credentialMap);
  91. }
  92. };
  93. /** @} */
  94. //! @private
  95. template<typename Call, typename Channel,
  96. typename std::enable_if_t<std::is_base_of<qtprotobuf::CallCredentials, Call>::value
  97. && std::is_base_of<qtprotobuf::ChannelCredentials, Channel>::value, int> = 0>
  98. AbstractCredentials::AbstractCredentials(const Call &call, const Channel &channel)
  99. {
  100. mCallCredentials = call.mCallCredentials;
  101. mChannelCredentials = channel.mChannelCredentials;
  102. }
  103. //! @private
  104. template<typename Call,
  105. typename std::enable_if_t<std::is_base_of<qtprotobuf::CallCredentials, Call>::value, int> = 0>
  106. AbstractCredentials::AbstractCredentials(const Call &call)
  107. {
  108. mCallCredentials = call.mCallCredentials;
  109. }
  110. //! @private
  111. template<typename Channel,
  112. typename std::enable_if_t<std::is_base_of<qtprotobuf::ChannelCredentials, Channel>::value, int> = 0>
  113. AbstractCredentials::AbstractCredentials(const Channel &channel)
  114. {
  115. mChannelCredentials = channel.mChannelCredentials;
  116. }
  117. }
  118. //! @private
  119. template<typename Call, typename Channel,
  120. typename std::enable_if_t<std::is_base_of<qtprotobuf::CallCredentials, Call>::value
  121. && std::is_base_of<qtprotobuf::ChannelCredentials, Channel>::value, int> = 0>
  122. qtprotobuf::AbstractCredentials operator|(const Call &call, const Channel &channel)
  123. {
  124. return qtprotobuf::AbstractCredentials(call, channel);
  125. }
  126. //! @private
  127. template<typename Call, typename Channel,
  128. typename std::enable_if_t<std::is_base_of<qtprotobuf::CallCredentials, Call>::value
  129. && std::is_base_of<qtprotobuf::ChannelCredentials, Channel>::value, int> = 0>
  130. qtprotobuf::AbstractCredentials operator|(const Channel &channel, const Call &call)
  131. {
  132. return qtprotobuf::AbstractCredentials(call, channel);
  133. }