YARP
Yet Another Robot Platform
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 iterator = typename std::vector<T>::iterator;
123 using const_iterator = typename std::vector<T>::const_iterator;
124
125 VectorOf() = default;
126
127 VectorOf(size_t size) : bytes(size) {
128 }
129
134 VectorOf(std::initializer_list<T> values) : bytes(values) {
135 }
136
142 VectorOf(size_t s, const T& def) : bytes(s, def) {
143 }
144
151 VectorOf(size_t s, const T *p)
152 {
153 this->resize(s);
154 memcpy(this->data(), p, sizeof(T)*s);
155 }
156
157 VectorOf(const VectorOf& r) = default;
158 VectorOf<T> &operator=(const VectorOf<T>& r) = default;
159 VectorOf(VectorOf<T>&& other) noexcept = default;
160 VectorOf& operator=(VectorOf<T>&& other) noexcept = default;
161 ~VectorOf() override = default;
162
163 size_t getElementSize() const override {
164 return sizeof(T);
165 }
166
167 int getBottleTag() const override {
168 return BottleTagMap <T>();
169 }
170
171 size_t getListSize() const override
172 {
173 return bytes.size();
174 }
175
176 const char* getMemoryBlock() const override
177 {
178 return reinterpret_cast<const char*>(this->data());
179 }
180
181 char* getMemoryBlock() override
182 {
183 return reinterpret_cast<char*>(this->data());
184 }
185#ifndef YARP_NO_DEPRECATED // since YARP 3.2.0
186 YARP_DEPRECATED_MSG("Use either data() if you need the pointer to the first element,"
187 " or cbegin() if you need the iterator")
188 inline const T *getFirst() const
189 {
190 return this->data();
191 }
192
193 YARP_DEPRECATED_MSG("Use either data() if you need the pointer to the first element,"
194 " or begin() if you need the iterator")
195 inline T *getFirst()
196 {
197 return this->data();
198 }
199#endif // YARP_NO_DEPRECATED
200
205 inline T *data()
206 { return bytes.empty() ? nullptr : &(bytes.at(0)); }
207
213 inline const T *data() const
214 { return bytes.empty() ? nullptr : &(bytes.at(0)); }
215
220 void resize(size_t size) override
221 {
222 bytes.resize(size);
223 }
224
230 void resize(size_t size, const T&def)
231 {
232 this->resize(size);
233 std::fill(bytes.begin(), bytes.end(), def);
234 }
235
241 void reserve(size_t size) {
242 bytes.reserve(size);
243 }
244
248 inline void push_back (const T &elem)
249 {
250 bytes.push_back(elem);
251 }
252
257 inline void push_back (T&& elem)
258 {
259 bytes.push_back(std::move(elem));
260 }
261
267 template<typename... _Args>
268 inline T& emplace_back(_Args&&... args)
269 {
270 return bytes.emplace_back(std::forward<_Args>(args)...);
271 }
272
276 inline void pop_back()
277 {
278 bytes.pop_back();
279 }
280
286 inline T &operator[](size_t i)
287 {
288 return bytes[i];
289 }
290
296 inline const T &operator[](size_t i) const
297 {
298 return bytes[i];
299 }
300
306 inline T &operator()(size_t i)
307 {
308 return this->data()[i];
309 }
310
316 inline const T &operator()(size_t i) const
317 {
318 return this->data()[i];
319 }
320
321 inline size_t size() const {
322 return bytes.size();
323 }
324
329 inline size_t length() const
330 { return this->size();}
331
336 inline size_t capacity() const {
337 return bytes.capacity();
338 }
339
343 void zero()
344 {
345 std::fill(bytes.begin(), bytes.end(), 0);
346 }
347
357 std::string toString(int precision=-1, int width=-1) const
358 {
359 std::string ret = "";
360 size_t c = 0;
361 const size_t buffSize = 256;
362 char tmp[buffSize];
363 std::string formatStr;
365 if (width<0) {
366 formatStr = "% .*lf\t";
367 for (c=0;c<length();c++) {
368 snprintf(tmp, buffSize, formatStr.c_str(), precision, (*this)[c]);
369 ret+=tmp;
370 }
371 }
372 else{
373 formatStr = "% *.*lf ";
374 for (c=0;c<length();c++){
375 snprintf(tmp, buffSize, formatStr.c_str(), width, precision, (*this)[c]);
376 ret+=tmp;
377 }
378 }
379 }
380 else {
381 formatStr = "%" + getFormatStr(getBottleTag()) + " ";
382 for (c=0;c<length();c++) {
383 snprintf(tmp, buffSize, formatStr.c_str(), (*this)[c]);
384 ret+=tmp;
385 }
386 }
387
388 if (length() >= 1) {
389 return ret.substr(0, ret.length() - 1);
390 }
391 return ret;
392 }
393
400 VectorOf<T> subVector(unsigned int first, unsigned int last) const
401 {
403 if ((first<=last)&&((int)last<(int)this->size()))
404 {
405 ret.resize(last-first+1);
406 for (unsigned int k = first; k <= last; k++) {
407 ret[k - first] = (*this)[k];
408 }
409 }
410 return ret;
411 }
412
422 bool setSubvector(int position, const VectorOf<T> &v)
423 {
424 if (position + v.size() > this->size()) {
425 return false;
426 }
427 for (size_t i = 0; i < v.size(); i++) {
428 (*this)[position + i] = v(i);
429 }
430 return true;
431 }
432
437 {
438 std::fill(bytes.begin(), bytes.end(), v);
439 return *this;
440 }
441
445 bool operator==(const VectorOf<T> &r) const
446 {
447 return bytes == r.bytes;
448 }
449
453 iterator begin() noexcept {
454 return bytes.begin();
455 }
456
460 iterator end() noexcept {
461 return bytes.end();
462 }
463
467 const_iterator begin() const noexcept {
468 return bytes.begin();
469 }
470
474 const_iterator end() const noexcept {
475 return bytes.end();
476 }
477
481 const_iterator cbegin() const noexcept {
482 return bytes.cbegin();
483 }
484
488 const_iterator cend() const noexcept {
489 return bytes.cend();
490 }
491 void clear() {
492 bytes.clear();
493 }
494
495 yarp::os::Type getType() const override {
496 return yarp::os::Type::byName("yarp/vector");
497 }
498};
499
500
501#ifdef _MSC_VER
502/*YARP_sig_EXTERN*/ template class YARP_sig_API yarp::sig::VectorOf<double>;
503#endif
504
505#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:383
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:100
virtual int getBottleTag() const =0
std::string toString(int precision=-1, int width=-1) const
Creates a string object containing a text representation of the object.
Definition: Vector.h:357
size_t getListSize() const override
Definition: Vector.h:171
VectorOf(std::initializer_list< T > values)
Initializer list constructor.
Definition: Vector.h:134
void resize(size_t size) override
Resize the vector.
Definition: Vector.h:220
VectorOf & operator=(VectorOf< T > &&other) noexcept=default
yarp::os::Type getType() const override
Definition: Vector.h:495
int getBottleTag() const override
Definition: Vector.h:167
const VectorOf< T > & operator=(T v)
Set all elements of the vector to a scalar.
Definition: Vector.h:436
void push_back(T &&elem)
Move a new element in the vector: size is changed.
Definition: Vector.h:257
size_t size() const
Definition: Vector.h:321
void push_back(const T &elem)
Push a new element in the vector: size is changed.
Definition: Vector.h:248
iterator begin() noexcept
Returns an iterator to the beginning of the VectorOf.
Definition: Vector.h:453
const T & operator()(size_t i) const
Single element access, no range check, const version.
Definition: Vector.h:316
bool operator==(const VectorOf< T > &r) const
True iff all elements of 'a' match all element of 'b'.
Definition: Vector.h:445
T * data()
Return a pointer to the first element of the vector.
Definition: Vector.h:205
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:400
const_iterator cend() const noexcept
Returns a const iterator to the end of the VectorOf.
Definition: Vector.h:488
~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 initilize the element to a default value.
Definition: Vector.h:230
const_iterator begin() const noexcept
Returns a const iterator to the beginning of the VectorOf.
Definition: Vector.h:467
const_iterator end() const noexcept
Returns a const iterator to the end of the VectorOf.
Definition: Vector.h:474
size_t capacity() const
capacity
Definition: Vector.h:336
const T * getFirst() const
Definition: Vector.h:188
const_iterator cbegin() const noexcept
Returns a const iterator to the beginning of the VectorOf.
Definition: Vector.h:481
T & operator[](size_t i)
Single element access, no range check.
Definition: Vector.h:286
VectorOf(size_t size)
Definition: Vector.h:127
const T * data() const
Return a pointer to the first element of the vector, const version.
Definition: Vector.h:213
size_t length() const
Get the length of the vector.
Definition: Vector.h:329
T & emplace_back(_Args &&... args)
Construct a new element in the vector: size is changed.
Definition: Vector.h:268
VectorOf(size_t s, const T &def)
Build a vector and initialize it with def.
Definition: Vector.h:142
char * getMemoryBlock() override
Definition: Vector.h:181
VectorOf(const VectorOf &r)=default
const char * getMemoryBlock() const override
Definition: Vector.h:176
void pop_back()
Pop an element out of the vector: size is changed.
Definition: Vector.h:276
typename std::vector< T >::const_iterator const_iterator
Definition: Vector.h:123
T & operator()(size_t i)
Single element access, no range check.
Definition: Vector.h:306
iterator end() noexcept
Returns an iterator to the end of the VectorOf.
Definition: Vector.h:460
void zero()
Zero the elements of the vector.
Definition: Vector.h:343
VectorOf(size_t s, const T *p)
Builds a vector and initialize it with values from 'p'.
Definition: Vector.h:151
const T & operator[](size_t i) const
Single element access, no range check, const version.
Definition: Vector.h:296
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:241
size_t getElementSize() const override
Definition: Vector.h:163
typename std::vector< T >::iterator iterator
Definition: Vector.h:122
bool setSubvector(int position, const VectorOf< T > &v)
Set a portion of this vector with the values of the specified vector.
Definition: Vector.h:422
#define YARP_DEPRECATED_MSG(MSG)
Expands to either the standard [[deprecated]] attribute or a compiler-specific decorator such as __at...
Definition: compiler.h:2895
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