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