daeSIDResolver.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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_SIDRESOLVER_H__
  14. #define __DAE_SIDRESOLVER_H__
  15. #include <string>
  16. #include <map>
  17. #include <dae/daeTypes.h>
  18. #include <dae/daeElement.h>
  19. // This is an alternative to the daeSIDResolver class. It's recommended you use
  20. // this class instead. Typical usage: get the element a sid ref points to. For
  21. // example, if you want to find the element with sid 'sampler' using a
  22. // daeElement pointer named 'effect', that would look like this:
  23. // daeElement* elt = daeSidRef("sampler", effect).resolve().elt
  24. struct DLLSPEC daeSidRef {
  25. // A helper class for returning all the data retrieved when a sid is resolved.
  26. struct DLLSPEC resolveData {
  27. resolveData();
  28. resolveData(daeElement* elt, daeDoubleArray* array, daeDouble* scalar);
  29. daeElement* elt;
  30. daeDoubleArray* array;
  31. daeDouble* scalar;
  32. };
  33. daeSidRef();
  34. daeSidRef(const std::string& sidRef, daeElement* referenceElt, const std::string& profile = "");
  35. bool operator<(const daeSidRef& other) const;
  36. resolveData resolve();
  37. std::string sidRef;
  38. daeElement* refElt;
  39. std::string profile;
  40. };
  41. /**
  42. * The daeSIDResolver class is designed to resolve sid references within a COLLADA document.
  43. * The rules for sid resolution are set forth by the Addressing Syntax section in Chapter 3 of the
  44. * COLLADA specification which can be found at https://www.khronos.org/collada .
  45. * This resolver always attempts to resolve to the daeElement which is referenced. If the element contains
  46. * a daeDoubleArray (domFloatArray) value, the resolver will set the pointer to that array. The
  47. * resolver will also do this if the sid target points to a <source> element which has a <float_array> as
  48. * a child. If the sid target specifies a value, i.e. blah.X or blah(6), the resolver will attempt to
  49. * get a pointer to that specific value. The resolver only attempts to resolve to that level for values which
  50. * are defined in the COMMON profile glossary of the COLLADA specification, or values reference with the (#)
  51. * syntax. You can check the return value from getState() to see which level of resolution is possible.
  52. */
  53. class DLLSPEC daeSIDResolver
  54. {
  55. public:
  56. /**
  57. * An enum describing the status of the SID resolution process.
  58. */
  59. enum ResolveState{
  60. /** No target specified */
  61. target_empty,
  62. /** target specified but not resolved */
  63. target_loaded,
  64. /** Resolution failed because target was not found */
  65. sid_failed_not_found,
  66. /** Resolution successful to the Element level */
  67. sid_success_element,
  68. /** Resolution successful to the Double Array level */
  69. sid_success_array,
  70. /** Resolution successful to the Double level */
  71. sid_success_double
  72. };
  73. /**
  74. * Constructor.
  75. * @param container The element which contains the target that you want to resolve.
  76. * @param target The target string which needs to be resolved.
  77. * @param platform The platform name of the technique to use. A NULL value indicates the common platform.
  78. */
  79. daeSIDResolver( daeElement *container, daeString target, daeString platform = NULL );
  80. /**
  81. * Gets the target string.
  82. * @return Returns the target string of this SID resolver.
  83. */
  84. daeString getTarget() const;
  85. /**
  86. * Sets the target string.
  87. * @param t The new target string for this resolver.
  88. */
  89. void setTarget( daeString t );
  90. /**
  91. * Gets the name of the profile to use when resolving.
  92. * @return Returns the name of the profile or NULL for the common profile.
  93. */
  94. daeString getProfile() const;
  95. /**
  96. * Sets the profile to use when resolving.
  97. * @param p The profile name of the technique to use. A NULL value indicates the common profile.
  98. */
  99. void setProfile( daeString p );
  100. /**
  101. * Gets a pointer to the @c daeElement that contains the target to resolve.
  102. * @return Returns the pointer to the containing daeElmement.
  103. */
  104. daeElement* getContainer() const;
  105. /**
  106. * Sets the pointer to the @c daeElement that contains the target to resolve.
  107. * @param element Pointer to the containing @c daeElmement.
  108. */
  109. void setContainer(daeElement* element);
  110. /**
  111. * Gets the element that this SID resolves to.
  112. * @return Returns the element that the URI resolves to.
  113. */
  114. daeElement* getElement();
  115. /**
  116. * Gets the value array of the element that the SID resolves to.
  117. * @return Returns a pointer to the value array that the SID resolves to
  118. * @note The daeSIDResolver can only resolve to this level for daeDoubleArray values.
  119. */
  120. daeDoubleArray *getDoubleArray();
  121. /**
  122. * Gets a pointer to the particle this target resolved to.
  123. * @return Returns a pointer to a double value which is the fully resolved target.
  124. * @note The daeSIDResolver can only resolve to this level for domDouble values and only if the
  125. * final symbolic name is from the COMMON profile or a cardinal value is specified.
  126. * @note The daeSIDResolver assumes the value is a 4x4 matrix if there are 2 cardinal values specified.
  127. */
  128. daeDouble *getDouble();
  129. // This method is deprecated. Don't use it.
  130. ResolveState getState() const;
  131. private:
  132. // This data is provided by the user
  133. std::string target;
  134. std::string profile;
  135. daeElement* container;
  136. };
  137. // A class to make sid ref lookups faster. Meant for DOM internal use only.
  138. class DLLSPEC daeSidRefCache {
  139. public:
  140. daeSidRefCache();
  141. daeSidRef::resolveData lookup(const daeSidRef& sidRef);
  142. void add(const daeSidRef& sidRef, const daeSidRef::resolveData& data);
  143. void clear();
  144. // For debugging/testing
  145. bool empty();
  146. int misses();
  147. int hits();
  148. private:
  149. std::map<daeSidRef, daeSidRef::resolveData> lookupTable;
  150. int hitCount;
  151. int missCount;
  152. };
  153. #endif