abstractcredentials.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 "qtgrpcglobal.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 Q_GRPC_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. mCallCredentials = call.mCallCredentials;
  49. mChannelCredentials = channel.mChannelCredentials;
  50. }
  51. template<typename Call,
  52. typename std::enable_if_t<std::is_base_of<QtProtobuf::CallCredentials, Call>::value, int> = 0>
  53. AbstractCredentials(const Call &call) {
  54. mCallCredentials = call.mCallCredentials;
  55. }
  56. template<typename Channel,
  57. typename std::enable_if_t<std::is_base_of<QtProtobuf::ChannelCredentials, Channel>::value, int> = 0>
  58. AbstractCredentials(const Channel &channel) {
  59. mChannelCredentials = channel.mChannelCredentials;
  60. }
  61. using CredentialMap = QHash<QLatin1String, QVariant>;
  62. CredentialMap callCredentials() {
  63. return mCallCredentials;
  64. }
  65. CredentialMap channelCredentials() {
  66. return mChannelCredentials;
  67. }
  68. protected:
  69. AbstractCredentials() = default;
  70. void setCallCredentials(const CredentialMap &credentialMap) { mCallCredentials = credentialMap; }
  71. void setChannelCredentials(const CredentialMap &credentialMap) { mChannelCredentials = credentialMap; }
  72. private:
  73. CredentialMap mCallCredentials;
  74. CredentialMap mChannelCredentials;
  75. };
  76. /*!
  77. * \brief The CallCredentials class
  78. */
  79. class Q_GRPC_EXPORT CallCredentials : public AbstractCredentials
  80. {
  81. protected:
  82. CallCredentials(const CredentialMap &credentialMap) {
  83. setCallCredentials(credentialMap);
  84. setChannelCredentials(CredentialMap{});
  85. }
  86. private:
  87. CallCredentials() = default;
  88. };
  89. /*!
  90. * \brief The ChannelCredentials class
  91. */
  92. class Q_GRPC_EXPORT ChannelCredentials : public AbstractCredentials
  93. {
  94. protected:
  95. ChannelCredentials(const CredentialMap &credentialMap) {
  96. setCallCredentials(CredentialMap{});
  97. setChannelCredentials(credentialMap);
  98. }
  99. };
  100. /*! \} */
  101. }
  102. //! \private
  103. template<typename Call, typename Channel,
  104. typename std::enable_if_t<std::is_base_of<QtProtobuf::CallCredentials, Call>::value
  105. && std::is_base_of<QtProtobuf::ChannelCredentials, Channel>::value, int> = 0>
  106. QtProtobuf::AbstractCredentials operator |(const Call &call, const Channel &channel)
  107. {
  108. return QtProtobuf::AbstractCredentials(call, channel);
  109. }
  110. //! \private
  111. template<typename Call, typename Channel,
  112. typename std::enable_if_t<std::is_base_of<QtProtobuf::CallCredentials, Call>::value
  113. && std::is_base_of<QtProtobuf::ChannelCredentials, Channel>::value, int> = 0>
  114. QtProtobuf::AbstractCredentials operator |(const Channel &channel, const Call &call)
  115. {
  116. return QtProtobuf::AbstractCredentials(call, channel);
  117. }