Urn.h
Go to the documentation of this file.
1 /****
2  * Urn.h - Construction of device/service URNs
3  *
4  * Copyright 2020 mikee47 <mike@sillyhouse.net>
5  *
6  * This file is part of the Sming SSDP Library
7  *
8  * This library is free software: you can redistribute it and/or modify it under the terms of the
9  * GNU General Public License as published by the Free Software Foundation, version 3 or later.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with this library.
16  * If not, see <https://www.gnu.org/licenses/>.
17  *
18  ****/
19 
20 #pragma once
21 
22 #include <WString.h>
23 #include <Data/Uuid.h>
24 
25 #define UPNP_URN_KIND_MAP(XX) \
26  XX(none, "invalid") \
27  XX(uuid, "uuid:{uuid}") \
28  XX(root, "upnp:rootdevice" \
29  "uuid:{uuid}::upnp:rootdevice") \
30  XX(device, "urn:{domain}:device:{deviceType}:{version}" \
31  "uuid:{uuid}::urn:{domain}:device:{deviceType}:{version}") \
32  XX(service, "urn:{domain}:service:{serviceType}:{version}" \
33  "uuid:{uuid}::urn:{domain}:service:{serviceType}:{version}")
34 
40 class Urn
41 {
42 public:
43  enum class Kind {
44 #define XX(tag, comment) tag,
46 #undef XX
47  };
48 
49  Urn(Kind kind = Kind::none) : kind(kind)
50  {
51  }
52 
53  Urn(const Uuid& uuid) : kind(Kind::uuid), uuid(uuid)
54  {
55  }
56 
57  Urn(const Urn& urn) : kind(urn.kind), uuid(urn.uuid), domain(urn.domain), type(urn.type), version(urn.version)
58  {
59  }
60 
61  Urn(Urn&& urn)
62  : kind(urn.kind), uuid(urn.uuid), domain(std::move(urn.domain)), type(std::move(urn.type)), version(urn.version)
63  {
64  }
65 
66  Urn(Kind kind, const String& uuid, const String& domain, const String& type, uint8_t version)
68  {
69  }
70 
71  Urn(Kind kind, const Uuid& uuid, const String& domain, const String& type, uint8_t version)
73  {
74  }
75 
76  Urn(Kind kind, const String& uuid, const String& domain, const String& type, const String& version)
77  : Urn(kind, uuid, domain, type, version.toInt())
78  {
79  }
80 
81  explicit Urn(const char* urn)
82  {
83  decompose(urn);
84  }
85 
86  explicit Urn(const String& urn)
87  {
88  decompose(urn);
89  }
90 
91  Urn& operator=(const Urn& urn)
92  {
93  kind = urn.kind;
94  uuid = urn.uuid;
95  domain = urn.domain;
96  type = urn.type;
97  version = urn.version;
98  return *this;
99  }
100 
101  Urn& operator=(const String& urn)
102  {
103  decompose(urn);
104  return *this;
105  }
106 
107  bool decompose(const char* s);
108 
109  bool decompose(const String& s)
110  {
111  return decompose(s.c_str());
112  }
113 
119  String toString() const;
120 
121  explicit operator String() const
122  {
123  return toString();
124  }
125 
129  explicit operator bool() const
130  {
131  return kind != Kind::none;
132  }
133 
134  bool operator==(const Urn& other) const;
135 
136  bool operator==(const String& urn) const
137  {
138  return urn == toString();
139  }
140 
145  uint8_t version{1};
146 };
147 
151 class RootDeviceUrn : public Urn
152 {
153 public:
154  RootDeviceUrn() : Urn(Kind::root)
155  {
156  }
157 };
158 
162 using UuidUrn = Urn;
163 
167 class DeviceUrn : public Urn
168 {
169 public:
170  template <typename TVersion>
171  DeviceUrn(const String& domain, const String& type, const TVersion& version)
172  : Urn(Kind::device, nullptr, domain, type, version)
173  {
174  }
175 
176  template <typename TUuid, typename TVersion>
177  DeviceUrn(const TUuid& uuid, const String& domain, const String& type, const TVersion& version)
178  : Urn(Kind::device, uuid, domain, type, version)
179  {
180  }
181 };
182 
186 struct ServiceUrn : public Urn {
187 public:
188  template <typename TVersion>
189  ServiceUrn(const String& domain, const String& type, const TVersion& version)
190  : Urn(Kind::service, nullptr, domain, type, version)
191  {
192  }
193 
194  template <typename TUuid, typename TVersion>
195  ServiceUrn(const TUuid& uuid, const String& domain, const String& type, const TVersion& version)
196  : Urn(Kind::service, uuid, domain, type, version)
197  {
198  }
199 };
200 
201 using Usn = Urn;
202 
204 
205 inline String toString(const Urn& urn)
206 {
207  return urn.toString();
208 }
209 
210 inline bool fromString(const char* s, Urn& urn)
211 {
212  return urn.decompose(s);
213 }
214 
215 inline bool fromString(const String& s, Urn& urn)
216 {
217  return fromString(s.c_str(), urn);
218 }
bool fromString(const char *s, Urn &urn)
Definition: Urn.h:210
String toString(Urn::Kind kind)
#define UPNP_URN_KIND_MAP(XX)
Definition: Urn.h:25
A UPnP Device URN.
Definition: Urn.h:168
DeviceUrn(const String &domain, const String &type, const TVersion &version)
Definition: Urn.h:171
DeviceUrn(const TUuid &uuid, const String &domain, const String &type, const TVersion &version)
Definition: Urn.h:177
A UPnP root device URN.
Definition: Urn.h:152
RootDeviceUrn()
Definition: Urn.h:154
The String class.
Definition: WString.h:137
const char * c_str() const
Get a constant (un-modifiable) pointer to String content.
Definition: WString.h:616
Structure for UPnP URNs.
Definition: Urn.h:41
String domain
e.g. PnP::schemas_upnp_org
Definition: Urn.h:143
uint8_t version
e.g. 1
Definition: Urn.h:145
Urn(const String &urn)
Definition: Urn.h:86
Urn(Kind kind, const String &uuid, const String &domain, const String &type, const String &version)
Definition: Urn.h:76
bool operator==(const String &urn) const
Definition: Urn.h:136
String uuid
Definition: Urn.h:142
bool decompose(const String &s)
Definition: Urn.h:109
Urn & operator=(const String &urn)
Definition: Urn.h:101
Urn(const Urn &urn)
Definition: Urn.h:57
Urn(const Uuid &uuid)
Definition: Urn.h:53
bool decompose(const char *s)
bool operator==(const Urn &other) const
Urn(Urn &&urn)
Definition: Urn.h:61
String toString() const
Get URN string.
Urn & operator=(const Urn &urn)
Definition: Urn.h:91
Kind
Definition: Urn.h:43
XX(tag, comment)
Urn(Kind kind=Kind::none)
Definition: Urn.h:49
Urn(const char *urn)
Definition: Urn.h:81
Urn(Kind kind, const String &uuid, const String &domain, const String &type, uint8_t version)
Definition: Urn.h:66
Urn(Kind kind, const Uuid &uuid, const String &domain, const String &type, uint8_t version)
Definition: Urn.h:71
Kind kind
Definition: Urn.h:141
String type
e.g. "Basic"
Definition: Urn.h:144
A UPnP Service URN.
Definition: Urn.h:186
ServiceUrn(const TUuid &uuid, const String &domain, const String &type, const TVersion &version)
Definition: Urn.h:195
ServiceUrn(const String &domain, const String &type, const TVersion &version)
Definition: Urn.h:189
Class for manipulating UUID (aka GUID) entities.
Definition: Uuid.h:26