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
104template<>
106 return BOTTLE_TAG_INT16;
107 }
108
109template<>
111 return BOTTLE_TAG_INT16;
112 }
113
114template<>
115inline int BottleTagMap <char> () {
116 return BOTTLE_TAG_INT8;
117 }
118
119template<>
121 return BOTTLE_TAG_INT8;
122 }
123
140template<class T>
142{
143private:
144 std::vector<T> bytes;
145
146public:
147 using value_type = T;
148 using iterator = typename std::vector<T>::iterator;
149 using const_iterator = typename std::vector<T>::const_iterator;
150
151 VectorOf() = default;
152
153 VectorOf(size_t size) : bytes(size) {
154 }
155
160 VectorOf(std::initializer_list<T> values) : bytes(values) {
161 }
162
168 VectorOf(size_t s, const T& def) : bytes(s, def) {
169 }
170
177 VectorOf(size_t s, const T *p)
178 {
179 this->resize(s);
180 memcpy(this->data(), p, sizeof(T)*s);
181 }
182
183 VectorOf(const VectorOf& r) = default;
184 VectorOf<T> &operator=(const VectorOf<T>& r) = default;
185 VectorOf(VectorOf<T>&& other) noexcept = default;
186 VectorOf& operator=(VectorOf<T>&& other) noexcept = default;
187 ~VectorOf() override = default;
188
189 size_t getElementSize() const override {
190 return sizeof(T);
191 }
192
193 int getBottleTag() const override {
194 return BottleTagMap <T>();
195 }
196
197 size_t getListSize() const override
198 {
199 return bytes.size();
200 }
201
202 const char* getMemoryBlock() const override
203 {
204 return reinterpret_cast<const char*>(this->data());
205 }
206
207 char* getMemoryBlock() override
208 {
209 return reinterpret_cast<char*>(this->data());
210 }
211
216 inline T *data()
217 { return bytes.empty() ? nullptr : &(bytes.at(0)); }
218
224 inline const T *data() const
225 { return bytes.empty() ? nullptr : &(bytes.at(0)); }
226
231 void resize(size_t size) override
232 {
233 bytes.resize(size);
234 }
235
240 void erase(iterator pos)
241 {
242 bytes.erase(pos);
243 }
244
250 void erase(iterator first, iterator last)
251 {
252 bytes.erase(first, last);
253 }
254
260 void resize(size_t size, const T&def)
261 {
262 this->resize(size);
263 std::fill(bytes.begin(), bytes.end(), def);
264 }
265
271 void reserve(size_t size) {
272 bytes.reserve(size);
273 }
274
278 inline void push_back (const T &elem)
279 {
280 bytes.push_back(elem);
281 }
282
287 inline void push_back (T&& elem)
288 {
289 bytes.push_back(std::move(elem));
290 }
291
297 template<typename... _Args>
298 inline T& emplace_back(_Args&&... args)
299 {
300 return bytes.emplace_back(std::forward<_Args>(args)...);
301 }
302
306 inline void pop_back()
307 {
308 bytes.pop_back();
309 }
310
316 inline T &operator[](size_t i)
317 {
318 return bytes[i];
319 }
320
326 inline const T &operator[](size_t i) const
327 {
328 return bytes[i];
329 }
330
336 inline T &operator()(size_t i)
337 {
338 return this->data()[i];
339 }
340
346 inline const T &operator()(size_t i) const
347 {
348 return this->data()[i];
349 }
350
351 inline size_t size() const {
352 return bytes.size();
353 }
354
359 inline size_t length() const
360 { return this->size();}
361
366 inline size_t capacity() const {
367 return bytes.capacity();
368 }
369
373 void zero()
374 {
375 std::fill(bytes.begin(), bytes.end(), 0);
376 }
377
387 std::string toString(int precision=-1, int width=-1) const
388 {
389 std::string ret = "";
390 size_t c = 0;
391 const size_t buffSize = 256;
392 char tmp[buffSize];
393 std::string formatStr;
395 if (width<0) {
396 formatStr = "% .*lf\t";
397 for (c=0;c<length();c++) {
398 snprintf(tmp, buffSize, formatStr.c_str(), precision, (*this)[c]);
399 ret+=tmp;
400 }
401 }
402 else{
403 formatStr = "% *.*lf ";
404 for (c=0;c<length();c++){
405 snprintf(tmp, buffSize, formatStr.c_str(), width, precision, (*this)[c]);
406 ret+=tmp;
407 }
408 }
409 }
410 else {
411 formatStr = "%" + getFormatStr(getBottleTag()) + " ";
412 for (c=0;c<length();c++) {
413 snprintf(tmp, buffSize, formatStr.c_str(), (*this)[c]);
414 ret+=tmp;
415 }
416 }
417
418 if (length() >= 1) {
419 return ret.substr(0, ret.length() - 1);
420 }
421 return ret;
422 }
423 std::string toFormattedString(int precision=-1, int width=-1) const
424 {
425 return toString(precision, width);
426 }
427
434 VectorOf<T> subVector(unsigned int first, unsigned int last) const
435 {
436 VectorOf<T> ret;
437 if ((first<=last)&&((int)last<(int)this->size()))
438 {
439 ret.resize(last-first+1);
440 for (unsigned int k = first; k <= last; k++) {
441 ret[k - first] = (*this)[k];
442 }
443 }
444 return ret;
445 }
446
456 bool setSubvector(int position, const VectorOf<T> &v)
457 {
458 if (position + v.size() > this->size()) {
459 return false;
460 }
461 for (size_t i = 0; i < v.size(); i++) {
462 (*this)[position + i] = v(i);
463 }
464 return true;
465 }
466
471 {
472 std::fill(bytes.begin(), bytes.end(), v);
473 return *this;
474 }
475
479 bool operator==(const VectorOf<T> &r) const
480 {
481 return bytes == r.bytes;
482 }
483
487 iterator begin() noexcept {
488 return bytes.begin();
489 }
490
494 iterator end() noexcept {
495 return bytes.end();
496 }
497
501 const_iterator begin() const noexcept {
502 return bytes.begin();
503 }
504
508 const_iterator end() const noexcept {
509 return bytes.end();
510 }
511
515 const_iterator cbegin() const noexcept {
516 return bytes.cbegin();
517 }
518
522 const_iterator cend() const noexcept {
523 return bytes.cend();
524 }
525 void clear() {
526 bytes.clear();
527 }
528
529 yarp::os::Type getType() const override {
530 return yarp::os::Type::byName("yarp/vector");
531 }
532};
533
534
535#ifdef _MSC_VER
536/*YARP_sig_EXTERN*/ template class YARP_sig_API yarp::sig::VectorOf<double>;
537#endif
538
539#endif // YARP_SIG_VECTOR_H
#define BOTTLE_TAG_INT8
Definition Bottle.h:19
#define BOTTLE_TAG_FLOAT64
Definition Bottle.h:26
#define BOTTLE_TAG_INT16
Definition Bottle.h:20
#define BOTTLE_TAG_INT32
Definition Bottle.h:21
#define BOTTLE_TAG_FLOAT32
Definition Bottle.h:25
#define yAssert(x)
Definition Log.h:388
int BottleTagMap< unsigned char >()
Definition Vector.h:120
int BottleTagMap< unsigned short int >()
Definition Vector.h:110
int BottleTagMap()
Definition Vector.h:83
int BottleTagMap< int >()
Definition Vector.h:100
int BottleTagMap< short int >()
Definition Vector.h:105
int BottleTagMap< float >()
Definition Vector.h:95
int BottleTagMap< char >()
Definition Vector.h:115
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:250
std::string toString(int precision=-1, int width=-1) const
Creates a string object containing a text representation of the object.
Definition Vector.h:387
void erase(iterator pos)
Remove an element from the vector.
Definition Vector.h:240
size_t getListSize() const override
Definition Vector.h:197
VectorOf(std::initializer_list< T > values)
Initializer list constructor.
Definition Vector.h:160
void resize(size_t size) override
Resize the vector.
Definition Vector.h:231
VectorOf & operator=(VectorOf< T > &&other) noexcept=default
yarp::os::Type getType() const override
Definition Vector.h:529
int getBottleTag() const override
Definition Vector.h:193
const VectorOf< T > & operator=(T v)
Set all elements of the vector to a scalar.
Definition Vector.h:470
void push_back(T &&elem)
Move a new element in the vector: size is changed.
Definition Vector.h:287
size_t size() const
Definition Vector.h:351
void push_back(const T &elem)
Push a new element in the vector: size is changed.
Definition Vector.h:278
iterator begin() noexcept
Returns an iterator to the beginning of the VectorOf.
Definition Vector.h:487
const T & operator()(size_t i) const
Single element access, no range check, const version.
Definition Vector.h:346
bool operator==(const VectorOf< T > &r) const
True iff all elements of 'a' match all element of 'b'.
Definition Vector.h:479
T * data()
Return a pointer to the first element of the vector.
Definition Vector.h:216
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:434
const_iterator cend() const noexcept
Returns a const iterator to the end of the VectorOf.
Definition Vector.h:522
~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:260
const_iterator begin() const noexcept
Returns a const iterator to the beginning of the VectorOf.
Definition Vector.h:501
std::string toFormattedString(int precision=-1, int width=-1) const
Definition Vector.h:423
const_iterator end() const noexcept
Returns a const iterator to the end of the VectorOf.
Definition Vector.h:508
size_t capacity() const
capacity
Definition Vector.h:366
const_iterator cbegin() const noexcept
Returns a const iterator to the beginning of the VectorOf.
Definition Vector.h:515
T & operator[](size_t i)
Single element access, no range check.
Definition Vector.h:316
VectorOf(size_t size)
Definition Vector.h:153
const T * data() const
Return a pointer to the first element of the vector, const version.
Definition Vector.h:224
size_t length() const
Get the length of the vector.
Definition Vector.h:359
T & emplace_back(_Args &&... args)
Construct a new element in the vector: size is changed.
Definition Vector.h:298
VectorOf(size_t s, const T &def)
Build a vector and initialize it with def.
Definition Vector.h:168
char * getMemoryBlock() override
Definition Vector.h:207
VectorOf(const VectorOf &r)=default
const char * getMemoryBlock() const override
Definition Vector.h:202
void pop_back()
Pop an element out of the vector: size is changed.
Definition Vector.h:306
typename std::vector< T >::const_iterator const_iterator
Definition Vector.h:149
T & operator()(size_t i)
Single element access, no range check.
Definition Vector.h:336
iterator end() noexcept
Returns an iterator to the end of the VectorOf.
Definition Vector.h:494
void zero()
Zero the elements of the vector.
Definition Vector.h:373
VectorOf(size_t s, const T *p)
Builds a vector and initialize it with values from 'p'.
Definition Vector.h:177
const T & operator[](size_t i) const
Single element access, no range check, const version.
Definition Vector.h:326
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:271
size_t getElementSize() const override
Definition Vector.h:189
typename std::vector< T >::iterator iterator
Definition Vector.h:148
bool setSubvector(int position, const VectorOf< T > &v)
Set a portion of this vector with the values of the specified vector.
Definition Vector.h:456
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