Audio plugin host https://kx.studio/carla
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.

172 lines
6.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the Water library.
  4. Copyright (c) 2016 ROLI Ltd.
  5. Copyright (C) 2017-2022 Filipe Coelho <falktx@falktx.com>
  6. Permission is granted to use this software under the terms of the ISC license
  7. http://www.isc.org/downloads/software-support-policy/isc-license/
  8. Permission to use, copy, modify, and/or distribute this software for any
  9. purpose with or without fee is hereby granted, provided that the above
  10. copyright notice and this permission notice appear in all copies.
  11. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  12. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  13. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  14. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  15. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  16. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  17. OF THIS SOFTWARE.
  18. ==============================================================================
  19. */
  20. #ifndef WATER_VARIANT_H_INCLUDED
  21. #define WATER_VARIANT_H_INCLUDED
  22. #include "../text/String.h"
  23. namespace water {
  24. //==============================================================================
  25. /**
  26. A variant class, that can be used to hold a range of primitive values.
  27. A var object can hold a range of simple primitive values, strings, or
  28. any kind of ReferenceCountedObject. The var class is intended to act like
  29. the kind of values used in dynamic scripting languages.
  30. You can save/load var objects either in a small, proprietary binary format
  31. using writeToStream()/readFromStream(), or as JSON by using the JSON class.
  32. @see JSON, DynamicObject
  33. */
  34. class var
  35. {
  36. public:
  37. //==============================================================================
  38. /** Creates a void variant. */
  39. var() noexcept;
  40. /** Destructor. */
  41. ~var() noexcept;
  42. var (const var& valueToCopy);
  43. var (int value) noexcept;
  44. var (int64 value) noexcept;
  45. var (bool value) noexcept;
  46. var (double value) noexcept;
  47. var (const char* value);
  48. var (const String& value);
  49. var& operator= (const var& valueToCopy);
  50. var& operator= (int value);
  51. var& operator= (int64 value);
  52. var& operator= (bool value);
  53. var& operator= (double value);
  54. var& operator= (const char* value);
  55. var& operator= (const String& value);
  56. void swapWith (var& other) noexcept;
  57. /** Returns a var object that can be used where you need the javascript "undefined" value. */
  58. static var undefined() noexcept;
  59. //==============================================================================
  60. operator int() const noexcept;
  61. operator int64() const noexcept;
  62. operator bool() const noexcept;
  63. operator float() const noexcept;
  64. operator double() const noexcept;
  65. operator String() const;
  66. String toString() const;
  67. //==============================================================================
  68. bool isVoid() const noexcept;
  69. bool isUndefined() const noexcept;
  70. bool isInt() const noexcept;
  71. bool isInt64() const noexcept;
  72. bool isBool() const noexcept;
  73. bool isDouble() const noexcept;
  74. bool isString() const noexcept;
  75. /** Returns true if this var has the same value as the one supplied.
  76. Note that this ignores the type, so a string var "123" and an integer var with the
  77. value 123 are considered to be equal.
  78. @see equalsWithSameType
  79. */
  80. bool equals (const var& other) const noexcept;
  81. /** Returns true if this var has the same value and type as the one supplied.
  82. This differs from equals() because e.g. "123" and 123 will be considered different.
  83. @see equals
  84. */
  85. bool equalsWithSameType (const var& other) const noexcept;
  86. /** Returns true if this var has the same type as the one supplied. */
  87. bool hasSameTypeAs (const var& other) const noexcept;
  88. /** Returns a deep copy of this object.
  89. For simple types this just returns a copy, but if the object contains any arrays
  90. or DynamicObjects, they will be cloned (recursively).
  91. */
  92. var clone() const noexcept;
  93. private:
  94. //==============================================================================
  95. class VariantType; friend class VariantType;
  96. class VariantType_Void; friend class VariantType_Void;
  97. class VariantType_Undefined; friend class VariantType_Undefined;
  98. class VariantType_Int; friend class VariantType_Int;
  99. class VariantType_Int64; friend class VariantType_Int64;
  100. class VariantType_Double; friend class VariantType_Double;
  101. class VariantType_Bool; friend class VariantType_Bool;
  102. class VariantType_String; friend class VariantType_String;
  103. union ValueUnion
  104. {
  105. int intValue;
  106. int64 int64Value;
  107. bool boolValue;
  108. double doubleValue;
  109. char stringValue [sizeof (String)];
  110. };
  111. const VariantType* type;
  112. ValueUnion value;
  113. var (const VariantType&) noexcept;
  114. };
  115. /** Compares the values of two var objects, using the var::equals() comparison. */
  116. bool operator== (const var&, const var&) noexcept;
  117. /** Compares the values of two var objects, using the var::equals() comparison. */
  118. bool operator!= (const var&, const var&) noexcept;
  119. bool operator== (const var&, const String&);
  120. bool operator!= (const var&, const String&);
  121. bool operator== (const var&, const char*);
  122. bool operator!= (const var&, const char*);
  123. //==============================================================================
  124. /** This template-overloaded class can be used to convert between var and custom types. */
  125. template <typename Type>
  126. struct VariantConverter
  127. {
  128. static Type fromVar (const var& v) { return static_cast<Type> (v); }
  129. static var toVar (const Type& t) { return t; }
  130. };
  131. /** This template-overloaded class can be used to convert between var and custom types. */
  132. template <>
  133. struct VariantConverter<String>
  134. {
  135. static String fromVar (const var& v) { return v.toString(); }
  136. static var toVar (const String& s) { return s; }
  137. };
  138. }
  139. #endif // WATER_VARIANT_H_INCLUDED