Proxy.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. ///
  2. /// \file Proxy/Proxy.hpp
  3. ///
  4. /// Definitions for the Proxy wrapper class.
  5. ///
  6. /// \copyright
  7. /// Copyright (c) 2013-2017 Josh Blum
  8. /// 2019 Nicholas Corgan
  9. /// SPDX-License-Identifier: BSL-1.0
  10. ///
  11. #pragma once
  12. #include <Pothos/Config.hpp>
  13. #include <Pothos/Object/Object.hpp>
  14. #include <memory>
  15. #include <string>
  16. namespace Pothos {
  17. class ProxyEnvironment;
  18. class ProxyHandle;
  19. /*!
  20. * The Proxy is a wrapper class for making calls in a ProxyEnvironment.
  21. * Proxys are created by the Environment and by using Proxy call().
  22. * The Proxy methods are simply just templated convenience methods
  23. * that take any argument type provided and handle the automatic conversions.
  24. */
  25. class POTHOS_API Proxy
  26. {
  27. public:
  28. /*!
  29. * Create a null Proxy.
  30. */
  31. Proxy(void);
  32. /*!
  33. * Create a Proxy from a handle.
  34. * This constructor will typically be called by the implementation.
  35. * \param handle a ProxyHandle shared pointer created by an environment
  36. */
  37. Proxy(const std::shared_ptr<ProxyHandle> &handle);
  38. /*!
  39. * Create a Proxy from a handle.
  40. * The Proxy is responsible for deletion of the pointer.
  41. * This constructor will typically be called by the implementation.
  42. * \param handle a ProxyHandle pointer created by an environment
  43. */
  44. Proxy(ProxyHandle *handle);
  45. /*!
  46. * Is this Proxy have a handle?
  47. * \return true if the handle is set.
  48. */
  49. explicit operator bool(void) const;
  50. //! Get the handle held in this proxy object.
  51. std::shared_ptr<ProxyHandle> getHandle(void) const;
  52. /*!
  53. * Get the Environment that created this Object's Handle.
  54. */
  55. std::shared_ptr<ProxyEnvironment> getEnvironment(void) const;
  56. /*!
  57. * Convert this proxy to the specified ValueType.
  58. * \throws ProxyEnvironmentConvertError if conversion failed
  59. * \return the Proxy's value as ValueType
  60. */
  61. template <typename ValueType>
  62. ValueType convert(void) const;
  63. /*!
  64. * Templated conversion operator to assign Proxy to a target type.
  65. * \throws ProxyEnvironmentConvertError if object cannot be converted
  66. */
  67. template <typename ValueType>
  68. operator ValueType(void) const;
  69. //! Call a method with a return type and variable args
  70. template <typename ReturnType, typename... ArgsType>
  71. ReturnType call(const std::string &name, ArgsType&&... args) const;
  72. //! Call a method with a Proxy return and variable args
  73. template <typename... ArgsType>
  74. Proxy call(const std::string &name, ArgsType&&... args) const;
  75. /*!
  76. * Call a method with a Proxy return and variable args
  77. * \deprecated use call overload without return type
  78. */
  79. template <typename... ArgsType>
  80. POTHOS_DEPRECATED("Replaced by call() overload without return type")
  81. Proxy callProxy(const std::string &name, ArgsType&&... args) const;
  82. /*!
  83. * Call a method with a void return and variable args
  84. * \deprecated use call overload without return type
  85. */
  86. template <typename... ArgsType>
  87. POTHOS_DEPRECATED("Replaced by call() overload without return type")
  88. void callVoid(const std::string &name, ArgsType&&... args) const;
  89. //! Call a field getter with specified return type
  90. template <typename ReturnType>
  91. ReturnType get(const std::string &name) const;
  92. //! Call a field getter with Proxy return type
  93. Proxy get(const std::string &name) const;
  94. //! Call a field setter
  95. template <typename ValueType>
  96. void set(const std::string &name, ValueType&& value) const;
  97. //! Call the function operator() with a Proxy return and variable args
  98. template <typename... ArgsType>
  99. Proxy operator()(ArgsType&&... args) const;
  100. /*!
  101. * Returns a negative integer, zero, or a positive integer as this object is
  102. * less than, equal to, or greater than the specified object.
  103. * \throws ProxyCompareError when the compare isn't possible
  104. * \param other the other proxy object to compare against
  105. * \return an int representing less than, equal to, or greater than
  106. */
  107. int compareTo(const Proxy &other) const;
  108. /*!
  109. * Get a hash code for the underlying object.
  110. * The hash code should be identical for equivalent objects.
  111. */
  112. size_t hashCode(void) const;
  113. /*!
  114. * Convert this proxy in this environment to a local object.
  115. * \throws ProxyEnvironmentConvertError if conversion failed
  116. * \return a new Object that contains something in local memory
  117. */
  118. Object toObject(void) const;
  119. /*!
  120. * Get the string representation of the Proxy.
  121. * The format of the string is highly specific,
  122. * depending upon the underlying object.
  123. */
  124. std::string toString(void) const;
  125. /*!
  126. * Get the class name of the underlying object.
  127. * The class name should be a unique identifier
  128. * for objects of the same type as the one contained.
  129. * This name is used to help convert proxies to local objects.
  130. */
  131. std::string getClassName(void) const;
  132. //! Comparable operator for stl containers
  133. bool operator<(const Proxy &obj) const;
  134. //! Comparable operator for stl containers
  135. bool operator>(const Proxy &obj) const;
  136. private:
  137. std::shared_ptr<ProxyHandle> _handle;
  138. };
  139. /*!
  140. * The equals operators checks if two Proxies represent the same memory.
  141. * Use myProxy.compareTo(other) == 0 for an equality comparison.
  142. * \param lhs the left hand object of the comparison
  143. * \param rhs the right hand object of the comparison
  144. * \return true if the objects represent the same internal data
  145. */
  146. POTHOS_API bool operator==(const Proxy &lhs, const Proxy &rhs);
  147. /*!
  148. * The not-equals operators checks if two Proxies represent different memory.
  149. * Use myProxy.compareTo(other) != 0 for an equality comparison.
  150. * \param lhs the left hand object of the comparison
  151. * \param rhs the right hand object of the comparison
  152. * \return true if the objects represent different internal data
  153. */
  154. inline bool operator!=(const Proxy &lhs, const Proxy &rhs)
  155. {
  156. return !(lhs == rhs);
  157. }
  158. } //namespace Pothos