12 #ifndef HPP_SERVICE_RESOURCE_IDENTIFIER_HPP_
13 #define HPP_SERVICE_RESOURCE_IDENTIFIER_HPP_
18 #include <dds/core/macros.hpp>
20 #include "reda/reda_inlineList.h"
21 #include "routingservice/routingservice_log.h"
22 #include "routingservice/routingservice_remote_config.h"
24 #include <dds/core/Value.hpp>
25 #include <rti/core/NativeValueType.hpp>
26 #include <rti/service/ServiceException.hpp>
27 #include <rti/service/EntityId.hpp>
29 namespace rti {
namespace service {
31 class ResourceIdentifier;
33 class RelativeResource {
36 typedef const RTI_RoutingServiceRelativeResourceInfo native_type;
39 RelativeResource(native_type *native_value)
40 : native_relative_resource_(native_value)
48 RelativeResource(
const RelativeResource &other)
49 : native_relative_resource_(other.native_relative_resource_)
53 RelativeResource operator * ()
const
55 return RelativeResource(*
this);
58 RelativeResource & operator = (
const RelativeResource& other)
60 native_relative_resource_ = other.native_relative_resource_;
64 RelativeResource& operator ++ ()
66 native_relative_resource_ = next();
70 RelativeResource& operator -- ()
72 native_relative_resource_ = prev();
76 friend bool operator == (
77 const RelativeResource & s1,
78 const RelativeResource & s2)
80 return s1.native_relative_resource_ == s2.native_relative_resource_;
83 friend bool operator != (
84 const RelativeResource & s1,
85 const RelativeResource & s2)
90 std::string name()
const
92 return RTI_RoutingServiceRelativeResourceInfo_get_name(
93 native_relative_resource_);
96 std::string kind()
const
98 return RTI_RoutingServiceRelativeResourceInfo_get_kind(
99 native_relative_resource_);
102 RelativeResource& last()
104 REDAInlineListNode *last = REDAInlineList_getLast(
105 native_relative_resource_->_node.inlineList);
106 RTIBOOST_ASSERT(last != NULL);
107 native_relative_resource_ =
reinterpret_cast<native_type*
>(last);
111 RelativeResource& begin()
113 REDAInlineListNode *first = REDAInlineList_getFirst(
114 native_relative_resource_->_node.inlineList);
115 RTIBOOST_ASSERT(first != NULL);
116 native_relative_resource_ =
reinterpret_cast<native_type*
> (first);
120 friend const std::string to_string(
121 const rti::service::RelativeResource& relative_id)
123 return relative_id.kind() +
"/" + relative_id.name();
126 friend class ResourceIdentifier;
129 static RelativeResource nil()
131 static RelativeResource nil(NULL);
137 return reinterpret_cast<native_type*
> (
138 native_relative_resource_->_node.next);
143 REDAInlineListNode *prev = native_relative_resource_->_node.prev;
145 return reinterpret_cast<native_type*
> (prev);
148 return native_relative_resource_;
152 native_type *native_relative_resource_;
155 class ResourceIdentifier {
157 typedef RTI_RoutingServiceResourceIdentifier native_type;
158 typedef RelativeResource iterator;
159 typedef RelativeResource relative_resource;
165 if (!RTI_RoutingServiceResourceIdentifier_initialize(&native_)) {
166 RTI_THROW_SERVICE_EXCEPTION(
167 &RTI_LOG_INIT_FAILURE_s,
168 "native resource identifier");
172 ResourceIdentifier(
const std::string& formatted_uri)
174 if (!RTI_RoutingServiceResourceIdentifier_initialize(&native_)) {
175 RTI_THROW_SERVICE_EXCEPTION(
176 &RTI_LOG_INIT_FAILURE_s,
177 "native resource identifier");
179 if (!RTI_RoutingServiceResourceIdentifier_parse_formatted_identifier(
181 formatted_uri.c_str())) {
182 RTI_THROW_SERVICE_EXCEPTION(
183 &RTI_SERVICE_LOG_MALFORMED_RESOURCE_IDENTIFIER_s,
184 formatted_uri.c_str());
186 parse(formatted_uri);
189 ResourceIdentifier(
const ResourceIdentifier& other)
191 if (!RTI_RoutingServiceResourceIdentifier_initialize(&native_)) {
192 RTI_THROW_SERVICE_EXCEPTION(
193 &RTI_LOG_INIT_FAILURE_s,
194 "native resource identifier");
200 const ResourceIdentifier& parent,
201 const ResourceIdentifier& child)
203 if (!RTI_RoutingServiceResourceIdentifier_initialize(&native_)) {
204 RTI_THROW_SERVICE_EXCEPTION(
205 &RTI_LOG_INIT_FAILURE_s,
206 "native resource identifier");
213 const std::string& kind,
214 const std::string& name)
216 if (!RTI_RoutingServiceResourceIdentifier_initialize(&native_)) {
217 RTI_THROW_SERVICE_EXCEPTION(
218 &RTI_LOG_INIT_FAILURE_s,
219 "native resource identifier");
225 const ResourceIdentifier::relative_resource& relative_resource)
227 if (!RTI_RoutingServiceResourceIdentifier_initialize(&native_)) {
228 RTI_THROW_SERVICE_EXCEPTION(
229 &RTI_LOG_INIT_FAILURE_s,
230 "native resource identifier");
232 add(relative_resource);
235 ResourceIdentifier& parse(
const std::string& formatted_uri)
237 if (!RTI_RoutingServiceResourceIdentifier_parse_formatted_identifier(
239 formatted_uri.c_str())) {
240 RTI_THROW_SERVICE_EXCEPTION(
241 &RTI_SERVICE_LOG_MALFORMED_RESOURCE_IDENTIFIER_s,
242 formatted_uri.c_str());
248 ~ResourceIdentifier()
250 RTI_RoutingServiceResourceIdentifier_finalize(&native_);
253 iterator last()
const
255 return iterator(RTI_RoutingServiceResourceIdentifier_get_resource(
256 const_cast<native_type*> (&native_)));
262 return iterator::nil();
265 iterator begin()
const
267 iterator last = this->last();
275 ResourceIdentifier parent()
const
277 ResourceIdentifier parent_id;
278 iterator it = begin();
279 for (; it != last(); ++it) {
280 parent_id += ResourceIdentifier(it.kind(), it.name());
286 const std::string fully_qualified_name()
const
288 return RTI_RoutingServiceResourceIdentifier_get_fully_qualified_name(
289 const_cast<native_type*> (&native_));
292 const std::string fully_qualified_kind()
const
294 return RTI_RoutingServiceResourceIdentifier_get_fully_qualified_kind(
295 const_cast<native_type*> (&native_));
298 const std::string custom_method()
const
300 return RTI_RoutingServiceResourceIdentifier_get_custom_method(
301 const_cast<native_type*> (&native_));
304 ResourceIdentifier& operator+=(
305 const ResourceIdentifier& right)
307 return append(right);
310 friend std::ostream& operator<<(
312 const ResourceIdentifier& resource_id)
314 os << to_string(resource_id);
319 friend const std::string to_string(
320 const rti::service::ResourceIdentifier& resource_id)
322 return std::string(to_native_string(resource_id));
325 friend const char* to_native_string(
326 const rti::service::ResourceIdentifier& resource_id)
328 return RTI_RoutingServiceResourceIdentifier_to_string(
329 const_cast<native_type*>(&resource_id.native_));
332 native_type& native()
const
334 return const_cast<native_type&
>(native_);
340 ResourceIdentifier& add(
341 const std::string& kind,
342 const std::string& name =
"")
344 if (!RTI_RoutingServiceResourceIdentifier_add_relative_resource(
348 RTI_THROW_SERVICE_EXCEPTION(
349 &RTI_LOG_ADD_FAILURE_s,
350 "relative resource");
356 ResourceIdentifier& add(
357 const ResourceIdentifier::relative_resource& relative_resource)
359 add(relative_resource.kind(), relative_resource.name());
363 ResourceIdentifier& append(
364 const ResourceIdentifier& right)
366 rti::service::ResourceIdentifier::iterator it = right.begin();
367 for (; it != end(); ++it) {