TypeObjectFactory.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. // Copyright 2018 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. #ifndef TYPE_OBJECT_TYPE_FACTORY_H
  15. #define TYPE_OBJECT_TYPE_FACTORY_H
  16. #include <fastrtps/types/TypeObject.h>
  17. #include <fastrtps/types/DynamicTypeBuilder.h>
  18. #include <fastrtps/types/DynamicTypeBuilderPtr.h>
  19. #include <fastrtps/types/DynamicTypePtr.h>
  20. #include <mutex>
  21. namespace eprosima {
  22. namespace fastrtps {
  23. namespace types {
  24. class TypeObjectFactory
  25. {
  26. private:
  27. mutable std::recursive_mutex m_MutexIdentifiers;
  28. mutable std::recursive_mutex m_MutexObjects;
  29. mutable std::recursive_mutex m_MutexInformations;
  30. protected:
  31. TypeObjectFactory();
  32. mutable std::map<const std::string, const TypeIdentifier*> identifiers_; // Basic, builtin and EK_MINIMAL
  33. std::map<const std::string, const TypeIdentifier*> complete_identifiers_; // Only EK_COMPLETE
  34. std::map<const TypeIdentifier*, const TypeObject*> objects_; // EK_MINIMAL
  35. std::map<const TypeIdentifier*, const TypeObject*> complete_objects_; // EK_COMPLETE
  36. mutable std::vector<TypeIdentifier*> identifiers_created_;
  37. mutable std::map<const TypeIdentifier*, TypeInformation*> informations_;
  38. mutable std::vector<TypeInformation*> informations_created_;
  39. std::map<std::string, std::string> aliases_; // Aliases
  40. DynamicType_ptr build_dynamic_type(
  41. TypeDescriptor& descriptor,
  42. const TypeObject* object,
  43. const DynamicType_ptr annotation_member_type = DynamicType_ptr(nullptr)) const;
  44. const TypeIdentifier* try_get_complete(
  45. const TypeIdentifier* identifier) const;
  46. const TypeIdentifier* get_stored_type_identifier(
  47. const TypeIdentifier* identifier) const;
  48. std::string generate_name_and_store_type_identifier(
  49. const TypeIdentifier* identifier) const;
  50. void nullify_all_entries(
  51. const TypeIdentifier* identifier);
  52. void create_builtin_annotations();
  53. void apply_type_annotations(
  54. DynamicTypeBuilder_ptr& type_builder,
  55. const AppliedAnnotationSeq& annotations) const;
  56. void apply_member_annotations(
  57. DynamicTypeBuilder_ptr& parent_type_builder,
  58. MemberId member_id,
  59. const AppliedAnnotationSeq& annotations) const;
  60. std::string get_key_from_hash(
  61. const DynamicType_ptr annotation_descriptor_type,
  62. const NameHash& hash) const;
  63. /**
  64. * @brief Fills the TypeInformation provided with the minimal TypeIdentifiers and its minimal dependencies.
  65. * @param info
  66. * @param ident
  67. */
  68. void fill_minimal_information(
  69. TypeInformation* info,
  70. const TypeIdentifier* ident) const;
  71. /**
  72. * @brief Fills the TypeInformation provided with the complete TypeIdentifiers and its complete dependencies
  73. * if possible, or minimal ones in other case.
  74. * @param info
  75. * @param ident
  76. */
  77. void fill_complete_information(
  78. TypeInformation* info,
  79. const TypeIdentifier* ident) const;
  80. /**
  81. * @brief Auxiliar function to fill minimal dependencies.
  82. * @param info
  83. * @param identifier
  84. */
  85. void fill_minimal_dependant_types(
  86. TypeInformation* info,
  87. const TypeIdentifier* identifier) const;
  88. /**
  89. * @brief Auxiliar function to fill complete dependencies.
  90. * @param info
  91. * @param identifier
  92. */
  93. void fill_complete_dependant_types(
  94. TypeInformation* info,
  95. const TypeIdentifier* identifier) const;
  96. /**
  97. * @brief Auxiliar function to fill minimal information with complete dependencies.
  98. * @param info
  99. * @param identifier
  100. */
  101. void fill_complete_minimal_dependant_types(
  102. TypeInformation* info,
  103. const TypeIdentifier* identifier) const;
  104. public:
  105. RTPS_DllAPI static TypeObjectFactory* get_instance();
  106. RTPS_DllAPI static ReturnCode_t delete_instance();
  107. ~TypeObjectFactory();
  108. /**
  109. * @brief get_type_information Retrieves the TypeInformation of the named type.
  110. * @param type_name
  111. * @return
  112. */
  113. RTPS_DllAPI const TypeInformation* get_type_information(
  114. const std::string &type_name) const;
  115. /**
  116. * @brief get_type_information Retrieves the TypeInformation of the given TypeIdentifier.
  117. * @param identifier
  118. * @return
  119. */
  120. RTPS_DllAPI TypeInformation* get_type_information(
  121. const TypeIdentifier* identifier) const;
  122. RTPS_DllAPI const TypeObject* get_type_object(
  123. const std::string& type_name,
  124. bool complete = false) const;
  125. RTPS_DllAPI const TypeObject* get_type_object(
  126. const TypeIdentifier* identifier) const;
  127. RTPS_DllAPI TypeKind get_type_kind(
  128. const std::string& type_name) const;
  129. RTPS_DllAPI std::string get_type_name(
  130. const TypeKind kind) const;
  131. RTPS_DllAPI std::string get_type_name(
  132. const TypeIdentifier* identifier) const;
  133. RTPS_DllAPI const TypeIdentifier* get_primitive_type_identifier(
  134. TypeKind kind) const;
  135. RTPS_DllAPI const TypeIdentifier* get_type_identifier(
  136. const std::string& type_name,
  137. bool complete = false) const;
  138. RTPS_DllAPI const TypeIdentifier* get_type_identifier_trying_complete(
  139. const std::string& type_name) const;
  140. RTPS_DllAPI const TypeIdentifier* get_string_identifier(
  141. uint32_t bound,
  142. bool wide = false);
  143. RTPS_DllAPI const TypeIdentifier* get_sequence_identifier(
  144. const std::string& type_name,
  145. uint32_t bound,
  146. bool complete = false);
  147. RTPS_DllAPI const TypeIdentifier* get_array_identifier(
  148. const std::string& type_name,
  149. const std::vector<uint32_t> &bound,
  150. bool complete = false);
  151. RTPS_DllAPI const TypeIdentifier* get_map_identifier(
  152. const std::string& key_type_name,
  153. const std::string& value_type_name,
  154. uint32_t bound,
  155. bool complete = false);
  156. RTPS_DllAPI DynamicType_ptr build_dynamic_type(
  157. const std::string& name,
  158. const TypeIdentifier* identifier,
  159. const TypeObject* object = nullptr) const;
  160. RTPS_DllAPI bool is_type_identifier_complete(
  161. const TypeIdentifier* identifier) const;
  162. RTPS_DllAPI void add_type_identifier(
  163. const std::string& type_name,
  164. const TypeIdentifier* identifier);
  165. RTPS_DllAPI void add_type_object(
  166. const std::string& type_name,
  167. const TypeIdentifier* identifier,
  168. const TypeObject* object);
  169. RTPS_DllAPI inline void add_alias(
  170. const std::string& alias_name,
  171. const std::string& target_type)
  172. {
  173. std::unique_lock<std::recursive_mutex> scoped(m_MutexIdentifiers);
  174. aliases_.emplace(std::pair<std::string, std::string>(alias_name, target_type));
  175. }
  176. /**
  177. * @brief Returns a TypeIdentifierWithSizeSeq object filled with the dependencies of the
  178. * given identifiers. If continuation_point isn't empty, then it will skip the first
  179. * (max_size * continuation_point) dependencies.
  180. * @param identifiers
  181. * @param in_continuation_point
  182. * @param out_continuation_point
  183. * @param max_size
  184. * @return
  185. */
  186. RTPS_DllAPI TypeIdentifierWithSizeSeq typelookup_get_type_dependencies(
  187. const TypeIdentifierSeq& identifiers,
  188. const OctetSeq& in_continuation_point,
  189. OctetSeq& out_continuation_point,
  190. size_t max_size) const;
  191. /**
  192. * @brief Fills the given object with the complete version of the given identifier.
  193. * If the given identifier was MINIMAL, then it will return the stored COMPLETE identifier pointer.
  194. * Otherwise, it will return the given identifier address (to make comparision trivial).
  195. * @param identifier
  196. * @param object
  197. * @return
  198. */
  199. RTPS_DllAPI const TypeIdentifier* typelookup_get_type(
  200. const TypeIdentifier& identifier,
  201. TypeObject& object) const;
  202. /**
  203. * @brief Checks if a TypeIdentifier is already known by the factory.
  204. * @param identifier
  205. * @return
  206. */
  207. RTPS_DllAPI bool typelookup_check_type_identifier(
  208. const TypeIdentifier& identifier) const;
  209. /**
  210. * @brief Retrieves the CompleteTypeObject from the given TypeInformation.
  211. * If it doesn't exist, it returns nullptr.
  212. * A user that received a TypeInformation from TypeLookupService that calls this method and returns nullptr,
  213. * must iterate through the TypeInformation dependencies calling recursively to getTypeDependencies method in
  214. * its participant (which will call the correspondent method in the TypeLookupService), retrieving the
  215. * TypeObject correspondent and registering the type into the Factory with a name using the add_type_object method,
  216. * for each COMPLETE TypeIdentifier received in this way until all the hierarchy is registered, and then,
  217. * the user may call again this method that should return the TypeObject.
  218. * @param information
  219. * @return
  220. */
  221. RTPS_DllAPI const TypeObject* typelookup_get_type_object_from_information(
  222. const TypeInformation& information) const;
  223. };
  224. } // namespace types
  225. } // namespace fastrtps
  226. } // namespace eprosima
  227. #endif // TYPE_OBJECT_TYPE_FACTORY_H