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 
10 
11 using namespace yarp::os;
12 
14 {
15 public:
16  bool fromString(const std::string& nFullName);
17 
18  std::string fullName;
19  std::string nodeName;
20  std::string nestedName;
21  std::string category;
22  std::string wireType;
23 };
24 
25 bool NestedContact::Private::fromString(const std::string& nFullName)
26 {
27  fullName = nFullName;
28  std::string::size_type idx2 = fullName.find(":/");
29  if (idx2 != std::string::npos) {
30  fullName = fullName.substr(idx2 + 2, fullName.length());
31  }
33  nestedName = "";
34  category = "";
35  std::string::size_type idx = fullName.find('~');
36  if (idx != std::string::npos) {
37  // We have a type name squeezed in here, into what promises
38  // to be a very full port name.
39  wireType = fullName.substr(idx + 1, fullName.length());
40  fullName = fullName.substr(0, idx);
41  }
42  idx = fullName.find('@');
43  if (idx != std::string::npos) {
44  // Great! Looks like we are using a new syntax suggested
45  // by Lorenzo Natale, /topic@/node
46  nestedName = fullName.substr(0, idx);
47  nodeName = fullName.substr(idx + 1, fullName.length());
48  char ch = nestedName[nestedName.length() - 1];
49  if (ch == '-' || ch == '+' || ch == '1') {
50  size_t offset = 1;
51  bool ok = true;
52  if (ch == '1') {
53  ok = false;
54  if (nestedName.length() >= 2) {
55  char ch0 = nestedName[nestedName.length() - 2];
56  if (ch0 == '-' || ch0 == '+') {
57  offset++;
58  category += ch0;
59  ok = true;
60  }
61  }
62  }
63  if (ok) {
64  category += ch;
65  nestedName = nestedName.substr(0, nestedName.length() - offset);
66  }
67  }
68  return true;
69  }
70  idx = fullName.find('=');
71  if (idx != std::string::npos) {
72  nodeName = fullName.substr(0, idx);
73  nestedName = fullName.substr(idx + 1, fullName.length());
74  idx = nestedName.find('/');
75  if (idx != std::string::npos) {
76  if (idx == 0) {
77  return true;
78  }
79  category = nestedName.substr(0, idx);
80  nestedName = nestedName.substr(idx, nestedName.length());
81  return true;
82  }
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  char ch = nodeName[nodeName.length() - 1];
89  if (ch == '-' || ch == '+') {
90  category += ch;
91  nodeName = nodeName.substr(0, nodeName.length() - 1);
92  }
93  return true;
94  }
95  return false;
96 }
97 
98 
99 NestedContact::NestedContact() :
100  mPriv(new Private())
101 {
102 }
103 
104 NestedContact::NestedContact(const std::string& fullName) :
105  mPriv(new Private())
106 {
107  fromString(fullName);
108 }
109 
111  mPriv(new Private(*(rhs.mPriv)))
112 {
113 }
114 
116  mPriv(rhs.mPriv)
117 {
118  rhs.mPriv = nullptr;
119 }
120 
122 {
123  delete mPriv;
124 }
125 
127 {
128  if (&rhs != this) {
129  *mPriv = *(rhs.mPriv);
130  }
131  return *this;
132 }
133 
135 {
136  if (&rhs != this) {
137  std::swap(mPriv, rhs.mPriv);
138  }
139  return *this;
140 }
141 
142 bool NestedContact::fromString(const std::string& fullName)
143 {
144  return mPriv->fromString(fullName);
145 }
146 
147 void NestedContact::setTypeName(const std::string& nWireType)
148 {
149  mPriv->wireType = nWireType;
150 }
151 
153 {
154  mPriv->category = "+";
155 }
156 
158 {
159  mPriv->category = "-";
160 }
161 
162 std::string NestedContact::getFullName() const
163 {
164  return mPriv->fullName;
165 }
166 
167 std::string NestedContact::getNodeName() const
168 {
169  return mPriv->nodeName;
170 }
171 
172 std::string NestedContact::getNestedName() const
173 {
174  return mPriv->nestedName;
175 }
176 
177 std::string NestedContact::getCategory() const
178 {
179  return mPriv->category;
180 }
181 
182 std::string NestedContact::getTypeName() const
183 {
184  return mPriv->wireType;
185 }
186 
188 {
189  return (!mPriv->wireType.empty()) ? mPriv->wireType : "*";
190 }
191 
193 {
194  return !mPriv->nestedName.empty();
195 }
196 
197 std::string NestedContact::toString() const
198 {
199  return mPriv->nestedName + mPriv->category + "@" + mPriv->nodeName;
200 }
yarp::os::NestedContact
A placeholder for rich contact information.
Definition: NestedContact.h:26
yarp::os::NestedContact::setTypeName
void setTypeName(const std::string &nWireType)
Definition: NestedContact.cpp:147
yarp::os::NestedContact::Private::nodeName
std::string nodeName
Definition: NestedContact.cpp:19
yarp::os::NestedContact::Private::fromString
bool fromString(const std::string &nFullName)
Definition: NestedContact.cpp:25
yarp::os::NestedContact::getTypeName
std::string getTypeName() const
Definition: NestedContact.cpp:182
yarp::os::NestedContact::Private::category
std::string category
Definition: NestedContact.cpp:21
yarp::os::NestedContact::getTypeNameStar
std::string getTypeNameStar() const
Definition: NestedContact.cpp:187
yarp::os::NestedContact::setCategoryRead
void setCategoryRead()
Definition: NestedContact.cpp:157
yarp::os::NestedContact::getNodeName
std::string getNodeName() const
Definition: NestedContact.cpp:167
yarp::os::NestedContact::toString
std::string toString() const
Definition: NestedContact.cpp:197
NestedContact.h
yarp::os::NestedContact::getCategory
std::string getCategory() const
Definition: NestedContact.cpp:177
yarp::os::NestedContact::Private
Definition: NestedContact.cpp:13
yarp::os::NestedContact::operator=
NestedContact & operator=(const NestedContact &rhs)
Copy assignment operator.
Definition: NestedContact.cpp:126
yarp::os::NestedContact::Private::fullName
std::string fullName
Definition: NestedContact.cpp:18
yarp::os::NestedContact::Private::nestedName
std::string nestedName
Definition: NestedContact.cpp:20
yarp::os::NestedContact::~NestedContact
~NestedContact()
Destructor.
Definition: NestedContact.cpp:121
yarp::os::NestedContact::fromString
bool fromString(const std::string &nFullName)
Definition: NestedContact.cpp:142
yarp::os
Definition: AbstractCarrier.h:17
yarp::os::NestedContact::isNested
bool isNested() const
Definition: NestedContact.cpp:192
yarp::os::NestedContact::Private::wireType
std::string wireType
Definition: NestedContact.cpp:22
yarp::os::NestedContact::setCategoryWrite
void setCategoryWrite()
Definition: NestedContact.cpp:152
yarp::os::NestedContact::getNestedName
std::string getNestedName() const
Definition: NestedContact.cpp:172
yarp::os::NestedContact::NestedContact
NestedContact()
Default constructor.
Definition: NestedContact.cpp:99
yarp::os::NestedContact::getFullName
std::string getFullName() const
Definition: NestedContact.cpp:162