Handle.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /*!
  15. * @file Handle.h
  16. */
  17. #ifndef _FASTDDS_RTPS_SECURITY_COMMON_HANDLE_H_
  18. #define _FASTDDS_RTPS_SECURITY_COMMON_HANDLE_H_
  19. #include <memory>
  20. #include <string>
  21. namespace eprosima {
  22. namespace fastrtps {
  23. namespace rtps {
  24. namespace security {
  25. class Handle
  26. {
  27. public:
  28. const std::string& get_class_id() const
  29. {
  30. return class_id_;
  31. }
  32. virtual bool nil() const = 0;
  33. protected:
  34. Handle(const std::string& class_id) : class_id_(class_id) {};
  35. virtual ~Handle(){}
  36. private:
  37. std::string class_id_;
  38. };
  39. template<typename T>
  40. class HandleImpl : public Handle
  41. {
  42. public:
  43. typedef T type;
  44. HandleImpl() : Handle(T::class_id_), impl_(new T) {}
  45. virtual ~HandleImpl() = default;
  46. static HandleImpl<T>& narrow(Handle& handle)
  47. {
  48. if(handle.get_class_id().compare(T::class_id_) == 0)
  49. return reinterpret_cast<HandleImpl<T>&>(handle);
  50. return HandleImpl<T>::nil_handle;
  51. }
  52. static const HandleImpl<T>& narrow(const Handle& handle)
  53. {
  54. if(handle.get_class_id().compare(T::class_id_) == 0)
  55. return reinterpret_cast<const HandleImpl<T>&>(handle);
  56. return HandleImpl<T>::nil_handle;
  57. }
  58. bool nil() const override
  59. {
  60. return impl_ ? false : true;
  61. }
  62. T* operator*()
  63. {
  64. return impl_.get();
  65. }
  66. const T* operator*() const
  67. {
  68. return impl_.get();
  69. }
  70. T* operator->()
  71. {
  72. return impl_.get();
  73. }
  74. const T* operator->() const
  75. {
  76. return impl_.get();
  77. }
  78. static HandleImpl<T> nil_handle;
  79. private:
  80. explicit HandleImpl(bool) : Handle(T::class_id_) {}
  81. std::unique_ptr<T> impl_;
  82. };
  83. template<typename T>
  84. HandleImpl<T> HandleImpl<T>::nil_handle(true);
  85. class NilHandle : public Handle
  86. {
  87. public:
  88. NilHandle() : Handle("nil_handle") {}
  89. virtual ~NilHandle() = default;
  90. bool nil() const override { return true; }
  91. };
  92. // Define common handlers
  93. typedef Handle IdentityHandle;
  94. typedef Handle PermissionsHandle;
  95. typedef Handle ParticipantCryptoHandle;
  96. typedef Handle EntityCryptoHandle;
  97. typedef Handle DatawriterCryptoHandle;
  98. typedef Handle DatareaderCryptoHandle;
  99. } //namespace security
  100. } //namespace rtps
  101. } //namespace fastrtps
  102. } //namespace eprosima
  103. #endif // _FASTDDS_RTPS_SECURITY_COMMON_HANDLE_H_