featureSet.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _FEATURESET_H_
  23. #define _FEATURESET_H_
  24. #ifndef _TORQUE_STRING_H_
  25. #include "core/util/str.h"
  26. #endif
  27. #ifndef _TVECTOR_H_
  28. #include "core/util/tVector.h"
  29. #endif
  30. class FeatureType;
  31. //
  32. class FeatureSet
  33. {
  34. protected:
  35. struct FeatureInfo
  36. {
  37. const FeatureType* type;
  38. S32 index;
  39. void* argStruct;
  40. };
  41. /// The list of featurs.
  42. Vector<FeatureInfo> mFeatures;
  43. /// A string representation of all the
  44. /// features used for comparisons.
  45. String mDescription;
  46. ///
  47. static S32 _typeCmp( const FeatureInfo* a, const FeatureInfo *b );
  48. ///
  49. void _rebuildDesc();
  50. public:
  51. FeatureSet()
  52. {
  53. }
  54. FeatureSet( const FeatureSet &h )
  55. : mFeatures( h.mFeatures ),
  56. mDescription( h.mDescription )
  57. {
  58. }
  59. FeatureSet& operator =( const FeatureSet &h );
  60. /// Equality operators.
  61. inline bool operator != ( const FeatureSet &h ) const { return h.getDescription() != getDescription(); }
  62. inline bool operator == ( const FeatureSet &h ) const { return h.getDescription() == getDescription(); }
  63. bool operator []( const FeatureType &type ) const { return hasFeature( type ); }
  64. /// Returns true if the feature set is empty.
  65. bool isEmpty() const { return mFeatures.empty(); }
  66. /// Returns true if the feature set is not empty.
  67. bool isNotEmpty() const { return !mFeatures.empty(); }
  68. /// Return the description string which uniquely identifies this feature set.
  69. const String& getDescription() const;
  70. /// Returns the feature count.
  71. U32 getCount() const { return mFeatures.size(); }
  72. /// Returns the feature at the index and optionally
  73. /// the feature index when it was added.
  74. const FeatureType& getAt( U32 index, S32 *outIndex = NULL ) const;
  75. void* getArguments(U32 index) const;
  76. /// Returns true if this handle has this feature.
  77. bool hasFeature( const FeatureType &type, S32 index = -1 ) const;
  78. ///
  79. void setFeature( const FeatureType &type, bool set, S32 index = -1 );
  80. /// <summary>
  81. /// Adds a feauter to the feature set.
  82. /// </summary>
  83. /// <param name="type">The shader feature type.</param>
  84. /// <param name="index">The inedx the shader feature will be sorted in the set.</param>
  85. /// <param name="argStruct">A struct representing arguments for a shader feature.</param>
  86. void addFeature( const FeatureType &type, S32 index = -1, void* argStruct = nullptr );
  87. ///
  88. void removeFeature( const FeatureType &type );
  89. ///
  90. S32 getNextFeatureIndex( const FeatureType &type, S32 index ) const;
  91. /// Removes features that are not in the input set.
  92. void filter( const FeatureSet &features );
  93. /// Removes features that are in the input set.
  94. void exclude( const FeatureSet &features );
  95. ///
  96. void merge( const FeatureSet &features );
  97. /// Clears all features.
  98. void clear();
  99. /// Default empty feature set.
  100. static const FeatureSet EmptySet;
  101. };
  102. inline const String& FeatureSet::getDescription() const
  103. {
  104. // Update the description if its empty and we have features.
  105. if ( mDescription.isEmpty() && !mFeatures.empty() )
  106. const_cast<FeatureSet*>(this)->_rebuildDesc();
  107. return mDescription;
  108. }
  109. #endif // _FEATURESET_H_