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