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.

175 lines
5.6KB

  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_DEBUGOBJECT_H__
  25. #define __DROWAUDIO_DEBUGOBJECT_H__
  26. #if JUCE_MSVC
  27. #pragma warning (disable: 4505)
  28. #endif
  29. //==============================================================================
  30. /**
  31. Useful class to convert some objects to a String representation.
  32. You shouldn't use one of these directly, instead use the DBG_OBJ macro to
  33. ensure you don't hang on for one longer than you should do.
  34. If you do want to e.g. for saving to a file etc. never hang on for long to
  35. avoid deleting any variables it may be referencing. Simply create one on the
  36. stack and immidiately use its toString() method.
  37. Alternatively just use one of the statis convertToString methods to convert
  38. almost any common JUCE object to a string.
  39. @example
  40. @code
  41. ValueTree myValueTree ("my_tree_type");
  42. String output (DebugObject (myValueTree).toString());
  43. @endcode
  44. */
  45. class DebugObject
  46. {
  47. public:
  48. //==============================================================================
  49. static String convertToString (const var& arg)
  50. {
  51. return arg.toString();
  52. }
  53. template <typename ValueType>
  54. static String convertToString (const Range<ValueType>& arg)
  55. {
  56. String os;
  57. return os << arg.getStart() << " - " << arg.getEnd() << " (" << arg.getLength() << ")";
  58. }
  59. template <typename ValueType>
  60. static String convertToString (const Point<ValueType>& arg)
  61. {
  62. return arg.toString();
  63. }
  64. template <typename ValueType>
  65. static String convertToString (const Line<ValueType>& arg)
  66. {
  67. String os;
  68. return os << "(" << arg.getStart().toString()
  69. << ") - (" << arg.getEnd().toString() << ")";
  70. }
  71. template <typename ValueType>
  72. static String convertToString (const Rectangle<ValueType>& arg)
  73. {
  74. return arg.toString();
  75. }
  76. static String convertToString (const XmlElement* arg)
  77. {
  78. return DebugObject (arg).toString();
  79. }
  80. static String convertToString (const XmlElement& arg)
  81. {
  82. return DebugObject (arg).toString();
  83. }
  84. static String convertToString (const ValueTree& arg)
  85. {
  86. return DebugObject (arg).toString();
  87. }
  88. static String convertToString (const StringArray& arg)
  89. {
  90. return arg.joinIntoString (",");
  91. }
  92. static String convertToString (const StringPairArray& arg)
  93. {
  94. return arg.getDescription();
  95. }
  96. //==============================================================================
  97. enum ObjectType
  98. {
  99. xmlType,
  100. valueTreeType
  101. };
  102. //==============================================================================
  103. explicit DebugObject (const XmlElement* arg)
  104. : type (xmlType), objectXml (arg) {}
  105. explicit DebugObject (const XmlElement& arg)
  106. : type (xmlType), objectXml (&arg) {}
  107. explicit DebugObject (const ValueTree arg)
  108. : type (valueTreeType), objectValueTree (arg) {}
  109. String toString() const
  110. {
  111. switch (type)
  112. {
  113. case xmlType: return getStringFromXml (objectXml, true);
  114. case valueTreeType: return getStringFromValueTree();
  115. default: return String();
  116. }
  117. }
  118. private:
  119. //==============================================================================
  120. ObjectType type;
  121. const XmlElement* objectXml;
  122. ValueTree objectValueTree;
  123. //==============================================================================
  124. static String getStringFromXml (const XmlElement* xml, bool includeXmlHeader)
  125. {
  126. if (xml == nullptr)
  127. return "invalid XmlElement";
  128. return String(NewLine::getDefault()) + xml->createDocument (String(), false, includeXmlHeader);
  129. }
  130. String getStringFromValueTree() const
  131. {
  132. ScopedPointer<XmlElement> treeAsXml (objectValueTree.createXml());
  133. return treeAsXml == nullptr ? "invalid ValueTree" : getStringFromXml (treeAsXml, false);
  134. }
  135. //==============================================================================
  136. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DebugObject);
  137. };
  138. #endif //__DROWAUDIO_DEBUGOBJECT_H__