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
231 void resize(size_t size, const T&def)
232 {
233 this->resize(size);
234 std::fill(bytes.begin(), bytes.end(), def);
235 }
236
242 void reserve(size_t size) {
243 bytes.reserve(size);
244 }
245
249 inline void push_back (const T &elem)
250 {
251 bytes.push_back(elem);
252 }
253
258 inline void push_back (T&& elem)
259 {
260 bytes.push_back(std::move(elem));
261 }
262
268 template<typename... _Args>
269 inline T& emplace_back(_Args&&... args)
270 {
271 return bytes.emplace_back(std::forward<_Args>(args)...);
272 }
273
277 inline void pop_back()
278 {
279 bytes.pop_back();
280 }
281
287 inline T &operator[](size_t i)
288 {
289 return bytes[i];
290 }
291
297 inline const T &operator[](size_t i) const
298 {
299 return bytes[i];
300 }
301
307 inline T &operator()(size_t i)
308 {
309 return this->data()[i];
310 }
311
317 inline const T &operator()(size_t i) const
318 {
319 return this->data()[i];
320 }
321
322 inline size_t size() const {
323 return bytes.size();
324 }
325
330 inline size_t length() const
331 { return this->size();}
332
337 inline size_t capacity() const {
338 return bytes.capacity();
339 }
340
344 void zero()
345 {
346 std::fill(bytes.begin(), bytes.end(), 0);
347 }
348
358 std::string toString(int precision=-1, int width=-1) const
359 {
360 std::string ret = "";
361 size_t c = 0;
362 const size_t buffSize = 256;
363 char tmp[buffSize];
364 std::string formatStr;
366 if (width<0) {
367 formatStr = "% .*lf\t";
368 for (c=0;c<length();c++) {
369 snprintf(tmp, buffSize, formatStr.c_str(), precision, (*this)[c]);
370 ret+=tmp;
371 }
372 }
373 else{
374 formatStr = "% *.*lf ";
375 for (c=0;c<length();c++){
376 snprintf(tmp, buffSize, formatStr.c_str(), width, precision, (*this)[c]);
377 ret+=tmp;
378 }
379 }
380 }
381 else {
382 formatStr = "%" + getFormatStr(getBottleTag()) + " ";
383 for (c=0;c<length();c++) {
384 snprintf(tmp, buffSize, formatStr.c_str(), (*this)[c]);
385 ret+=tmp;
386 }
387 }
388
389 if (length() >= 1) {
390 return ret.substr(0, ret.length() - 1);
391 }
392 return ret;
393 }
394
401 VectorOf<T> subVector(unsigned int first, unsigned int last) const
402 {
404 if ((first<=last)&&((int)last<(int)this->size()))
405 {
406 ret.resize(last-first+1);
407 for (unsigned int k = first; k <= last; k++) {
408 ret[k - first] = (*this)[k];
409 }
410 }
411 return ret;
412 }
413
423 bool setSubvector(int position, const VectorOf<T> &v)
424 {
425 if (position + v.size() > this->size()) {
426 return false;
427 }
428 for (size_t i = 0; i < v.size(); i++) {
429 (*this)[position + i] = v(i);
430 }
431 return true;
432 }
433
438 {
439 std::fill(bytes.begin(), bytes.end(), v);
440 return *this;
441 }
442
446 bool operator==(const VectorOf<T> &r) const
447 {
448 return bytes == r.bytes;
449 }
450
454 iterator begin() noexcept {
455 return bytes.begin();
456 }
457
461 iterator end() noexcept {
462 return bytes.end();
463 }
464
468 const_iterator begin() const noexcept {
469 return bytes.begin();
470 }
471
475 const_iterator end() const noexcept {
476 return bytes.end();
477 }
478
482 const_iterator cbegin() const noexcept {
483 return bytes.cbegin();
484 }
485
489 const_iterator cend() const noexcept {
490 return bytes.cend();
491 }
492 void clear() {
493 bytes.clear();
494 }
495
496 yarp::os::Type getType() const override {
497 return yarp::os::Type::byName("yarp/vector");
498 }
499};
500
501
502#ifdef _MSC_VER
503/*YARP_sig_EXTERN*/ template class YARP_sig_API yarp::sig::VectorOf<double>;
504#endif
505
506#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: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:358
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:496
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:437
void push_back(T &&elem)
Move a new element in the vector: size is changed.
Definition Vector.h:258
size_t size() const
Definition Vector.h:322
void push_back(const T &elem)
Push a new element in the vector: size is changed.
Definition Vector.h:249
iterator begin() noexcept
Returns an iterator to the beginning of the VectorOf.
Definition Vector.h:454
const T & operator()(size_t i) const
Single element access, no range check, const version.
Definition Vector.h:317
bool operator==(const VectorOf< T > &r) const
True iff all elements of 'a' match all element of 'b'.
Definition Vector.h:446
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:401
const_iterator cend() const noexcept
Returns a const iterator to the end of the VectorOf.
Definition Vector.h:489
~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:231
const_iterator begin() const noexcept
Returns a const iterator to the beginning of the VectorOf.
Definition Vector.h:468
const_iterator end() const noexcept
Returns a const iterator to the end of the VectorOf.
Definition Vector.h:475
size_t capacity() const
capacity
Definition Vector.h:337
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:482
T & operator[](size_t i)
Single element access, no range check.
Definition Vector.h:287
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:330
T & emplace_back(_Args &&... args)
Construct a new element in the vector: size is changed.
Definition Vector.h:269
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:277
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:307
iterator end() noexcept
Returns an iterator to the end of the VectorOf.
Definition Vector.h:461
void zero()
Zero the elements of the vector.
Definition Vector.h:344
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:297
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:242
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:423
#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