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.

648 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 value_) noexcept : type (&VariantType_Int::instance) { value.intValue = value_; }
  289. var::var (const int64 value_) noexcept : type (&VariantType_Int64::instance) { value.int64Value = value_; }
  290. var::var (const bool value_) noexcept : type (&VariantType_Bool::instance) { value.boolValue = value_; }
  291. var::var (const double value_) noexcept : type (&VariantType_Double::instance) { value.doubleValue = value_; }
  292. var::var (MethodFunction method_) noexcept : type (&VariantType_Method::instance) { value.methodValue = method_; }
  293. var::var (const String& value_) : type (&VariantType_String::instance) { new (value.stringValue) String (value_); }
  294. var::var (const char* const value_) : type (&VariantType_String::instance) { new (value.stringValue) String (value_); }
  295. var::var (const wchar_t* const value_) : type (&VariantType_String::instance) { new (value.stringValue) String (value_); }
  296. var::var (const Array<var>& value_) : type (&VariantType_Array::instance)
  297. {
  298. value.arrayValue = new Array<var> (value_);
  299. }
  300. var::var (ReferenceCountedObject* const object) : type (&VariantType_Object::instance)
  301. {
  302. value.objectValue = object;
  303. if (object != nullptr)
  304. object->incReferenceCount();
  305. }
  306. //==============================================================================
  307. bool var::isVoid() const noexcept { return type->isVoid(); }
  308. bool var::isInt() const noexcept { return type->isInt(); }
  309. bool var::isInt64() const noexcept { return type->isInt64(); }
  310. bool var::isBool() const noexcept { return type->isBool(); }
  311. bool var::isDouble() const noexcept { return type->isDouble(); }
  312. bool var::isString() const noexcept { return type->isString(); }
  313. bool var::isObject() const noexcept { return type->isObject(); }
  314. bool var::isArray() const noexcept { return type->isArray(); }
  315. bool var::isMethod() const noexcept { return type->isMethod(); }
  316. var::operator int() const noexcept { return type->toInt (value); }
  317. var::operator int64() const noexcept { return type->toInt64 (value); }
  318. var::operator bool() const noexcept { return type->toBool (value); }
  319. var::operator float() const noexcept { return (float) type->toDouble (value); }
  320. var::operator double() const noexcept { return type->toDouble (value); }
  321. String var::toString() const { return type->toString (value); }
  322. var::operator String() const { return type->toString (value); }
  323. ReferenceCountedObject* var::getObject() const noexcept { return type->toObject (value); }
  324. Array<var>* var::getArray() const noexcept { return type->toArray (value); }
  325. DynamicObject* var::getDynamicObject() const noexcept { return dynamic_cast <DynamicObject*> (getObject()); }
  326. //==============================================================================
  327. void var::swapWith (var& other) noexcept
  328. {
  329. std::swap (type, other.type);
  330. std::swap (value, other.value);
  331. }
  332. var& var::operator= (const var& v) { type->cleanUp (value); type = v.type; type->createCopy (value, v.value); return *this; }
  333. var& var::operator= (const int v) { type->cleanUp (value); type = &VariantType_Int::instance; value.intValue = v; return *this; }
  334. var& var::operator= (const int64 v) { type->cleanUp (value); type = &VariantType_Int64::instance; value.int64Value = v; return *this; }
  335. var& var::operator= (const bool v) { type->cleanUp (value); type = &VariantType_Bool::instance; value.boolValue = v; return *this; }
  336. var& var::operator= (const double v) { type->cleanUp (value); type = &VariantType_Double::instance; value.doubleValue = v; return *this; }
  337. var& var::operator= (const char* const v) { type->cleanUp (value); type = &VariantType_String::instance; new (value.stringValue) String (v); return *this; }
  338. var& var::operator= (const wchar_t* const v) { type->cleanUp (value); type = &VariantType_String::instance; new (value.stringValue) String (v); return *this; }
  339. var& var::operator= (const String& v) { type->cleanUp (value); type = &VariantType_String::instance; new (value.stringValue) String (v); return *this; }
  340. var& var::operator= (const Array<var>& v) { var v2 (v); swapWith (v2); return *this; }
  341. var& var::operator= (ReferenceCountedObject* v) { var v2 (v); swapWith (v2); return *this; }
  342. var& var::operator= (MethodFunction v) { var v2 (v); swapWith (v2); return *this; }
  343. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  344. var::var (var&& other) noexcept
  345. : type (other.type),
  346. value (other.value)
  347. {
  348. other.type = &VariantType_Void::instance;
  349. }
  350. var& var::operator= (var&& other) noexcept
  351. {
  352. swapWith (other);
  353. return *this;
  354. }
  355. var::var (String&& value_) : type (&VariantType_String::instance)
  356. {
  357. new (value.stringValue) String (static_cast<String&&> (value_));
  358. }
  359. var& var::operator= (String&& v)
  360. {
  361. type->cleanUp (value);
  362. type = &VariantType_String::instance;
  363. new (value.stringValue) String (static_cast<String&&> (v));
  364. return *this;
  365. }
  366. #endif
  367. //==============================================================================
  368. bool var::equals (const var& other) const noexcept
  369. {
  370. return type->equals (value, other.value, *other.type);
  371. }
  372. bool var::equalsWithSameType (const var& other) const noexcept
  373. {
  374. return type == other.type && equals (other);
  375. }
  376. bool operator== (const var& v1, const var& v2) noexcept { return v1.equals (v2); }
  377. bool operator!= (const var& v1, const var& v2) noexcept { return ! v1.equals (v2); }
  378. bool operator== (const var& v1, const String& v2) { return v1.toString() == v2; }
  379. bool operator!= (const var& v1, const String& v2) { return v1.toString() != v2; }
  380. bool operator== (const var& v1, const char* const v2) { return v1.toString() == v2; }
  381. bool operator!= (const var& v1, const char* const v2) { return v1.toString() != v2; }
  382. //==============================================================================
  383. var var::operator[] (const Identifier& propertyName) const
  384. {
  385. DynamicObject* const o = getDynamicObject();
  386. return o != nullptr ? o->getProperty (propertyName) : var::null;
  387. }
  388. var var::operator[] (const char* const propertyName) const
  389. {
  390. return operator[] (Identifier (propertyName));
  391. }
  392. var var::getProperty (const Identifier& propertyName, const var& defaultReturnValue) const
  393. {
  394. DynamicObject* const o = getDynamicObject();
  395. return o != nullptr ? o->getProperties().getWithDefault (propertyName, defaultReturnValue) : defaultReturnValue;
  396. }
  397. var var::invoke (const Identifier& method, const var* arguments, int numArguments) const
  398. {
  399. DynamicObject* const o = getDynamicObject();
  400. return o != nullptr ? o->invokeMethod (method, arguments, numArguments) : var::null;
  401. }
  402. var var::invokeMethod (DynamicObject* const target, const var* const arguments, const int numArguments) const
  403. {
  404. jassert (target != nullptr);
  405. if (isMethod())
  406. return (target->*(value.methodValue)) (arguments, numArguments);
  407. return var::null;
  408. }
  409. var var::call (const Identifier& method) const
  410. {
  411. return invoke (method, nullptr, 0);
  412. }
  413. var var::call (const Identifier& method, const var& arg1) const
  414. {
  415. return invoke (method, &arg1, 1);
  416. }
  417. var var::call (const Identifier& method, const var& arg1, const var& arg2) const
  418. {
  419. var args[] = { arg1, arg2 };
  420. return invoke (method, args, 2);
  421. }
  422. var var::call (const Identifier& method, const var& arg1, const var& arg2, const var& arg3)
  423. {
  424. var args[] = { arg1, arg2, arg3 };
  425. return invoke (method, args, 3);
  426. }
  427. var var::call (const Identifier& method, const var& arg1, const var& arg2, const var& arg3, const var& arg4) const
  428. {
  429. var args[] = { arg1, arg2, arg3, arg4 };
  430. return invoke (method, args, 4);
  431. }
  432. var var::call (const Identifier& method, const var& arg1, const var& arg2, const var& arg3, const var& arg4, const var& arg5) const
  433. {
  434. var args[] = { arg1, arg2, arg3, arg4, arg5 };
  435. return invoke (method, args, 5);
  436. }
  437. //==============================================================================
  438. int var::size() const
  439. {
  440. const Array<var>* const array = getArray();
  441. return array != nullptr ? array->size() : 0;
  442. }
  443. const var& var::operator[] (int arrayIndex) const
  444. {
  445. const Array<var>* const array = getArray();
  446. // When using this method, the var must actually be an array, and the index
  447. // must be in-range!
  448. jassert (array != nullptr && isPositiveAndBelow (arrayIndex, array->size()));
  449. return array->getReference (arrayIndex);
  450. }
  451. var& var::operator[] (int arrayIndex)
  452. {
  453. const Array<var>* const array = getArray();
  454. // When using this method, the var must actually be an array, and the index
  455. // must be in-range!
  456. jassert (array != nullptr && isPositiveAndBelow (arrayIndex, array->size()));
  457. return array->getReference (arrayIndex);
  458. }
  459. Array<var>* var::convertToArray()
  460. {
  461. Array<var>* array = getArray();
  462. if (array == nullptr)
  463. {
  464. const Array<var> tempVar;
  465. var v (tempVar);
  466. array = v.value.arrayValue;
  467. if (! isVoid())
  468. array->add (*this);
  469. swapWith (v);
  470. }
  471. return array;
  472. }
  473. void var::append (const var& n)
  474. {
  475. convertToArray()->add (n);
  476. }
  477. void var::remove (const int index)
  478. {
  479. Array<var>* const array = getArray();
  480. if (array != nullptr)
  481. array->remove (index);
  482. }
  483. void var::insert (const int index, const var& n)
  484. {
  485. convertToArray()->insert (index, n);
  486. }
  487. void var::resize (const int numArrayElementsWanted)
  488. {
  489. convertToArray()->resize (numArrayElementsWanted);
  490. }
  491. int var::indexOf (const var& n) const
  492. {
  493. const Array<var>* const array = getArray();
  494. return array != nullptr ? array->indexOf (n) : -1;
  495. }
  496. //==============================================================================
  497. void var::writeToStream (OutputStream& output) const
  498. {
  499. type->writeToStream (value, output);
  500. }
  501. var var::readFromStream (InputStream& input)
  502. {
  503. const int numBytes = input.readCompressedInt();
  504. if (numBytes > 0)
  505. {
  506. switch (input.readByte())
  507. {
  508. case varMarker_Int: return var (input.readInt());
  509. case varMarker_Int64: return var (input.readInt64());
  510. case varMarker_BoolTrue: return var (true);
  511. case varMarker_BoolFalse: return var (false);
  512. case varMarker_Double: return var (input.readDouble());
  513. case varMarker_String:
  514. {
  515. MemoryOutputStream mo;
  516. mo.writeFromInputStream (input, numBytes - 1);
  517. return var (mo.toUTF8());
  518. }
  519. case varMarker_Array:
  520. {
  521. var v;
  522. Array<var>* const destArray = v.convertToArray();
  523. for (int i = input.readCompressedInt(); --i >= 0;)
  524. destArray->add (readFromStream (input));
  525. return v;
  526. }
  527. default:
  528. input.skipNextBytes (numBytes - 1); break;
  529. }
  530. }
  531. return var::null;
  532. }