Nebula
Loading...
Searching...
No Matches
urn.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
14#include "core/types.h"
15#include "util/string.h"
16#include "util/stringatom.h"
17#include "util/dictionary.h"
18#include "io/uri.h"
19
20//------------------------------------------------------------------------------
21namespace IO
22{
23class URN
24{
25public:
27 URN();
29 explicit URN(const Util::String& s);
31 explicit URN(const char* s);
33 explicit URN(const char* nid, const Util::String& nss);
35 explicit URN(const char* nid, const char* nss);
37 URN(const URN& rhs);
39 void operator=(const URN& rhs);
41 bool operator==(const URN& rhs) const;
43 bool operator!=(const URN& rhs) const;
44
46 void Set(const Util::String& s);
48 Util::String AsString() const;
49
51 bool IsEmpty() const;
53 bool IsValid() const;
55 void Clear();
57 void SetNamespace(const Util::String& s);
59 const Util::String& GetNamespace() const;
61 void SetSpecific(const Util::String& s);
63 const Util::String& GetSpecific() const;
65 void SetQuery(const Util::String& s);
67 const Util::String& GetQuery() const;
69 void SetFragment(const Util::String& s);
71 const Util::String& GetFragment() const;
72
74 const IO::URI MakeURI(const Util::StringAtom& extension) const;
75private:
77 bool Split(const Util::String& s);
79 Util::String Build() const;
80
81 bool isEmpty;
86};
87
88//------------------------------------------------------------------------------
91inline
93 isEmpty(true)
94{
95 // empty
96}
97
98//------------------------------------------------------------------------------
101inline
103 isEmpty(true)
104{
105 bool validUrn = this->Split(s);
106 n_assert2(validUrn, s.AsCharPtr());
107}
108
109//------------------------------------------------------------------------------
112inline
113URN::URN(const char* s) :
114 isEmpty(true)
115{
116 bool validUrn = this->Split(s);
117 n_assert2(validUrn, s);
118}
119
120//------------------------------------------------------------------------------
123inline
124URN::URN(const char* nid, const Util::String& nss)
125{
126 this->isEmpty = strlen(nid) == 0 || nss.Length() == 0;
127 this->nid = nid;
128 this->nss = nss;
129 n_assert(this->IsValid());
130}
131
132//------------------------------------------------------------------------------
135inline
136URN::URN(const char* nid, const char* nss)
137{
138 this->isEmpty = strlen(nid) == 0 || strlen(nss) == 0;
139 this->nid = nid;
140 this->nss = nss;
141 n_assert(this->IsValid());
142}
143
144//------------------------------------------------------------------------------
147inline
148URN::URN(const URN& rhs) :
149 isEmpty(rhs.isEmpty),
150 nid(rhs.nid),
151 nss(rhs.nss),
152 query(rhs.query),
153 fragment(rhs.fragment)
154{
155 // empty
156}
157
158//------------------------------------------------------------------------------
161inline
162void
164{
165 this->isEmpty = rhs.isEmpty;
166 this->nid = rhs.nid;
167 this->nss = rhs.nss;
168 this->query = rhs.query;
169 this->fragment = rhs.fragment;
170}
171
172//------------------------------------------------------------------------------
175inline
176bool
177URN::operator==(const URN& rhs) const
178{
179 if (this->isEmpty && rhs.isEmpty)
180 {
181 return true;
182 }
183 return ((this->nid == rhs.nid) &&
184 (this->nss == rhs.nss) &&
185 (this->query == rhs.query) &&
186 (this->fragment == rhs.fragment));
187}
188
189//------------------------------------------------------------------------------
192inline
193bool
194URN::operator!=(const URN& rhs) const
195{
196 return !(*this == rhs);
197}
198
199//------------------------------------------------------------------------------
202inline
203bool
205{
206 return this->isEmpty;
207}
208
209//------------------------------------------------------------------------------
212inline
213bool
215{
216 return !this->isEmpty;
217}
218
219//------------------------------------------------------------------------------
222inline
223void
225{
226 this->isEmpty = true;
227 this->nid.Clear();
228 this->nss.Clear();
229 this->query.Clear();
230 this->fragment.Clear();
231}
232
233//------------------------------------------------------------------------------
236inline
239{
240 return this->Build();
241}
242
243//------------------------------------------------------------------------------
246inline
247void
249{
250 this->Split(s);
251}
252
253//------------------------------------------------------------------------------
256inline
257void
259{
260 this->isEmpty = false;
261 this->nid = s;
262}
263
264//------------------------------------------------------------------------------
267inline
268const Util::String&
270{
271 return this->nid;
272}
273
274//------------------------------------------------------------------------------
277inline
278void
280{
281 this->isEmpty = false;
282 this->nss = s;
283}
284
285//------------------------------------------------------------------------------
288inline
289const Util::String&
291{
292 return this->nss;
293}
294
295//------------------------------------------------------------------------------
298inline
299void
301{
302 this->isEmpty = false;
303 this->query = s;
304}
305
306//------------------------------------------------------------------------------
309inline
310const Util::String&
312{
313 return this->query;
314}
315
316//------------------------------------------------------------------------------
319inline
320void
322{
323 this->isEmpty = false;
324 this->fragment = s;
325}
326
327//------------------------------------------------------------------------------
330inline
331const Util::String&
333{
334 return this->fragment;
335}
336
337//------------------------------------------------------------------------------
340inline const IO::URI
341URN::MakeURI(const Util::StringAtom& extension) const
342{
343 return IO::URI(Util::Format("%s:%s.%s", this->nid.AsCharPtr(), this->nss.AsCharPtr(), extension.Value()));
344}
345
346} // namespace IO
347//------------------------------------------------------------------------------
350IO::URN operator ""_urn(const char* c, std::size_t s);
351
352//------------------------------------------------------------------------------
353
An URI object can split a Uniform Resource Identifier string into its components or build a string fr...
Definition uri.h:67
A URN (Uniform Resource Name) is a URI (Uniform Resource Identifier) that uses the "urn" scheme.
Definition urn.h:24
bool IsEmpty() const
return true if the URN is empty
Definition urn.h:204
void Set(const Util::String &s)
set complete URI string
Definition urn.h:248
bool Split(const Util::String &s)
split string into components
Definition urn.cc:32
const Util::String & GetSpecific() const
get Specific component (can be empty)
Definition urn.h:290
void SetSpecific(const Util::String &s)
set Specific component
Definition urn.h:279
void SetFragment(const Util::String &s)
set fragment component
Definition urn.h:321
Util::String query
Definition urn.h:84
Util::String nss
Definition urn.h:83
void SetQuery(const Util::String &s)
set query component
Definition urn.h:300
Util::String Build() const
build string from components
Definition urn.cc:106
Util::String nid
Definition urn.h:82
Util::String fragment
Definition urn.h:85
void Clear()
clear the URN
Definition urn.h:224
const Util::String & GetNamespace() const
get Namespace component
Definition urn.h:269
const Util::String & GetQuery() const
get query component (can be empty)
Definition urn.h:311
bool IsValid() const
return true if the URN is not empty
Definition urn.h:214
const Util::String & GetFragment() const
get fragment component (can be empty)
Definition urn.h:332
const IO::URI MakeURI(const Util::StringAtom &extension) const
Convert to URI using file extension.
Definition urn.h:341
Util::String AsString() const
return as concatenated string
Definition urn.h:238
void SetNamespace(const Util::String &s)
set Namespace component
Definition urn.h:258
bool operator!=(const URN &rhs) const
inequality operator
Definition urn.h:194
URN()
default constructor
Definition urn.h:92
void operator=(const URN &rhs)
assignmnent operator
Definition urn.h:163
bool operator==(const URN &rhs) const
equality operator
Definition urn.h:177
bool isEmpty
Definition urn.h:81
A StringAtom.
Definition stringatom.h:22
const char * Value() const
get contained string as char ptr (fast)
Definition stringatom.h:362
Nebula's universal string class.
Definition string.h:50
const char * AsCharPtr() const
return contents as character pointer
Definition string.h:577
#define n_assert2(exp, msg)
Definition debug.h:51
#define n_assert(exp)
Definition debug.h:50
Instances of wrapped stream classes.
Definition multiplayerfeatureunit.cc:324
auto Format
Definition string.h:1313