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.

StringRef.h 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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_STRINGREF_H_INCLUDED
  21. #define WATER_STRINGREF_H_INCLUDED
  22. #include "String.h"
  23. namespace water {
  24. //==============================================================================
  25. /**
  26. A simple class for holding temporary references to a string literal or String.
  27. Unlike a real String object, the StringRef does not allocate any memory or
  28. take ownership of the strings you give to it - it simply holds a reference to
  29. a string that has been allocated elsewhere.
  30. The main purpose of the class is to be used instead of a const String& as the type
  31. of function arguments where the caller may pass either a string literal or a String
  32. object. This means that when the called uses a string literal, there's no need
  33. for an temporary String object to be allocated, and this cuts down overheads
  34. substantially.
  35. Because the class is simply a wrapper around a pointer, you should always pass
  36. it by value, not by reference.
  37. @code
  38. void myStringFunction1 (const String&);
  39. void myStringFunction2 (StringRef);
  40. myStringFunction1 ("abc"); // Implicitly allocates a temporary String object.
  41. myStringFunction2 ("abc"); // Much faster, as no local allocations are needed.
  42. @endcode
  43. For examples of it in use, see the XmlElement or StringArray classes.
  44. Bear in mind that there are still many cases where it's better to use an argument
  45. which is a const String&. For example if the function stores the string or needs
  46. to internally create a String from the argument, then it's better for the original
  47. argument to already be a String.
  48. @see String
  49. */
  50. class StringRef
  51. {
  52. public:
  53. /** Creates a StringRef from a raw string literal.
  54. The StringRef object does NOT take ownership or copy this data, so you must
  55. ensure that the data does not change during the lifetime of the StringRef.
  56. Note that this pointer not be null!
  57. */
  58. StringRef (const char* stringLiteral) noexcept;
  59. /** Creates a StringRef from a raw char pointer.
  60. The StringRef object does NOT take ownership or copy this data, so you must
  61. ensure that the data does not change during the lifetime of the StringRef.
  62. */
  63. StringRef (String::CharPointerType stringLiteral) noexcept;
  64. /** Creates a StringRef from a String.
  65. The StringRef object does NOT take ownership or copy the data from the String,
  66. so you must ensure that the String is not modified or deleted during the lifetime
  67. of the StringRef.
  68. */
  69. StringRef (const String& string) noexcept;
  70. /** Creates a StringRef pointer to an empty string. */
  71. StringRef() noexcept;
  72. //==============================================================================
  73. /** Returns a raw pointer to the underlying string data. */
  74. operator const String::CharPointerType::CharType*() const noexcept { return text.getAddress(); }
  75. /** Returns a pointer to the underlying string data as a char pointer object. */
  76. operator String::CharPointerType() const noexcept { return text; }
  77. /** Returns true if the string is empty. */
  78. bool isEmpty() const noexcept { return text.isEmpty(); }
  79. /** Returns true if the string is not empty. */
  80. bool isNotEmpty() const noexcept { return ! text.isEmpty(); }
  81. /** Returns the number of characters in the string. */
  82. int length() const noexcept { return (int) text.length(); }
  83. /** Retrieves a character by index. */
  84. water_uchar operator[] (int index) const noexcept { return text[index]; }
  85. /** Compares this StringRef with a String. */
  86. bool operator== (const String& s) const noexcept { return text.compare (s.getCharPointer()) == 0; }
  87. /** Compares this StringRef with a String. */
  88. bool operator!= (const String& s) const noexcept { return text.compare (s.getCharPointer()) != 0; }
  89. /** Case-sensitive comparison of two StringRefs. */
  90. bool operator== (StringRef s) const noexcept { return text.compare (s.text) == 0; }
  91. /** Case-sensitive comparison of two StringRefs. */
  92. bool operator!= (StringRef s) const noexcept { return text.compare (s.text) != 0; }
  93. //==============================================================================
  94. /** The text that is referenced. */
  95. String::CharPointerType text;
  96. };
  97. //==============================================================================
  98. /** Case-sensitive comparison of two strings. */
  99. bool operator== (const String& string1, StringRef string2) noexcept;
  100. /** Case-sensitive comparison of two strings. */
  101. bool operator!= (const String& string1, StringRef string2) noexcept;
  102. inline String operator+ (String s1, StringRef s2) { return s1 += String (s2.text); }
  103. inline String operator+ (StringRef s1, const String& s2) { return String (s1.text) + s2; }
  104. inline String operator+ (const char* s1, StringRef s2) { return String (s1) + String (s2.text); }
  105. inline String operator+ (StringRef s1, const char* s2) { return String (s1.text) + String (s2); }
  106. }
  107. #endif // WATER_STRINGREF_H_INCLUDED