YARP
Yet Another Robot Platform
NestedContact.cpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
8 
9 using namespace yarp::os;
10 
11 namespace {
12 YARP_OS_LOG_COMPONENT(NESTEDCONTACT, "yarp.os.NestedContact" )
13 }
14 
16 {
17 public:
18  bool fromString(const std::string& nFullName);
19 
20  std::string fullName;
21  std::string nodeName;
22  std::string nestedName;
23  std::string category;
24  std::string wireType;
25 
26  void dump()
27  {
28  yCTrace(NESTEDCONTACT, "fullName = %s", fullName.c_str());
29  yCTrace(NESTEDCONTACT, "nodeName = %s", nodeName.c_str());
30  yCTrace(NESTEDCONTACT, "nestedName = %s", nestedName.c_str());
31  yCTrace(NESTEDCONTACT, "category = %s", category.c_str());
32  yCTrace(NESTEDCONTACT, "wireType = %s", wireType.c_str());
33  }
34 };
35 
36 bool NestedContact::Private::fromString(const std::string& nFullName)
37 {
38  fullName = nFullName;
39  std::string::size_type idx2 = fullName.find(":/");
40  if (idx2 != std::string::npos) {
41  fullName = fullName.substr(idx2 + 2, fullName.length());
42  }
44  nestedName = "";
45  category = "";
46  std::string::size_type idx = fullName.find('~');
47  if (idx != std::string::npos) {
48  // We have a type name squeezed in here, into what promises
49  // to be a very full port name.
50  wireType = fullName.substr(idx + 1, fullName.length());
51  fullName = fullName.substr(0, idx);
52  }
53  idx = fullName.find('@');
54  if (idx != std::string::npos) {
55  // Great! Looks like we are using a new syntax suggested
56  // by Lorenzo Natale, /topic@/node
57  nestedName = fullName.substr(0, idx);
58  nodeName = fullName.substr(idx + 1, fullName.length());
59  char ch = nestedName[nestedName.length() - 1];
60  if (ch == '-' || ch == '+' || ch == '1') {
61  size_t offset = 1;
62  bool ok = true;
63  if (ch == '1') {
64  ok = false;
65  if (nestedName.length() >= 2) {
66  char ch0 = nestedName[nestedName.length() - 2];
67  if (ch0 == '-' || ch0 == '+') {
68  offset++;
69  category += ch0;
70  ok = true;
71  }
72  }
73  }
74  if (ok) {
75  category += ch;
76  nestedName = nestedName.substr(0, nestedName.length() - offset);
77  }
78  }
79  return true;
80  }
81  idx = fullName.find('=');
82  if (idx != std::string::npos) {
83  nodeName = fullName.substr(0, idx);
84  nestedName = fullName.substr(idx + 1, fullName.length());
85  idx = nestedName.find('/');
86  if (idx != std::string::npos) {
87  if (idx == 0) {
88  return true;
89  }
90  category = nestedName.substr(0, idx);
91  nestedName = nestedName.substr(idx, nestedName.length());
92  return true;
93  }
94  }
95  idx = fullName.find('#');
96  if (idx != std::string::npos) {
97  nodeName = fullName.substr(0, idx);
98  nestedName = fullName.substr(idx + 1, fullName.length());
99  char ch = nodeName[nodeName.length() - 1];
100  if (ch == '-' || ch == '+') {
101  category += ch;
102  nodeName = nodeName.substr(0, nodeName.length() - 1);
103  }
104  return true;
105  }
106  return false;
107 }
108 
109 
110 NestedContact::NestedContact() :
111  mPriv(new Private())
112 {
113 }
114 
115 NestedContact::NestedContact(const std::string& fullName) :
116  mPriv(new Private())
117 {
118  fromString(fullName);
119 }
120 
122  mPriv(new Private(*(rhs.mPriv)))
123 {
124 }
125 
127  mPriv(rhs.mPriv)
128 {
129  rhs.mPriv = nullptr;
130 }
131 
133 {
134  delete mPriv;
135 }
136 
138 {
139  if (&rhs != this) {
140  *mPriv = *(rhs.mPriv);
141  }
142  return *this;
143 }
144 
146 {
147  if (&rhs != this) {
148  std::swap(mPriv, rhs.mPriv);
149  }
150  return *this;
151 }
152 
153 bool NestedContact::fromString(const std::string& fullName)
154 {
155  auto ret = mPriv->fromString(fullName);
156  mPriv->dump();
157  return ret;
158 
159 }
160 
161 void NestedContact::setTypeName(const std::string& nWireType)
162 {
163  mPriv->wireType = nWireType;
164 }
165 
167 {
168  mPriv->category = "+";
169 }
170 
172 {
173  mPriv->category = "-";
174 }
175 
176 std::string NestedContact::getFullName() const
177 {
178  return mPriv->fullName;
179 }
180 
181 std::string NestedContact::getNodeName() const
182 {
183  return mPriv->nodeName;
184 }
185 
186 std::string NestedContact::getNestedName() const
187 {
188  return mPriv->nestedName;
189 }
190 
191 std::string NestedContact::getCategory() const
192 {
193  return mPriv->category;
194 }
195 
196 std::string NestedContact::getTypeName() const
197 {
198  return mPriv->wireType;
199 }
200 
202 {
203  return (!mPriv->wireType.empty()) ? mPriv->wireType : "*";
204 }
205 
207 {
208  return !mPriv->nestedName.empty();
209 }
210 
211 std::string NestedContact::toString() const
212 {
213  return mPriv->nestedName + mPriv->category + "@" + mPriv->nodeName;
214 }
bool ret
bool fromString(const std::string &nFullName)
A placeholder for rich contact information.
Definition: NestedContact.h:24
std::string getFullName() const
bool fromString(const std::string &nFullName)
std::string getNodeName() const
NestedContact()
Default constructor.
NestedContact & operator=(const NestedContact &rhs)
Copy assignment operator.
std::string toString() const
std::string getNestedName() const
void setTypeName(const std::string &nWireType)
std::string getTypeName() const
std::string getCategory() const
std::string getTypeNameStar() const
#define yCTrace(component,...)
Definition: LogComponent.h:85
#define YARP_OS_LOG_COMPONENT(name, name_string)
Definition: LogComponent.h:34
An interface to the operating system, including Port based communication.