YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
Vector.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#ifndef YARP_SIG_VECTOR_H
8#define YARP_SIG_VECTOR_H
9
10#include <cstring>
11#include <cstddef> //defines size_t
12#include <memory>
13#include <string>
14#include <vector>
15
16#include <yarp/os/Portable.h>
18#include <yarp/os/Type.h>
19
20#include <yarp/sig/api.h>
21#include <yarp/os/Log.h>
22
26namespace yarp::sig {
27
28class VectorBase;
29template<class T> class VectorOf;
30// Swig(3.0.12) crashes when generating
31// ruby bindings without these guards.
32// Bindings for Vector are generated
33// anyways throught the %template directive
34// in the interface file.
35#ifndef SWIG
37#endif
38
39} // namespace yarp::sig
40
41
50{
51public:
52 virtual size_t getElementSize() const = 0;
53 virtual int getBottleTag() const = 0;
54
55 virtual size_t getListSize() const = 0;
56 virtual const char *getMemoryBlock() const = 0;
57 virtual char *getMemoryBlock() = 0;
58 virtual void resize(size_t size) = 0;
59
60 /*
61 * Read vector from a connection.
62 * return true iff a vector was read correctly
63 */
64 bool read(yarp::os::ConnectionReader& connection) override;
65
70 bool write(yarp::os::ConnectionWriter& connection) const override;
71
72protected:
73 virtual std::string getFormatStr(int tag) const;
74
75};
76
77/*
78* This is a simple function that maps a type into its corresponding BOTTLE tag.
79* Used for bottle compatible serialization, called inside getBottleTag().
80* Needs to be instantiated for each type T used in VectorOf<T>.
81*/
82template<class T>
83inline int BottleTagMap () {
84 /* make sure this is never called unspecified */
85 yAssert(0);
86 return 0;
87 }
88
89template<>
90inline int BottleTagMap <double> () {
91 return BOTTLE_TAG_FLOAT64;
92 }
93
94template<>
95inline int BottleTagMap <float> () {
96 return BOTTLE_TAG_FLOAT32;
97 }
98
99template<>
100inline int BottleTagMap <int> () {
101 return BOTTLE_TAG_INT32;
102 }
103
120template<class T>
122{
123private:
124 std::vector<T> bytes;
125
126public:
127 using value_type = T;
128 using iterator = typename std::vector<T>::iterator;
129 using const_iterator = typename std::vector<T>::const_iterator;
130
131 VectorOf() = default;
132
133 VectorOf(size_t size) : bytes(size) {
134 }
135
140 VectorOf(std::initializer_list<T> values) : bytes(values) {
141 }
142
148 VectorOf(size_t s, const T& def) : bytes(s, def) {
149 }
150
157 VectorOf(size_t s, const T *p)
158 {
159 this->resize(s);
160 memcpy(this->data(), p, sizeof(T)*s);
161 }
162
163 VectorOf(const VectorOf& r) = default;
164 VectorOf<T> &operator=(const VectorOf<T>& r) = default;
165 VectorOf(VectorOf<T>&& other) noexcept = default;
166 VectorOf& operator=(VectorOf<T>&& other) noexcept = default;
167 ~VectorOf() override = default;
168
169 size_t getElementSize() const override {
170 return sizeof(T);
171 }
172
173 int getBottleTag() const override {
174 return BottleTagMap <T>();
175 }
176
177 size_t getListSize() const override
178 {
179 return bytes.size();
180 }
181
182 const char* getMemoryBlock() const override
183 {
184 return reinterpret_cast<const char*>(this->data());
185 }
186
187 char* getMemoryBlock() override
188 {
189 return reinterpret_cast<char*>(this->data());
190 }
191#ifndef YARP_NO_DEPRECATED // since YARP 3.2.0
192 YARP_DEPRECATED_MSG("Use either data() if you need the pointer to the first element,"
193 " or cbegin() if you need the iterator")
194 inline const T *getFirst() const
195 {
196 return this->data();
197 }
198
199 YARP_DEPRECATED_MSG("Use either data() if you need the pointer to the first element,"
200 " or begin() if you need the iterator")
201 inline T *getFirst()
202 {
203 return this->data();
204 }
205#endif // YARP_NO_DEPRECATED
206
211 inline T *data()
212 { return bytes.empty() ? nullptr : &(bytes.at(0)); }
213
219 inline const T *data() const
220 { return bytes.empty() ? nullptr : &(bytes.at(0)); }
221
226 void resize(size_t size) override
227 {
228 bytes.resize(size);
229 }
230
235 void erase(iterator pos)
236 {
237 bytes.erase(pos);
238 }
239
245 void erase(iterator first, iterator last)
246 {
247 bytes.erase(first, last);
248 }
249
255 void resize(size_t size, const T&def)
256 {
257 this->resize(size);
258 std::fill(bytes.begin(), bytes.end(), def);
259 }
260
266 void reserve(size_t size) {
267 bytes.reserve(size);
268 }
269
273 inline void push_back (const T &elem)
274 {
275 bytes.push_back(elem);
276 }
277
282 inline void push_back (T&& elem)
283 {
284 bytes.push_back(std::move(elem));
285 }
286
292 template<typename... _Args>
293 inline T& emplace_back(_Args&&... args)
294 {
295 return bytes.emplace_back(std::forward<_Args>(args)...);
296 }
297
301 inline void pop_back()
302 {
303 bytes.pop_back();
304 }
305
311 inline T &operator[](size_t i)
312 {
313 return bytes[i];
314 }
315
321 inline const T &operator[](size_t i) const
322 {
323 return bytes[i];
324 }
325
331 inline T &operator()(size_t i)
332 {
333 return this->data()[i];
334 }
335
341 inline const T &operator()(size_t i) const
342 {
343 return this->data()[i];
344 }
345
346 inline size_t size() const {
347 return bytes.size();
348 }
349
354 inline size_t length() const
355 { return this->size();}
356
361 inline size_t capacity() const {
362 return bytes.capacity();
363 }
364
368 void zero()
369 {
370 std::fill(bytes.begin(), bytes.end(), 0);
371 }
372
382 std::string toString(int precision=-1, int width=-1) const
383 {
384 std::string ret = "";
385 size_t c = 0;
386 const size_t buffSize = 256;
387 char tmp[buffSize];
388 std::string formatStr;
390 if (width<0) {
391 formatStr = "% .*lf\t";
392 for (c=0;c<length();c++) {
393 snprintf(tmp, buffSize, formatStr.c_str(), precision, (*this)[c]);
394 ret+=tmp;
395 }
396 }
397 else{
398 formatStr = "% *.*lf ";
399 for (c=0;c<length();c++){
400 snprintf(tmp, buffSize, formatStr.c_str(), width, precision, (*this)[c]);
401 ret+=tmp;
402 }
403 }
404 }
405 else {
406 formatStr = "%" + getFormatStr(getBottleTag()) + " ";
407 for (c=0;c<length();c++) {
408 snprintf(tmp, buffSize, formatStr.c_str(), (*this)[c]);
409 ret+=tmp;
410 }
411 }
412
413 if (length() >= 1) {
414 return ret.substr(0, ret.length() - 1);
415 }
416 return ret;
417 }
418
425 VectorOf<T> subVector(unsigned int first, unsigned int last) const
426 {
428 if ((first<=last)&&((int)last<(int)this->size()))
429 {
430 ret.resize(last-first+1);
431 for (unsigned int k = first; k <= last; k++) {
432 ret[k - first] = (*this)[k];
433 }
434 }
435 return ret;
436 }
437
447 bool setSubvector(int position, const VectorOf<T> &v)
448 {
449 if (position + v.size() > this->size()) {
450 return false;
451 }
452 for (size_t i = 0; i < v.size(); i++) {
453 (*this)[position + i] = v(i);
454 }
455 return true;
456 }
457
462 {
463 std::fill(bytes.begin(), bytes.end(), v);
464 return *this;
465 }
466
470 bool operator==(const VectorOf<T> &r) const
471 {
472 return bytes == r.bytes;
473 }
474
478 iterator begin() noexcept {
479 return bytes.begin();
480 }
481
485 iterator end() noexcept {
486 return bytes.end();
487 }
488
492 const_iterator begin() const noexcept {
493 return bytes.begin();
494 }
495
499 const_iterator end() const noexcept {
500 return bytes.end();
501 }
502
506 const_iterator cbegin() const noexcept {
507 return bytes.cbegin();
508 }
509
513 const_iterator cend() const noexcept {
514 return bytes.cend();
515 }
516 void clear() {
517 bytes.clear();
518 }
519
520 yarp::os::Type getType() const override {
521 return yarp::os::Type::byName("yarp/vector");
522 }
523};
524
525
526#ifdef _MSC_VER
527/*YARP_sig_EXTERN*/ template class YARP_sig_API yarp::sig::VectorOf<double>;
528#endif
529
530#endif // YARP_SIG_VECTOR_H
#define BOTTLE_TAG_FLOAT64
Definition Bottle.h:25
#define BOTTLE_TAG_INT32
Definition Bottle.h:21
#define BOTTLE_TAG_FLOAT32
Definition Bottle.h:24
bool ret
#define yAssert(x)
Definition Log.h:388
int BottleTagMap()
Definition Vector.h:83
int BottleTagMap< int >()
Definition Vector.h:100
int BottleTagMap< float >()
Definition Vector.h:95
int BottleTagMap< double >()
Definition Vector.h:90
An interface for reading from a network connection.
An interface for writing to a network connection.
This is a base class for objects that can be both read from and be written to the YARP network.
Definition Portable.h:25
static Type byName(const char *name)
Definition Type.cpp:171
A Base class for a VectorOf<T>, provide default implementation for read/write methods.
Definition Vector.h:50
virtual size_t getListSize() const =0
virtual char * getMemoryBlock()=0
virtual size_t getElementSize() const =0
virtual void resize(size_t size)=0
virtual const char * getMemoryBlock() const =0
bool write(yarp::os::ConnectionWriter &connection) const override
Write vector to a connection.
Definition Vector.cpp:80
bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
Definition Vector.cpp:52
virtual std::string getFormatStr(int tag) const
Definition Vector.cpp:105
virtual int getBottleTag() const =0
void erase(iterator first, iterator last)
Remove one or more elements from the vector.
Definition Vector.h:245
std::string toString(int precision=-1, int width=-1) const
Creates a string object containing a text representation of the object.
Definition Vector.h:382
void erase(iterator pos)
Remove an element from the vector.
Definition Vector.h:235
size_t getListSize() const override
Definition Vector.h:177
VectorOf(std::initializer_list< T > values)
Initializer list constructor.
Definition Vector.h:140
void resize(size_t size) override
Resize the vector.
Definition Vector.h:226
VectorOf & operator=(VectorOf< T > &&other) noexcept=default
yarp::os::Type getType() const override
Definition Vector.h:520
int getBottleTag() const override
Definition Vector.h:173
const VectorOf< T > & operator=(T v)
Set all elements of the vector to a scalar.
Definition Vector.h:461
void push_back(T &&elem)
Move a new element in the vector: size is changed.
Definition Vector.h:282
size_t size() const
Definition Vector.h:346
void push_back(const T &elem)
Push a new element in the vector: size is changed.
Definition Vector.h:273
iterator begin() noexcept
Returns an iterator to the beginning of the VectorOf.
Definition Vector.h:478
const T & operator()(size_t i) const
Single element access, no range check, const version.
Definition Vector.h:341
bool operator==(const VectorOf< T > &r) const
True iff all elements of 'a' match all element of 'b'.
Definition Vector.h:470
T * data()
Return a pointer to the first element of the vector.
Definition Vector.h:211
VectorOf< T > subVector(unsigned int first, unsigned int last) const
Creates and returns a new vector, being the portion of the original vector defined by the first and l...
Definition Vector.h:425
const_iterator cend() const noexcept
Returns a const iterator to the end of the VectorOf.
Definition Vector.h:513
~VectorOf() override=default
VectorOf< T > & operator=(const VectorOf< T > &r)=default
VectorOf(VectorOf< T > &&other) noexcept=default
void resize(size_t size, const T &def)
Resize the vector and initialize the element to a default value.
Definition Vector.h:255
const_iterator begin() const noexcept
Returns a const iterator to the beginning of the VectorOf.
Definition Vector.h:492
const_iterator end() const noexcept
Returns a const iterator to the end of the VectorOf.
Definition Vector.h:499
size_t capacity() const
capacity
Definition Vector.h:361
const T * getFirst() const
Definition Vector.h:194
const_iterator cbegin() const noexcept
Returns a const iterator to the beginning of the VectorOf.
Definition Vector.h:506
T & operator[](size_t i)
Single element access, no range check.
Definition Vector.h:311
VectorOf(size_t size)
Definition Vector.h:133
const T * data() const
Return a pointer to the first element of the vector, const version.
Definition Vector.h:219
size_t length() const
Get the length of the vector.
Definition Vector.h:354
T & emplace_back(_Args &&... args)
Construct a new element in the vector: size is changed.
Definition Vector.h:293
VectorOf(size_t s, const T &def)
Build a vector and initialize it with def.
Definition Vector.h:148
char * getMemoryBlock() override
Definition Vector.h:187
VectorOf(const VectorOf &r)=default
const char * getMemoryBlock() const override
Definition Vector.h:182
void pop_back()
Pop an element out of the vector: size is changed.
Definition Vector.h:301
typename std::vector< T >::const_iterator const_iterator
Definition Vector.h:129
T & operator()(size_t i)
Single element access, no range check.
Definition Vector.h:331
iterator end() noexcept
Returns an iterator to the end of the VectorOf.
Definition Vector.h:485
void zero()
Zero the elements of the vector.
Definition Vector.h:368
VectorOf(size_t s, const T *p)
Builds a vector and initialize it with values from 'p'.
Definition Vector.h:157
const T & operator[](size_t i) const
Single element access, no range check, const version.
Definition Vector.h:321
void reserve(size_t size)
reserve, increase the capacity of the vector to a value that's greater or equal to size.
Definition Vector.h:266
size_t getElementSize() const override
Definition Vector.h:169
typename std::vector< T >::iterator iterator
Definition Vector.h:128
bool setSubvector(int position, const VectorOf< T > &v)
Set a portion of this vector with the values of the specified vector.
Definition Vector.h:447
#define YARP_DEPRECATED_MSG(MSG)
Expands to either the standard [[deprecated]] attribute or a compiler-specific decorator such as __at...
Definition compiler.h:2885
VectorOf< double > Vector
Definition Vector.h:36
The main, catch-all namespace for YARP.
Definition dirs.h:16
#define YARP_sig_API
Definition api.h:18