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