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.

143 lines
6.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. #ifndef JUCE_IDENTIFIER_H_INCLUDED
  24. #define JUCE_IDENTIFIER_H_INCLUDED
  25. //==============================================================================
  26. /**
  27. Represents a string identifier, designed for accessing properties by name.
  28. Comparing two Identifier objects is very fast (an O(1) operation), but creating
  29. them can be slower than just using a String directly, so the optimal way to use them
  30. is to keep some static Identifier objects for the things you use often.
  31. @see NamedValueSet, ValueTree
  32. */
  33. class JUCE_API Identifier
  34. {
  35. public:
  36. /** Creates a null identifier. */
  37. Identifier() noexcept;
  38. /** Creates an identifier with a specified name.
  39. Because this name may need to be used in contexts such as script variables or XML
  40. tags, it must only contain ascii letters and digits, or the underscore character.
  41. */
  42. Identifier (const char* name);
  43. /** Creates an identifier with a specified name.
  44. Because this name may need to be used in contexts such as script variables or XML
  45. tags, it must only contain ascii letters and digits, or the underscore character.
  46. */
  47. Identifier (const String& name);
  48. /** Creates an identifier with a specified name.
  49. Because this name may need to be used in contexts such as script variables or XML
  50. tags, it must only contain ascii letters and digits, or the underscore character.
  51. */
  52. Identifier (String::CharPointerType nameStart, String::CharPointerType nameEnd);
  53. /** Creates a copy of another identifier. */
  54. Identifier (const Identifier& other) noexcept;
  55. /** Creates a copy of another identifier. */
  56. Identifier& operator= (const Identifier& other) noexcept;
  57. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  58. /** Creates a copy of another identifier. */
  59. Identifier (Identifier&& other) noexcept;
  60. /** Creates a copy of another identifier. */
  61. Identifier& operator= (Identifier&& other) noexcept;
  62. #endif
  63. /** Destructor */
  64. ~Identifier() noexcept;
  65. /** Compares two identifiers. This is a very fast operation. */
  66. inline bool operator== (const Identifier& other) const noexcept { return name.getCharPointer() == other.name.getCharPointer(); }
  67. /** Compares two identifiers. This is a very fast operation. */
  68. inline bool operator!= (const Identifier& other) const noexcept { return name.getCharPointer() != other.name.getCharPointer(); }
  69. /** Compares the identifier with a string. */
  70. inline bool operator== (StringRef other) const noexcept { return name == other; }
  71. /** Compares the identifier with a string. */
  72. inline bool operator!= (StringRef other) const noexcept { return name != other; }
  73. /** Compares the identifier with a string. */
  74. inline bool operator< (StringRef other) const noexcept { return name < other; }
  75. /** Compares the identifier with a string. */
  76. inline bool operator<= (StringRef other) const noexcept { return name <= other; }
  77. /** Compares the identifier with a string. */
  78. inline bool operator> (StringRef other) const noexcept { return name > other; }
  79. /** Compares the identifier with a string. */
  80. inline bool operator>= (StringRef other) const noexcept { return name >= other; }
  81. /** Returns this identifier as a string. */
  82. const String& toString() const noexcept { return name; }
  83. /** Returns this identifier's raw string pointer. */
  84. operator String::CharPointerType() const noexcept { return name.getCharPointer(); }
  85. /** Returns this identifier's raw string pointer. */
  86. String::CharPointerType getCharPointer() const noexcept { return name.getCharPointer(); }
  87. /** Returns this identifier as a StringRef. */
  88. operator StringRef() const noexcept { return name; }
  89. /** Returns true if this Identifier is not null */
  90. bool isValid() const noexcept { return name.isNotEmpty(); }
  91. /** Returns true if this Identifier is null */
  92. bool isNull() const noexcept { return name.isEmpty(); }
  93. /** A null identifier. */
  94. static Identifier null;
  95. /** Checks a given string for characters that might not be valid in an Identifier.
  96. Since Identifiers are used as a script variables and XML attributes, they should only contain
  97. alphanumeric characters, underscores, or the '-' and ':' characters.
  98. */
  99. static bool isValidIdentifier (const String& possibleIdentifier) noexcept;
  100. private:
  101. String name;
  102. };
  103. #endif // JUCE_IDENTIFIER_H_INCLUDED