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.

348 lines
15KB

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