juce_Identifier.h 5.3 KB

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