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
196 inline T *data()
197 { return bytes.empty() ? nullptr : &(bytes.at(0)); }
198
204 inline const T *data() const
205 { return bytes.empty() ? nullptr : &(bytes.at(0)); }
206
211 void resize(size_t size) override
212 {
213 bytes.resize(size);
214 }
215
220 void erase(iterator pos)
221 {
222 bytes.erase(pos);
223 }
224
230 void erase(iterator first, iterator last)
231 {
232 bytes.erase(first, last);
233 }
234
240 void resize(size_t size, const T&def)
241 {
242 this->resize(size);
243 std::fill(bytes.begin(), bytes.end(), def);
244 }
245
251 void reserve(size_t size) {
252 bytes.reserve(size);
253 }
254
258 inline void push_back (const T &elem)
259 {
260 bytes.push_back(elem);
261 }
262
267 inline void push_back (T&& elem)
268 {
269 bytes.push_back(std::move(elem));
270 }
271
277 template<typename... _Args>
278 inline T& emplace_back(_Args&&... args)
279 {
280 return bytes.emplace_back(std::forward<_Args>(args)...);
281 }
282
286 inline void pop_back()
287 {
288 bytes.pop_back();
289 }
290
296 inline T &operator[](size_t i)
297 {
298 return bytes[i];
299 }
300
306 inline const T &operator[](size_t i) const
307 {
308 return bytes[i];
309 }
310
316 inline T &operator()(size_t i)
317 {
318 return this->data()[i];
319 }
320
326 inline const T &operator()(size_t i) const
327 {
328 return this->data()[i];
329 }
330
331 inline size_t size() const {
332 return bytes.size();
333 }
334
339 inline size_t length() const
340 { return this->size();}
341
346 inline size_t capacity() const {
347 return bytes.capacity();
348 }
349
353 void zero()
354 {
355 std::fill(bytes.begin(), bytes.end(), 0);
356 }
357
367 std::string toString(int precision=-1, int width=-1) const
368 {
369 std::string ret = "";
370 size_t c = 0;
371 const size_t buffSize = 256;
372 char tmp[buffSize];
373 std::string formatStr;
375 if (width<0) {
376 formatStr = "% .*lf\t";
377 for (c=0;c<length();c++) {
378 snprintf(tmp, buffSize, formatStr.c_str(), precision, (*this)[c]);
379 ret+=tmp;
380 }
381 }
382 else{
383 formatStr = "% *.*lf ";
384 for (c=0;c<length();c++){
385 snprintf(tmp, buffSize, formatStr.c_str(), width, precision, (*this)[c]);
386 ret+=tmp;
387 }
388 }
389 }
390 else {
391 formatStr = "%" + getFormatStr(getBottleTag()) + " ";
392 for (c=0;c<length();c++) {
393 snprintf(tmp, buffSize, formatStr.c_str(), (*this)[c]);
394 ret+=tmp;
395 }
396 }
397
398 if (length() >= 1) {
399 return ret.substr(0, ret.length() - 1);
400 }
401 return ret;
402 }
403
410 VectorOf<T> subVector(unsigned int first, unsigned int last) const
411 {
413 if ((first<=last)&&((int)last<(int)this->size()))
414 {
415 ret.resize(last-first+1);
416 for (unsigned int k = first; k <= last; k++) {
417 ret[k - first] = (*this)[k];
418 }
419 }
420 return ret;
421 }
422
432 bool setSubvector(int position, const VectorOf<T> &v)
433 {
434 if (position + v.size() > this->size()) {
435 return false;
436 }
437 for (size_t i = 0; i < v.size(); i++) {
438 (*this)[position + i] = v(i);
439 }
440 return true;
441 }
442
447 {
448 std::fill(bytes.begin(), bytes.end(), v);
449 return *this;
450 }
451
455 bool operator==(const VectorOf<T> &r) const
456 {
457 return bytes == r.bytes;
458 }
459
463 iterator begin() noexcept {
464 return bytes.begin();
465 }
466
470 iterator end() noexcept {
471 return bytes.end();
472 }
473
477 const_iterator begin() const noexcept {
478 return bytes.begin();
479 }
480
484 const_iterator end() const noexcept {
485 return bytes.end();
486 }
487
491 const_iterator cbegin() const noexcept {
492 return bytes.cbegin();
493 }
494
498 const_iterator cend() const noexcept {
499 return bytes.cend();
500 }
501 void clear() {
502 bytes.clear();
503 }
504
505 yarp::os::Type getType() const override {
506 return yarp::os::Type::byName("yarp/vector");
507 }
508};
509
510
511#ifdef _MSC_VER
512/*YARP_sig_EXTERN*/ template class YARP_sig_API yarp::sig::VectorOf<double>;
513#endif
514
515#endif // YARP_SIG_VECTOR_H
#define BOTTLE_TAG_FLOAT64
Definition Bottle.h:26
#define BOTTLE_TAG_INT32
Definition Bottle.h:21
#define BOTTLE_TAG_FLOAT32
Definition Bottle.h:25
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:230
std::string toString(int precision=-1, int width=-1) const
Creates a string object containing a text representation of the object.
Definition Vector.h:367
void erase(iterator pos)
Remove an element from the vector.
Definition Vector.h:220
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:211
VectorOf & operator=(VectorOf< T > &&other) noexcept=default
yarp::os::Type getType() const override
Definition Vector.h:505
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:446
void push_back(T &&elem)
Move a new element in the vector: size is changed.
Definition Vector.h:267
size_t size() const
Definition Vector.h:331
void push_back(const T &elem)
Push a new element in the vector: size is changed.
Definition Vector.h:258
iterator begin() noexcept
Returns an iterator to the beginning of the VectorOf.
Definition Vector.h:463
const T & operator()(size_t i) const
Single element access, no range check, const version.
Definition Vector.h:326
bool operator==(const VectorOf< T > &r) const
True iff all elements of 'a' match all element of 'b'.
Definition Vector.h:455
T * data()
Return a pointer to the first element of the vector.
Definition Vector.h:196
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:410
const_iterator cend() const noexcept
Returns a const iterator to the end of the VectorOf.
Definition Vector.h:498
~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:240
const_iterator begin() const noexcept
Returns a const iterator to the beginning of the VectorOf.
Definition Vector.h:477
const_iterator end() const noexcept
Returns a const iterator to the end of the VectorOf.
Definition Vector.h:484
size_t capacity() const
capacity
Definition Vector.h:346
const_iterator cbegin() const noexcept
Returns a const iterator to the beginning of the VectorOf.
Definition Vector.h:491
T & operator[](size_t i)
Single element access, no range check.
Definition Vector.h:296
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:204
size_t length() const
Get the length of the vector.
Definition Vector.h:339
T & emplace_back(_Args &&... args)
Construct a new element in the vector: size is changed.
Definition Vector.h:278
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:286
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:316
iterator end() noexcept
Returns an iterator to the end of the VectorOf.
Definition Vector.h:470
void zero()
Zero the elements of the vector.
Definition Vector.h:353
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:306
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:251
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:432
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