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 <int> () {
96 return BOTTLE_TAG_INT32;
97 }
98
115template<class T>
117{
118private:
119 std::vector<T> bytes;
120
121public:
122 using value_type = T;
123 using iterator = typename std::vector<T>::iterator;
124 using const_iterator = typename std::vector<T>::const_iterator;
125
126 VectorOf() = default;
127
128 VectorOf(size_t size) : bytes(size) {
129 }
130
135 VectorOf(std::initializer_list<T> values) : bytes(values) {
136 }
137
143 VectorOf(size_t s, const T& def) : bytes(s, def) {
144 }
145
152 VectorOf(size_t s, const T *p)
153 {
154 this->resize(s);
155 memcpy(this->data(), p, sizeof(T)*s);
156 }
157
158 VectorOf(const VectorOf& r) = default;
159 VectorOf<T> &operator=(const VectorOf<T>& r) = default;
160 VectorOf(VectorOf<T>&& other) noexcept = default;
161 VectorOf& operator=(VectorOf<T>&& other) noexcept = default;
162 ~VectorOf() override = default;
163
164 size_t getElementSize() const override {
165 return sizeof(T);
166 }
167
168 int getBottleTag() const override {
169 return BottleTagMap <T>();
170 }
171
172 size_t getListSize() const override
173 {
174 return bytes.size();
175 }
176
177 const char* getMemoryBlock() const override
178 {
179 return reinterpret_cast<const char*>(this->data());
180 }
181
182 char* getMemoryBlock() override
183 {
184 return reinterpret_cast<char*>(this->data());
185 }
186#ifndef YARP_NO_DEPRECATED // since YARP 3.2.0
187 YARP_DEPRECATED_MSG("Use either data() if you need the pointer to the first element,"
188 " or cbegin() if you need the iterator")
189 inline const T *getFirst() const
190 {
191 return this->data();
192 }
193
194 YARP_DEPRECATED_MSG("Use either data() if you need the pointer to the first element,"
195 " or begin() if you need the iterator")
196 inline T *getFirst()
197 {
198 return this->data();
199 }
200#endif // YARP_NO_DEPRECATED
201
206 inline T *data()
207 { return bytes.empty() ? nullptr : &(bytes.at(0)); }
208
214 inline const T *data() const
215 { return bytes.empty() ? nullptr : &(bytes.at(0)); }
216
221 void resize(size_t size) override
222 {
223 bytes.resize(size);
224 }
225
230 void erase(iterator pos)
231 {
232 bytes.erase(pos);
233 }
234
240 void erase(iterator first, iterator last)
241 {
242 bytes.erase(first, last);
243 }
244
250 void resize(size_t size, const T&def)
251 {
252 this->resize(size);
253 std::fill(bytes.begin(), bytes.end(), def);
254 }
255
261 void reserve(size_t size) {
262 bytes.reserve(size);
263 }
264
268 inline void push_back (const T &elem)
269 {
270 bytes.push_back(elem);
271 }
272
277 inline void push_back (T&& elem)
278 {
279 bytes.push_back(std::move(elem));
280 }
281
287 template<typename... _Args>
288 inline T& emplace_back(_Args&&... args)
289 {
290 return bytes.emplace_back(std::forward<_Args>(args)...);
291 }
292
296 inline void pop_back()
297 {
298 bytes.pop_back();
299 }
300
306 inline T &operator[](size_t i)
307 {
308 return bytes[i];
309 }
310
316 inline const T &operator[](size_t i) const
317 {
318 return bytes[i];
319 }
320
326 inline T &operator()(size_t i)
327 {
328 return this->data()[i];
329 }
330
336 inline const T &operator()(size_t i) const
337 {
338 return this->data()[i];
339 }
340
341 inline size_t size() const {
342 return bytes.size();
343 }
344
349 inline size_t length() const
350 { return this->size();}
351
356 inline size_t capacity() const {
357 return bytes.capacity();
358 }
359
363 void zero()
364 {
365 std::fill(bytes.begin(), bytes.end(), 0);
366 }
367
377 std::string toString(int precision=-1, int width=-1) const
378 {
379 std::string ret = "";
380 size_t c = 0;
381 const size_t buffSize = 256;
382 char tmp[buffSize];
383 std::string formatStr;
385 if (width<0) {
386 formatStr = "% .*lf\t";
387 for (c=0;c<length();c++) {
388 snprintf(tmp, buffSize, formatStr.c_str(), precision, (*this)[c]);
389 ret+=tmp;
390 }
391 }
392 else{
393 formatStr = "% *.*lf ";
394 for (c=0;c<length();c++){
395 snprintf(tmp, buffSize, formatStr.c_str(), width, precision, (*this)[c]);
396 ret+=tmp;
397 }
398 }
399 }
400 else {
401 formatStr = "%" + getFormatStr(getBottleTag()) + " ";
402 for (c=0;c<length();c++) {
403 snprintf(tmp, buffSize, formatStr.c_str(), (*this)[c]);
404 ret+=tmp;
405 }
406 }
407
408 if (length() >= 1) {
409 return ret.substr(0, ret.length() - 1);
410 }
411 return ret;
412 }
413
420 VectorOf<T> subVector(unsigned int first, unsigned int last) const
421 {
423 if ((first<=last)&&((int)last<(int)this->size()))
424 {
425 ret.resize(last-first+1);
426 for (unsigned int k = first; k <= last; k++) {
427 ret[k - first] = (*this)[k];
428 }
429 }
430 return ret;
431 }
432
442 bool setSubvector(int position, const VectorOf<T> &v)
443 {
444 if (position + v.size() > this->size()) {
445 return false;
446 }
447 for (size_t i = 0; i < v.size(); i++) {
448 (*this)[position + i] = v(i);
449 }
450 return true;
451 }
452
457 {
458 std::fill(bytes.begin(), bytes.end(), v);
459 return *this;
460 }
461
465 bool operator==(const VectorOf<T> &r) const
466 {
467 return bytes == r.bytes;
468 }
469
473 iterator begin() noexcept {
474 return bytes.begin();
475 }
476
480 iterator end() noexcept {
481 return bytes.end();
482 }
483
487 const_iterator begin() const noexcept {
488 return bytes.begin();
489 }
490
494 const_iterator end() const noexcept {
495 return bytes.end();
496 }
497
501 const_iterator cbegin() const noexcept {
502 return bytes.cbegin();
503 }
504
508 const_iterator cend() const noexcept {
509 return bytes.cend();
510 }
511 void clear() {
512 bytes.clear();
513 }
514
515 yarp::os::Type getType() const override {
516 return yarp::os::Type::byName("yarp/vector");
517 }
518};
519
520
521#ifdef _MSC_VER
522/*YARP_sig_EXTERN*/ template class YARP_sig_API yarp::sig::VectorOf<double>;
523#endif
524
525#endif // YARP_SIG_VECTOR_H
#define BOTTLE_TAG_FLOAT64
Definition Bottle.h:25
#define BOTTLE_TAG_INT32
Definition Bottle.h:21
bool ret
#define yAssert(x)
Definition Log.h:388
int BottleTagMap()
Definition Vector.h:83
int BottleTagMap< int >()
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:79
bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
Definition Vector.cpp:51
virtual std::string getFormatStr(int tag) const
Definition Vector.cpp:104
virtual int getBottleTag() const =0
void erase(iterator first, iterator last)
Remove one or more elements from the vector.
Definition Vector.h:240
std::string toString(int precision=-1, int width=-1) const
Creates a string object containing a text representation of the object.
Definition Vector.h:377
void erase(iterator pos)
Remove an element from the vector.
Definition Vector.h:230
size_t getListSize() const override
Definition Vector.h:172
VectorOf(std::initializer_list< T > values)
Initializer list constructor.
Definition Vector.h:135
void resize(size_t size) override
Resize the vector.
Definition Vector.h:221
VectorOf & operator=(VectorOf< T > &&other) noexcept=default
yarp::os::Type getType() const override
Definition Vector.h:515
int getBottleTag() const override
Definition Vector.h:168
const VectorOf< T > & operator=(T v)
Set all elements of the vector to a scalar.
Definition Vector.h:456
void push_back(T &&elem)
Move a new element in the vector: size is changed.
Definition Vector.h:277
size_t size() const
Definition Vector.h:341
void push_back(const T &elem)
Push a new element in the vector: size is changed.
Definition Vector.h:268
iterator begin() noexcept
Returns an iterator to the beginning of the VectorOf.
Definition Vector.h:473
const T & operator()(size_t i) const
Single element access, no range check, const version.
Definition Vector.h:336
bool operator==(const VectorOf< T > &r) const
True iff all elements of 'a' match all element of 'b'.
Definition Vector.h:465
T * data()
Return a pointer to the first element of the vector.
Definition Vector.h:206
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:420
const_iterator cend() const noexcept
Returns a const iterator to the end of the VectorOf.
Definition Vector.h:508
~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:250
const_iterator begin() const noexcept
Returns a const iterator to the beginning of the VectorOf.
Definition Vector.h:487
const_iterator end() const noexcept
Returns a const iterator to the end of the VectorOf.
Definition Vector.h:494
size_t capacity() const
capacity
Definition Vector.h:356
const T * getFirst() const
Definition Vector.h:189
const_iterator cbegin() const noexcept
Returns a const iterator to the beginning of the VectorOf.
Definition Vector.h:501
T & operator[](size_t i)
Single element access, no range check.
Definition Vector.h:306
VectorOf(size_t size)
Definition Vector.h:128
const T * data() const
Return a pointer to the first element of the vector, const version.
Definition Vector.h:214
size_t length() const
Get the length of the vector.
Definition Vector.h:349
T & emplace_back(_Args &&... args)
Construct a new element in the vector: size is changed.
Definition Vector.h:288
VectorOf(size_t s, const T &def)
Build a vector and initialize it with def.
Definition Vector.h:143
char * getMemoryBlock() override
Definition Vector.h:182
VectorOf(const VectorOf &r)=default
const char * getMemoryBlock() const override
Definition Vector.h:177
void pop_back()
Pop an element out of the vector: size is changed.
Definition Vector.h:296
typename std::vector< T >::const_iterator const_iterator
Definition Vector.h:124
T & operator()(size_t i)
Single element access, no range check.
Definition Vector.h:326
iterator end() noexcept
Returns an iterator to the end of the VectorOf.
Definition Vector.h:480
void zero()
Zero the elements of the vector.
Definition Vector.h:363
VectorOf(size_t s, const T *p)
Builds a vector and initialize it with values from 'p'.
Definition Vector.h:152
const T & operator[](size_t i) const
Single element access, no range check, const version.
Definition Vector.h:316
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:261
size_t getElementSize() const override
Definition Vector.h:164
typename std::vector< T >::iterator iterator
Definition Vector.h:123
bool setSubvector(int position, const VectorOf< T > &v)
Set a portion of this vector with the values of the specified vector.
Definition Vector.h:442
#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