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.

366 lines
16KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  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. namespace juce
  18. {
  19. //==============================================================================
  20. /**
  21. A variant class, that can be used to hold a range of primitive values.
  22. A var object can hold a range of simple primitive values, strings, or
  23. any kind of ReferenceCountedObject. The var class is intended to act like
  24. the kind of values used in dynamic scripting languages.
  25. You can save/load var objects either in a small, proprietary binary format
  26. using writeToStream()/readFromStream(), or as JSON by using the JSON class.
  27. @see JSON, DynamicObject
  28. @tags{Core}
  29. */
  30. class JUCE_API var
  31. {
  32. public:
  33. //==============================================================================
  34. /** This structure is passed to a NativeFunction callback, and contains invocation
  35. details about the function's arguments and context.
  36. */
  37. struct JUCE_API NativeFunctionArgs
  38. {
  39. NativeFunctionArgs (const var& thisObject, const var* args, int numArgs) noexcept;
  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. var (const var& valueToCopy);
  51. var (int value) noexcept;
  52. var (int64 value) noexcept;
  53. var (bool value) noexcept;
  54. var (double value) noexcept;
  55. var (const char* value);
  56. var (const wchar_t* value);
  57. var (const String& value);
  58. var (const Array<var>& value);
  59. var (const StringArray& value);
  60. var (ReferenceCountedObject* object);
  61. var (NativeFunction method) noexcept;
  62. var (const void* binaryData, size_t dataSize);
  63. var (const MemoryBlock& binaryData);
  64. var& operator= (const var& valueToCopy);
  65. var& operator= (int value);
  66. var& operator= (int64 value);
  67. var& operator= (bool value);
  68. var& operator= (double value);
  69. var& operator= (const char* value);
  70. var& operator= (const wchar_t* value);
  71. var& operator= (const String& value);
  72. var& operator= (const MemoryBlock& value);
  73. var& operator= (const Array<var>& value);
  74. var& operator= (ReferenceCountedObject* object);
  75. var& operator= (NativeFunction method);
  76. var (var&&) noexcept;
  77. var (String&&);
  78. var (MemoryBlock&&);
  79. var (Array<var>&&);
  80. var& operator= (var&&) noexcept;
  81. var& operator= (String&&);
  82. void swapWith (var& other) noexcept;
  83. /** Returns a var object that can be used where you need the javascript "undefined" value. */
  84. static var undefined() noexcept;
  85. //==============================================================================
  86. operator int() const noexcept;
  87. operator int64() const noexcept;
  88. operator bool() const noexcept;
  89. operator float() const noexcept;
  90. operator double() const noexcept;
  91. operator String() const;
  92. String toString() const;
  93. /** If this variant holds an array, this provides access to it.
  94. NOTE: Beware when you use this - the array pointer is only valid for the lifetime
  95. of the variant that returned it, so be very careful not to call this method on temporary
  96. var objects that are the return-value of a function, and which may go out of scope before
  97. you use the array!
  98. */
  99. Array<var>* getArray() const noexcept;
  100. /** If this variant holds a memory block, this provides access to it.
  101. NOTE: Beware when you use this - the MemoryBlock pointer is only valid for the lifetime
  102. of the variant that returned it, so be very careful not to call this method on temporary
  103. var objects that are the return-value of a function, and which may go out of scope before
  104. you use the MemoryBlock!
  105. */
  106. MemoryBlock* getBinaryData() const noexcept;
  107. ReferenceCountedObject* getObject() const noexcept;
  108. DynamicObject* getDynamicObject() const noexcept;
  109. //==============================================================================
  110. bool isVoid() const noexcept;
  111. bool isUndefined() const noexcept;
  112. bool isInt() const noexcept;
  113. bool isInt64() const noexcept;
  114. bool isBool() const noexcept;
  115. bool isDouble() const noexcept;
  116. bool isString() const noexcept;
  117. bool isObject() const noexcept;
  118. bool isArray() const noexcept;
  119. bool isBinaryData() const noexcept;
  120. bool isMethod() const noexcept;
  121. /** Returns true if this var has the same value as the one supplied.
  122. Note that this ignores the type, so a string var "123" and an integer var with the
  123. value 123 are considered to be equal.
  124. Note that equality checking depends on the "wrapped" type of the object on which
  125. equals() is called. That means the following code will convert the right-hand-side
  126. argument to a string and compare the string values, because the object on the
  127. left-hand-side was initialised from a string:
  128. @code var ("123").equals (var (123)) @endcode
  129. However, the following code will convert the right-hand-side argument to a double
  130. and compare the values as doubles, because the object on the left-hand-side was
  131. initialised from a double:
  132. @code var (45.6).equals ("45.6000") @endcode
  133. @see equalsWithSameType
  134. */
  135. bool equals (const var& other) const noexcept;
  136. /** Returns true if this var has the same value and type as the one supplied.
  137. This differs from equals() because e.g. "123" and 123 will be considered different.
  138. @see equals
  139. */
  140. bool equalsWithSameType (const var& other) const noexcept;
  141. /** Returns true if this var has the same type as the one supplied. */
  142. bool hasSameTypeAs (const var& other) const noexcept;
  143. /** Returns a deep copy of this object.
  144. For simple types this just returns a copy, but if the object contains any arrays
  145. or DynamicObjects, they will be cloned (recursively).
  146. */
  147. var clone() const noexcept;
  148. //==============================================================================
  149. /** If the var is an array, this returns the number of elements.
  150. If the var isn't actually an array, this will return 0.
  151. */
  152. int size() const;
  153. /** If the var is an array, this can be used to return one of its elements.
  154. To call this method, you must make sure that the var is actually an array, and
  155. that the index is a valid number. If these conditions aren't met, behaviour is
  156. undefined.
  157. For more control over the array's contents, you can call getArray() and manipulate
  158. it directly as an Array\<var\>.
  159. */
  160. const var& operator[] (int arrayIndex) const;
  161. /** If the var is an array, this can be used to return one of its elements.
  162. To call this method, you must make sure that the var is actually an array, and
  163. that the index is a valid number. If these conditions aren't met, behaviour is
  164. undefined.
  165. For more control over the array's contents, you can call getArray() and manipulate
  166. it directly as an Array\<var\>.
  167. */
  168. var& operator[] (int arrayIndex);
  169. /** Appends an element to the var, converting it to an array if it isn't already one.
  170. If the var isn't an array, it will be converted to one, and if its value was non-void,
  171. this value will be kept as the first element of the new array. The parameter value
  172. will then be appended to it.
  173. For more control over the array's contents, you can call getArray() and manipulate
  174. it directly as an Array\<var\>.
  175. */
  176. void append (const var& valueToAppend);
  177. /** Inserts an element to the var, converting it to an array if it isn't already one.
  178. If the var isn't an array, it will be converted to one, and if its value was non-void,
  179. this value will be kept as the first element of the new array. The parameter value
  180. will then be inserted into it.
  181. For more control over the array's contents, you can call getArray() and manipulate
  182. it directly as an Array\<var\>.
  183. */
  184. void insert (int index, const var& value);
  185. /** If the var is an array, this removes one of its elements.
  186. If the index is out-of-range or the var isn't an array, nothing will be done.
  187. For more control over the array's contents, you can call getArray() and manipulate
  188. it directly as an Array\<var\>.
  189. */
  190. void remove (int index);
  191. /** Treating the var as an array, this resizes it to contain the specified number of elements.
  192. If the var isn't an array, it will be converted to one, and if its value was non-void,
  193. this value will be kept as the first element of the new array before resizing.
  194. For more control over the array's contents, you can call getArray() and manipulate
  195. it directly as an Array\<var\>.
  196. */
  197. void resize (int numArrayElementsWanted);
  198. /** If the var is an array, this searches it for the first occurrence of the specified value,
  199. and returns its index.
  200. If the var isn't an array, or if the value isn't found, this returns -1.
  201. */
  202. int indexOf (const var& value) const;
  203. //==============================================================================
  204. /** If this variant is an object, this returns one of its properties. */
  205. const var& operator[] (const Identifier& propertyName) const;
  206. /** If this variant is an object, this returns one of its properties. */
  207. const var& operator[] (const char* propertyName) const;
  208. /** If this variant is an object, this returns one of its properties, or a default
  209. fallback value if the property is not set. */
  210. var getProperty (const Identifier& propertyName, const var& defaultReturnValue) const;
  211. /** Returns true if this variant is an object and if it has the given property. */
  212. bool hasProperty (const Identifier& propertyName) const noexcept;
  213. /** Invokes a named method call with no arguments. */
  214. var call (const Identifier& method) const;
  215. /** Invokes a named method call with one argument. */
  216. var call (const Identifier& method, const var& arg1) const;
  217. /** Invokes a named method call with 2 arguments. */
  218. var call (const Identifier& method, const var& arg1, const var& arg2) const;
  219. /** Invokes a named method call with 3 arguments. */
  220. var call (const Identifier& method, const var& arg1, const var& arg2, const var& arg3);
  221. /** Invokes a named method call with 4 arguments. */
  222. var call (const Identifier& method, const var& arg1, const var& arg2, const var& arg3, const var& arg4) const;
  223. /** Invokes a named method call with 5 arguments. */
  224. var call (const Identifier& method, const var& arg1, const var& arg2, const var& arg3, const var& arg4, const var& arg5) const;
  225. /** Invokes a named method call with a list of arguments. */
  226. var invoke (const Identifier& method, const var* arguments, int numArguments) const;
  227. /** If this object is a method, this returns the function pointer. */
  228. NativeFunction getNativeFunction() const;
  229. //==============================================================================
  230. /** Writes a binary representation of this value to a stream.
  231. The data can be read back later using readFromStream().
  232. @see JSON
  233. */
  234. void writeToStream (OutputStream& output) const;
  235. /** Reads back a stored binary representation of a value.
  236. The data in the stream must have been written using writeToStream(), or this
  237. will have unpredictable results.
  238. @see JSON
  239. */
  240. static var readFromStream (InputStream& input);
  241. //==============================================================================
  242. #if JUCE_ALLOW_STATIC_NULL_VARIABLES && ! defined (DOXYGEN)
  243. [[deprecated ("This was a static empty var object, but is now deprecated as it's too easy to accidentally "
  244. "use it indirectly during a static constructor leading to hard-to-find order-of-initialisation "
  245. "problems. Use var() or {} instead. For returning an empty var from a function by reference, "
  246. "use a function-local static var and return that.")]]
  247. static const var null;
  248. #endif
  249. private:
  250. //==============================================================================
  251. struct VariantType;
  252. struct Instance;
  253. union ValueUnion
  254. {
  255. int intValue;
  256. int64 int64Value;
  257. bool boolValue;
  258. double doubleValue;
  259. char stringValue[sizeof (String)];
  260. ReferenceCountedObject* objectValue;
  261. MemoryBlock* binaryValue;
  262. NativeFunction* methodValue;
  263. };
  264. friend bool canCompare (const var&, const var&);
  265. const VariantType* type;
  266. ValueUnion value;
  267. Array<var>* convertToArray();
  268. var (const VariantType&) noexcept;
  269. // This is needed to prevent the wrong constructor/operator being called
  270. var (const ReferenceCountedObject*) = delete;
  271. var& operator= (const ReferenceCountedObject*) = delete;
  272. var (const void*) = delete;
  273. var& operator= (const void*) = delete;
  274. };
  275. /** Compares the values of two var objects, using the var::equals() comparison. */
  276. JUCE_API bool operator== (const var&, const var&);
  277. /** Compares the values of two var objects, using the var::equals() comparison. */
  278. JUCE_API bool operator!= (const var&, const var&);
  279. /** Compares the values of two var objects, using the var::equals() comparison. */
  280. JUCE_API bool operator< (const var&, const var&);
  281. /** Compares the values of two var objects, using the var::equals() comparison. */
  282. JUCE_API bool operator<= (const var&, const var&);
  283. /** Compares the values of two var objects, using the var::equals() comparison. */
  284. JUCE_API bool operator> (const var&, const var&);
  285. /** Compares the values of two var objects, using the var::equals() comparison. */
  286. JUCE_API bool operator>= (const var&, const var&);
  287. JUCE_API bool operator== (const var&, const String&);
  288. JUCE_API bool operator!= (const var&, const String&);
  289. JUCE_API bool operator== (const var&, const char*);
  290. JUCE_API bool operator!= (const var&, const char*);
  291. //==============================================================================
  292. /** This template-overloaded class can be used to convert between var and custom types.
  293. @tags{Core}
  294. */
  295. template <typename Type>
  296. struct VariantConverter
  297. {
  298. static Type fromVar (const var& v) { return static_cast<Type> (v); }
  299. static var toVar (const Type& t) { return t; }
  300. };
  301. #ifndef DOXYGEN
  302. template <>
  303. struct VariantConverter<String>
  304. {
  305. static String fromVar (const var& v) { return v.toString(); }
  306. static var toVar (const String& s) { return s; }
  307. };
  308. #endif
  309. } // namespace juce