The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

183 lines
7.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-10 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_VARIANT_JUCEHEADER__
  19. #define __JUCE_VARIANT_JUCEHEADER__
  20. #include "../text/juce_Identifier.h"
  21. #include "../io/streams/juce_OutputStream.h"
  22. #include "../io/streams/juce_InputStream.h"
  23. class JUCE_API DynamicObject;
  24. //==============================================================================
  25. /**
  26. A variant class, that can be used to hold a range of primitive values.
  27. A var object can hold a range of simple primitive values, strings, or
  28. a reference-counted pointer to a DynamicObject. The var class is intended
  29. to act like the values used in dynamic scripting languages.
  30. @see DynamicObject
  31. */
  32. class JUCE_API var
  33. {
  34. public:
  35. //==============================================================================
  36. typedef const var (DynamicObject::*MethodFunction) (const var* arguments, int numArguments);
  37. typedef Identifier identifier;
  38. //==============================================================================
  39. /** Creates a void variant. */
  40. var() throw();
  41. /** Destructor. */
  42. ~var() throw();
  43. /** A static var object that can be used where you need an empty variant object. */
  44. static const var null;
  45. var (const var& valueToCopy);
  46. var (int value) throw();
  47. var (bool value) throw();
  48. var (double value) throw();
  49. var (const char* value);
  50. var (const juce_wchar* value);
  51. var (const String& value);
  52. var (DynamicObject* object);
  53. var (MethodFunction method) throw();
  54. var& operator= (const var& valueToCopy);
  55. var& operator= (int value);
  56. var& operator= (bool value);
  57. var& operator= (double value);
  58. var& operator= (const char* value);
  59. var& operator= (const juce_wchar* value);
  60. var& operator= (const String& value);
  61. var& operator= (DynamicObject* object);
  62. var& operator= (MethodFunction method);
  63. void swapWith (var& other) throw();
  64. operator int() const;
  65. operator bool() const;
  66. operator float() const;
  67. operator double() const;
  68. operator const String() const;
  69. const String toString() const;
  70. DynamicObject* getObject() const;
  71. bool isVoid() const throw();
  72. bool isInt() const throw();
  73. bool isBool() const throw();
  74. bool isDouble() const throw();
  75. bool isString() const throw();
  76. bool isObject() const throw();
  77. bool isMethod() const throw();
  78. //==============================================================================
  79. /** Writes a binary representation of this value to a stream.
  80. The data can be read back later using readFromStream().
  81. */
  82. void writeToStream (OutputStream& output) const;
  83. /** Reads back a stored binary representation of a value.
  84. The data in the stream must have been written using writeToStream(), or this
  85. will have unpredictable results.
  86. */
  87. static const var readFromStream (InputStream& input);
  88. //==============================================================================
  89. /** If this variant is an object, this returns one of its properties. */
  90. const var operator[] (const Identifier& propertyName) const;
  91. //==============================================================================
  92. /** If this variant is an object, this invokes one of its methods with no arguments. */
  93. const var call (const Identifier& method) const;
  94. /** If this variant is an object, this invokes one of its methods with one argument. */
  95. const var call (const Identifier& method, const var& arg1) const;
  96. /** If this variant is an object, this invokes one of its methods with 2 arguments. */
  97. const var call (const Identifier& method, const var& arg1, const var& arg2) const;
  98. /** If this variant is an object, this invokes one of its methods with 3 arguments. */
  99. const var call (const Identifier& method, const var& arg1, const var& arg2, const var& arg3);
  100. /** If this variant is an object, this invokes one of its methods with 4 arguments. */
  101. const var call (const Identifier& method, const var& arg1, const var& arg2, const var& arg3, const var& arg4) const;
  102. /** If this variant is an object, this invokes one of its methods with 5 arguments. */
  103. const var call (const Identifier& method, const var& arg1, const var& arg2, const var& arg3, const var& arg4, const var& arg5) const;
  104. /** If this variant is an object, this invokes one of its methods with a list of arguments. */
  105. const var invoke (const Identifier& method, const var* arguments, int numArguments) const;
  106. //==============================================================================
  107. /** If this variant is a method pointer, this invokes it on a target object. */
  108. const var invoke (const var& targetObject, const var* arguments, int numArguments) const;
  109. //==============================================================================
  110. /** Returns true if this var has the same value as the one supplied. */
  111. bool equals (const var& other) const throw();
  112. private:
  113. class VariantType;
  114. friend class VariantType;
  115. class VariantType_Void;
  116. friend class VariantType_Void;
  117. class VariantType_Int;
  118. friend class VariantType_Int;
  119. class VariantType_Double;
  120. friend class VariantType_Double;
  121. class VariantType_Float;
  122. friend class VariantType_Float;
  123. class VariantType_Bool;
  124. friend class VariantType_Bool;
  125. class VariantType_String;
  126. friend class VariantType_String;
  127. class VariantType_Object;
  128. friend class VariantType_Object;
  129. class VariantType_Method;
  130. friend class VariantType_Method;
  131. union ValueUnion
  132. {
  133. int intValue;
  134. bool boolValue;
  135. double doubleValue;
  136. String* stringValue;
  137. DynamicObject* objectValue;
  138. MethodFunction methodValue;
  139. };
  140. const VariantType* type;
  141. ValueUnion value;
  142. };
  143. bool operator== (const var& v1, const var& v2) throw();
  144. bool operator!= (const var& v1, const var& v2) throw();
  145. bool operator== (const var& v1, const String& v2) throw();
  146. bool operator!= (const var& v1, const String& v2) throw();
  147. #endif // __JUCE_VARIANT_JUCEHEADER__