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.

337 lines
15KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. #pragma once
  18. //==============================================================================
  19. /**
  20. A variant class, that can be used to hold a range of primitive values.
  21. A var object can hold a range of simple primitive values, strings, or
  22. any kind of ReferenceCountedObject. The var class is intended to act like
  23. the kind of values used in dynamic scripting languages.
  24. You can save/load var objects either in a small, proprietary binary format
  25. using writeToStream()/readFromStream(), or as JSON by using the JSON class.
  26. @see JSON, DynamicObject
  27. */
  28. class JUCE_API var
  29. {
  30. public:
  31. //==============================================================================
  32. /** This structure is passed to a NativeFunction callback, and contains invocation
  33. details about the function's arguments and context.
  34. */
  35. struct JUCE_API NativeFunctionArgs
  36. {
  37. NativeFunctionArgs (const var& thisObject, const var* args, int numArgs) noexcept;
  38. // Suppress a VS2013 compiler warning
  39. NativeFunctionArgs& operator= (const NativeFunctionArgs&) = delete;
  40. const var& thisObject;
  41. const var* arguments;
  42. int numArguments;
  43. };
  44. using NativeFunction = std::function<var (const NativeFunctionArgs&)>;
  45. //==============================================================================
  46. /** Creates a void variant. */
  47. var() noexcept;
  48. /** Destructor. */
  49. ~var() noexcept;
  50. #if JUCE_ALLOW_STATIC_NULL_VARIABLES
  51. /** A static var object that can be used where you need an empty variant object. */
  52. static const var null;
  53. #endif
  54. var (const var& valueToCopy);
  55. var (int value) noexcept;
  56. var (int64 value) noexcept;
  57. var (bool value) noexcept;
  58. var (double value) noexcept;
  59. var (const char* value);
  60. var (const wchar_t* value);
  61. var (const String& value);
  62. var (const Array<var>& value);
  63. var (const StringArray& value);
  64. var (ReferenceCountedObject* object);
  65. var (NativeFunction method) noexcept;
  66. var (const void* binaryData, size_t dataSize);
  67. var (const MemoryBlock& binaryData);
  68. var& operator= (const var& valueToCopy);
  69. var& operator= (int value);
  70. var& operator= (int64 value);
  71. var& operator= (bool value);
  72. var& operator= (double value);
  73. var& operator= (const char* value);
  74. var& operator= (const wchar_t* value);
  75. var& operator= (const String& value);
  76. var& operator= (const MemoryBlock& value);
  77. var& operator= (const Array<var>& value);
  78. var& operator= (ReferenceCountedObject* object);
  79. var& operator= (NativeFunction method);
  80. var (var&&) noexcept;
  81. var (String&&);
  82. var (MemoryBlock&&);
  83. var (Array<var>&&);
  84. var& operator= (var&&) noexcept;
  85. var& operator= (String&&);
  86. void swapWith (var& other) noexcept;
  87. /** Returns a var object that can be used where you need the javascript "undefined" value. */
  88. static var undefined() noexcept;
  89. //==============================================================================
  90. operator int() const noexcept;
  91. operator int64() const noexcept;
  92. operator bool() const noexcept;
  93. operator float() const noexcept;
  94. operator double() const noexcept;
  95. operator String() const;
  96. String toString() const;
  97. /** If this variant holds an array, this provides access to it.
  98. NOTE: Beware when you use this - the array pointer is only valid for the lifetime
  99. of the variant that returned it, so be very careful not to call this method on temporary
  100. var objects that are the return-value of a function, and which may go out of scope before
  101. you use the array!
  102. */
  103. Array<var>* getArray() const noexcept;
  104. /** If this variant holds a memory block, this provides access to it.
  105. NOTE: Beware when you use this - the MemoryBlock pointer is only valid for the lifetime
  106. of the variant that returned it, so be very careful not to call this method on temporary
  107. var objects that are the return-value of a function, and which may go out of scope before
  108. you use the MemoryBlock!
  109. */
  110. MemoryBlock* getBinaryData() const noexcept;
  111. ReferenceCountedObject* getObject() const noexcept;
  112. DynamicObject* getDynamicObject() const noexcept;
  113. //==============================================================================
  114. bool isVoid() const noexcept;
  115. bool isUndefined() const noexcept;
  116. bool isInt() const noexcept;
  117. bool isInt64() const noexcept;
  118. bool isBool() const noexcept;
  119. bool isDouble() const noexcept;
  120. bool isString() const noexcept;
  121. bool isObject() const noexcept;
  122. bool isArray() const noexcept;
  123. bool isBinaryData() const noexcept;
  124. bool isMethod() const noexcept;
  125. /** Returns true if this var has the same value as the one supplied.
  126. Note that this ignores the type, so a string var "123" and an integer var with the
  127. value 123 are considered to be equal.
  128. @see equalsWithSameType
  129. */
  130. bool equals (const var& other) const noexcept;
  131. /** Returns true if this var has the same value and type as the one supplied.
  132. This differs from equals() because e.g. "123" and 123 will be considered different.
  133. @see equals
  134. */
  135. bool equalsWithSameType (const var& other) const noexcept;
  136. /** Returns true if this var has the same type as the one supplied. */
  137. bool hasSameTypeAs (const var& other) const noexcept;
  138. /** Returns a deep copy of this object.
  139. For simple types this just returns a copy, but if the object contains any arrays
  140. or DynamicObjects, they will be cloned (recursively).
  141. */
  142. var clone() const noexcept;
  143. //==============================================================================
  144. /** If the var is an array, this returns the number of elements.
  145. If the var isn't actually an array, this will return 0.
  146. */
  147. int size() const;
  148. /** If the var is an array, this can be used to return one of its elements.
  149. To call this method, you must make sure that the var is actually an array, and
  150. that the index is a valid number. If these conditions aren't met, behaviour is
  151. undefined.
  152. For more control over the array's contents, you can call getArray() and manipulate
  153. it directly as an Array\<var\>.
  154. */
  155. const var& operator[] (int arrayIndex) const;
  156. /** If the var is an array, this can be used to return one of its elements.
  157. To call this method, you must make sure that the var is actually an array, and
  158. that the index is a valid number. If these conditions aren't met, behaviour is
  159. undefined.
  160. For more control over the array's contents, you can call getArray() and manipulate
  161. it directly as an Array\<var\>.
  162. */
  163. var& operator[] (int arrayIndex);
  164. /** Appends an element to the var, converting it to an array if it isn't already one.
  165. If the var isn't an array, it will be converted to one, and if its value was non-void,
  166. this value will be kept as the first element of the new array. The parameter value
  167. will then be appended to it.
  168. For more control over the array's contents, you can call getArray() and manipulate
  169. it directly as an Array\<var\>.
  170. */
  171. void append (const var& valueToAppend);
  172. /** Inserts an element to the var, converting it to an array if it isn't already one.
  173. If the var isn't an array, it will be converted to one, and if its value was non-void,
  174. this value will be kept as the first element of the new array. The parameter value
  175. will then be inserted into it.
  176. For more control over the array's contents, you can call getArray() and manipulate
  177. it directly as an Array\<var\>.
  178. */
  179. void insert (int index, const var& value);
  180. /** If the var is an array, this removes one of its elements.
  181. If the index is out-of-range or the var isn't an array, nothing will be done.
  182. For more control over the array's contents, you can call getArray() and manipulate
  183. it directly as an Array\<var\>.
  184. */
  185. void remove (int index);
  186. /** Treating the var as an array, this resizes it to contain the specified number of elements.
  187. If the var isn't an array, it will be converted to one, and if its value was non-void,
  188. this value will be kept as the first element of the new array before resizing.
  189. For more control over the array's contents, you can call getArray() and manipulate
  190. it directly as an Array\<var\>.
  191. */
  192. void resize (int numArrayElementsWanted);
  193. /** If the var is an array, this searches it for the first occurrence of the specified value,
  194. and returns its index.
  195. If the var isn't an array, or if the value isn't found, this returns -1.
  196. */
  197. int indexOf (const var& value) const;
  198. //==============================================================================
  199. /** If this variant is an object, this returns one of its properties. */
  200. const var& operator[] (const Identifier& propertyName) const;
  201. /** If this variant is an object, this returns one of its properties. */
  202. const var& operator[] (const char* propertyName) const;
  203. /** If this variant is an object, this returns one of its properties, or a default
  204. fallback value if the property is not set. */
  205. var getProperty (const Identifier& propertyName, const var& defaultReturnValue) const;
  206. /** Invokes a named method call with no arguments. */
  207. var call (const Identifier& method) const;
  208. /** Invokes a named method call with one argument. */
  209. var call (const Identifier& method, const var& arg1) const;
  210. /** Invokes a named method call with 2 arguments. */
  211. var call (const Identifier& method, const var& arg1, const var& arg2) const;
  212. /** Invokes a named method call with 3 arguments. */
  213. var call (const Identifier& method, const var& arg1, const var& arg2, const var& arg3);
  214. /** Invokes a named method call with 4 arguments. */
  215. var call (const Identifier& method, const var& arg1, const var& arg2, const var& arg3, const var& arg4) const;
  216. /** Invokes a named method call with 5 arguments. */
  217. var call (const Identifier& method, const var& arg1, const var& arg2, const var& arg3, const var& arg4, const var& arg5) const;
  218. /** Invokes a named method call with a list of arguments. */
  219. var invoke (const Identifier& method, const var* arguments, int numArguments) const;
  220. /** If this object is a method, this returns the function pointer. */
  221. NativeFunction getNativeFunction() const;
  222. //==============================================================================
  223. /** Writes a binary representation of this value to a stream.
  224. The data can be read back later using readFromStream().
  225. @see JSON
  226. */
  227. void writeToStream (OutputStream& output) const;
  228. /** Reads back a stored binary representation of a value.
  229. The data in the stream must have been written using writeToStream(), or this
  230. will have unpredictable results.
  231. @see JSON
  232. */
  233. static var readFromStream (InputStream& input);
  234. private:
  235. //==============================================================================
  236. class VariantType; friend class VariantType;
  237. class VariantType_Void; friend class VariantType_Void;
  238. class VariantType_Undefined; friend class VariantType_Undefined;
  239. class VariantType_Int; friend class VariantType_Int;
  240. class VariantType_Int64; friend class VariantType_Int64;
  241. class VariantType_Double; friend class VariantType_Double;
  242. class VariantType_Bool; friend class VariantType_Bool;
  243. class VariantType_String; friend class VariantType_String;
  244. class VariantType_Object; friend class VariantType_Object;
  245. class VariantType_Array; friend class VariantType_Array;
  246. class VariantType_Binary; friend class VariantType_Binary;
  247. class VariantType_Method; friend class VariantType_Method;
  248. union ValueUnion
  249. {
  250. int intValue;
  251. int64 int64Value;
  252. bool boolValue;
  253. double doubleValue;
  254. char stringValue [sizeof (String)];
  255. ReferenceCountedObject* objectValue;
  256. MemoryBlock* binaryValue;
  257. NativeFunction* methodValue;
  258. };
  259. const VariantType* type;
  260. ValueUnion value;
  261. Array<var>* convertToArray();
  262. var (const VariantType&) noexcept;
  263. };
  264. /** Compares the values of two var objects, using the var::equals() comparison. */
  265. JUCE_API bool operator== (const var&, const var&) noexcept;
  266. /** Compares the values of two var objects, using the var::equals() comparison. */
  267. JUCE_API bool operator!= (const var&, const var&) noexcept;
  268. JUCE_API bool operator== (const var&, const String&);
  269. JUCE_API bool operator!= (const var&, const String&);
  270. JUCE_API bool operator== (const var&, const char*);
  271. JUCE_API bool operator!= (const var&, const char*);
  272. //==============================================================================
  273. /** This template-overloaded class can be used to convert between var and custom types. */
  274. template <typename Type>
  275. struct VariantConverter
  276. {
  277. static Type fromVar (const var& v) { return static_cast<Type> (v); }
  278. static var toVar (const Type& t) { return t; }
  279. };
  280. /** This template-overloaded class can be used to convert between var and custom types. */
  281. template <>
  282. struct VariantConverter<String>
  283. {
  284. static String fromVar (const var& v) { return v.toString(); }
  285. static var toVar (const String& s) { return s; }
  286. };