YARP
Yet Another Robot Platform
Type.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 
6 #include <yarp/os/Type.h>
7 
8 #include <yarp/os/Property.h>
9 #include <yarp/os/Searchable.h>
10 #include <yarp/os/Value.h>
11 
12 using namespace yarp::os;
13 
15 {
16 public:
17  Private() = default;
18 
19  Private(const Private& rhs) :
20  prop(nullptr),
21  name(rhs.name),
23  {
24  if (rhs.prop != nullptr) {
26  *prop = *(rhs.prop);
27  }
28  }
29 
30  Private(Private&& rhs) noexcept :
31  prop(rhs.prop),
32  name(std::move(rhs.name)),
33  name_on_wire(std::move(rhs.name_on_wire))
34  {
35  rhs.prop = nullptr;
36  }
37 
39  {
40  delete prop;
41  }
42 
43  Private& operator=(const Private& rhs)
44  {
45  if (&rhs != this) {
46  name = rhs.name;
48  if (rhs.prop != nullptr) {
50  *prop = *(rhs.prop);
51  } else if (prop != nullptr) {
52  delete prop;
53  prop = nullptr;
54  }
55  }
56  return *this;
57  }
58 
60  {
61  if (prop == nullptr) {
62  prop = new Property();
63  }
64  return *prop;
65  }
66 
67  const Searchable& readProperties() const
68  {
69  if (prop == nullptr) {
70  return Bottle::getNullBottle();
71  }
72  return *prop;
73  }
74 
75  void addProperty(const char* key, const Value& val)
76  {
78  prop->put(key, val);
79  }
80 
81  Property* prop{nullptr};
82  std::string name;
83  std::string name_on_wire;
84 };
85 
86 
88  mPriv(new Private())
89 {
90 }
91 
92 Type::Type(const Type& rhs) :
93  mPriv(new Private(*(rhs.mPriv)))
94 {
95 }
96 
97 Type::Type(Type&& rhs) noexcept :
98  mPriv(rhs.mPriv)
99 {
100  rhs.mPriv = nullptr;
101 }
102 
104 {
105  delete mPriv;
106 }
107 
109 {
110  if (&rhs != this) {
111  *mPriv = *(rhs.mPriv);
112  }
113  return *this;
114 }
115 
116 Type& Type::operator=(Type&& rhs) noexcept
117 {
118  if (&rhs != this) {
119  std::swap(mPriv, rhs.mPriv);
120  }
121  return *this;
122 }
123 
125 {
126  return mPriv->readProperties();
127 }
128 
130 {
131  return mPriv->writeProperties();
132 }
133 
134 Type& Type::addProperty(const char* key, const Value& val)
135 {
136  mPriv->addProperty(key, val);
137  return *this;
138 }
139 std::string Type::getName() const
140 {
141  return mPriv->name;
142 }
143 
144 std::string Type::getNameOnWire() const
145 {
146  return mPriv->name_on_wire;
147 }
148 
149 bool Type::hasName() const
150 {
151  return !mPriv->name.empty();
152 }
153 
154 bool Type::isValid() const
155 {
156  return hasName();
157 }
158 
159 std::string Type::toString() const
160 {
161  if (!mPriv->name_on_wire.empty()) {
162  return mPriv->name + ":" + mPriv->name_on_wire;
163  }
164  if (!mPriv->name.empty()) {
165  return mPriv->name;
166  }
167  return "null";
168 }
169 
170 
171 Type Type::byName(const char* name)
172 {
173  Type t;
174  t.mPriv->name = name;
175  return t;
176 }
177 
178 Type Type::byName(const char* name, const char* name_on_wire)
179 {
180  Type t;
181  t.mPriv->name = name;
182  t.mPriv->name_on_wire = name_on_wire;
183  return t;
184 }
185 
186 Type Type::byNameOnWire(const char* name_on_wire)
187 {
188  Type t;
189  t.mPriv->name = "yarp/bottle";
190  t.mPriv->name_on_wire = name_on_wire;
191  return t;
192 }
193 
195 {
196  return Type();
197 }
float t
Property & writeProperties()
Definition: Type.cpp:59
std::string name_on_wire
Definition: Type.cpp:83
void addProperty(const char *key, const Value &val)
Definition: Type.cpp:75
Property * prop
Definition: Type.cpp:81
Private(Private &&rhs) noexcept
Definition: Type.cpp:30
const Searchable & readProperties() const
Definition: Type.cpp:67
Private & operator=(const Private &rhs)
Definition: Type.cpp:43
std::string name
Definition: Type.cpp:82
Private(const Private &rhs)
Definition: Type.cpp:19
static Bottle & getNullBottle()
A special Bottle with no content.
Definition: Bottle.cpp:342
A class for storing options and configuration information.
Definition: Property.h:34
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
Definition: Property.cpp:1015
A base class for nested structures that can be searched.
Definition: Searchable.h:66
static Type byNameOnWire(const char *name_on_wire)
Definition: Type.cpp:186
const Searchable & readProperties() const
Definition: Type.cpp:124
static Type byName(const char *name)
Definition: Type.cpp:171
Type()
Constructor.
Definition: Type.cpp:87
virtual ~Type()
Destructor.
Definition: Type.cpp:103
bool hasName() const
Definition: Type.cpp:149
static Type anon()
Definition: Type.cpp:194
std::string getNameOnWire() const
Definition: Type.cpp:144
Type & addProperty(const char *key, const Value &val)
Definition: Type.cpp:134
std::string toString() const
Definition: Type.cpp:159
bool isValid() const
Definition: Type.cpp:154
std::string getName() const
Definition: Type.cpp:139
Type & operator=(const Type &rhs)
Copy assignment operator.
Definition: Type.cpp:108
Property & writeProperties()
Definition: Type.cpp:129
A single value (typically within a Bottle).
Definition: Value.h:45
An interface to the operating system, including Port based communication.