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.

Variant.h 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. ==============================================================================
  3. This file is part of the Water library.
  4. Copyright (c) 2016 ROLI Ltd.
  5. Copyright (C) 2017 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 "../water.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. #if WATER_COMPILER_SUPPORTS_MOVE_SEMANTICS
  57. var (var&&) noexcept;
  58. var (String&&);
  59. var& operator= (var&&) noexcept;
  60. var& operator= (String&&);
  61. #endif
  62. void swapWith (var& other) noexcept;
  63. /** Returns a var object that can be used where you need the javascript "undefined" value. */
  64. static var undefined() noexcept;
  65. //==============================================================================
  66. operator int() const noexcept;
  67. operator int64() const noexcept;
  68. operator bool() const noexcept;
  69. operator float() const noexcept;
  70. operator double() const noexcept;
  71. operator String() const;
  72. String toString() const;
  73. //==============================================================================
  74. bool isVoid() const noexcept;
  75. bool isUndefined() const noexcept;
  76. bool isInt() const noexcept;
  77. bool isInt64() const noexcept;
  78. bool isBool() const noexcept;
  79. bool isDouble() const noexcept;
  80. bool isString() const noexcept;
  81. /** Returns true if this var has the same value as the one supplied.
  82. Note that this ignores the type, so a string var "123" and an integer var with the
  83. value 123 are considered to be equal.
  84. @see equalsWithSameType
  85. */
  86. bool equals (const var& other) const noexcept;
  87. /** Returns true if this var has the same value and type as the one supplied.
  88. This differs from equals() because e.g. "123" and 123 will be considered different.
  89. @see equals
  90. */
  91. bool equalsWithSameType (const var& other) const noexcept;
  92. /** Returns true if this var has the same type as the one supplied. */
  93. bool hasSameTypeAs (const var& other) const noexcept;
  94. /** Returns a deep copy of this object.
  95. For simple types this just returns a copy, but if the object contains any arrays
  96. or DynamicObjects, they will be cloned (recursively).
  97. */
  98. var clone() const noexcept;
  99. private:
  100. //==============================================================================
  101. class VariantType; friend class VariantType;
  102. class VariantType_Void; friend class VariantType_Void;
  103. class VariantType_Undefined; friend class VariantType_Undefined;
  104. class VariantType_Int; friend class VariantType_Int;
  105. class VariantType_Int64; friend class VariantType_Int64;
  106. class VariantType_Double; friend class VariantType_Double;
  107. class VariantType_Bool; friend class VariantType_Bool;
  108. class VariantType_String; friend class VariantType_String;
  109. union ValueUnion
  110. {
  111. int intValue;
  112. int64 int64Value;
  113. bool boolValue;
  114. double doubleValue;
  115. char stringValue [sizeof (String)];
  116. };
  117. const VariantType* type;
  118. ValueUnion value;
  119. var (const VariantType&) noexcept;
  120. };
  121. /** Compares the values of two var objects, using the var::equals() comparison. */
  122. bool operator== (const var&, const var&) noexcept;
  123. /** Compares the values of two var objects, using the var::equals() comparison. */
  124. bool operator!= (const var&, const var&) noexcept;
  125. bool operator== (const var&, const String&);
  126. bool operator!= (const var&, const String&);
  127. bool operator== (const var&, const char*);
  128. bool operator!= (const var&, const char*);
  129. //==============================================================================
  130. /** This template-overloaded class can be used to convert between var and custom types. */
  131. template <typename Type>
  132. struct VariantConverter
  133. {
  134. static Type fromVar (const var& v) { return static_cast<Type> (v); }
  135. static var toVar (const Type& t) { return t; }
  136. };
  137. /** This template-overloaded class can be used to convert between var and custom types. */
  138. template <>
  139. struct VariantConverter<String>
  140. {
  141. static String fromVar (const var& v) { return v.toString(); }
  142. static var toVar (const String& s) { return s; }
  143. };
  144. }
  145. #endif // WATER_VARIANT_H_INCLUDED