RTI Routing Service  Version 6.0.0
 All Data Structures Files Functions Typedefs Enumerations Enumerator Groups Pages
EntityId.hpp
1 /*
2  * (c) Copyright, Real-Time Innovations, 2017-.
3  * All rights reserved.
4  * No duplications, whole or partial, manual or electronic, may be made
5  * without express written permission. Any such copies, or
6  * revisions thereof, must display this notice unaltered.
7  * This code contains trade secrets of Real-Time Innovations, Inc.
8  *
9  */
10 
11 #ifndef HPP_SERVICE_ENTITY_ID_HPP_
12 #define HPP_SERVICE_ENTITY_ID_HPP_
13 
14 #include "rtixmlutils/rtixmlutils_object.h"
15 
16 namespace rti { namespace service {
17 
18 class EntityFullNameIterator {
19 public:
20 
21  EntityFullNameIterator(
22  const std::string& full_name)
23  : full_name_(full_name), substring_pos_(0), next_substring_pos_(0)
24  {
25  begin();
26  }
27 
28  ~EntityFullNameIterator()
29  {
30  }
31 
32  size_t length() const
33  {
34  size_t name_length = next_substring_pos_ - substring_pos_;
35  if (next_substring_pos_ != full_name_.length()) {
36  name_length -= strlen(RTIXMLUTILS_OBJECT_NAME_SEPARATOR);
37  }
38 
39  return name_length;
40  }
41 
42  bool is_last() const
43  {
44  return substring_pos_ == full_name_.length();
45  }
46 
47  EntityFullNameIterator& end()
48  {
49  substring_pos_ = full_name_.length();
50  next_substring_pos_ = substring_pos_;
51 
52  return *this;
53  }
54 
55  EntityFullNameIterator& begin()
56  {
57  substring_pos_ = 0;
58  next_substring_pos_ = first_position();
59  next();
60 
61  return *this;
62  }
63 
64  std::string parent()
65  {
66  size_t parent_length = substring_pos_ == 0
67  ? 0
68  : substring_pos_ - strlen(RTIXMLUTILS_OBJECT_NAME_SEPARATOR);
69  return full_name_.substr(0, parent_length);
70  }
71 
72  std::string operator*() const
73  {
74 
75  return full_name_.substr(substring_pos_, length());
76  }
77 
78  EntityFullNameIterator& operator++()
79  {
80  next();
81  return *this;
82  }
83 
84  EntityFullNameIterator& operator--()
85  {
86  prev();
87  return *this;
88  }
89 
90  friend bool operator==(
91  const EntityFullNameIterator &s1,
92  const EntityFullNameIterator &s2)
93  {
94  return s1.substring_pos_ == s2.substring_pos_
95  && s1.next_substring_pos_ == s2.next_substring_pos_
96  && s1.full_name_.c_str() == s2.full_name_.c_str();
97  }
98 
99  friend bool operator!=(
100  const EntityFullNameIterator &s1,
101  const EntityFullNameIterator &s2)
102  {
103  return !(s1 == s2);
104  }
105 
106 
107 private:
108 
109  void next()
110  {
111  substring_pos_ = next_substring_pos_;
112  next_substring_pos_ = full_name_.find(
113  RTIXMLUTILS_OBJECT_NAME_SEPARATOR,
114  next_substring_pos_);
115  if (next_substring_pos_ != std::string::npos) {
116  next_substring_pos_ += strlen(RTIXMLUTILS_OBJECT_NAME_SEPARATOR);
117  } else {
118  next_substring_pos_ = full_name_.length();
119  }
120  }
121 
122  void prev()
123  {
124  next_substring_pos_ = substring_pos_;
125  if (!is_last() && substring_pos_ != 0) {
126  substring_pos_ -= strlen(RTIXMLUTILS_OBJECT_NAME_SEPARATOR) + 1;
127  }
128  substring_pos_ = full_name_.rfind(
129  RTIXMLUTILS_OBJECT_NAME_SEPARATOR,
130  substring_pos_);
131  if (substring_pos_ == std::string::npos
132  || substring_pos_ == 0) {
133  begin();
134  } else {
135  substring_pos_ += strlen(RTIXMLUTILS_OBJECT_NAME_SEPARATOR);
136  }
137  }
138 
139  size_t first_position()
140  {
141  size_t substring_pos = 0;
142  if (full_name_.find(RTIXMLUTILS_OBJECT_NAME_SEPARATOR, 0) == 0) {
143  substring_pos += strlen(RTIXMLUTILS_OBJECT_NAME_SEPARATOR);
144  }
145  return substring_pos;
146  }
147 
148 private:
149  const std::string& full_name_;
150  size_t substring_pos_;
151  size_t next_substring_pos_;
152 };
153 
154 template<typename ENTITY_KIND_TYPE>
155 class EntityId {
156 public:
157  typedef EntityFullNameIterator name_iterator;
158 
159 public:
160 
161  EntityId(const std::string& the_full_name, ENTITY_KIND_TYPE the_kind)
162  : full_name_(the_full_name), kind_(the_kind)
163  {
164  }
165 
166  bool is_nil()
167  {
168  return kind_ == ENTITY_KIND_TYPE::count_;
169  }
170 
171  name_iterator begin() const
172  {
173  return EntityFullNameIterator(full_name_);
174  }
175 
176  name_iterator end() const
177  {
178  return EntityFullNameIterator(full_name_).end();
179  }
180 
181  const std::string& full_name() const
182  {
183  return full_name_;
184  }
185 
186  const std::string name() const
187  {
188  return *(--end());
189  }
190 
191  const std::string parent_name() const
192  {
193  return (--end()).parent();
194  }
195 
196  const ENTITY_KIND_TYPE& kind() const
197  {
198  return kind_;
199  }
200 
201  static EntityId<ENTITY_KIND_TYPE> nil()
202  {
203  static EntityId<ENTITY_KIND_TYPE> nil(
204  "",
205  ENTITY_KIND_TYPE::count_);
206 
207  return nil;
208  }
209 
210 private:
211  std::string full_name_;
212  ENTITY_KIND_TYPE kind_;
213 };
214 
215 } } /* namespace rti::rservice */
216 
217 
218 #endif //HPP_SERVICE_ENTITY_ID_HPP_
219 

RTI Routing Service Version 6.0.0 Copyright © Sun Mar 3 2019 Real-Time Innovations, Inc