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.

899 lines
38KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - 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. enum VariantStreamMarkers
  20. {
  21. varMarker_Int = 1,
  22. varMarker_BoolTrue = 2,
  23. varMarker_BoolFalse = 3,
  24. varMarker_Double = 4,
  25. varMarker_String = 5,
  26. varMarker_Int64 = 6,
  27. varMarker_Array = 7,
  28. varMarker_Binary = 8,
  29. varMarker_Undefined = 9
  30. };
  31. //==============================================================================
  32. struct var::VariantType
  33. {
  34. struct VoidTag {};
  35. struct UndefinedTag {};
  36. struct IntTag {};
  37. struct Int64Tag {};
  38. struct DoubleTag {};
  39. struct BoolTag {};
  40. struct StringTag {};
  41. struct ObjectTag {};
  42. struct ArrayTag {};
  43. struct BinaryTag {};
  44. struct MethodTag {};
  45. // members =====================================================================
  46. bool isVoid = false;
  47. bool isUndefined = false;
  48. bool isInt = false;
  49. bool isInt64 = false;
  50. bool isBool = false;
  51. bool isDouble = false;
  52. bool isString = false;
  53. bool isObject = false;
  54. bool isArray = false;
  55. bool isBinary = false;
  56. bool isMethod = false;
  57. bool isComparable = false;
  58. int (*toInt) (const ValueUnion&) = defaultToInt;
  59. int64 (*toInt64) (const ValueUnion&) = defaultToInt64;
  60. double (*toDouble) (const ValueUnion&) = defaultToDouble;
  61. String (*toString) (const ValueUnion&) = defaultToString;
  62. bool (*toBool) (const ValueUnion&) = defaultToBool;
  63. ReferenceCountedObject* (*toObject) (const ValueUnion&) = defaultToObject;
  64. Array<var>* (*toArray) (const ValueUnion&) = defaultToArray;
  65. MemoryBlock* (*toBinary) (const ValueUnion&) = defaultToBinary;
  66. var (*clone) (const var&) = defaultClone;
  67. void (*cleanUp) (ValueUnion&) = defaultCleanUp;
  68. void (*createCopy) (ValueUnion&, const ValueUnion&) = defaultCreateCopy;
  69. bool (*equals) (const ValueUnion&, const ValueUnion&, const VariantType&) = nullptr;
  70. void (*writeToStream) (const ValueUnion&, OutputStream&) = nullptr;
  71. // defaults ====================================================================
  72. static int defaultToInt (const ValueUnion&) { return 0; }
  73. static int64 defaultToInt64 (const ValueUnion&) { return 0; }
  74. static double defaultToDouble (const ValueUnion&) { return 0; }
  75. static String defaultToString (const ValueUnion&) { return {}; }
  76. static bool defaultToBool (const ValueUnion&) { return false; }
  77. static ReferenceCountedObject* defaultToObject (const ValueUnion&) { return nullptr; }
  78. static Array<var>* defaultToArray (const ValueUnion&) { return nullptr; }
  79. static MemoryBlock* defaultToBinary (const ValueUnion&) { return nullptr; }
  80. static var defaultClone (const var& other) { return other; }
  81. static void defaultCleanUp (ValueUnion&) {}
  82. static void defaultCreateCopy (ValueUnion& dest, const ValueUnion& source) { dest = source; }
  83. // void ========================================================================
  84. static bool voidEquals (const ValueUnion&, const ValueUnion&, const VariantType& otherType) noexcept
  85. {
  86. return otherType.isVoid || otherType.isUndefined;
  87. }
  88. static void voidWriteToStream (const ValueUnion&, OutputStream& output)
  89. {
  90. output.writeCompressedInt (0);
  91. }
  92. constexpr explicit VariantType (VoidTag) noexcept
  93. : isVoid (true),
  94. isComparable (true),
  95. equals (voidEquals),
  96. writeToStream (voidWriteToStream) {}
  97. // undefined ===================================================================
  98. static String undefinedToString (const ValueUnion&) { return "undefined"; }
  99. static bool undefinedEquals (const ValueUnion&, const ValueUnion&, const VariantType& otherType) noexcept
  100. {
  101. return otherType.isVoid || otherType.isUndefined;
  102. }
  103. static void undefinedWriteToStream (const ValueUnion&, OutputStream& output)
  104. {
  105. output.writeCompressedInt (1);
  106. output.writeByte (varMarker_Undefined);
  107. }
  108. constexpr explicit VariantType (UndefinedTag) noexcept
  109. : isUndefined (true),
  110. toString (undefinedToString),
  111. equals (undefinedEquals),
  112. writeToStream (undefinedWriteToStream) {}
  113. // int =========================================================================
  114. static int intToInt (const ValueUnion& data) noexcept { return data.intValue; }
  115. static int64 intToInt64 (const ValueUnion& data) noexcept { return (int64) data.intValue; }
  116. static double intToDouble (const ValueUnion& data) noexcept { return (double) data.intValue; }
  117. static String intToString (const ValueUnion& data) { return String (data.intValue); }
  118. static bool intToBool (const ValueUnion& data) noexcept { return data.intValue != 0; }
  119. static bool intEquals (const ValueUnion& data, const ValueUnion& otherData, const VariantType& otherType) noexcept
  120. {
  121. if (otherType.isDouble || otherType.isInt64 || otherType.isString)
  122. return otherType.equals (otherData, data, VariantType { IntTag{} });
  123. return otherType.toInt (otherData) == data.intValue;
  124. }
  125. static void intWriteToStream (const ValueUnion& data, OutputStream& output)
  126. {
  127. output.writeCompressedInt (5);
  128. output.writeByte (varMarker_Int);
  129. output.writeInt (data.intValue);
  130. }
  131. constexpr explicit VariantType (IntTag) noexcept
  132. : isInt (true),
  133. isComparable (true),
  134. toInt (intToInt),
  135. toInt64 (intToInt64),
  136. toDouble (intToDouble),
  137. toString (intToString),
  138. toBool (intToBool),
  139. equals (intEquals),
  140. writeToStream (intWriteToStream) {}
  141. // int64 =======================================================================
  142. static int int64ToInt (const ValueUnion& data) noexcept { return (int) data.int64Value; }
  143. static int64 int64ToInt64 (const ValueUnion& data) noexcept { return data.int64Value; }
  144. static double int64ToDouble (const ValueUnion& data) noexcept { return (double) data.int64Value; }
  145. static String int64ToString (const ValueUnion& data) { return String (data.int64Value); }
  146. static bool int64ToBool (const ValueUnion& data) noexcept { return data.int64Value != 0; }
  147. static bool int64Equals (const ValueUnion& data, const ValueUnion& otherData, const VariantType& otherType) noexcept
  148. {
  149. if (otherType.isDouble || otherType.isString)
  150. return otherType.equals (otherData, data, VariantType { Int64Tag{} });
  151. return otherType.toInt64 (otherData) == data.int64Value;
  152. }
  153. static void int64WriteToStream (const ValueUnion& data, OutputStream& output)
  154. {
  155. output.writeCompressedInt (9);
  156. output.writeByte (varMarker_Int64);
  157. output.writeInt64 (data.int64Value);
  158. }
  159. constexpr explicit VariantType (Int64Tag) noexcept
  160. : isInt64 (true),
  161. isComparable (true),
  162. toInt (int64ToInt),
  163. toInt64 (int64ToInt64),
  164. toDouble (int64ToDouble),
  165. toString (int64ToString),
  166. toBool (int64ToBool),
  167. equals (int64Equals),
  168. writeToStream (int64WriteToStream) {}
  169. // double ======================================================================
  170. static int doubleToInt (const ValueUnion& data) noexcept { return (int) data.doubleValue; }
  171. static int64 doubleToInt64 (const ValueUnion& data) noexcept { return (int64) data.doubleValue; }
  172. static double doubleToDouble (const ValueUnion& data) noexcept { return data.doubleValue; }
  173. static String doubleToString (const ValueUnion& data) { return serialiseDouble (data.doubleValue); }
  174. static bool doubleToBool (const ValueUnion& data) noexcept { return data.doubleValue != 0.0; }
  175. static bool doubleEquals (const ValueUnion& data, const ValueUnion& otherData, const VariantType& otherType) noexcept
  176. {
  177. return std::abs (otherType.toDouble (otherData) - data.doubleValue) < std::numeric_limits<double>::epsilon();
  178. }
  179. static void doubleWriteToStream (const ValueUnion& data, OutputStream& output)
  180. {
  181. output.writeCompressedInt (9);
  182. output.writeByte (varMarker_Double);
  183. output.writeDouble (data.doubleValue);
  184. }
  185. constexpr explicit VariantType (DoubleTag) noexcept
  186. : isDouble (true),
  187. isComparable (true),
  188. toInt (doubleToInt),
  189. toInt64 (doubleToInt64),
  190. toDouble (doubleToDouble),
  191. toString (doubleToString),
  192. toBool (doubleToBool),
  193. equals (doubleEquals),
  194. writeToStream (doubleWriteToStream) {}
  195. // bool ========================================================================
  196. static int boolToInt (const ValueUnion& data) noexcept { return data.boolValue ? 1 : 0; }
  197. static int64 boolToInt64 (const ValueUnion& data) noexcept { return data.boolValue ? 1 : 0; }
  198. static double boolToDouble (const ValueUnion& data) noexcept { return data.boolValue ? 1.0 : 0.0; }
  199. static String boolToString (const ValueUnion& data) { return String::charToString (data.boolValue ? (juce_wchar) '1' : (juce_wchar) '0'); }
  200. static bool boolToBool (const ValueUnion& data) noexcept { return data.boolValue; }
  201. static bool boolEquals (const ValueUnion& data, const ValueUnion& otherData, const VariantType& otherType) noexcept
  202. {
  203. return otherType.toBool (otherData) == data.boolValue;
  204. }
  205. static void boolWriteToStream (const ValueUnion& data, OutputStream& output)
  206. {
  207. output.writeCompressedInt (1);
  208. output.writeByte (data.boolValue ? (char) varMarker_BoolTrue : (char) varMarker_BoolFalse);
  209. }
  210. constexpr explicit VariantType (BoolTag) noexcept
  211. : isBool (true),
  212. isComparable (true),
  213. toInt (boolToInt),
  214. toInt64 (boolToInt64),
  215. toDouble (boolToDouble),
  216. toString (boolToString),
  217. toBool (boolToBool),
  218. equals (boolEquals),
  219. writeToStream (boolWriteToStream) {}
  220. // string ======================================================================
  221. static const String* getString (const ValueUnion& data) noexcept { return unalignedPointerCast<const String*> (data.stringValue); }
  222. static String* getString ( ValueUnion& data) noexcept { return unalignedPointerCast<String*> (data.stringValue); }
  223. static int stringToInt (const ValueUnion& data) noexcept { return getString (data)->getIntValue(); }
  224. static int64 stringToInt64 (const ValueUnion& data) noexcept { return getString (data)->getLargeIntValue(); }
  225. static double stringToDouble (const ValueUnion& data) noexcept { return getString (data)->getDoubleValue(); }
  226. static String stringToString (const ValueUnion& data) { return *getString (data); }
  227. static bool stringToBool (const ValueUnion& data) noexcept
  228. {
  229. return getString (data)->getIntValue() != 0
  230. || getString (data)->trim().equalsIgnoreCase ("true")
  231. || getString (data)->trim().equalsIgnoreCase ("yes");
  232. }
  233. static void stringCleanUp (ValueUnion& data) noexcept { getString (data)-> ~String(); }
  234. static void stringCreateCopy (ValueUnion& dest, const ValueUnion& source) { new (dest.stringValue) String (*getString (source)); }
  235. static bool stringEquals (const ValueUnion& data, const ValueUnion& otherData, const VariantType& otherType) noexcept
  236. {
  237. return otherType.toString (otherData) == *getString (data);
  238. }
  239. static void stringWriteToStream (const ValueUnion& data, OutputStream& output)
  240. {
  241. auto* s = getString (data);
  242. const size_t len = s->getNumBytesAsUTF8() + 1;
  243. HeapBlock<char> temp (len);
  244. s->copyToUTF8 (temp, len);
  245. output.writeCompressedInt ((int) (len + 1));
  246. output.writeByte (varMarker_String);
  247. output.write (temp, len);
  248. }
  249. constexpr explicit VariantType (StringTag) noexcept
  250. : isString (true),
  251. isComparable (true),
  252. toInt (stringToInt),
  253. toInt64 (stringToInt64),
  254. toDouble (stringToDouble),
  255. toString (stringToString),
  256. toBool (stringToBool),
  257. cleanUp (stringCleanUp),
  258. createCopy (stringCreateCopy),
  259. equals (stringEquals),
  260. writeToStream (stringWriteToStream) {}
  261. // object ======================================================================
  262. static String objectToString (const ValueUnion& data)
  263. {
  264. return "Object 0x" + String::toHexString ((int) (pointer_sized_int) data.objectValue);
  265. }
  266. static bool objectToBool (const ValueUnion& data) noexcept { return data.objectValue != nullptr; }
  267. static ReferenceCountedObject* objectToObject (const ValueUnion& data) noexcept { return data.objectValue; }
  268. static var objectClone (const var& original)
  269. {
  270. if (auto* d = original.getDynamicObject())
  271. return d->clone().get();
  272. jassertfalse; // can only clone DynamicObjects!
  273. return {};
  274. }
  275. static void objectCleanUp (ValueUnion& data) noexcept { if (data.objectValue != nullptr) data.objectValue->decReferenceCount(); }
  276. static void objectCreateCopy (ValueUnion& dest, const ValueUnion& source)
  277. {
  278. dest.objectValue = source.objectValue;
  279. if (dest.objectValue != nullptr)
  280. dest.objectValue->incReferenceCount();
  281. }
  282. static bool objectEquals (const ValueUnion& data, const ValueUnion& otherData, const VariantType& otherType) noexcept
  283. {
  284. return otherType.toObject (otherData) == data.objectValue;
  285. }
  286. static void objectWriteToStream (const ValueUnion&, OutputStream& output)
  287. {
  288. jassertfalse; // Can't write an object to a stream!
  289. output.writeCompressedInt (0);
  290. }
  291. constexpr explicit VariantType (ObjectTag) noexcept
  292. : isObject (true),
  293. toString (objectToString),
  294. toBool (objectToBool),
  295. toObject (objectToObject),
  296. clone (objectClone),
  297. cleanUp (objectCleanUp),
  298. createCopy (objectCreateCopy),
  299. equals (objectEquals),
  300. writeToStream (objectWriteToStream) {}
  301. // array =======================================================================
  302. static String arrayToString (const ValueUnion&) { return "[Array]"; }
  303. static ReferenceCountedObject* arrayToObject (const ValueUnion&) noexcept { return nullptr; }
  304. static Array<var>* arrayToArray (const ValueUnion& data) noexcept
  305. {
  306. if (auto* a = dynamic_cast<RefCountedArray*> (data.objectValue))
  307. return &(a->array);
  308. return nullptr;
  309. }
  310. static bool arrayEquals (const ValueUnion& data, const ValueUnion& otherData, const VariantType& otherType) noexcept
  311. {
  312. auto* thisArray = arrayToArray (data);
  313. auto* otherArray = otherType.toArray (otherData);
  314. return thisArray == otherArray || (thisArray != nullptr && otherArray != nullptr && *otherArray == *thisArray);
  315. }
  316. static var arrayClone (const var& original)
  317. {
  318. Array<var> arrayCopy;
  319. if (auto* array = arrayToArray (original.value))
  320. {
  321. arrayCopy.ensureStorageAllocated (array->size());
  322. for (auto& i : *array)
  323. arrayCopy.add (i.clone());
  324. }
  325. return var (arrayCopy);
  326. }
  327. static void arrayWriteToStream (const ValueUnion& data, OutputStream& output)
  328. {
  329. if (auto* array = arrayToArray (data))
  330. {
  331. MemoryOutputStream buffer (512);
  332. buffer.writeCompressedInt (array->size());
  333. for (auto& i : *array)
  334. i.writeToStream (buffer);
  335. output.writeCompressedInt (1 + (int) buffer.getDataSize());
  336. output.writeByte (varMarker_Array);
  337. output << buffer;
  338. }
  339. }
  340. struct RefCountedArray : public ReferenceCountedObject
  341. {
  342. RefCountedArray (const Array<var>& a) : array (a) { incReferenceCount(); }
  343. RefCountedArray (Array<var>&& a) : array (std::move (a)) { incReferenceCount(); }
  344. Array<var> array;
  345. };
  346. constexpr explicit VariantType (ArrayTag) noexcept
  347. : isObject (true),
  348. isArray (true),
  349. toString (arrayToString),
  350. toBool (objectToBool),
  351. toObject (arrayToObject),
  352. toArray (arrayToArray),
  353. clone (arrayClone),
  354. cleanUp (objectCleanUp),
  355. createCopy (objectCreateCopy),
  356. equals (arrayEquals),
  357. writeToStream (arrayWriteToStream) {}
  358. // binary ======================================================================
  359. static void binaryCleanUp (ValueUnion& data) noexcept { delete data.binaryValue; }
  360. static void binaryCreateCopy (ValueUnion& dest, const ValueUnion& source) { dest.binaryValue = new MemoryBlock (*source.binaryValue); }
  361. static String binaryToString (const ValueUnion& data) { return data.binaryValue->toBase64Encoding(); }
  362. static MemoryBlock* binaryToBinary (const ValueUnion& data) noexcept { return data.binaryValue; }
  363. static bool binaryEquals (const ValueUnion& data, const ValueUnion& otherData, const VariantType& otherType) noexcept
  364. {
  365. const MemoryBlock* const otherBlock = otherType.toBinary (otherData);
  366. return otherBlock != nullptr && *otherBlock == *data.binaryValue;
  367. }
  368. static void binaryWriteToStream (const ValueUnion& data, OutputStream& output)
  369. {
  370. output.writeCompressedInt (1 + (int) data.binaryValue->getSize());
  371. output.writeByte (varMarker_Binary);
  372. output << *data.binaryValue;
  373. }
  374. constexpr explicit VariantType (BinaryTag) noexcept
  375. : isBinary (true),
  376. toString (binaryToString),
  377. toBinary (binaryToBinary),
  378. cleanUp (binaryCleanUp),
  379. createCopy (binaryCreateCopy),
  380. equals (binaryEquals),
  381. writeToStream (binaryWriteToStream) {}
  382. // method ======================================================================
  383. static void methodCleanUp (ValueUnion& data) noexcept { if (data.methodValue != nullptr ) delete data.methodValue; }
  384. static void methodCreateCopy (ValueUnion& dest, const ValueUnion& source) { dest.methodValue = new NativeFunction (*source.methodValue); }
  385. static String methodToString (const ValueUnion&) { return "Method"; }
  386. static bool methodToBool (const ValueUnion& data) noexcept { return data.methodValue != nullptr; }
  387. static bool methodEquals (const ValueUnion& data, const ValueUnion& otherData, const VariantType& otherType) noexcept
  388. {
  389. return otherType.isMethod && otherData.methodValue == data.methodValue;
  390. }
  391. static void methodWriteToStream (const ValueUnion&, OutputStream& output)
  392. {
  393. jassertfalse; // Can't write a method to a stream!
  394. output.writeCompressedInt (0);
  395. }
  396. constexpr explicit VariantType (MethodTag) noexcept
  397. : isMethod (true),
  398. toString (methodToString),
  399. toBool (methodToBool),
  400. cleanUp (methodCleanUp),
  401. createCopy (methodCreateCopy),
  402. equals (methodEquals),
  403. writeToStream (methodWriteToStream) {}
  404. };
  405. struct var::Instance
  406. {
  407. static constexpr VariantType attributesVoid { VariantType::VoidTag{} };
  408. static constexpr VariantType attributesUndefined { VariantType::UndefinedTag{} };
  409. static constexpr VariantType attributesInt { VariantType::IntTag{} };
  410. static constexpr VariantType attributesInt64 { VariantType::Int64Tag{} };
  411. static constexpr VariantType attributesBool { VariantType::BoolTag{} };
  412. static constexpr VariantType attributesDouble { VariantType::DoubleTag{} };
  413. static constexpr VariantType attributesMethod { VariantType::MethodTag{} };
  414. static constexpr VariantType attributesArray { VariantType::ArrayTag{} };
  415. static constexpr VariantType attributesString { VariantType::StringTag{} };
  416. static constexpr VariantType attributesBinary { VariantType::BinaryTag{} };
  417. static constexpr VariantType attributesObject { VariantType::ObjectTag{} };
  418. };
  419. constexpr var::VariantType var::Instance::attributesVoid;
  420. constexpr var::VariantType var::Instance::attributesUndefined;
  421. constexpr var::VariantType var::Instance::attributesInt;
  422. constexpr var::VariantType var::Instance::attributesInt64;
  423. constexpr var::VariantType var::Instance::attributesBool;
  424. constexpr var::VariantType var::Instance::attributesDouble;
  425. constexpr var::VariantType var::Instance::attributesMethod;
  426. constexpr var::VariantType var::Instance::attributesArray;
  427. constexpr var::VariantType var::Instance::attributesString;
  428. constexpr var::VariantType var::Instance::attributesBinary;
  429. constexpr var::VariantType var::Instance::attributesObject;
  430. //==============================================================================
  431. var::var() noexcept : type (&Instance::attributesVoid) {}
  432. var::var (const VariantType& t) noexcept : type (&t) {}
  433. var::~var() noexcept { type->cleanUp (value); }
  434. JUCE_DECLARE_DEPRECATED_STATIC (const var var::null;)
  435. //==============================================================================
  436. var::var (const var& valueToCopy) : type (valueToCopy.type)
  437. {
  438. type->createCopy (value, valueToCopy.value);
  439. }
  440. var::var (const int v) noexcept : type (&Instance::attributesInt) { value.intValue = v; }
  441. var::var (const int64 v) noexcept : type (&Instance::attributesInt64) { value.int64Value = v; }
  442. var::var (const bool v) noexcept : type (&Instance::attributesBool) { value.boolValue = v; }
  443. var::var (const double v) noexcept : type (&Instance::attributesDouble) { value.doubleValue = v; }
  444. var::var (NativeFunction m) noexcept : type (&Instance::attributesMethod) { value.methodValue = new NativeFunction (m); }
  445. var::var (const Array<var>& v) : type (&Instance::attributesArray) { value.objectValue = new VariantType::RefCountedArray (v); }
  446. var::var (const String& v) : type (&Instance::attributesString) { new (value.stringValue) String (v); }
  447. var::var (const char* const v) : type (&Instance::attributesString) { new (value.stringValue) String (v); }
  448. var::var (const wchar_t* const v) : type (&Instance::attributesString) { new (value.stringValue) String (v); }
  449. var::var (const void* v, size_t sz) : type (&Instance::attributesBinary) { value.binaryValue = new MemoryBlock (v, sz); }
  450. var::var (const MemoryBlock& v) : type (&Instance::attributesBinary) { value.binaryValue = new MemoryBlock (v); }
  451. var::var (const StringArray& v) : type (&Instance::attributesArray)
  452. {
  453. Array<var> strings;
  454. strings.ensureStorageAllocated (v.size());
  455. for (auto& i : v)
  456. strings.add (var (i));
  457. value.objectValue = new VariantType::RefCountedArray (strings);
  458. }
  459. var::var (ReferenceCountedObject* const object) : type (&Instance::attributesObject)
  460. {
  461. value.objectValue = object;
  462. if (object != nullptr)
  463. object->incReferenceCount();
  464. }
  465. var var::undefined() noexcept { return var (Instance::attributesUndefined); }
  466. //==============================================================================
  467. bool var::isVoid() const noexcept { return type->isVoid; }
  468. bool var::isUndefined() const noexcept { return type->isUndefined; }
  469. bool var::isInt() const noexcept { return type->isInt; }
  470. bool var::isInt64() const noexcept { return type->isInt64; }
  471. bool var::isBool() const noexcept { return type->isBool; }
  472. bool var::isDouble() const noexcept { return type->isDouble; }
  473. bool var::isString() const noexcept { return type->isString; }
  474. bool var::isObject() const noexcept { return type->isObject; }
  475. bool var::isArray() const noexcept { return type->isArray; }
  476. bool var::isBinaryData() const noexcept { return type->isBinary; }
  477. bool var::isMethod() const noexcept { return type->isMethod; }
  478. var::operator int() const noexcept { return type->toInt (value); }
  479. var::operator int64() const noexcept { return type->toInt64 (value); }
  480. var::operator bool() const noexcept { return type->toBool (value); }
  481. var::operator float() const noexcept { return (float) type->toDouble (value); }
  482. var::operator double() const noexcept { return type->toDouble (value); }
  483. String var::toString() const { return type->toString (value); }
  484. var::operator String() const { return type->toString (value); }
  485. ReferenceCountedObject* var::getObject() const noexcept { return type->toObject (value); }
  486. Array<var>* var::getArray() const noexcept { return type->toArray (value); }
  487. MemoryBlock* var::getBinaryData() const noexcept { return type->toBinary (value); }
  488. DynamicObject* var::getDynamicObject() const noexcept { return dynamic_cast<DynamicObject*> (getObject()); }
  489. //==============================================================================
  490. void var::swapWith (var& other) noexcept
  491. {
  492. std::swap (type, other.type);
  493. std::swap (value, other.value);
  494. }
  495. var& var::operator= (const var& v) { type->cleanUp (value); type = v.type; type->createCopy (value, v.value); return *this; }
  496. var& var::operator= (const int v) { type->cleanUp (value); type = &Instance::attributesInt; value.intValue = v; return *this; }
  497. var& var::operator= (const int64 v) { type->cleanUp (value); type = &Instance::attributesInt64; value.int64Value = v; return *this; }
  498. var& var::operator= (const bool v) { type->cleanUp (value); type = &Instance::attributesBool; value.boolValue = v; return *this; }
  499. var& var::operator= (const double v) { type->cleanUp (value); type = &Instance::attributesDouble; value.doubleValue = v; return *this; }
  500. var& var::operator= (const char* const v) { type->cleanUp (value); type = &Instance::attributesString; new (value.stringValue) String (v); return *this; }
  501. var& var::operator= (const wchar_t* const v) { type->cleanUp (value); type = &Instance::attributesString; new (value.stringValue) String (v); return *this; }
  502. var& var::operator= (const String& v) { type->cleanUp (value); type = &Instance::attributesString; new (value.stringValue) String (v); return *this; }
  503. var& var::operator= (const MemoryBlock& v) { type->cleanUp (value); type = &Instance::attributesBinary; value.binaryValue = new MemoryBlock (v); return *this; }
  504. var& var::operator= (const Array<var>& v) { var v2 (v); swapWith (v2); return *this; }
  505. var& var::operator= (ReferenceCountedObject* v) { var v2 (v); swapWith (v2); return *this; }
  506. var& var::operator= (NativeFunction v) { var v2 (v); swapWith (v2); return *this; }
  507. var::var (var&& other) noexcept
  508. : type (other.type),
  509. value (other.value)
  510. {
  511. other.type = &Instance::attributesVoid;
  512. }
  513. var& var::operator= (var&& other) noexcept
  514. {
  515. swapWith (other);
  516. return *this;
  517. }
  518. var::var (String&& v) : type (&Instance::attributesString)
  519. {
  520. new (value.stringValue) String (std::move (v));
  521. }
  522. var::var (MemoryBlock&& v) : type (&Instance::attributesBinary)
  523. {
  524. value.binaryValue = new MemoryBlock (std::move (v));
  525. }
  526. var::var (Array<var>&& v) : type (&Instance::attributesArray)
  527. {
  528. value.objectValue = new VariantType::RefCountedArray (std::move (v));
  529. }
  530. var& var::operator= (String&& v)
  531. {
  532. type->cleanUp (value);
  533. type = &Instance::attributesString;
  534. new (value.stringValue) String (std::move (v));
  535. return *this;
  536. }
  537. //==============================================================================
  538. bool var::equals (const var& other) const noexcept
  539. {
  540. return type->equals (value, other.value, *other.type);
  541. }
  542. bool var::equalsWithSameType (const var& other) const noexcept
  543. {
  544. return hasSameTypeAs (other) && equals (other);
  545. }
  546. bool var::hasSameTypeAs (const var& other) const noexcept
  547. {
  548. return type == other.type;
  549. }
  550. bool canCompare (const var& v1, const var& v2)
  551. {
  552. return v1.type->isComparable && v2.type->isComparable;
  553. }
  554. static int compare (const var& v1, const var& v2)
  555. {
  556. if (v1.isString() && v2.isString())
  557. return v1.toString().compare (v2.toString());
  558. auto diff = static_cast<double> (v1) - static_cast<double> (v2);
  559. return diff == 0 ? 0 : (diff < 0 ? -1 : 1);
  560. }
  561. bool operator== (const var& v1, const var& v2) { return v1.equals (v2); }
  562. bool operator!= (const var& v1, const var& v2) { return ! v1.equals (v2); }
  563. bool operator< (const var& v1, const var& v2) { return canCompare (v1, v2) && compare (v1, v2) < 0; }
  564. bool operator> (const var& v1, const var& v2) { return canCompare (v1, v2) && compare (v1, v2) > 0; }
  565. bool operator<= (const var& v1, const var& v2) { return canCompare (v1, v2) && compare (v1, v2) <= 0; }
  566. bool operator>= (const var& v1, const var& v2) { return canCompare (v1, v2) && compare (v1, v2) >= 0; }
  567. bool operator== (const var& v1, const String& v2) { return v1.toString() == v2; }
  568. bool operator!= (const var& v1, const String& v2) { return v1.toString() != v2; }
  569. bool operator== (const var& v1, const char* v2) { return v1.toString() == v2; }
  570. bool operator!= (const var& v1, const char* v2) { return v1.toString() != v2; }
  571. //==============================================================================
  572. var var::clone() const noexcept
  573. {
  574. return type->clone (*this);
  575. }
  576. //==============================================================================
  577. const var& var::operator[] (const Identifier& propertyName) const
  578. {
  579. if (auto* o = getDynamicObject())
  580. return o->getProperty (propertyName);
  581. return getNullVarRef();
  582. }
  583. const var& var::operator[] (const char* const propertyName) const
  584. {
  585. return operator[] (Identifier (propertyName));
  586. }
  587. var var::getProperty (const Identifier& propertyName, const var& defaultReturnValue) const
  588. {
  589. if (auto* o = getDynamicObject())
  590. return o->getProperties().getWithDefault (propertyName, defaultReturnValue);
  591. return defaultReturnValue;
  592. }
  593. bool var::hasProperty (const Identifier& propertyName) const noexcept
  594. {
  595. if (auto* o = getDynamicObject())
  596. return o->hasProperty (propertyName);
  597. return false;
  598. }
  599. var::NativeFunction var::getNativeFunction() const
  600. {
  601. return isMethod() && (value.methodValue != nullptr) ? *value.methodValue : nullptr;
  602. }
  603. var var::invoke (const Identifier& method, const var* arguments, int numArguments) const
  604. {
  605. if (auto* o = getDynamicObject())
  606. return o->invokeMethod (method, var::NativeFunctionArgs (*this, arguments, numArguments));
  607. return {};
  608. }
  609. var var::call (const Identifier& method) const
  610. {
  611. return invoke (method, nullptr, 0);
  612. }
  613. var var::call (const Identifier& method, const var& arg1) const
  614. {
  615. return invoke (method, &arg1, 1);
  616. }
  617. var var::call (const Identifier& method, const var& arg1, const var& arg2) const
  618. {
  619. var args[] = { arg1, arg2 };
  620. return invoke (method, args, 2);
  621. }
  622. var var::call (const Identifier& method, const var& arg1, const var& arg2, const var& arg3)
  623. {
  624. var args[] = { arg1, arg2, arg3 };
  625. return invoke (method, args, 3);
  626. }
  627. var var::call (const Identifier& method, const var& arg1, const var& arg2, const var& arg3, const var& arg4) const
  628. {
  629. var args[] = { arg1, arg2, arg3, arg4 };
  630. return invoke (method, args, 4);
  631. }
  632. var var::call (const Identifier& method, const var& arg1, const var& arg2, const var& arg3, const var& arg4, const var& arg5) const
  633. {
  634. var args[] = { arg1, arg2, arg3, arg4, arg5 };
  635. return invoke (method, args, 5);
  636. }
  637. //==============================================================================
  638. int var::size() const
  639. {
  640. if (auto array = getArray())
  641. return array->size();
  642. return 0;
  643. }
  644. const var& var::operator[] (int arrayIndex) const
  645. {
  646. auto array = getArray();
  647. // When using this method, the var must actually be an array, and the index
  648. // must be in-range!
  649. jassert (array != nullptr && isPositiveAndBelow (arrayIndex, array->size()));
  650. return array->getReference (arrayIndex);
  651. }
  652. var& var::operator[] (int arrayIndex)
  653. {
  654. auto array = getArray();
  655. // When using this method, the var must actually be an array, and the index
  656. // must be in-range!
  657. jassert (array != nullptr && isPositiveAndBelow (arrayIndex, array->size()));
  658. return array->getReference (arrayIndex);
  659. }
  660. Array<var>* var::convertToArray()
  661. {
  662. if (auto array = getArray())
  663. return array;
  664. Array<var> tempVar;
  665. if (! isVoid())
  666. tempVar.add (*this);
  667. *this = tempVar;
  668. return getArray();
  669. }
  670. void var::append (const var& n)
  671. {
  672. convertToArray()->add (n);
  673. }
  674. void var::remove (const int index)
  675. {
  676. if (auto array = getArray())
  677. array->remove (index);
  678. }
  679. void var::insert (const int index, const var& n)
  680. {
  681. convertToArray()->insert (index, n);
  682. }
  683. void var::resize (const int numArrayElementsWanted)
  684. {
  685. convertToArray()->resize (numArrayElementsWanted);
  686. }
  687. int var::indexOf (const var& n) const
  688. {
  689. if (auto array = getArray())
  690. return array->indexOf (n);
  691. return -1;
  692. }
  693. //==============================================================================
  694. void var::writeToStream (OutputStream& output) const
  695. {
  696. type->writeToStream (value, output);
  697. }
  698. var var::readFromStream (InputStream& input)
  699. {
  700. const int numBytes = input.readCompressedInt();
  701. if (numBytes > 0)
  702. {
  703. switch (input.readByte())
  704. {
  705. case varMarker_Int: return var (input.readInt());
  706. case varMarker_Int64: return var (input.readInt64());
  707. case varMarker_BoolTrue: return var (true);
  708. case varMarker_BoolFalse: return var (false);
  709. case varMarker_Double: return var (input.readDouble());
  710. case varMarker_String:
  711. {
  712. MemoryOutputStream mo;
  713. mo.writeFromInputStream (input, numBytes - 1);
  714. return var (mo.toUTF8());
  715. }
  716. case varMarker_Binary:
  717. {
  718. MemoryBlock mb ((size_t) numBytes - 1);
  719. if (numBytes > 1)
  720. {
  721. const int numRead = input.read (mb.getData(), numBytes - 1);
  722. mb.setSize ((size_t) numRead);
  723. }
  724. return var (mb);
  725. }
  726. case varMarker_Array:
  727. {
  728. var v;
  729. auto* destArray = v.convertToArray();
  730. for (int i = input.readCompressedInt(); --i >= 0;)
  731. destArray->add (readFromStream (input));
  732. return v;
  733. }
  734. default:
  735. input.skipNextBytes (numBytes - 1); break;
  736. }
  737. }
  738. return {};
  739. }
  740. var::NativeFunctionArgs::NativeFunctionArgs (const var& t, const var* args, int numArgs) noexcept
  741. : thisObject (t), arguments (args), numArguments (numArgs)
  742. {
  743. }
  744. } // namespace juce