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.

Identifier.h 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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_IDENTIFIER_H_INCLUDED
  21. #define WATER_IDENTIFIER_H_INCLUDED
  22. #include "String.h"
  23. namespace water {
  24. //==============================================================================
  25. /**
  26. Represents a string identifier, designed for accessing properties by name.
  27. Comparing two Identifier objects is very fast (an O(1) operation), but creating
  28. them can be slower than just using a String directly, so the optimal way to use them
  29. is to keep some static Identifier objects for the things you use often.
  30. @see NamedValueSet, ValueTree
  31. */
  32. class Identifier
  33. {
  34. public:
  35. /** Creates a null identifier. */
  36. Identifier() noexcept;
  37. /** Creates an identifier with a specified name.
  38. Because this name may need to be used in contexts such as script variables or XML
  39. tags, it must only contain ascii letters and digits, or the underscore character.
  40. */
  41. Identifier (const char* name);
  42. /** Creates an identifier with a specified name.
  43. Because this name may need to be used in contexts such as script variables or XML
  44. tags, it must only contain ascii letters and digits, or the underscore character.
  45. */
  46. Identifier (const String& name);
  47. /** Creates an identifier with a specified name.
  48. Because this name may need to be used in contexts such as script variables or XML
  49. tags, it must only contain ascii letters and digits, or the underscore character.
  50. */
  51. Identifier (String::CharPointerType nameStart, String::CharPointerType nameEnd);
  52. /** Creates a copy of another identifier. */
  53. Identifier (const Identifier& other) noexcept;
  54. /** Creates a copy of another identifier. */
  55. Identifier& operator= (const Identifier& other) noexcept;
  56. #if WATER_COMPILER_SUPPORTS_MOVE_SEMANTICS
  57. /** Creates a copy of another identifier. */
  58. Identifier (Identifier&& other) noexcept;
  59. /** Creates a copy of another identifier. */
  60. Identifier& operator= (Identifier&& other) noexcept;
  61. #endif
  62. /** Destructor */
  63. ~Identifier() noexcept;
  64. /** Compares two identifiers. This is a very fast operation. */
  65. inline bool operator== (const Identifier& other) const noexcept { return name.getCharPointer() == other.name.getCharPointer(); }
  66. /** Compares two identifiers. This is a very fast operation. */
  67. inline bool operator!= (const Identifier& other) const noexcept { return name.getCharPointer() != other.name.getCharPointer(); }
  68. /** Compares the identifier with a string. */
  69. inline bool operator== (StringRef other) const noexcept { return name == other; }
  70. /** Compares the identifier with a string. */
  71. inline bool operator!= (StringRef other) const noexcept { return name != other; }
  72. /** Compares the identifier with a string. */
  73. inline bool operator< (StringRef other) const noexcept { return name < other; }
  74. /** Compares the identifier with a string. */
  75. inline bool operator<= (StringRef other) const noexcept { return name <= other; }
  76. /** Compares the identifier with a string. */
  77. inline bool operator> (StringRef other) const noexcept { return name > other; }
  78. /** Compares the identifier with a string. */
  79. inline bool operator>= (StringRef other) const noexcept { return name >= other; }
  80. /** Returns this identifier as a string. */
  81. const String& toString() const noexcept { return name; }
  82. /** Returns this identifier's raw string pointer. */
  83. operator String::CharPointerType() const noexcept { return name.getCharPointer(); }
  84. /** Returns this identifier's raw string pointer. */
  85. String::CharPointerType getCharPointer() const noexcept { return name.getCharPointer(); }
  86. /** Returns this identifier as a StringRef. */
  87. operator StringRef() const noexcept { return name; }
  88. /** Returns true if this Identifier is not null */
  89. bool isValid() const noexcept { return name.isNotEmpty(); }
  90. /** Returns true if this Identifier is null */
  91. bool isNull() const noexcept { return name.isEmpty(); }
  92. /** A null identifier. */
  93. static Identifier null;
  94. /** Checks a given string for characters that might not be valid in an Identifier.
  95. Since Identifiers are used as a script variables and XML attributes, they should only contain
  96. alphanumeric characters, underscores, or the '-' and ':' characters.
  97. */
  98. static bool isValidIdentifier (const String& possibleIdentifier) noexcept;
  99. private:
  100. String name;
  101. };
  102. }
  103. #endif // WATER_IDENTIFIER_H_INCLUDED