Class.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. ///
  2. /// \file Managed/Class.hpp
  3. ///
  4. /// Interface definition for a ManagedClass.
  5. ///
  6. /// \copyright
  7. /// Copyright (c) 2013-2016 Josh Blum
  8. /// SPDX-License-Identifier: BSL-1.0
  9. ///
  10. #pragma once
  11. #include <Pothos/Config.hpp>
  12. #include <Pothos/Callable/Callable.hpp>
  13. #include <string>
  14. #include <memory>
  15. #include <typeinfo>
  16. namespace Pothos {
  17. /*!
  18. * ManagedClass represents registered constructors and methods of a class.
  19. * A ManagedClass makes calls available to users via the Callable interface.
  20. */
  21. class POTHOS_API ManagedClass
  22. {
  23. public:
  24. /*!
  25. * Lookup a managed class based on typeinfo for the class.
  26. */
  27. static ManagedClass lookup(const std::type_info &type);
  28. //! Create a new empty ManagedClass
  29. ManagedClass(void);
  30. /*!
  31. * Register information and conversions about the ClassType.
  32. * This routine is automatically called during
  33. * constructor and method registration calls.
  34. */
  35. template <typename ClassType>
  36. ManagedClass &registerClass(void);
  37. /*!
  38. * Register a public base class of this managed class.
  39. * This adopts the inherited methods of the base class.
  40. */
  41. template <typename ClassType, typename BaseClassType>
  42. ManagedClass &registerBaseClass(void);
  43. /*!
  44. * Register a conversion function from this class to a base class.
  45. * \throws ManagedClassTypeError if the class type differs from the registered type
  46. * \param toBase a callable that converts between class types
  47. */
  48. ManagedClass &registerToBaseClass(const Callable &toBase);
  49. /*!
  50. * Register a constructor given class type and variable argument types.
  51. * \throws ManagedClassTypeError if the class type differs from the registered type
  52. */
  53. template <typename ClassType, typename... ArgsType>
  54. ManagedClass &registerConstructor(void);
  55. /*!
  56. * Register a static method given class name and function pointer of variable args.
  57. * When specifying overloads using the ArgsType, the entire pack must be specified.
  58. */
  59. template <typename ReturnType, typename... ArgsType>
  60. ManagedClass &registerStaticMethod(const std::string &name, ReturnType(*method)(ArgsType...));
  61. /*!
  62. * Register a method given class name and function pointer of variable args.
  63. * When specifying overloads using the ArgsType, the entire pack must be specified.
  64. * \throws ManagedClassTypeError if the class type differs from the registered type
  65. */
  66. template <typename ReturnType, typename ClassType, typename... ArgsType>
  67. ManagedClass &registerMethod(const std::string &name, ReturnType(ClassType::*method)(ArgsType...));
  68. /*!
  69. * Register a method given class name and function pointer of variable args.
  70. * When specifying overloads using the ArgsType, the entire pack must be specified.
  71. * \throws ManagedClassTypeError if the class type differs from the registered type
  72. */
  73. template <typename ReturnType, typename ClassType, typename... ArgsType>
  74. ManagedClass &registerMethod(const std::string &name, ReturnType(ClassType::*method)(ArgsType...) const);
  75. /*!
  76. * Register an accessor and mutator method for the field.
  77. * The accessor and mutator are registered under the names "get:name" and "set:name".
  78. * Example usage: myManagedClass.registerField(&MyClass:myField);
  79. * The accessor method can be called on a class instance and takes no arguments.
  80. * The mutator method can be called on a class instance and takes the new value as the first argument.
  81. * \throws ManagedClassTypeError if the class type differs from the registered type
  82. * \param name the name of the field
  83. * \param member a pointer to a data member of Class
  84. */
  85. template <typename ClassType, typename ValueType>
  86. ManagedClass &registerField(const std::string &name, ValueType ClassType::*member);
  87. /*!
  88. * Register a conversion function from reference to a ref wrapper.
  89. * Called automatically when a constructor is registered via the templated interface.
  90. * \throws ManagedClassTypeError if the class type differs from the registered type
  91. */
  92. ManagedClass &registerReferenceToWrapper(const Callable &toPointer);
  93. /*!
  94. * Register a conversion function from pointer to a ref wrapper.
  95. * Called automatically when a constructor is registered via the templated interface.
  96. * \throws ManagedClassTypeError if the class type differs from the registered type
  97. */
  98. ManagedClass &registerPointerToWrapper(const Callable &toPointer);
  99. /*!
  100. * Register a conversion function from shared pointer to ref wrapper.
  101. * Called automatically when a constructor is registered via the templated interface.
  102. * \throws ManagedClassTypeError if the class type differs from the registered type
  103. */
  104. ManagedClass &registerSharedToWrapper(const Callable &toPointer);
  105. /*!
  106. * Register a constructor via a callable.
  107. * This method is called by the templated method of the same name.
  108. * \throws ManagedClassTypeError if the class type differs from the registered type
  109. */
  110. ManagedClass &registerConstructor(const Callable &constructor);
  111. /*!
  112. * Register a static method via a callable.
  113. * This method is called by the templated method of the same name.
  114. */
  115. ManagedClass &registerStaticMethod(const std::string &name, const Callable &method);
  116. /*!
  117. * Register a method via a callable.
  118. * This method is called by the templated method of the same name.
  119. * \throws ManagedClassTypeError if the class type differs from the registered type
  120. */
  121. ManagedClass &registerMethod(const std::string &name, const Callable &method);
  122. /*!
  123. * Register a callable that takes generic arguments (Object *args, size_t num).
  124. * \throws ManagedClassTypeError if the class type differs from the registered type
  125. */
  126. ManagedClass &registerOpaqueConstructor(const Callable &constructor);
  127. /*!
  128. * Template version of registerOpaqueConstructor for user's convenience.
  129. */
  130. template <typename ClassType>
  131. ManagedClass &registerOpaqueConstructor(void);
  132. /*!
  133. * Register a static method with the following signature:
  134. * Object staticMethod(Object *args, size_t num).
  135. */
  136. ManagedClass &registerOpaqueStaticMethod(const std::string &name, const Callable &method);
  137. /*!
  138. * Register a static method with the following signature:
  139. * Object staticMethod(std::string name, Object *args, size_t num).
  140. */
  141. ManagedClass &registerWildcardStaticMethod(const Callable &method);
  142. /*!
  143. * Register a static method with the following signature:
  144. * Object method(instance, Object *args, size_t num).
  145. * \throws ManagedClassTypeError if the class type differs from the registered type
  146. */
  147. ManagedClass &registerOpaqueMethod(const std::string &name, const Callable &method);
  148. /*!
  149. * Register a static method with the following signature:
  150. * Object method(instance, std::string name, Object *args, size_t num).
  151. * \throws ManagedClassTypeError if the class type differs from the registered type
  152. */
  153. ManagedClass &registerWildcardMethod(const Callable &method);
  154. /*!
  155. * Commit this registration into the plugin tree.
  156. * The actual registration will be stored to /managed/classPath.
  157. * An example class path might be "MyNamespace/MyClass".
  158. * \throws PluginPathError if the classPath is invalid
  159. * \param classPath the namespaces and class name
  160. */
  161. ManagedClass &commit(const std::string &classPath);
  162. /*!
  163. * Unload a managed class from the plugin tree.
  164. * This reverses the effect of ManagedClass::commit().
  165. * This call is mainly used for testing purposes.
  166. * \throws PluginPathError if the classPath is invalid
  167. * \param classPath the namespaces and class name
  168. */
  169. static void unload(const std::string &classPath);
  170. /*!
  171. * Get the type of the class represented.
  172. * This is the type created by the constructors,
  173. * and the type taken as the class by the bound methods.
  174. * \throws ManagedClassTypeError if no constructors registered
  175. */
  176. const std::type_info &type(void) const;
  177. /*!
  178. * Get the pointer version of the class type represented.
  179. * \throws ManagedClassTypeError if no constructors registered
  180. */
  181. const std::type_info &pointerType(void) const;
  182. /*!
  183. * Get the shared pointer version of the class type represented.
  184. * \throws ManagedClassTypeError if no constructors registered
  185. */
  186. const std::type_info &sharedType(void) const;
  187. /*!
  188. * Get a callable that converts a reference into a ref wrapper.
  189. */
  190. const Callable &getReferenceToWrapper(void) const;
  191. /*!
  192. * Get a callable that converts a pointer into a ref wrapper.
  193. */
  194. const Callable &getPointerToWrapper(void) const;
  195. /*!
  196. * Get a callable that converts a shared pointer into a ref wrapper.
  197. */
  198. const Callable &getSharedToWrapper(void) const;
  199. /*!
  200. * Get a list of available converters to base classes
  201. * \return a list of converters as callables
  202. */
  203. const std::vector<Callable> &getBaseClassConverters(void) const;
  204. /*!
  205. * Get a list of available constructors.
  206. * \return a list of constructors as callables
  207. */
  208. const std::vector<Callable> &getConstructors(void) const;
  209. /*!
  210. * Get a list of available static methods for the given method name.
  211. * \throws ManagedClassNameError if the name does not exist in the registry
  212. * \param name the name of the static method to look for
  213. * \return a list of static methods as callables
  214. */
  215. const std::vector<Callable> &getStaticMethods(const std::string &name) const;
  216. /*!
  217. * Get a list of available methods for the given method name.
  218. * \throws ManagedClassNameError if the name does not exist in the registry
  219. * \param name the name of the method to look for
  220. * \return a list of methods as callables
  221. */
  222. const std::vector<Callable> &getMethods(const std::string &name) const;
  223. /*!
  224. * Get the opaque constructor.
  225. * The opaque constructor takes (Object *args, size_t num).
  226. * \return a callable object for a constructor
  227. */
  228. const Callable &getOpaqueConstructor(void) const;
  229. /*!
  230. * Get the opaque static methods for the given method name.
  231. * The opaque static method takes (Object *args, size_t num).
  232. * \throws ManagedClassNameError if the name does not exist in the registry
  233. * \param name the name of the static method to look for
  234. * \return a callable object for a static method
  235. */
  236. const Callable &getOpaqueStaticMethod(const std::string &name) const;
  237. /*!
  238. * Get the wildcard opaque static method.
  239. * This is a catch-all for static methods when there is not a name match.
  240. * The opaque wildcard static method takes (std::string name, Object *args, size_t num).
  241. * \return a callable object for a static method
  242. */
  243. const Callable &getWildcardStaticMethod(void) const;
  244. /*!
  245. * Get the opaque method for the given method name.
  246. * The opaque method takes (Object *args, size_t num).
  247. * \throws ManagedClassNameError if the name does not exist in the registry
  248. * \param name the name of the method to look for
  249. * \return a callable object for a method
  250. */
  251. const Callable &getOpaqueMethod(const std::string &name) const;
  252. /*!
  253. * Get the wildcard static method.
  254. * This is a catch-all for methods when there is not a name match.
  255. * The opaque wildcard method takes (std::string name, Object *args, size_t num).
  256. * \return a callable object for a method
  257. */
  258. const Callable &getWildcardMethod(void) const;
  259. private:
  260. struct Impl;
  261. std::shared_ptr<Impl> _impl;
  262. };
  263. } //namespace Pothos