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.

643 lines
27KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 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. enum VariantStreamMarkers
  19. {
  20. varMarker_Int = 1,
  21. varMarker_BoolTrue = 2,
  22. varMarker_BoolFalse = 3,
  23. varMarker_Double = 4,
  24. varMarker_String = 5,
  25. varMarker_Int64 = 6,
  26. varMarker_Array = 7
  27. };
  28. //==============================================================================
  29. class var::VariantType
  30. {
  31. public:
  32. VariantType() noexcept {}
  33. virtual ~VariantType() noexcept {}
  34. virtual int toInt (const ValueUnion&) const noexcept { return 0; }
  35. virtual int64 toInt64 (const ValueUnion&) const noexcept { return 0; }
  36. virtual double toDouble (const ValueUnion&) const noexcept { return 0; }
  37. virtual String toString (const ValueUnion&) const { return String::empty; }
  38. virtual bool toBool (const ValueUnion&) const noexcept { return false; }
  39. virtual ReferenceCountedObject* toObject (const ValueUnion&) const noexcept { return nullptr; }
  40. virtual Array<var>* toArray (const ValueUnion&) const noexcept { return 0; }
  41. virtual bool isVoid() const noexcept { return false; }
  42. virtual bool isInt() const noexcept { return false; }
  43. virtual bool isInt64() const noexcept { return false; }
  44. virtual bool isBool() const noexcept { return false; }
  45. virtual bool isDouble() const noexcept { return false; }
  46. virtual bool isString() const noexcept { return false; }
  47. virtual bool isObject() const noexcept { return false; }
  48. virtual bool isArray() const noexcept { return false; }
  49. virtual bool isMethod() const noexcept { return false; }
  50. virtual void cleanUp (ValueUnion&) const noexcept {}
  51. virtual void createCopy (ValueUnion& dest, const ValueUnion& source) const { dest = source; }
  52. virtual bool equals (const ValueUnion& data, const ValueUnion& otherData, const VariantType& otherType) const noexcept = 0;
  53. virtual void writeToStream (const ValueUnion& data, OutputStream& output) const = 0;
  54. };
  55. //==============================================================================
  56. class var::VariantType_Void : public var::VariantType
  57. {
  58. public:
  59. VariantType_Void() noexcept {}
  60. static const VariantType_Void instance;
  61. bool isVoid() const noexcept { return true; }
  62. bool equals (const ValueUnion&, const ValueUnion&, const VariantType& otherType) const noexcept { return otherType.isVoid(); }
  63. void writeToStream (const ValueUnion&, OutputStream& output) const { output.writeCompressedInt (0); }
  64. };
  65. //==============================================================================
  66. class var::VariantType_Int : public var::VariantType
  67. {
  68. public:
  69. VariantType_Int() noexcept {}
  70. static const VariantType_Int instance;
  71. int toInt (const ValueUnion& data) const noexcept { return data.intValue; };
  72. int64 toInt64 (const ValueUnion& data) const noexcept { return (int64) data.intValue; };
  73. double toDouble (const ValueUnion& data) const noexcept { return (double) data.intValue; }
  74. String toString (const ValueUnion& data) const { return String (data.intValue); }
  75. bool toBool (const ValueUnion& data) const noexcept { return data.intValue != 0; }
  76. bool isInt() const noexcept { return true; }
  77. bool equals (const ValueUnion& data, const ValueUnion& otherData, const VariantType& otherType) const noexcept
  78. {
  79. return otherType.toInt (otherData) == data.intValue;
  80. }
  81. void writeToStream (const ValueUnion& data, OutputStream& output) const
  82. {
  83. output.writeCompressedInt (5);
  84. output.writeByte (varMarker_Int);
  85. output.writeInt (data.intValue);
  86. }
  87. };
  88. //==============================================================================
  89. class var::VariantType_Int64 : public var::VariantType
  90. {
  91. public:
  92. VariantType_Int64() noexcept {}
  93. static const VariantType_Int64 instance;
  94. int toInt (const ValueUnion& data) const noexcept { return (int) data.int64Value; };
  95. int64 toInt64 (const ValueUnion& data) const noexcept { return data.int64Value; };
  96. double toDouble (const ValueUnion& data) const noexcept { return (double) data.int64Value; }
  97. String toString (const ValueUnion& data) const { return String (data.int64Value); }
  98. bool toBool (const ValueUnion& data) const noexcept { return data.int64Value != 0; }
  99. bool isInt64() const noexcept { return true; }
  100. bool equals (const ValueUnion& data, const ValueUnion& otherData, const VariantType& otherType) const noexcept
  101. {
  102. return otherType.toInt64 (otherData) == data.int64Value;
  103. }
  104. void writeToStream (const ValueUnion& data, OutputStream& output) const
  105. {
  106. output.writeCompressedInt (9);
  107. output.writeByte (varMarker_Int64);
  108. output.writeInt64 (data.int64Value);
  109. }
  110. };
  111. //==============================================================================
  112. class var::VariantType_Double : public var::VariantType
  113. {
  114. public:
  115. VariantType_Double() noexcept {}
  116. static const VariantType_Double instance;
  117. int toInt (const ValueUnion& data) const noexcept { return (int) data.doubleValue; };
  118. int64 toInt64 (const ValueUnion& data) const noexcept { return (int64) data.doubleValue; };
  119. double toDouble (const ValueUnion& data) const noexcept { return data.doubleValue; }
  120. String toString (const ValueUnion& data) const { return String (data.doubleValue); }
  121. bool toBool (const ValueUnion& data) const noexcept { return data.doubleValue != 0; }
  122. bool isDouble() const noexcept { return true; }
  123. bool equals (const ValueUnion& data, const ValueUnion& otherData, const VariantType& otherType) const noexcept
  124. {
  125. return std::abs (otherType.toDouble (otherData) - data.doubleValue) < std::numeric_limits<double>::epsilon();
  126. }
  127. void writeToStream (const ValueUnion& data, OutputStream& output) const
  128. {
  129. output.writeCompressedInt (9);
  130. output.writeByte (varMarker_Double);
  131. output.writeDouble (data.doubleValue);
  132. }
  133. };
  134. //==============================================================================
  135. class var::VariantType_Bool : public var::VariantType
  136. {
  137. public:
  138. VariantType_Bool() noexcept {}
  139. static const VariantType_Bool instance;
  140. int toInt (const ValueUnion& data) const noexcept { return data.boolValue ? 1 : 0; };
  141. int64 toInt64 (const ValueUnion& data) const noexcept { return data.boolValue ? 1 : 0; };
  142. double toDouble (const ValueUnion& data) const noexcept { return data.boolValue ? 1.0 : 0.0; }
  143. String toString (const ValueUnion& data) const { return String::charToString (data.boolValue ? (juce_wchar) '1' : (juce_wchar) '0'); }
  144. bool toBool (const ValueUnion& data) const noexcept { return data.boolValue; }
  145. bool isBool() const noexcept { return true; }
  146. bool equals (const ValueUnion& data, const ValueUnion& otherData, const VariantType& otherType) const noexcept
  147. {
  148. return otherType.toBool (otherData) == data.boolValue;
  149. }
  150. void writeToStream (const ValueUnion& data, OutputStream& output) const
  151. {
  152. output.writeCompressedInt (1);
  153. output.writeByte (data.boolValue ? (char) varMarker_BoolTrue : (char) varMarker_BoolFalse);
  154. }
  155. };
  156. //==============================================================================
  157. class var::VariantType_String : public var::VariantType
  158. {
  159. public:
  160. VariantType_String() noexcept {}
  161. static const VariantType_String instance;
  162. void cleanUp (ValueUnion& data) const noexcept { getString (data)-> ~String(); }
  163. void createCopy (ValueUnion& dest, const ValueUnion& source) const { new (dest.stringValue) String (*getString (source)); }
  164. bool isString() const noexcept { return true; }
  165. int toInt (const ValueUnion& data) const noexcept { return getString (data)->getIntValue(); };
  166. int64 toInt64 (const ValueUnion& data) const noexcept { return getString (data)->getLargeIntValue(); };
  167. double toDouble (const ValueUnion& data) const noexcept { return getString (data)->getDoubleValue(); }
  168. String toString (const ValueUnion& data) const { return *getString (data); }
  169. bool toBool (const ValueUnion& data) const noexcept { return getString (data)->getIntValue() != 0
  170. || getString (data)->trim().equalsIgnoreCase ("true")
  171. || getString (data)->trim().equalsIgnoreCase ("yes"); }
  172. bool equals (const ValueUnion& data, const ValueUnion& otherData, const VariantType& otherType) const noexcept
  173. {
  174. return otherType.toString (otherData) == *getString (data);
  175. }
  176. void writeToStream (const ValueUnion& data, OutputStream& output) const
  177. {
  178. const String* const s = getString (data);
  179. const int len = s->getNumBytesAsUTF8() + 1;
  180. HeapBlock<char> temp ((size_t) len);
  181. s->copyToUTF8 (temp, len);
  182. output.writeCompressedInt (len + 1);
  183. output.writeByte (varMarker_String);
  184. output.write (temp, len);
  185. }
  186. private:
  187. static inline const String* getString (const ValueUnion& data) noexcept { return reinterpret_cast <const String*> (data.stringValue); }
  188. static inline String* getString (ValueUnion& data) noexcept { return reinterpret_cast <String*> (data.stringValue); }
  189. };
  190. //==============================================================================
  191. class var::VariantType_Object : public var::VariantType
  192. {
  193. public:
  194. VariantType_Object() noexcept {}
  195. static const VariantType_Object instance;
  196. void cleanUp (ValueUnion& data) const noexcept { if (data.objectValue != nullptr) data.objectValue->decReferenceCount(); }
  197. void createCopy (ValueUnion& dest, const ValueUnion& source) const
  198. {
  199. dest.objectValue = source.objectValue;
  200. if (dest.objectValue != nullptr)
  201. dest.objectValue->incReferenceCount();
  202. }
  203. String toString (const ValueUnion& data) const { return "Object 0x" + String::toHexString ((int) (pointer_sized_int) data.objectValue); }
  204. bool toBool (const ValueUnion& data) const noexcept { return data.objectValue != 0; }
  205. ReferenceCountedObject* toObject (const ValueUnion& data) const noexcept { return data.objectValue; }
  206. bool isObject() const noexcept { return true; }
  207. bool equals (const ValueUnion& data, const ValueUnion& otherData, const VariantType& otherType) const noexcept
  208. {
  209. return otherType.toObject (otherData) == data.objectValue;
  210. }
  211. void writeToStream (const ValueUnion&, OutputStream& output) const
  212. {
  213. jassertfalse; // Can't write an object to a stream!
  214. output.writeCompressedInt (0);
  215. }
  216. };
  217. //==============================================================================
  218. class var::VariantType_Array : public var::VariantType
  219. {
  220. public:
  221. VariantType_Array() noexcept {}
  222. static const VariantType_Array instance;
  223. void cleanUp (ValueUnion& data) const noexcept { delete data.arrayValue; }
  224. void createCopy (ValueUnion& dest, const ValueUnion& source) const { dest.arrayValue = new Array<var> (*(source.arrayValue)); }
  225. String toString (const ValueUnion&) const { return "[Array]"; }
  226. bool isArray() const noexcept { return true; }
  227. Array<var>* toArray (const ValueUnion& data) const noexcept { return data.arrayValue; }
  228. bool equals (const ValueUnion& data, const ValueUnion& otherData, const VariantType& otherType) const noexcept
  229. {
  230. const Array<var>* const otherArray = otherType.toArray (otherData);
  231. return otherArray != nullptr && *otherArray == *(data.arrayValue);
  232. }
  233. void writeToStream (const ValueUnion& data, OutputStream& output) const
  234. {
  235. MemoryOutputStream buffer (512);
  236. const int numItems = data.arrayValue->size();
  237. buffer.writeCompressedInt (numItems);
  238. for (int i = 0; i < numItems; ++i)
  239. data.arrayValue->getReference(i).writeToStream (buffer);
  240. output.writeCompressedInt (1 + (int) buffer.getDataSize());
  241. output.writeByte (varMarker_Array);
  242. output << buffer;
  243. }
  244. };
  245. //==============================================================================
  246. class var::VariantType_Method : public var::VariantType
  247. {
  248. public:
  249. VariantType_Method() noexcept {}
  250. static const VariantType_Method instance;
  251. String toString (const ValueUnion&) const { return "Method"; }
  252. bool toBool (const ValueUnion& data) const noexcept { return data.methodValue != 0; }
  253. bool isMethod() const noexcept { return true; }
  254. bool equals (const ValueUnion& data, const ValueUnion& otherData, const VariantType& otherType) const noexcept
  255. {
  256. return otherType.isMethod() && otherData.methodValue == data.methodValue;
  257. }
  258. void writeToStream (const ValueUnion&, OutputStream& output) const
  259. {
  260. jassertfalse; // Can't write a method to a stream!
  261. output.writeCompressedInt (0);
  262. }
  263. };
  264. //==============================================================================
  265. const var::VariantType_Void var::VariantType_Void::instance;
  266. const var::VariantType_Int var::VariantType_Int::instance;
  267. const var::VariantType_Int64 var::VariantType_Int64::instance;
  268. const var::VariantType_Bool var::VariantType_Bool::instance;
  269. const var::VariantType_Double var::VariantType_Double::instance;
  270. const var::VariantType_String var::VariantType_String::instance;
  271. const var::VariantType_Object var::VariantType_Object::instance;
  272. const var::VariantType_Array var::VariantType_Array::instance;
  273. const var::VariantType_Method var::VariantType_Method::instance;
  274. //==============================================================================
  275. var::var() noexcept : type (&VariantType_Void::instance)
  276. {
  277. }
  278. var::~var() noexcept
  279. {
  280. type->cleanUp (value);
  281. }
  282. const var var::null;
  283. //==============================================================================
  284. var::var (const var& valueToCopy) : type (valueToCopy.type)
  285. {
  286. type->createCopy (value, valueToCopy.value);
  287. }
  288. var::var (const int v) noexcept : type (&VariantType_Int::instance) { value.intValue = v; }
  289. var::var (const int64 v) noexcept : type (&VariantType_Int64::instance) { value.int64Value = v; }
  290. var::var (const bool v) noexcept : type (&VariantType_Bool::instance) { value.boolValue = v; }
  291. var::var (const double v) noexcept : type (&VariantType_Double::instance) { value.doubleValue = v; }
  292. var::var (MethodFunction m) noexcept : type (&VariantType_Method::instance) { value.methodValue = m; }
  293. var::var (const Array<var>& v) : type (&VariantType_Array::instance) { value.arrayValue = new Array<var> (v); }
  294. var::var (const String& v) : type (&VariantType_String::instance) { new (value.stringValue) String (v); }
  295. var::var (const char* const v) : type (&VariantType_String::instance) { new (value.stringValue) String (v); }
  296. var::var (const wchar_t* const v) : type (&VariantType_String::instance) { new (value.stringValue) String (v); }
  297. var::var (ReferenceCountedObject* const object) : type (&VariantType_Object::instance)
  298. {
  299. value.objectValue = object;
  300. if (object != nullptr)
  301. object->incReferenceCount();
  302. }
  303. //==============================================================================
  304. bool var::isVoid() const noexcept { return type->isVoid(); }
  305. bool var::isInt() const noexcept { return type->isInt(); }
  306. bool var::isInt64() const noexcept { return type->isInt64(); }
  307. bool var::isBool() const noexcept { return type->isBool(); }
  308. bool var::isDouble() const noexcept { return type->isDouble(); }
  309. bool var::isString() const noexcept { return type->isString(); }
  310. bool var::isObject() const noexcept { return type->isObject(); }
  311. bool var::isArray() const noexcept { return type->isArray(); }
  312. bool var::isMethod() const noexcept { return type->isMethod(); }
  313. var::operator int() const noexcept { return type->toInt (value); }
  314. var::operator int64() const noexcept { return type->toInt64 (value); }
  315. var::operator bool() const noexcept { return type->toBool (value); }
  316. var::operator float() const noexcept { return (float) type->toDouble (value); }
  317. var::operator double() const noexcept { return type->toDouble (value); }
  318. String var::toString() const { return type->toString (value); }
  319. var::operator String() const { return type->toString (value); }
  320. ReferenceCountedObject* var::getObject() const noexcept { return type->toObject (value); }
  321. Array<var>* var::getArray() const noexcept { return type->toArray (value); }
  322. DynamicObject* var::getDynamicObject() const noexcept { return dynamic_cast <DynamicObject*> (getObject()); }
  323. //==============================================================================
  324. void var::swapWith (var& other) noexcept
  325. {
  326. std::swap (type, other.type);
  327. std::swap (value, other.value);
  328. }
  329. var& var::operator= (const var& v) { type->cleanUp (value); type = v.type; type->createCopy (value, v.value); return *this; }
  330. var& var::operator= (const int v) { type->cleanUp (value); type = &VariantType_Int::instance; value.intValue = v; return *this; }
  331. var& var::operator= (const int64 v) { type->cleanUp (value); type = &VariantType_Int64::instance; value.int64Value = v; return *this; }
  332. var& var::operator= (const bool v) { type->cleanUp (value); type = &VariantType_Bool::instance; value.boolValue = v; return *this; }
  333. var& var::operator= (const double v) { type->cleanUp (value); type = &VariantType_Double::instance; value.doubleValue = v; return *this; }
  334. var& var::operator= (const char* const v) { type->cleanUp (value); type = &VariantType_String::instance; new (value.stringValue) String (v); return *this; }
  335. var& var::operator= (const wchar_t* const v) { type->cleanUp (value); type = &VariantType_String::instance; new (value.stringValue) String (v); return *this; }
  336. var& var::operator= (const String& v) { type->cleanUp (value); type = &VariantType_String::instance; new (value.stringValue) String (v); return *this; }
  337. var& var::operator= (const Array<var>& v) { var v2 (v); swapWith (v2); return *this; }
  338. var& var::operator= (ReferenceCountedObject* v) { var v2 (v); swapWith (v2); return *this; }
  339. var& var::operator= (MethodFunction v) { var v2 (v); swapWith (v2); return *this; }
  340. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  341. var::var (var&& other) noexcept
  342. : type (other.type),
  343. value (other.value)
  344. {
  345. other.type = &VariantType_Void::instance;
  346. }
  347. var& var::operator= (var&& other) noexcept
  348. {
  349. swapWith (other);
  350. return *this;
  351. }
  352. var::var (String&& v) : type (&VariantType_String::instance)
  353. {
  354. new (value.stringValue) String (static_cast<String&&> (v));
  355. }
  356. var& var::operator= (String&& v)
  357. {
  358. type->cleanUp (value);
  359. type = &VariantType_String::instance;
  360. new (value.stringValue) String (static_cast<String&&> (v));
  361. return *this;
  362. }
  363. #endif
  364. //==============================================================================
  365. bool var::equals (const var& other) const noexcept
  366. {
  367. return type->equals (value, other.value, *other.type);
  368. }
  369. bool var::equalsWithSameType (const var& other) const noexcept
  370. {
  371. return type == other.type && equals (other);
  372. }
  373. bool operator== (const var& v1, const var& v2) noexcept { return v1.equals (v2); }
  374. bool operator!= (const var& v1, const var& v2) noexcept { return ! v1.equals (v2); }
  375. bool operator== (const var& v1, const String& v2) { return v1.toString() == v2; }
  376. bool operator!= (const var& v1, const String& v2) { return v1.toString() != v2; }
  377. bool operator== (const var& v1, const char* const v2) { return v1.toString() == v2; }
  378. bool operator!= (const var& v1, const char* const v2) { return v1.toString() != v2; }
  379. //==============================================================================
  380. var var::operator[] (const Identifier& propertyName) const
  381. {
  382. DynamicObject* const o = getDynamicObject();
  383. return o != nullptr ? o->getProperty (propertyName) : var::null;
  384. }
  385. var var::operator[] (const char* const propertyName) const
  386. {
  387. return operator[] (Identifier (propertyName));
  388. }
  389. var var::getProperty (const Identifier& propertyName, const var& defaultReturnValue) const
  390. {
  391. DynamicObject* const o = getDynamicObject();
  392. return o != nullptr ? o->getProperties().getWithDefault (propertyName, defaultReturnValue) : defaultReturnValue;
  393. }
  394. var var::invoke (const Identifier& method, const var* arguments, int numArguments) const
  395. {
  396. DynamicObject* const o = getDynamicObject();
  397. return o != nullptr ? o->invokeMethod (method, arguments, numArguments) : var::null;
  398. }
  399. var var::invokeMethod (DynamicObject* const target, const var* const arguments, const int numArguments) const
  400. {
  401. jassert (target != nullptr);
  402. if (isMethod())
  403. return (target->*(value.methodValue)) (arguments, numArguments);
  404. return var::null;
  405. }
  406. var var::call (const Identifier& method) const
  407. {
  408. return invoke (method, nullptr, 0);
  409. }
  410. var var::call (const Identifier& method, const var& arg1) const
  411. {
  412. return invoke (method, &arg1, 1);
  413. }
  414. var var::call (const Identifier& method, const var& arg1, const var& arg2) const
  415. {
  416. var args[] = { arg1, arg2 };
  417. return invoke (method, args, 2);
  418. }
  419. var var::call (const Identifier& method, const var& arg1, const var& arg2, const var& arg3)
  420. {
  421. var args[] = { arg1, arg2, arg3 };
  422. return invoke (method, args, 3);
  423. }
  424. var var::call (const Identifier& method, const var& arg1, const var& arg2, const var& arg3, const var& arg4) const
  425. {
  426. var args[] = { arg1, arg2, arg3, arg4 };
  427. return invoke (method, args, 4);
  428. }
  429. var var::call (const Identifier& method, const var& arg1, const var& arg2, const var& arg3, const var& arg4, const var& arg5) const
  430. {
  431. var args[] = { arg1, arg2, arg3, arg4, arg5 };
  432. return invoke (method, args, 5);
  433. }
  434. //==============================================================================
  435. int var::size() const
  436. {
  437. const Array<var>* const array = getArray();
  438. return array != nullptr ? array->size() : 0;
  439. }
  440. const var& var::operator[] (int arrayIndex) const
  441. {
  442. const Array<var>* const array = getArray();
  443. // When using this method, the var must actually be an array, and the index
  444. // must be in-range!
  445. jassert (array != nullptr && isPositiveAndBelow (arrayIndex, array->size()));
  446. return array->getReference (arrayIndex);
  447. }
  448. var& var::operator[] (int arrayIndex)
  449. {
  450. const Array<var>* const array = getArray();
  451. // When using this method, the var must actually be an array, and the index
  452. // must be in-range!
  453. jassert (array != nullptr && isPositiveAndBelow (arrayIndex, array->size()));
  454. return array->getReference (arrayIndex);
  455. }
  456. Array<var>* var::convertToArray()
  457. {
  458. Array<var>* array = getArray();
  459. if (array == nullptr)
  460. {
  461. const Array<var> tempVar;
  462. var v (tempVar);
  463. array = v.value.arrayValue;
  464. if (! isVoid())
  465. array->add (*this);
  466. swapWith (v);
  467. }
  468. return array;
  469. }
  470. void var::append (const var& n)
  471. {
  472. convertToArray()->add (n);
  473. }
  474. void var::remove (const int index)
  475. {
  476. Array<var>* const array = getArray();
  477. if (array != nullptr)
  478. array->remove (index);
  479. }
  480. void var::insert (const int index, const var& n)
  481. {
  482. convertToArray()->insert (index, n);
  483. }
  484. void var::resize (const int numArrayElementsWanted)
  485. {
  486. convertToArray()->resize (numArrayElementsWanted);
  487. }
  488. int var::indexOf (const var& n) const
  489. {
  490. const Array<var>* const array = getArray();
  491. return array != nullptr ? array->indexOf (n) : -1;
  492. }
  493. //==============================================================================
  494. void var::writeToStream (OutputStream& output) const
  495. {
  496. type->writeToStream (value, output);
  497. }
  498. var var::readFromStream (InputStream& input)
  499. {
  500. const int numBytes = input.readCompressedInt();
  501. if (numBytes > 0)
  502. {
  503. switch (input.readByte())
  504. {
  505. case varMarker_Int: return var (input.readInt());
  506. case varMarker_Int64: return var (input.readInt64());
  507. case varMarker_BoolTrue: return var (true);
  508. case varMarker_BoolFalse: return var (false);
  509. case varMarker_Double: return var (input.readDouble());
  510. case varMarker_String:
  511. {
  512. MemoryOutputStream mo;
  513. mo.writeFromInputStream (input, numBytes - 1);
  514. return var (mo.toUTF8());
  515. }
  516. case varMarker_Array:
  517. {
  518. var v;
  519. Array<var>* const destArray = v.convertToArray();
  520. for (int i = input.readCompressedInt(); --i >= 0;)
  521. destArray->add (readFromStream (input));
  522. return v;
  523. }
  524. default:
  525. input.skipNextBytes (numBytes - 1); break;
  526. }
  527. }
  528. return var::null;
  529. }