You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

192 lines
6.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the dRowAudio JUCE module
  4. Copyright 2004-13 by dRowAudio.
  5. ------------------------------------------------------------------------------
  6. dRowAudio is provided under the terms of The MIT License (MIT):
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in all
  14. copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. SOFTWARE.
  22. ==============================================================================
  23. */
  24. #ifndef __DROWAUDIO_COMPARATORS_H__
  25. #define __DROWAUDIO_COMPARATORS_H__
  26. //==============================================================================
  27. namespace ValueTreeComparators
  28. {
  29. //==============================================================================
  30. /** A standard Lexiographc ValueTreeComparator. */
  31. class Lexicographic
  32. {
  33. public:
  34. Lexicographic (const Identifier attributeToSort_, bool forwards)
  35. : attributeToSort (attributeToSort_),
  36. direction (forwards ? 1 : -1)
  37. {
  38. }
  39. int compareElements (const ValueTree& first, const ValueTree& second) const
  40. {
  41. const int result = first[attributeToSort].toString().compareNatural (second[attributeToSort].toString());
  42. return direction * result;
  43. }
  44. private:
  45. const Identifier attributeToSort;
  46. const int direction;
  47. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Lexicographic)
  48. };
  49. //==============================================================================
  50. /** A standard Numberical ValueTreeComparator. */
  51. template<typename NumericalType>
  52. class Numerical
  53. {
  54. public:
  55. Numerical (const Identifier attributeToSort_, bool forwards)
  56. : attributeToSort (attributeToSort_),
  57. direction (forwards ? 1 : -1)
  58. {
  59. }
  60. int compareElements (const ValueTree& first, const ValueTree& second) const
  61. {
  62. const int result = (NumericalType (first[attributeToSort]) > NumericalType (second[attributeToSort])) ? 1 : -1;
  63. return direction * result;
  64. }
  65. private:
  66. const Identifier attributeToSort;
  67. const int direction;
  68. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Numerical)
  69. };
  70. //==============================================================================
  71. /** A Lexiographc ValueTreeComparator which will use a second comparitor if the
  72. result is the same, useful when sorting tables without using the much
  73. slower maintainSortOrder method.
  74. */
  75. class LexicographicWithBackup
  76. {
  77. public:
  78. LexicographicWithBackup (const Identifier attributeToSort_, const Identifier backupAttribute_, bool forwards)
  79. : attributeToSort (attributeToSort_),
  80. backupAttribute (backupAttribute_),
  81. direction (forwards ? 1 : -1)
  82. {
  83. }
  84. int compareElements (const ValueTree& first, const ValueTree& second) const
  85. {
  86. int result = first[attributeToSort].toString().compareNatural (second[attributeToSort].toString());
  87. if (result == 0)
  88. result = first[backupAttribute].toString().compareNatural (second[backupAttribute].toString());
  89. return direction * result;
  90. }
  91. private:
  92. const Identifier attributeToSort;
  93. const Identifier backupAttribute;
  94. const int direction;
  95. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (LexicographicWithBackup)
  96. };
  97. } //ValueTreeComparators
  98. namespace XmlComparators
  99. {
  100. //==============================================================================
  101. /** An Xml comparator used to sort Lexicographical data.
  102. */
  103. class LexicographicSorter
  104. {
  105. public:
  106. LexicographicSorter (const String attributeToSort_, bool forwards)
  107. : attributeToSort (attributeToSort_),
  108. direction (forwards ? 1 : -1)
  109. {
  110. }
  111. int compareElements (XmlElement* first, XmlElement* second) const
  112. {
  113. int result = first->getStringAttribute (attributeToSort)
  114. .compareNatural (second->getStringAttribute (attributeToSort));
  115. if (result == 0)
  116. result = first->getStringAttribute ("ID")
  117. .compareNatural (second->getStringAttribute ("ID"));
  118. return direction * result;
  119. }
  120. private:
  121. const String attributeToSort;
  122. const int direction;
  123. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (LexicographicSorter);
  124. };
  125. //==============================================================================
  126. /** An Xml comparator used to sort number data.
  127. */
  128. class NumberDataSorter
  129. {
  130. public:
  131. NumberDataSorter (const String attributeToSort_, bool forwards)
  132. : attributeToSort (attributeToSort_),
  133. direction (forwards ? 1 : -1)
  134. {
  135. }
  136. int compareElements (XmlElement* first, XmlElement* second) const
  137. {
  138. if (first->getStringAttribute (attributeToSort).isEmpty()
  139. || second->getStringAttribute (attributeToSort).isEmpty()) {
  140. return direction * -1;
  141. }
  142. int result = ((first->getStringAttribute (attributeToSort).getDoubleValue()
  143. <= (second->getStringAttribute (attributeToSort).getDoubleValue()))
  144. ? -1 : 1);
  145. return direction * result;
  146. }
  147. private:
  148. const String attributeToSort;
  149. const int direction;
  150. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NumberDataSorter);
  151. };
  152. } //XmlComparators
  153. #endif // __DROWAUDIO_COMPARATORS_H__