RTI Routing Service  Version 6.0.1
 All Data Structures Files Functions Typedefs Enumerations Enumerator Groups Pages
ResourceIdentifier.hpp
1 /*
2  * $Id$
3  *
4  * (c) Copyright, Real-Time Innovations, 2016-.
5  * All rights reserved.
6  * No duplications, whole or partial, manual or electronic, may be made
7  * without express written permission. Any such copies, or
8  * revisions thereof, must display this notice unaltered.
9  * This code contains trade secrets of Real-Time Innovations, Inc.
10  */
11 
12 #ifndef HPP_SERVICE_RESOURCE_IDENTIFIER_HPP_
13 #define HPP_SERVICE_RESOURCE_IDENTIFIER_HPP_
14 
15 
16 // IMPORTANT: macros.hpp must be the first RTI header included in every header
17 // file so that symbols are exported correctly on Windows
18 #include <dds/core/macros.hpp>
19 
20 #include "reda/reda_inlineList.h"
21 #include "routingservice/routingservice_log.h"
22 #include "routingservice/routingservice_remote_config.h"
23 
24 #include <dds/core/Value.hpp>
25 #include <rti/core/NativeValueType.hpp>
26 #include <rti/service/ServiceException.hpp>
27 #include <rti/service/EntityId.hpp>
28 
29 namespace rti { namespace service {
30 
31 class ResourceIdentifier;
32 
33 class RelativeResource {
34 public:
35 
36  typedef const RTI_RoutingServiceRelativeResourceInfo native_type;
37 
38 
39  RelativeResource(native_type *native_value)
40  : native_relative_resource_(native_value)
41  {
42  }
43 
44  ~RelativeResource()
45  {
46  }
47 
48  RelativeResource(const RelativeResource &other)
49  : native_relative_resource_(other.native_relative_resource_)
50  {
51  }
52 
53  RelativeResource operator * () const
54  {
55  return RelativeResource(*this);
56  }
57 
58  RelativeResource & operator = (const RelativeResource& other)
59  {
60  native_relative_resource_ = other.native_relative_resource_;
61  return *this;
62  }
63 
64  RelativeResource& operator ++ ()
65  {
66  native_relative_resource_ = next();
67  return *this;
68  }
69 
70  RelativeResource& operator -- ()
71  {
72  native_relative_resource_ = prev();
73  return *this;
74  }
75 
76  friend bool operator == (
77  const RelativeResource & s1,
78  const RelativeResource & s2)
79  {
80  return s1.native_relative_resource_ == s2.native_relative_resource_;
81  }
82 
83  friend bool operator != (
84  const RelativeResource & s1,
85  const RelativeResource & s2)
86  {
87  return !(s1 == s2);
88  }
89 
90  std::string name() const
91  {
92  return RTI_RoutingServiceRelativeResourceInfo_get_name(
93  native_relative_resource_);
94  }
95 
96  std::string kind() const
97  {
98  return RTI_RoutingServiceRelativeResourceInfo_get_kind(
99  native_relative_resource_);
100  }
101 
102  RelativeResource& last()
103  {
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);
108  return *this;
109  }
110 
111  RelativeResource& begin()
112  {
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);
117  return *this;
118  }
119 
120  friend const std::string to_string(
121  const rti::service::RelativeResource& relative_id)
122  {
123  return relative_id.kind() + "/" + relative_id.name();
124  }
125 
126  friend class ResourceIdentifier;
127 private:
128 
129  static RelativeResource nil()
130  {
131  static RelativeResource nil(NULL);
132 
133  return nil;
134  }
135  native_type * next()
136  {
137  return reinterpret_cast<native_type*> (
138  native_relative_resource_->_node.next);
139  }
140 
141  native_type * prev()
142  {
143  REDAInlineListNode *prev = native_relative_resource_->_node.prev;
144  if (prev != NULL) {
145  return reinterpret_cast<native_type*> (prev);
146  }
147 
148  return native_relative_resource_;
149  }
150 
151 private:
152  native_type *native_relative_resource_;
153 };
154 
155 class ResourceIdentifier {
156 public:
157  typedef RTI_RoutingServiceResourceIdentifier native_type;
158  typedef RelativeResource iterator;
159  typedef RelativeResource relative_resource;
160 
161 public:
162 
163  ResourceIdentifier()
164  {
165  if (!RTI_RoutingServiceResourceIdentifier_initialize(&native_)) {
166  RTI_THROW_SERVICE_EXCEPTION(
167  &RTI_LOG_INIT_FAILURE_s,
168  "native resource identifier");
169  }
170  }
171 
172  ResourceIdentifier(const std::string& formatted_uri)
173  {
174  if (!RTI_RoutingServiceResourceIdentifier_initialize(&native_)) {
175  RTI_THROW_SERVICE_EXCEPTION(
176  &RTI_LOG_INIT_FAILURE_s,
177  "native resource identifier");
178  }
179  if (!RTI_RoutingServiceResourceIdentifier_parse_formatted_identifier(
180  &native_,
181  formatted_uri.c_str())) {
182  RTI_THROW_SERVICE_EXCEPTION(
183  &RTI_SERVICE_LOG_MALFORMED_RESOURCE_IDENTIFIER_s,
184  formatted_uri.c_str());
185  }
186  parse(formatted_uri);
187  }
188 
189  ResourceIdentifier(const ResourceIdentifier& other)
190  {
191  if (!RTI_RoutingServiceResourceIdentifier_initialize(&native_)) {
192  RTI_THROW_SERVICE_EXCEPTION(
193  &RTI_LOG_INIT_FAILURE_s,
194  "native resource identifier");
195  }
196  append(other);
197  }
198 
199  ResourceIdentifier(
200  const ResourceIdentifier& parent,
201  const ResourceIdentifier& child)
202  {
203  if (!RTI_RoutingServiceResourceIdentifier_initialize(&native_)) {
204  RTI_THROW_SERVICE_EXCEPTION(
205  &RTI_LOG_INIT_FAILURE_s,
206  "native resource identifier");
207  }
208  append(parent);
209  append(child);
210  }
211 
212  ResourceIdentifier(
213  const std::string& kind,
214  const std::string& name)
215  {
216  if (!RTI_RoutingServiceResourceIdentifier_initialize(&native_)) {
217  RTI_THROW_SERVICE_EXCEPTION(
218  &RTI_LOG_INIT_FAILURE_s,
219  "native resource identifier");
220  }
221  add(kind, name);
222  }
223 
224  ResourceIdentifier(
225  const ResourceIdentifier::relative_resource& relative_resource)
226  {
227  if (!RTI_RoutingServiceResourceIdentifier_initialize(&native_)) {
228  RTI_THROW_SERVICE_EXCEPTION(
229  &RTI_LOG_INIT_FAILURE_s,
230  "native resource identifier");
231  }
232  add(relative_resource);
233  }
234 
235  ResourceIdentifier& parse(const std::string& formatted_uri)
236  {
237  if (!RTI_RoutingServiceResourceIdentifier_parse_formatted_identifier(
238  &native_,
239  formatted_uri.c_str())) {
240  RTI_THROW_SERVICE_EXCEPTION(
241  &RTI_SERVICE_LOG_MALFORMED_RESOURCE_IDENTIFIER_s,
242  formatted_uri.c_str());
243  }
244 
245  return *this;
246  }
247 
248  ~ResourceIdentifier()
249  {
250  RTI_RoutingServiceResourceIdentifier_finalize(&native_);
251  }
252 
253  iterator last() const
254  {
255  return iterator(RTI_RoutingServiceResourceIdentifier_get_resource(
256  const_cast<native_type*> (&native_)));
257 
258  }
259 
260  iterator end() const
261  {
262  return iterator::nil();
263  }
264 
265  iterator begin() const
266  {
267  iterator last = this->last();
268  if (last != end()) {
269  return last.begin();
270  }
271 
272  return last;
273  }
274 
275  ResourceIdentifier parent() const
276  {
277  ResourceIdentifier parent_id;
278  iterator it = begin();
279  for (; it != last(); ++it) {
280  parent_id += ResourceIdentifier(it.kind(), it.name());
281  }
282 
283  return parent_id;
284  }
285 
286  const std::string fully_qualified_name() const
287  {
288  return RTI_RoutingServiceResourceIdentifier_get_fully_qualified_name(
289  const_cast<native_type*> (&native_));
290  }
291 
292  const std::string fully_qualified_kind() const
293  {
294  return RTI_RoutingServiceResourceIdentifier_get_fully_qualified_kind(
295  const_cast<native_type*> (&native_));
296  }
297 
298  const std::string custom_method() const
299  {
300  return RTI_RoutingServiceResourceIdentifier_get_custom_method(
301  const_cast<native_type*> (&native_));
302  }
303 
304  ResourceIdentifier& operator+=(
305  const ResourceIdentifier& right)
306  {
307  return append(right);
308  }
309 
310  friend std::ostream& operator<<(
311  std::ostream& os,
312  const ResourceIdentifier& resource_id)
313  {
314  os << to_string(resource_id);
315 
316  return os;
317  }
318 
319  friend const std::string to_string(
320  const rti::service::ResourceIdentifier& resource_id)
321  {
322  return std::string(to_native_string(resource_id));
323  }
324 
325  friend const char* to_native_string(
326  const rti::service::ResourceIdentifier& resource_id)
327  {
328  return RTI_RoutingServiceResourceIdentifier_to_string(
329  const_cast<native_type*>(&resource_id.native_));
330  }
331 
332  native_type& native() const
333  {
334  return const_cast<native_type&>(native_);
335  }
336 
337 
338 private:
339 
340  ResourceIdentifier& add(
341  const std::string& kind,
342  const std::string& name = "")
343  {
344  if (!RTI_RoutingServiceResourceIdentifier_add_relative_resource(
345  &native_,
346  kind.c_str(),
347  name.c_str())) {
348  RTI_THROW_SERVICE_EXCEPTION(
349  &RTI_LOG_ADD_FAILURE_s,
350  "relative resource");
351  }
352 
353  return *this;
354  }
355 
356  ResourceIdentifier& add(
357  const ResourceIdentifier::relative_resource& relative_resource)
358  {
359  add(relative_resource.kind(), relative_resource.name());
360  return *this;
361  }
362 
363  ResourceIdentifier& append(
364  const ResourceIdentifier& right)
365  {
366  rti::service::ResourceIdentifier::iterator it = right.begin();
367  for (; it != end(); ++it) {
368  add(it);
369  }
370 
371  return *this;
372  }
373 
374  native_type native_;
375 
376 };
377 
378 } } // rti::service
379 
380 
381 #endif /* HPP_SERVICE_RESOURCE_IDENTIFIER_HPP_ */

RTI Routing Service Version 6.0.1 Copyright © Sun Nov 17 2019 Real-Time Innovations, Inc