Audacity  3.0.3
Identifier.h
Go to the documentation of this file.
1 /**********************************************************************
2 
3  Audacity: A Digital Audio Editor
4 
5  Identifier.h
6 
7  Paul Licameli split from Types.h
8 
9  **********************************************************************/
10 
11 #ifndef __AUDACITY_IDENTIFIER__
12 #define __AUDACITY_IDENTIFIER__
13 
14 #include <vector>
15 #include <wx/string.h>
16 
18 
21 class STRINGS_API Identifier
22 {
23 public:
24 
25  Identifier() = default;
26 
28  Identifier(const wxString &str) : value{ str } {}
29 
31  Identifier(const wxChar *str) : value{ str } {}
32 
34  Identifier(const char *str) : value{ str } {}
35 
36  // Copy construction and assignment
37  Identifier( const Identifier & ) = default;
38  Identifier &operator = ( const Identifier& ) = default;
39 
40  // Move construction and assignment
41  Identifier( wxString && str )
42  { value.swap( str ); }
44  { swap( id ); }
45  Identifier &operator= ( Identifier&& id )
46  {
47  if ( this != &id )
48  value.clear(), swap( id );
49  return *this;
50  }
51 
52  // Implements moves
53  void swap( Identifier &id ) { value.swap( id.value ); }
54 
56 
58  explicit
59  Identifier(std::initializer_list<Identifier> components, wxChar separator);
60 
61  bool empty() const { return value.empty(); }
62  size_t size() const { return value.size(); }
63  size_t length() const { return value.length(); }
64 
66  const wxString &GET() const { return value; }
67 
68  std::vector< Identifier > split( wxChar separator ) const;
69 
70 private:
71  wxString value;
72 };
73 
75 inline bool operator == ( const Identifier &x, const Identifier &y )
76 { return x.GET() == y.GET(); }
77 
78 inline bool operator != ( const Identifier &x, const Identifier &y )
79 { return !(x == y); }
80 
81 inline bool operator < ( const Identifier &x, const Identifier &y )
82 { return x.GET() < y.GET(); }
83 
84 inline bool operator > ( const Identifier &x, const Identifier &y )
85 { return y < x; }
86 
87 inline bool operator <= ( const Identifier &x, const Identifier &y )
88 { return !(y < x); }
89 
90 inline bool operator >= ( const Identifier &x, const Identifier &y )
91 { return !(x < y); }
92 
93 namespace std
94 {
95  template<> struct hash< Identifier > {
96  size_t operator () ( const Identifier &id ) const // noexcept
97  { return hash<wxString>{}( id.GET() ); }
98  };
99 }
100 
102 inline bool wxFromString(const wxString& str, Identifier *id)
103  { if (id) { *id = str; return true; } else return false; }
104 
106 inline wxString wxToString( const Identifier& str ) { return str.GET(); }
107 
109 
111 template<typename Tag, bool CaseSensitive = true >
113 {
114 public:
115 
116  using TagType = Tag;
117 
119  TaggedIdentifier() = default;
120 
122  TaggedIdentifier( const TaggedIdentifier& ) = default;
129 
131  template< typename Tag2, bool b >
134  template< typename Tag2, bool b >
137  template< typename Tag2, bool b >
138  TaggedIdentifier& operator= ( const TaggedIdentifier<Tag2, b>& ) PROHIBITED;
140  template< typename Tag2, bool b >
141  TaggedIdentifier& operator= ( TaggedIdentifier<Tag2, b>&& ) PROHIBITED;
142 
146 
148  template<typename String, typename = typename String::TagType>
149  String CONVERT() const
150  { return String{ this->GET() }; }
151 };
152 
156 template< typename Tag1, typename Tag2, bool b1, bool b2 >
157 inline bool operator == (
159 {
160  static_assert( std::is_same< Tag1, Tag2 >::value && b1 == b2,
161  "TaggedIdentifiers with different tags or sensitivity are not comparable" );
162  // This test should be eliminated at compile time:
163  if ( b1 )
164  return x.GET(). Cmp ( y.GET() ) == 0;
165  else
166  return x.GET(). CmpNoCase ( y.GET() ) == 0;
167 }
168 
169 template< typename Tag1, typename Tag2, bool b1, bool b2 >
170 inline bool operator != (
172 { return !(x == y); }
173 
174 template< typename Tag1, typename Tag2, bool b1, bool b2 >
175 inline bool operator < (
177 {
178  static_assert( std::is_same< Tag1, Tag2 >::value && b1 == b2,
179  "TaggedIdentifiers with different tags or sensitivity are not comparable" );
180  // This test should be eliminated at compile time:
181  if ( b1 )
182  return x.GET(). Cmp ( y.GET() ) < 0;
183  else
184  return x.GET(). CmpNoCase ( y.GET() ) < 0;
185 }
186 
187 template< typename Tag1, typename Tag2, bool b1, bool b2 >
188 inline bool operator > (
190 { return y < x; }
191 
192 template< typename Tag1, typename Tag2, bool b1, bool b2 >
193 inline bool operator <= (
195 { return !(y < x); }
196 
197 template< typename Tag1, typename Tag2, bool b1, bool b2 >
198 inline bool operator >= (
200 { return !(x < y); }
201 
202 namespace std
203 {
204  template<typename Tag, bool b > struct hash< TaggedIdentifier<Tag, b> >
205  : hash< Identifier > {};
206 }
207 
208 /**************************************************************************/
214 using PluginPath = wxString;
215 using PluginPaths = std::vector< PluginPath >;
216 
217 // A key to be passed to wxConfigBase
218 using RegistryPath = wxString;
219 using RegistryPaths = std::vector< RegistryPath >;
220 
221 class wxArrayStringEx;
222 
224 using FileExtension = wxString;
226 
227 using FilePath = wxString;
229 
230 struct CommandIdTag;
233 using CommandIDs = std::vector<CommandID>;
234 
235 struct ManualPageIDTag;
237 
238 #endif
239 
TaggedIdentifier::CONVERT
String CONVERT() const
Explicit conversion to another kind of TaggedIdentifier.
Definition: Identifier.h:149
str
#define str(a)
Definition: DBConnection.cpp:30
RegistryPaths
std::vector< RegistryPath > RegistryPaths
Definition: Identifier.h:219
PluginPath
wxString PluginPath
type alias for identifying a Plugin supplied by a module, each module defining its own interpretation...
Definition: Identifier.h:214
Identifier::Identifier
Identifier()=default
operator<=
bool operator<=(const Identifier &x, const Identifier &y)
Definition: Identifier.h:87
wxArrayStringEx::wxArrayStringEx
wxArrayStringEx()=default
wxArrayStringEx
Extend wxArrayString with move operations and construction and insertion fromstd::initializer_list.
Definition: wxArrayStringEx.h:18
TaggedIdentifier< ManualPageIDTag >::TagType
ManualPageIDTag TagType
Definition: Identifier.h:116
TaggedIdentifier::TaggedIdentifier
TaggedIdentifier(TaggedIdentifier &&)=default
Move allowed only for the same Tag class and case sensitivity.
Identifier
An explicitly nonlocalized string, not meant for the user to see.
Definition: Identifier.h:22
TaggedIdentifier::TaggedIdentifier
TaggedIdentifier()=default
CommandIDs
std::vector< CommandID > CommandIDs
Definition: Identifier.h:233
FileExtension
wxString FileExtension
File extension, not including any leading dot.
Definition: Identifier.h:224
FilePath
wxString FilePath
Definition: Project.h:20
operator!=
bool operator!=(const Identifier &x, const Identifier &y)
Definition: Identifier.h:78
TaggedIdentifier::TaggedIdentifier
TaggedIdentifier(TaggedIdentifier< Tag2, b > &&) PROHIBITED
Move prohibited for other Tag classes or case sensitivity.
TaggedIdentifier::TaggedIdentifier
TaggedIdentifier(const TaggedIdentifier &)=default
Copy allowed only for the same Tag class and case sensitivity.
Identifier::value
wxString value
Definition: Identifier.h:71
Identifier::length
size_t length() const
Definition: Identifier.h:63
operator==
bool operator==(const Identifier &x, const Identifier &y)
Comparisons of Identifiers are case-sensitive.
Definition: Identifier.h:75
anonymous_namespace{NoteTrack.cpp}::swap
void swap(std::unique_ptr< Alg_seq > &a, std::unique_ptr< Alg_seq > &b)
Definition: NoteTrack.cpp:735
Identifier::size
size_t size() const
Definition: Identifier.h:62
operator<
bool operator<(const Identifier &x, const Identifier &y)
Definition: Identifier.h:81
TaggedIdentifier::operator=
TaggedIdentifier & operator=(const TaggedIdentifier &)=default
Copy allowed only for the same Tag class and case sensitivity.
RegistryPath
wxString RegistryPath
Definition: Identifier.h:218
Identifier::GET
const wxString & GET() const
Explicit conversion to wxString, meant to be ugly-looking and demanding of a comment why it's correct...
Definition: Identifier.h:66
operator>=
bool operator>=(const Identifier &x, const Identifier &y)
Definition: Identifier.h:90
PluginPaths
std::vector< PluginPath > PluginPaths
Definition: Identifier.h:215
TaggedIdentifier
Template generates different TaggedIdentifier classes that don't interconvert implicitly.
Definition: Identifier.h:113
Identifier::swap
void swap(Identifier &id)
Definition: Identifier.h:53
operator>
bool operator>(const Identifier &x, const Identifier &y)
Definition: Identifier.h:84
wxFromString
bool wxFromString(const wxString &str, Identifier *id)
This lets you pass Identifier into wxFileConfig::Read.
Definition: Identifier.h:102
TaggedIdentifier::TaggedIdentifier
TaggedIdentifier(const TaggedIdentifier< Tag2, b > &) PROHIBITED
Copy prohibited for other Tag classes or case sensitivity.
Identifier::empty
bool empty() const
Definition: Identifier.h:61
wxToString
wxString wxToString(const Identifier &str)
This lets you pass Identifier into wxFileConfig::Write.
Definition: Identifier.h:106