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 }
yarp::os::NestedContact
A placeholder for rich contact information.
Definition: NestedContact.h:27
yarp::os::NestedContact::setTypeName
void setTypeName(const std::string &nWireType)
Definition: NestedContact.cpp:164
yarp::os::NestedContact::Private::nodeName
std::string nodeName
Definition: NestedContact.cpp:24
yarp::os::NestedContact::Private::fromString
bool fromString(const std::string &nFullName)
Definition: NestedContact.cpp:39
yarp::os::NestedContact::getTypeName
std::string getTypeName() const
Definition: NestedContact.cpp:199
yarp::os::NestedContact::Private::category
std::string category
Definition: NestedContact.cpp:26
yarp::os::NestedContact::getTypeNameStar
std::string getTypeNameStar() const
Definition: NestedContact.cpp:204
yarp::os::NestedContact::setCategoryRead
void setCategoryRead()
Definition: NestedContact.cpp:174
yarp::os::NestedContact::getNodeName
std::string getNodeName() const
Definition: NestedContact.cpp:184
ret
bool ret
Definition: ImplementAxisInfo.cpp:72
LogComponent.h
yarp::os::NestedContact::toString
std::string toString() const
Definition: NestedContact.cpp:214
NestedContact.h
yarp::os::NestedContact::getCategory
std::string getCategory() const
Definition: NestedContact.cpp:194
yarp::os::NestedContact::Private
Definition: NestedContact.cpp:19
yarp::os::NestedContact::operator=
NestedContact & operator=(const NestedContact &rhs)
Copy assignment operator.
Definition: NestedContact.cpp:140
yarp::os::NestedContact::Private::fullName
std::string fullName
Definition: NestedContact.cpp:23
yarp::os::NestedContact::Private::nestedName
std::string nestedName
Definition: NestedContact.cpp:25
yarp::os::NestedContact::~NestedContact
~NestedContact()
Destructor.
Definition: NestedContact.cpp:135
yarp::os::NestedContact::fromString
bool fromString(const std::string &nFullName)
Definition: NestedContact.cpp:156
yarp::os
An interface to the operating system, including Port based communication.
Definition: AbstractCarrier.h:17
yarp::os::NestedContact::isNested
bool isNested() const
Definition: NestedContact.cpp:209
yarp::os::NestedContact::Private::wireType
std::string wireType
Definition: NestedContact.cpp:27
yCTrace
#define yCTrace(component,...)
Definition: LogComponent.h:88
yarp::os::NestedContact::Private::dump
void dump()
Definition: NestedContact.cpp:29
YARP_OS_LOG_COMPONENT
#define YARP_OS_LOG_COMPONENT(name, name_string)
Definition: LogComponent.h:37
yarp::os::NestedContact::setCategoryWrite
void setCategoryWrite()
Definition: NestedContact.cpp:169
yarp::os::NestedContact::getNestedName
std::string getNestedName() const
Definition: NestedContact.cpp:189
yarp::os::NestedContact::NestedContact
NestedContact()
Default constructor.
Definition: NestedContact.cpp:113
yarp::os::NestedContact::getFullName
std::string getFullName() const
Definition: NestedContact.cpp:179