featureSet.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. };
  40. /// The list of featurs.
  41. Vector<FeatureInfo> mFeatures;
  42. /// A string representation of all the
  43. /// features used for comparisons.
  44. String mDescription;
  45. ///
  46. static S32 _typeCmp( const FeatureInfo* a, const FeatureInfo *b );
  47. ///
  48. void _rebuildDesc();
  49. public:
  50. FeatureSet()
  51. {
  52. }
  53. FeatureSet( const FeatureSet &h )
  54. : mFeatures( h.mFeatures ),
  55. mDescription( h.mDescription )
  56. {
  57. }
  58. FeatureSet& operator =( const FeatureSet &h );
  59. /// Equality operators.
  60. inline bool operator != ( const FeatureSet &h ) const { return h.getDescription() != getDescription(); }
  61. inline bool operator == ( const FeatureSet &h ) const { return h.getDescription() == getDescription(); }
  62. bool operator []( const FeatureType &type ) const { return hasFeature( type ); }
  63. /// Returns true if the feature set is empty.
  64. bool isEmpty() const { return mFeatures.empty(); }
  65. /// Returns true if the feature set is not empty.
  66. bool isNotEmpty() const { return !mFeatures.empty(); }
  67. /// Return the description string which uniquely identifies this feature set.
  68. const String& getDescription() const;
  69. /// Returns the feature count.
  70. U32 getCount() const { return mFeatures.size(); }
  71. /// Returns the feature at the index and optionally
  72. /// the feature index when it was added.
  73. const FeatureType& getAt( U32 index, S32 *outIndex = NULL ) const;
  74. /// Returns true if this handle has this feature.
  75. bool hasFeature( const FeatureType &type, S32 index = -1 ) const;
  76. ///
  77. void setFeature( const FeatureType &type, bool set, S32 index = -1 );
  78. ///
  79. void addFeature( const FeatureType &type, S32 index = -1 );
  80. ///
  81. void removeFeature( const FeatureType &type );
  82. ///
  83. S32 getNextFeatureIndex( const FeatureType &type, S32 index ) const;
  84. /// Removes features that are not in the input set.
  85. void filter( const FeatureSet &features );
  86. /// Removes features that are in the input set.
  87. void exclude( const FeatureSet &features );
  88. ///
  89. void merge( const FeatureSet &features );
  90. /// Clears all features.
  91. void clear();
  92. /// Default empty feature set.
  93. static const FeatureSet EmptySet;
  94. };
  95. inline const String& FeatureSet::getDescription() const
  96. {
  97. // Update the description if its empty and we have features.
  98. if ( mDescription.isEmpty() && !mFeatures.empty() )
  99. const_cast<FeatureSet*>(this)->_rebuildDesc();
  100. return mDescription;
  101. }
  102. #endif // _FEATURESET_H_