daeIDRef.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Copyright 2006 Sony Computer Entertainment Inc.
  3. *
  4. * Licensed under the SCEA Shared Source License, Version 1.0 (the "License"); you may not use this
  5. * file except in compliance with the License. You may obtain a copy of the License at:
  6. * http://research.scea.com/scea_shared_source_license.html
  7. *
  8. * Unless required by applicable law or agreed to in writing, software distributed under the License
  9. * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  10. * implied. See the License for the specific language governing permissions and limitations under the
  11. * License.
  12. */
  13. #ifndef __DAE_IDREF_H__
  14. #define __DAE_IDREF_H__
  15. #include <string>
  16. #include <dae/daeTypes.h>
  17. #include <dae/daeElement.h>
  18. class DAE;
  19. /**
  20. * The @c daeIDRef is a simple class designed to aid in the parsing and resolution of
  21. * ID references inside of COLLADA elements.
  22. * A @c daeIDRef is created for every IDREF data type in the COLLADA schema.
  23. * It also has the capability to attempt to resolve this reference
  24. * into a @c daeElement. If a @c daeIDRef is stored within a @c daeElement it fills
  25. * in its container field to point to the containing element.
  26. *
  27. * The main API is the @c daeIDRef::resolveElement() will use a @c daeIDRefResolver
  28. * to search for the @c daeElement inside of a @c daeDatabase.
  29. *
  30. */
  31. class DLLSPEC daeIDRef
  32. {
  33. public:
  34. /**
  35. * An enum describing the status of the ID resolution process.
  36. */
  37. enum ResolveState{
  38. /** No ID specified */
  39. id_empty,
  40. /** ID specified but not resolved */
  41. id_loaded,
  42. /** ID resolution pending */
  43. id_pending,
  44. /** ID resolved correctly */
  45. id_success,
  46. /** Resolution failed because ID was not found */
  47. id_failed_id_not_found,
  48. /** Resolution failed because ID was invalid */
  49. id_failed_invalid_id,
  50. /** Resoltion failed due to invalid reference */
  51. id_failed_invalid_reference,
  52. /** Resolution failed due to an external error */
  53. id_failed_externalization,
  54. /** Resolution failed because we don't have a document in which to search for the element.
  55. This means you probably forgot to set a container element. */
  56. id_failed_no_document
  57. };
  58. private:
  59. /** ID used to refer to another element */
  60. std::string id;
  61. /** Element that owns this ID (if any) */
  62. daeElement* container;
  63. public:
  64. /**
  65. * Simple Constructor
  66. */
  67. daeIDRef();
  68. /**
  69. * Constructs an id reference via a string, using @c setID(); loads the status.
  70. * @param id ID to construct a reference for, passed to @c setID() automatically.
  71. */
  72. daeIDRef(daeString id);
  73. /**
  74. * Constructs a new id reference by copying an existing one.
  75. * @param constructFromIDRef @c daeIDRef to copy into this one.
  76. */
  77. daeIDRef(const daeIDRef& constructFromIDRef);
  78. /**
  79. * Constructs an id reference with a container element
  80. * @param container The container element.
  81. */
  82. daeIDRef(daeElement& container);
  83. /**
  84. * Gets the ID string
  85. * @return Returns the full ID string from <tt><i>id.</i></tt>
  86. */
  87. daeString getID() const;
  88. /**
  89. * Copies <tt><i>ID</i></tt> into the <tt><i>id </i></tt> data member.
  90. * After the call to @c setID(), the <tt><i>state</i></tt> is set to @c id_loaded
  91. * @param ID String to use to configure this @c daeIDRef.
  92. */
  93. void setID(daeString ID);
  94. /**
  95. * Gets the element that this URI resolves to in memory.
  96. * @return Returns a ref to the element.
  97. */
  98. daeElement* getElement() const;
  99. /**
  100. * Gets a pointer to the @c daeElement that contains this URI.
  101. * @return Returns the pointer to the containing daeElmement.
  102. */
  103. daeElement* getContainer() const;
  104. /**
  105. * Sets the pointer to the @c daeElement that contains this URI.
  106. * @param cont Pointer to the containing @c daeElmement.
  107. */
  108. void setContainer(daeElement* cont);
  109. /**
  110. * Outputs all components of this @c daeIDRef to stderr.
  111. */
  112. void print();
  113. /**
  114. * Resets this @c daeIDRef; frees all string references
  115. * and returns <tt><i>state</i></tt> to @c empty.
  116. */
  117. void reset();
  118. /**
  119. * Initializes the @c daeIDREf, setting <tt><i>id, element,</i></tt> and <tt><i>container</i></tt> to NULL.
  120. */
  121. void initialize();
  122. /**
  123. * Comparison operator.
  124. * @return Returns true if URI's are equal.
  125. */
  126. bool operator==(const daeIDRef& other) const;
  127. /**
  128. * Assignment operator.
  129. * @return Returns a reference to this object.
  130. */
  131. daeIDRef &operator=( const daeIDRef& other);
  132. // These methods are only provided for backwards compatibility. Use the listed alternatives.
  133. daeIDRef &get( daeUInt idx ); // Never should have existed. No alternative.
  134. size_t getCount() const; // Never should have existed. No alternative.
  135. daeIDRef& operator[](size_t index); // Never should have existed. No alternative.
  136. void resolveElement( daeString typeNameHint = NULL ); // Call getElement. No separate "resolve" step needed.
  137. void resolveID(); // Never should have existed. No alternative.
  138. void validate(); // Never should have existed. No alternative.
  139. void copyFrom(const daeIDRef& from); // Use the assignment operator instead.
  140. ResolveState getState() const; // Never should have existed. No alternative.
  141. };
  142. /**
  143. * The @c daeIDRefResolver class is the plugin point for @c daeIDRef resolution.
  144. * This class is an abstract base class that defines an interface for
  145. * resolving @c daeIDRefs.
  146. */
  147. class DLLSPEC daeIDRefResolver
  148. {
  149. public:
  150. /**
  151. * Constructor
  152. */
  153. daeIDRefResolver(DAE& dae);
  154. /**
  155. * Destructor
  156. */
  157. virtual ~daeIDRefResolver();
  158. /**
  159. * Provides an abstract interface to convert a @c daeIDRef into a @c daeElement.
  160. * @param id The ID of the element to find.
  161. * @param doc The document containing the element.
  162. * @return Returns a daeElement with matching ID, if one is found.
  163. */
  164. virtual daeElement* resolveElement(const std::string& id, daeDocument* doc) = 0;
  165. /**
  166. * Gets the name of this resolver.
  167. * @return Returns the string name.
  168. */
  169. virtual daeString getName() = 0;
  170. protected:
  171. DAE* dae;
  172. };
  173. /**
  174. * The @c daeDefaultIDRefResolver resolves a @c daeIDRef by checking with a database.
  175. * It is a concrete implementation for @c daeIDRefResolver.
  176. */
  177. class DLLSPEC daeDefaultIDRefResolver : public daeIDRefResolver
  178. {
  179. public:
  180. daeDefaultIDRefResolver(DAE& dae);
  181. ~daeDefaultIDRefResolver();
  182. virtual daeElement* resolveElement(const std::string& id, daeDocument* doc);
  183. virtual daeString getName();
  184. };
  185. // This is a container class for storing a modifiable list of daeIDRefResolver objects.
  186. class DLLSPEC daeIDRefResolverList {
  187. public:
  188. daeIDRefResolverList();
  189. ~daeIDRefResolverList();
  190. void addResolver(daeIDRefResolver* resolver);
  191. void removeResolver(daeIDRefResolver* resolver);
  192. daeElement* resolveElement(const std::string& id, daeDocument* doc);
  193. private:
  194. // Disabled copy constructor/assignment operator
  195. daeIDRefResolverList(const daeIDRefResolverList& resolverList) { };
  196. daeIDRefResolverList& operator=(const daeIDRefResolverList& resolverList) { return *this; };
  197. daeTArray<daeIDRefResolver*> resolvers;
  198. };
  199. #endif //__DAE_IDREF_H__