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 const Util::String& AsString();
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 ExportURI() const;
76 const IO::URI WorkURI(const Util::StringAtom& root);
77
79 static void AddExportMapping(const Util::StringAtom& ns, const Util::StringAtom& extension);
81 static void AddWorkMapping(const Util::StringAtom& ns, const Util::StringAtom& extension);
83 static void SetWorkRoot(const Util::StringAtom& root);
84
86 void Build();
87
88private:
90 bool Split(const Util::String& s);
91
92
93 bool isEmpty;
94 bool isDirty;
100
103};
104
105//------------------------------------------------------------------------------
108inline
110 isEmpty(true)
111{
112 // empty
113}
114
115//------------------------------------------------------------------------------
118inline
120 isEmpty(true)
121{
122 bool validUrn = this->Split(s);
123 n_assert2(validUrn, s.AsCharPtr());
124}
125
126//------------------------------------------------------------------------------
129inline
130URN::URN(const char* s) :
131 isEmpty(true)
132{
133 bool validUrn = this->Split(s);
134 n_assert2(validUrn, s);
135}
136
137//------------------------------------------------------------------------------
140inline
141URN::URN(const char* nid, const Util::String& nss)
142{
143 this->isEmpty = strlen(nid) == 0 || nss.Length() == 0;
144 this->nid = nid;
145 this->nss = nss;
146 this->Build();
147 n_assert(this->IsValid());
148}
149
150//------------------------------------------------------------------------------
153inline
154URN::URN(const char* nid, const char* nss)
155{
156 this->isEmpty = strlen(nid) == 0 || strlen(nss) == 0;
157 this->nid = nid;
158 this->nss = nss;
159 this->Build();
160 n_assert(this->IsValid());
161}
162
163//------------------------------------------------------------------------------
166inline
167URN::URN(const URN& rhs) :
168 isEmpty(rhs.isEmpty),
169 nid(rhs.nid),
170 nss(rhs.nss),
171 query(rhs.query),
172 fragment(rhs.fragment),
173 string(rhs.string)
174{
175 // empty
176}
177
178//------------------------------------------------------------------------------
181inline
182void
184{
185 this->isEmpty = rhs.isEmpty;
186 this->nid = rhs.nid;
187 this->nss = rhs.nss;
188 this->query = rhs.query;
189 this->fragment = rhs.fragment;
190 this->string = rhs.string;
191}
192
193//------------------------------------------------------------------------------
196inline
197bool
198URN::operator==(const URN& rhs) const
199{
200 if (this->isEmpty && rhs.isEmpty)
201 {
202 return true;
203 }
204 return ((this->nid == rhs.nid) &&
205 (this->nss == rhs.nss) &&
206 (this->query == rhs.query) &&
207 (this->fragment == rhs.fragment));
208}
209
210//------------------------------------------------------------------------------
213inline
214bool
215URN::operator!=(const URN& rhs) const
216{
217 return !(*this == rhs);
218}
219
220//------------------------------------------------------------------------------
223inline
224bool
226{
227 return this->isEmpty;
228}
229
230//------------------------------------------------------------------------------
233inline
234bool
236{
237 return !this->isEmpty;
238}
239
240//------------------------------------------------------------------------------
243inline
244void
246{
247 this->isEmpty = true;
248 this->nid.Clear();
249 this->nss.Clear();
250 this->query.Clear();
251 this->fragment.Clear();
252 this->string.Clear();
253}
254
255//------------------------------------------------------------------------------
258inline const Util::String&
260{
261 if (this->isDirty)
262 this->Build();
263 this->isDirty = false;
264 return this->string;
265}
266
267//------------------------------------------------------------------------------
270inline void
272{
273 this->Split(s);
274}
275
276//------------------------------------------------------------------------------
279inline void
281{
282 this->isEmpty = false;
283 this->isDirty = true;
284 this->nid = s;
285}
286
287//------------------------------------------------------------------------------
290inline const Util::String&
292{
293 return this->nid;
294}
295
296//------------------------------------------------------------------------------
299inline void
301{
302 this->isEmpty = false;
303 this->isDirty = true;
304 this->nss = s;
305}
306
307//------------------------------------------------------------------------------
310inline const Util::String&
312{
313 return this->nss;
314}
315
316//------------------------------------------------------------------------------
319inline void
321{
322 this->isEmpty = false;
323 this->isDirty = true;
324 this->query = s;
325}
326
327//------------------------------------------------------------------------------
330inline const Util::String&
332{
333 return this->query;
334}
335
336//------------------------------------------------------------------------------
339inline void
341{
342 this->isEmpty = false;
343 this->isDirty = true;
344 this->fragment = s;
345}
346
347//------------------------------------------------------------------------------
350inline const Util::String&
352{
353 return this->fragment;
354}
355
356//------------------------------------------------------------------------------
359inline const IO::URI
361{
362 IndexT i = URN::ExportExtensions.FindIndex(this->nid);
364 return IO::URI(Util::Format("%s:%s.%s", this->nid.AsCharPtr(), this->nss.AsCharPtr(), URN::ExportExtensions.ValueAtIndex(i).Value()));
365}
366
367//------------------------------------------------------------------------------
370inline const IO::URI
372{
374 IndexT i = URN::ExportExtensions.FindIndex(this->nid);
376 return IO::URI(Util::Format("%s:%s.%s", URN::WorkRoot.Value(), (this->nss + "/" + root.AsString()).AsCharPtr(), URN::WorkExtensions.ValueAtIndex(i).Value()));
377}
378
379//------------------------------------------------------------------------------
382inline void
384{
386 URN::ExportExtensions.Add(ns, extension);
387}
388
389//------------------------------------------------------------------------------
392inline void
394{
395 n_assert(URN::WorkExtensions.FindIndex(ns) == InvalidIndex);
396 URN::WorkExtensions.Add(ns, extension);
397}
398
399//------------------------------------------------------------------------------
402inline void
404{
405 URN::WorkRoot = root;
406}
407
408} // namespace IO
409//------------------------------------------------------------------------------
412IO::URN operator ""_urn(const char* c, std::size_t s);
413
414//------------------------------------------------------------------------------
415
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:225
void Set(const Util::String &s)
set complete URI string
Definition urn.h:271
bool Split(const Util::String &s)
split string into components
Definition urn.cc:35
const Util::String & GetSpecific() const
get Specific component (can be empty)
Definition urn.h:311
static Util::Dictionary< Util::StringAtom, Util::StringAtom > ExportExtensions
Definition urn.h:101
void SetSpecific(const Util::String &s)
set Specific component
Definition urn.h:300
static Util::StringAtom WorkRoot
Definition urn.h:102
static void SetWorkRoot(const Util::StringAtom &root)
Set the work folder.
Definition urn.h:403
void SetFragment(const Util::String &s)
set fragment component
Definition urn.h:340
Util::String string
Definition urn.h:99
Util::String query
Definition urn.h:97
Util::String nss
Definition urn.h:96
void SetQuery(const Util::String &s)
set query component
Definition urn.h:320
static void AddExportMapping(const Util::StringAtom &ns, const Util::StringAtom &extension)
Add an export binding using namespace -> file extension to use with ExportURI.
Definition urn.h:383
Util::String nid
Definition urn.h:95
Util::String fragment
Definition urn.h:98
void Clear()
clear the URN
Definition urn.h:245
const Util::String & GetNamespace() const
get Namespace component
Definition urn.h:291
const Util::String & GetQuery() const
get query component (can be empty)
Definition urn.h:331
bool IsValid() const
return true if the URN is not empty
Definition urn.h:235
void Build()
build string from components
Definition urn.cc:99
static Util::Dictionary< Util::StringAtom, Util::StringAtom > WorkExtensions
Definition urn.h:101
const Util::String & GetFragment() const
get fragment component (can be empty)
Definition urn.h:351
const Util::String & AsString()
return as concatenated string
Definition urn.h:259
void SetNamespace(const Util::String &s)
set Namespace component
Definition urn.h:280
bool operator!=(const URN &rhs) const
inequality operator
Definition urn.h:215
URN()
default constructor
Definition urn.h:109
const IO::URI ExportURI() const
Convert to an export URI using file extension, mappings are setup with AddExportMapping.
Definition urn.h:360
void operator=(const URN &rhs)
assignmnent operator
Definition urn.h:183
bool isDirty
Definition urn.h:94
static void AddWorkMapping(const Util::StringAtom &ns, const Util::StringAtom &extension)
Add a work binding using namespace -> file extension to use with WorkURI.
Definition urn.h:393
const IO::URI WorkURI(const Util::StringAtom &root)
Convert to a work URI using project folder and extension, mappings are setup with AddWorkMapping.
Definition urn.h:371
bool operator==(const URN &rhs) const
equality operator
Definition urn.h:198
bool isEmpty
Definition urn.h:93
A collection of key/value pairs with quick value retrieval by key at roughly O(log n).
Definition dictionary.h:35
A StringAtom.
Definition stringatom.h:22
String AsString() const
get containted string as string object (SLOW!!!)
Definition stringatom.h:372
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:59
#define n_assert(exp)
Definition debug.h:58
Instances of wrapped stream classes.
Definition multiplayerfeatureunit.cc:324
auto Format
Definition string.h:1313
static const int InvalidIndex
Definition types.h:47
int IndexT
Definition types.h:41