abstractcredentials.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. namespace qtprotobuf {
  31. class CallCredentials;
  32. class ChannelCredentials;
  33. class AbstractCredentials
  34. {
  35. public:
  36. template<typename Call, typename Channel,
  37. typename std::enable_if_t<std::is_base_of<qtprotobuf::CallCredentials, Call>::value
  38. && std::is_base_of<qtprotobuf::ChannelCredentials, Channel>::value, int> = 0>
  39. AbstractCredentials(const Call &call, const Channel &channel);
  40. template<typename Call,
  41. typename std::enable_if_t<std::is_base_of<qtprotobuf::CallCredentials, Call>::value, int> = 0>
  42. AbstractCredentials(const Call &call);
  43. template<typename Channel,
  44. typename std::enable_if_t<std::is_base_of<qtprotobuf::ChannelCredentials, Channel>::value, int> = 0>
  45. AbstractCredentials(const Channel &channel);
  46. using CredentialMap = QHash<QLatin1String, QVariant>;
  47. CredentialMap callCredentials() {
  48. if (callCredentials_p) {
  49. return callCredentials_p();
  50. }
  51. return CredentialMap();
  52. }
  53. CredentialMap channelCredentials() {
  54. if (channelCredentials_p) {
  55. return channelCredentials_p();
  56. }
  57. return CredentialMap();
  58. }
  59. protected:
  60. AbstractCredentials() = default;
  61. using CredentialsFunction = std::function<CredentialMap(void)>;
  62. CredentialsFunction callCredentials_p;
  63. CredentialsFunction channelCredentials_p;
  64. };
  65. class CallCredentials : public AbstractCredentials
  66. {
  67. protected:
  68. CallCredentials() {
  69. channelCredentials_p = [](){ return CredentialMap(); };
  70. }
  71. private:
  72. using AbstractCredentials::channelCredentials_p;
  73. };
  74. class ChannelCredentials : public AbstractCredentials
  75. {
  76. protected:
  77. ChannelCredentials() {
  78. callCredentials_p = [](){ return CredentialMap(); };
  79. }
  80. private:
  81. using AbstractCredentials::callCredentials_p;
  82. };
  83. template<typename Call, typename Channel,
  84. typename std::enable_if_t<std::is_base_of<qtprotobuf::CallCredentials, Call>::value
  85. && std::is_base_of<qtprotobuf::ChannelCredentials, Channel>::value, int> = 0>
  86. AbstractCredentials::AbstractCredentials(const Call &call, const Channel &channel)
  87. {
  88. callCredentials_p = call.callCredentials_p;
  89. channelCredentials_p = channel.channelCredentials_p;
  90. }
  91. template<typename Call,
  92. typename std::enable_if_t<std::is_base_of<qtprotobuf::CallCredentials, Call>::value, int> = 0>
  93. AbstractCredentials::AbstractCredentials(const Call &call)
  94. {
  95. callCredentials_p = call.callCredentials_p;
  96. }
  97. template<typename Channel,
  98. typename std::enable_if_t<std::is_base_of<qtprotobuf::ChannelCredentials, Channel>::value, int> = 0>
  99. AbstractCredentials::AbstractCredentials(const Channel &channel)
  100. {
  101. channelCredentials_p = channel.channelCredentials_p;
  102. }
  103. }
  104. template<typename Call, typename Channel,
  105. typename std::enable_if_t<std::is_base_of<qtprotobuf::CallCredentials, Call>::value
  106. && std::is_base_of<qtprotobuf::ChannelCredentials, Channel>::value, int> = 0>
  107. qtprotobuf::AbstractCredentials operator|(const Call &call, const Channel &channel)
  108. {
  109. return qtprotobuf::AbstractCredentials(call, channel);
  110. }
  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. }