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.

133 lines
5.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. //==============================================================================
  20. /**
  21. Represents a string identifier, designed for accessing properties by name.
  22. Comparing two Identifier objects is very fast (an O(1) operation), but creating
  23. them can be slower than just using a String directly, so the optimal way to use them
  24. is to keep some static Identifier objects for the things you use often.
  25. @see NamedValueSet, ValueTree
  26. @tags{Core}
  27. */
  28. class JUCE_API Identifier final
  29. {
  30. public:
  31. /** Creates a null identifier. */
  32. Identifier() noexcept;
  33. /** Creates an identifier with a specified name.
  34. Because this name may need to be used in contexts such as script variables or XML
  35. tags, it must only contain ascii letters and digits, or the underscore character.
  36. */
  37. Identifier (const char* name);
  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 String& 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 (String::CharPointerType nameStart, String::CharPointerType nameEnd);
  48. /** Creates a copy of another identifier. */
  49. Identifier (const Identifier& other) noexcept;
  50. /** Creates a copy of another identifier. */
  51. Identifier& operator= (const Identifier& other) noexcept;
  52. /** Creates a copy of another identifier. */
  53. Identifier (Identifier&& other) noexcept;
  54. /** Creates a copy of another identifier. */
  55. Identifier& operator= (Identifier&& other) noexcept;
  56. /** Destructor */
  57. ~Identifier() noexcept;
  58. /** Compares two identifiers. This is a very fast operation. */
  59. inline bool operator== (const Identifier& other) const noexcept { return name.getCharPointer() == other.name.getCharPointer(); }
  60. /** Compares two identifiers. This is a very fast operation. */
  61. inline bool operator!= (const Identifier& other) const noexcept { return name.getCharPointer() != other.name.getCharPointer(); }
  62. /** Compares the identifier with a string. */
  63. inline bool operator== (StringRef other) const noexcept { return name == other; }
  64. /** Compares the identifier with a string. */
  65. inline bool operator!= (StringRef other) const noexcept { return name != other; }
  66. /** Compares the identifier with a string. */
  67. inline bool operator< (StringRef other) const noexcept { return name < other; }
  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. /** Returns this identifier as a string. */
  75. const String& toString() const noexcept { return name; }
  76. /** Returns this identifier's raw string pointer. */
  77. operator String::CharPointerType() const noexcept { return name.getCharPointer(); }
  78. /** Returns this identifier's raw string pointer. */
  79. String::CharPointerType getCharPointer() const noexcept { return name.getCharPointer(); }
  80. /** Returns this identifier as a StringRef. */
  81. operator StringRef() const noexcept { return name; }
  82. /** Returns true if this Identifier is not null */
  83. bool isValid() const noexcept { return name.isNotEmpty(); }
  84. /** Returns true if this Identifier is null */
  85. bool isNull() const noexcept { return name.isEmpty(); }
  86. /** A null identifier. */
  87. static Identifier null;
  88. /** Checks a given string for characters that might not be valid in an Identifier.
  89. Since Identifiers are used as a script variables and XML attributes, they should only contain
  90. alphanumeric characters, underscores, or the '-' and ':' characters.
  91. */
  92. static bool isValidIdentifier (const String& possibleIdentifier) noexcept;
  93. private:
  94. String name;
  95. };
  96. } // namespace juce